0% found this document useful (0 votes)
8 views2 pages

Prblms Bit

The document discusses four solutions: 1) finding a missing number in an array, 2) finding the maximum of two pairs in an array, 3) finding the longest consecutive 1's in a number's binary representation, and 4) checking if a number has alternating 1's and 0's in its binary representation.

Uploaded by

Pranav Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Prblms Bit

The document discusses four solutions: 1) finding a missing number in an array, 2) finding the maximum of two pairs in an array, 3) finding the longest consecutive 1's in a number's binary representation, and 4) checking if a number has alternating 1's and 0's in its binary representation.

Uploaded by

Pranav Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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