You’re given an array of N 64 bit integers. N may be very large. You know that every integer 1..N appears once in the array, except there is one integer missing and one integer duplicated.
Write a linear time algorithm to find the missing and duplicated numbers. Further, your algorithm should run in small constant space and leave the array untouched.
If all numbers were present in the array the sum would be
N(N+1)/2.Determine the actual sum by summing up all numbers in the array in O(n), let this be
Sum(Actual).One number is missing, let this be
jand one number is duplicated, let this bek. That means thatderived from that
Also we can calculate the sum of squares in the array, which would sum up to
n3/3 + n2/2 + n/6 if all numbers were present.
Now we can calculate the actual sum of squares in O(n), let this be
Sum(Actual Squares).Now we have two equations with which we can determine
jandk.