Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Solutions

1.) find missing number in array If n is the array size and from 0 to n elements are there in array

Ex: if N =5 array size is 4

Arr = [1,3,2,5] the missing element is 4.

Sol : find the sum of original array and the given array then subtract.

2.) find max and of two pairs in the given array.

Sol:

3.) longest consecutives 1’s

Sol: so we will find no of trialing 1’s then run the loop

N = N& N<<1 gives out 1 trailing 1 for every loop

Another best solution :


class Solution

{
public:
int maxConsecutiveOnes(int N)
{
int cnt = 0;
int maxi = 0;
for(int i = 0;i < 32;i++)
{

if((N & (1<<i))!= 0)


{
cnt++;
}
else
{

maxi = max(maxi , cnt);


cnt = 0;
// cout <<maxi;
}
}
return maxi;
}
};
4.) to check if the umber have all 1’s (this is the major part to find if the number has alternative 1’s
and 0’s
Sol : (x+1)&x gives that and if number is 1010 then xor of left shift of that number gives you all 1s

You might also like