Skip to main content

Array

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.

Comments

  1. Solution 7:
    Do 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)

    ReplyDelete
  2. Solution 8:Find the two number which is non-repeating in a array
    Step 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Car Parking Problem

There is n parking slots and n-1 car already parked. Lets say car parked with initial arrangement and we want to make the car to be parked to some other arrangement. Lets say n = 5, inital = free, 3, 4, 1, 2 desired = 1, free, 2, 4 ,3 Give an algorithm with minimum steps needed to get desired arrangement. Told by one of my friend and after a lot of search i really got a nice solution. I will post solution in comment part

Median of Five Numbers

U have 5 NOs , X1,X2,X3,X4,X5 With minimum no. of comparisons we have to find a median. SWAP(X,Y) function is available to u . I have a answer of six comparisons and eight swaps....wait for people to find out by themselves.

Consistent Hashing

I will try to explain consistent hashing with a real world example. Let's assume I have a restaurant with 60 tables and 5 servers (waiter). Each server is given an equal number of tables to serve. Now let's assume we have addition of a new server (waiter), so his addition will be marked in the circle and he will be receiving tables from the previous server to his distance only. Check the attached example. Assume a server (waiter) has left the organisation and we have only 4 servers now. Server3 has left the restaurant, so his table will be assigned to server 4. I am sure you have noticed the load is not equally distributed. But to make the system less prone to addition/removal we just rotate in clockwise and assign range from the previous server to present server.  To make sure load is balanced or optimally balanced we need to use virtual nodes. Check links here: http://tom-e-white.com/2007/11/consistent-hashing.html https://www.toptal.com/big-data/...