Saturday, September 24, 2022
Software Development

# Reduce subsequence multiplication to transform Array sum to be energy of two

Given an array A[] of dimension N such that each component is a constructive energy of two. In a single operation, select a non-empty subsequence of the array and multiply all the weather of the subsequence with any constructive 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

{4}
Clarification: Select a subsequence {4} and multiplying it by 5
turns the array into [20, 8, 4, 32], whose sum is 64 = 26
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
Clarification: The array already has a sum 16 which is energy of two.

Strategy: The issue will be solved primarily based on the next remark:

Observations:

If the sum of sequence is already energy of two, we want 0 operations.

In any other case, we are able to make do itusing precisely one operation.

• Let S be the sum of the sequence. We all know that S isn’t an influence of two. After some variety of operations, allow us to assume  that we obtain a sum of twor.
• S < 2r: It’s because, in every operation we multiply some subsequence with a constructive integer. This could enhance the worth of the weather of that subsequence and thus the general sum.
• Which means we’ve got to extend S a minimum of to 2r such that 2r−1 < S < 2r. For this, we have to add 2r − S to the sequence.
Let M denote the smallest component of the sequence. Then 2r − S is a a number of of M.

To transform S to 2r, we are able to merely multiply M by (2r − (S − M)) / M. This could 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 under to implement the thought:

• Test if the sequence is already the facility of two, the reply is 0.
• In any other case, we require just one operation
• Let S be the sum of the sequence (the place S isn’t an influence of two) and r be the smallest integer such that S < 2r
• In a single operation, we select the smallest variety of the sequence (M) and multiply it with X = 2r − (S − M) / M.
• Print the component whose worth is minimal within the array.

Beneath is the implementation of the above method:

## Java

 ` `  `import` `java.io.*;` `import` `java.util.*;` `class` `GFG {` ` `  `    ` `    ` `    ` `    ``public` `static` `void` `minOperation(``int` `a[], ``int` `n)` `    ``{` `        ``lengthy` `sum = ``0``;` `        ``int` `idx = ``0``;` `        ``lengthy` `min = Lengthy.MAX_VALUE;` `        ``for` `(``int` `i = ``0``; i < n; i++) {` `            ``sum += a[i];` `            ``if` `(a[i] < min) {` `                ``min = a[i];` `                ``idx = i + ``1``;` `            ``}` `        ``}` `        ``lengthy` `energy = (``lengthy``)(Math.log(sum) / Math.log(``2``));` `        ``if` `(sum == (``lengthy``)Math.pow(``2``, energy)) {` `            ``System.out.println(``0``);` `        ``}` `        ``else` `{` `            ``System.out.println(``1``);` `            ``System.out.println((` `                ``((``lengthy``)(Math.pow(``2``, energy + ``1``) - sum) / min)` `                ``+ ``1``));` `            ``System.out.println(a[idx - ``1``]);` `        ``}` `    ``}` ` `  `    ` `    ``public` `static` `void` `principal(String[] args)` `    ``{` `        ``int` `A[] = { ``4``, ``8``, ``4``, ``32` `};` `        ``int` `N = A.size;` ` `  `        ` `        ``minOperation(A, N);` `    ``}` `}`

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