Category Archives: Arrays

View algorithms on Arrays

Print 2 repeating elements in a given array.

Question: Given an array with n+2 elements, all elements of the array are in range 1 to n and also all elements occur only once except 2 numbers which occur twice. Find those 2 repeating numbers. Input: arr[] = {6, 2, 6, 5, 2, 3, 1} Output: 6 2 One simple way is to scan the complete array… Read More »

Find the number occuring odd number of times in an array.

Question: Given an array of positive integers, all numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time and constant space. Input:- arr[] = {1, 2, 3, 2, 3, 1, 3} Output:- 3 This is a very simple problem. The basic and naive approach will be to… Read More »

Write a program to rotate an array. (Method 2)

Question: Write a program to rotate an array arr[], having a size ‘n’ by ‘d’ elements? Input: arr[] = { 4, 8, 15, 16, 23, 42, 99 }, d = 3 Output: 16, 23, 42, 99, 4, 8, 15 Let us suppose our array is in this way: On rotating the array by 3 elements, it should look… Read More »

Write a program to rotate an array.

Question: Write a program to rotate an array arr[], having a size ‘n’ by ‘d’ elements? Input: arr[] = { 4, 8, 15, 16, 23, 42, 99 }, d = 3 Output: 16, 23, 42, 99, 4, 8, 15 Let us suppose our array is in this way: On rotating the array by 3 elements, it should look… Read More »

Find the missing number in an array. (Method 4)

Question:- We are given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing. Give an algorithm to find the missing integer. Input:- 1, 2, 4, 6, 3, 7, 8 Output:- 5 We solved this problem using array summation here.… Read More »

Find the missing number in an array. (Method 3)

Question:- We are given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing. Give an algorithm to find the missing integer. Input:- 1, 2, 4, 6, 3, 7, 8 Output:- 5 We solved the same problem via sorting here.… Read More »

Find the missing number in an array. (Method 2)

Question:- We are given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing. Give an algorithm to find the missing integer. Input:- 1, 2, 4, 6, 3, 7, 8 Output:- 5 After the brute force method we can also… Read More »

Find the missing number in an array.

Question:- We are given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing. Give an algorithm to find the missing integer. Input:- 1, 2, 4, 6, 3, 7, 8 Output:- 5 Brute Force Solution:- One simple solution to this… Read More »