Find the two repeating elements in a given array | 6 Approaches

Objective: Given an array of n+2 elements. All elements of the array are in range 1 to n and all elements occur once except two numbers which occur twice. Write an algorithm to find the two repeating numbers. Example: int [] A = {1,4,5,6,3,2,5,2}; int n = 6; Output: Two Repeated elements are: 2 and … Read more Find the two repeating elements in a given array | 6 Approaches

Find the right most set bit of a number

Bit Manipulation

Objective: Given a number, write and algorithm  to find the right most set bit in it (In binary representation). Example: Number : 1 Binary representation: 1 Position of right most set bit: 1 Number : 6 Binary representation: 1 1 0 Position of right most set bit: 2 Number : 11 Binary representation: 1 0 … Read more Find the right most set bit of a number

Find a Number occurring odd number of times in a Given array

Objective: Given a array of integers, in which every elements occurs even number of times except one number which occurs add number of times. Find out that number. Example:   int[] A = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7 }; Element appearing add … Read more Find a Number occurring odd number of times in a Given array

Find a Missing Number From a Sequence of Consecutive Numbers | XOR Method

Input:  Array, arrA[] with a miss­ing num­ber and Range

Out­put : miss­ing number

Example:

int A[] = { 1, 2, 7, 6, 3, 4 };
int range = 7;
Output: MIssing No is :5

In our earlier approach ” Click Here ” we have seen the method where we had calculated the Sum of numbers, but this approach might fail when number goes beyond the integer range.

XOR method will better solution in that case.

Approach: – Time Complexity -O(N), Space Complexity – O(1)

Read moreFind a Missing Number From a Sequence of Consecutive Numbers | XOR Method

Generate All Strings of n bits.

Objec­tive: Generate All Strings of n bits, consider A[0..n-1] is an array of size n.

Example :

n = 3
Output:
[0, 0, 0]    [1, 0, 0]    [0, 1, 0]    [1, 1, 0]

[0, 0, 1]     [1, 0, 1]     [0, 1, 1]    [1, 1, 1]

Read moreGenerate All Strings of n bits.

Find The Missing Duplicate in a Given Array.

Bit Manipulation

Objec­tive: Given an Integer array. Array contains duplicates of all the numbers in array except one number . Find that number.

Example :

int [] A = { 2,1,3,5,5,3,2,1,6,7,7,8,8};
Output : Missing duplicate is 6

Read moreFind The Missing Duplicate in a Given Array.

Reverse the binary representation of a number.

Objective: Write a program to Reverse the binary representation of a number

Example:

Input : 30
Output : 15

Explanation:

binary representation of 30 is : 11110
reverse of binary representation : 01111
decimal of reversed binary representation is : 15

Input: A Number

Output: Decimal of reversed binary representation of a number.

Approach:

Read moreReverse the binary representation of a number.