Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Given an array A[] of measurement N such that each factor is a optimistic energy of two. In a single operation, select a nonempty subsequence of the array and multiply all the weather of the subsequence with any optimistic integer such that the sum of the array turns into an influence of two. The duty is to reduce this operation.
Examples:
Enter: S = {4, 8, 4, 32}
Output:
1
5
{4}
Rationalization: Select a subsequence {4} and multiplying it by 5
turns the array into [20, 8, 4, 32], whose sum is 64 = 2^{6}.
Therefore minimal variety of operations required to make
sum of sequence into energy of two is 1.Enter: S = {2, 2, 4, 8}
Output: 0
Rationalization: The array already has a sum 16 which is energy of two.
Strategy: The issue will be solved primarily based on the next statement:
Observations:
If the sum of sequence is already energy of two, we’d like 0 operations.
In any other case, we will make do itusing precisely one operation.
 Let S be the sum of the sequence. We all know that S shouldn’t be an influence of two. After some variety of operations, allow us to assume that we obtain a sum of two^{r}.
 S < 2^{r}: It is because, in every operation we multiply some subsequence with a optimistic integer. This may enhance the worth of the weather of that subsequence and thus the general sum.
 Which means now we have to extend S a minimum of to 2^{r} such that 2^{r−1 }< S < 2^{r}. For this, we have to add 2^{r }− S to the sequence.
Let M denote the smallest factor of the sequence. Then 2^{r} − S is a a number of of M.To transform S to 2^{r}, we will merely multiply M by (2^{r }− (S − M)) / M. This may take just one operation and make the sum of sequence equal to an influence of two. The chosen subsequence within the operation solely consists of M.
Comply with the steps talked about beneath to implement the concept:
Beneath is the implementation of the above strategy:

Time Complexity: O(N)
Auxiliary House: O(1)