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

前端 未结 12 907
梦谈多话
梦谈多话 2021-01-30 09:33

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

相关标签:
12条回答
  • 2021-01-30 10:13

    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);
    }
    
    0 讨论(0)
  • 2021-01-30 10:15

    Let x and y be the roots of a quadratic equation.

    • Sum of the roots, SUM = x + y
    • Product of the roots, PRODUCT = x * y

    If we know the sum and the product, we can reconstruct the quadratic equation as:

    z^2 - (SUM)z + (PRODUCT) = 0
    

    In the algorithm you mentioned, we find the sum and the product, and from that, we reconstruct the quadratic equation using the above formula.

    If you are interested in a detailed derivation, here is a reference. Read "Reconstruction of the quadratic equation from the sum and product of roots".

    0 讨论(0)
  • 2021-01-30 10:15
    public class MissingNumber{
    
    static int[] array = { 1, 3, 5 };
    
    public static void getMissingNumber() {
    
    for (int i = 0; i < array.length; i++)
        System.out.println(array[i] + " ");
    
    System.out.println("The Missing Number is:");
     int j = 0;
    for (int i = 1; i <= 5; i++) {
        if (j < array.length && i == array[j])
        j++;
        else
        System.out.println(i + " ");
    }
    
    }
    public static void main(String[] args) {
    getMissingNumber();
    }
    

    }

    0 讨论(0)
  • 2021-01-30 10:19
    Below is the generic answer in java code for any number of missing numbers in a given array
    //assumes that there are no duplicates
    a = [1,2,3,4,5]
    b = [1,2,5]
    a-b=[3,4]
    
    public list<integer> find(int[] input){
      int[] a= new int[] {1,2,3,4,5};//create a new array without missing numbers
      List<Integer> l = new ArrayList<Integer>();//list for missing numbers
      Map<Integer,Integer> m = new HashMap<Integer>();
    
      //populate a hashmap with the elements in the new array
      for(int i=0;i<input.length;i++){  
       m.put(input[i], 1);
      }
    //loop through the given input array and check for missing numbers
     for(int i=0;i<a.length;i++){
      if (!m.contains(input[i]))
       l.add(input[i]);
    }
     return l;
    }
    
    0 讨论(0)
  • 2021-01-30 10:21

    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.

    0 讨论(0)
  • 2021-01-30 10:24
    #include <stdio.h>
    #include <math.h>
    
    /*
        the sum should be 1+...+n = n(n+1)/2
        the sum of squares should be 1^2+...+n^2 = n(n+1)(2n+1)/6.
    */
    
    void find_missing_2_numbers(int *arr, int n);
    
    int main()
    {
        int arr[] = {3,7,1,6,8,5};
    
        find_missing_2_numbers(arr, 8);
    
        return 0;
    }
    
    void find_missing_2_numbers(int *arr, int n)
    {
    
        int i, size, a, b, sum, sum_of_sqr, a_p_b, as_p_bs, a_i_b, a_m_b;
        size = n - 2;
    
        sum = 0;
        sum_of_sqr = 0;
        for (i = 0; i < size; i++)
        {
            sum += arr[i];
            sum_of_sqr += (arr[i] * arr[i]);
        }
    
        a_p_b = (n*(n+1))/2 - sum;
        as_p_bs = (n*(n+1)*(2 * n + 1))/6 - sum_of_sqr;
        a_i_b = ((a_p_b * a_p_b) - as_p_bs ) / 2;
        a_m_b = (int) sqrt((a_p_b * a_p_b) - 4 * a_i_b);
        a = (a_p_b + a_m_b) / 2;
        b = a_p_b - a;
    
        printf ("A: %d, B: %d\n", a, b);
    }
    
    0 讨论(0)
提交回复
热议问题