1. Given a arrary return an element not in array.
2. Previous problem with number will be in range of 0- (n-1).
3. Find the number repeating most number of times in a n size array with no in the range of 0-(n-1).
4. Find whether a[i]+a[j]=x in a sorted and unsorted array.
5. Maximum of two number without if statement.
6. Print 1-100 without loop, if, goto etc.
7. Find the only non-repeating number in a array.
8. Find the two number which is non-repeating in a array
I am posting questions here only. I will shortly update with answer to them.
Till then keep trying.
2. Previous problem with number will be in range of 0- (n-1).
3. Find the number repeating most number of times in a n size array with no in the range of 0-(n-1).
4. Find whether a[i]+a[j]=x in a sorted and unsorted array.
5. Maximum of two number without if statement.
6. Print 1-100 without loop, if, goto etc.
7. Find the only non-repeating number in a array.
8. Find the two number which is non-repeating in a array
I am posting questions here only. I will shortly update with answer to them.
Till then keep trying.
Solution 7:
ReplyDeleteDo an XOR of all the elements. The value after XOR is the non repeating element as rest would become 0 since X ^ X is 0 and X ^ 0 is X.
Solution 6:
print(int n)
{
if(n==0)
return;
print(n-1);
cout << " " << n ;
}
Solution 5:
max of x and y:
x ^ ((x^y) & -(x < y))
Solution 4:
For sorted array use binary search
For unsorted array use a hash
Solution 3
Use a bucket sort since the range of numbers is known. Time is O(N) and space is also O(N)
Solution 8:Find the two number which is non-repeating in a array
ReplyDeleteStep 1: Do XOR of all elements. This is actually the XOR of the two non repeating elements as rest would cancel each other.
Step 2: Any set bit in the final XOR is set in either the first element or the second. Find any set bit of this XOR say the rightmost by doing x & (x-1). Call this as set_bit
Step 3: Divide the original array in two subsets. One that has set_bit as set and the other as not.
Step4: Both sets would have one non repeating element. Now do XOR of each subsets and find the respective non repeating element.