answer in psuedo code Given an array A of size n 2 that con

answer in psuedo code

Given an array A of size n - 2 that contains all the numbers 1, 2, 3, ..., n except for two. Give an algorithm to find the two missing numbers. Use a constant extra space (namely apart from the array, only a constant number of variables).

Solution

sum=0;
mul=1;
fact=1;
for(i=0;i<n-2;i++)
{
   sum+=A[i];
   mul*=A[i];
}
for(i=1;i<=n;i++)
{
   fact*=i;
}
sum = ((n*(n+1))/2) - sum;
mul = fact/mul;
det = sqrt(sum*sum-4*mul)
a = (sum+det)/2
b = (sum-det)/2

// a &b are two missing numbers.

answer in psuedo code Given an array A of size n - 2 that contains all the numbers 1, 2, 3, ..., n except for two. Give an algorithm to find the two missing num

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site