I came across this problem today, in this we have been given a sorted array a[]
in ascending order and then we find sum of the numbers 2a1, 2a2, 2a3, ..., 2an
and we need to find the minimum number of additional integers of the type 2l
(l
is non-negative) that must be required such that the sum total of all integers present equals 2k - 1
for some integer k
(k
is non-negative).
For example:
The input consists of an integer n in first line. The second input line contains n space-separated integers a(1), a(2), ..., a(n).
Example 1:
3
0 1 2
The answer in this case is 0 since 20+21+22 = 7 is already in the form of 2k-1 for k=3.
Example 2:
3
2 4 5
the answer for this case is 3 since 22+24+25=52 and to make it of 2k-1 we have 63 so required 20,21,23 as the three term.
I approached this problem to find the sum and then count the number of 1's in the binary representation and then answer equals max(a[i])-count
.
But the problem that I get is the given constraints in the problem
1 ≤ n ≤ 10^5
0 ≤ a[i] ≤ 2*10^9
Can someone help? This problem i got in a programming contest and solutions accepted in 2.6 MB memory. So,Storing or taking array bits of 2*10^9 bits would not be better.There will be a different approach or idea i think.