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

JAVA CLASSLOADER- Types, usages.

JVM loads library and classes dynamically only. Its on demand only. A Class will be loaded only when needed and only once. There can be system supported class loader and user supplied class loader. When JVM starts it loads three type of class loader 1. Bootstrap loader - When system boots. Loads from jre_home/lib/ 2. External class loader - Loads from jre_home/lib/ext. 3. System Class loader - Loads classes from system property CLASSPATH. Besides this user can provide their own class loader which is pretty easy to implement in Java. User supplied loader will work in conjunction with other loader i.e. system loader too. Some of the examples are: 1. Load library at runtime from http resources. Example scripting classes, bean classes. 2. Can load encrypted class files with new class loader. 3. Modify the byte code. Application Container loads classes from deployed WAR or EAR files using a tree of class loaders.

Permutations Sum(xi)

You have given "k" dice. How many way you can get a sum "S" and yes you have to throw all the dice. Write program for this. Its same permutations program...but we have to try with all the six S(1,2,3,4,5,6) possibilities for a dice. Exit condition will be If all the dice run out. SumP(dice,sum) = SumP(dice-1,sum-i)+i (from S).