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

前端 未结 12 1036
梦谈多话
梦谈多话 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条回答
  •  萌比男神i
    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.

提交回复
热议问题