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

JDBC connection factory

  Class ConnectionManager {   Queue<Connection> availableConnection;   List <Connection> allotedConnection; ConnectionManager( Integer noOfConnections, ConnectionPoperties props ) {     //Create the no of connection objects and assign to avaialbleConnection } Conection getConnection() {     syncronized( this.class) { if (availableConnection.isEmpty() ) {          throw ConnectionExhausted();       }       conn = availableCOnnection.poll();       alottledConectiion.add(conn); } return conn; } synchronized Conection releaseConnection(Connection conn) {        alottedconnection.remove(conn);    avaialbleConection.add(conn); }

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.