Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find 2 missing numbers in an array of integers with two missing values

How do you do this? The values are unsorted but are of [1..n] Example array [3,1,2,5,7,8]. Answer: 4, 6

I saw this solution in another similar post, but I do not understand the last step:

  • Find the sum of the numbers S=a1+...+an.
  • Also find the sum of squares T=a1²+...+an².
  • You know that the sum should be S'=1+...+n=n(n+1)/2
  • You know that the sum of squares should be T'=1²+...+n²=n(n+1)(2n+1)/6.
  • Now set up the following system of equations x+y=S'-S, x²+y²=T'-T.
  • Solve by writing x²+y²=(x+y)²-2xy => xy=((S'-S)²-(T'-T))/2.
  • And now the numbers are merely the roots of the quadratic in z: z²-(S'-S)z+((S'-S)²-(T'-T))/2=0.

What is the explanation for setting up that quadratic equation in the final step with z as the unknown? What's the intuition behind that being the solution to this problem?

like image 508
ordinary Avatar asked Nov 17 '13 01:11

ordinary


2 Answers

This method is not advisable as it suffers from integer overflow problems. So use XOR method to find the two numbers, which is highly performant. If you are interested i can explain.

As per the request from @ordinary below, i am explaining the algorithm:

EDIT

Suppose the maximum element of the array a[] is B i.e. suppose a[]={1,2,4} and here 3 and 5 are not present in a[] so max element is B=5.

  • xor all the elements of the array a to X
  • xor all the elements from 1 to B to x
  • find the left most bit set of x by x = x &(~(x-1));
  • Now if a[i] ^ x == x than xor a[i] to p else xor with q
  • Now for all k from 1 to B if k ^ x == x than xor with p else xor with q
  • Now print p and q

proof:

Let a = {1,2,4} and B is 5 i.e. from 1 to 5 the missing numbers are 3 and 5

Once we XOR elements of a and the numbers from 1 to 5 we left with XOR of 3 and 5 i.e. x.

Now when we find the leftmost bit set of x it is nothing but the left most different bit among 3 and 5. (3--> 011, 5 --> 101 and x = 010 where x = 3 ^ 5)

After this we are trying to divide into two groups according to the bit set of x so the two groups will be:

p = 2 , 2 , 3 (all has the 2nd last bit set)

q = 1, 1, 4, 4, 5 (all has the 2nd last bit unset)

if we XOR the elements of p among themselves we will find 3 and similarly if we xor all the elements of q among themselves than we will get 5. Hence the answer.

code in java

public void findNumbers(int[] a, int B){
    int x=0;
    for(int i=0; i<a.length;i++){
        x=x^a[i];
    }
    for(int i=1;i<=B;i++){
        x=x^i;
    }
    x = x &(~(x-1));
    int p=0, q=0;
    for(int i=0;i<a.length;i++){
        if((a[i] & x) == x){
            p=p^a[i];
        }
        else{
            q=q^a[i];
        }   
    }
    for(int i=1;i<=B;i++){
        if((i & x) == x){
            p=p^i;
        }
        else{
            q=q^i;
        }
    }

    System.out.println("p: "+p+" : "+q);
}
like image 73
Trying Avatar answered Sep 22 '22 15:09

Trying


I have an algorithm for above problem.

Suppose

Actual Series: 1 2 3 4 5 6          a:sum=21 product= 720
Missing Number series: 1 * 3 4 * 6  b:sum=14 product= 72

a+b=21-14= 7 , ab=720/72=10

Now we need to find a-b= sqrt[(a+b)^2 -4ab].

If you calculate:

a-b= 3

Now

a+b=7
a-b=3

Add both equations:

2a=10, a=5

then b=7-5=2 so, 2 and 5 are missing.

like image 40
Manik Gupta Avatar answered Sep 21 '22 15:09

Manik Gupta