#C6743. Minimum Operations to Transform Array
Minimum Operations to Transform Array
Minimum Operations to Transform Array
You are given an array of N integers. Your task is to perform a series of operations on the array so that for every index i (with i > 0), the following condition holds:
\(a_i = 2 \times a_{i-1}\)
In a single operation, you may either multiply an element by 2 or perform an integer division by 2 (i.e. floor division) on an element. The goal is to achieve the above condition for the entire array using the minimum number of operations. If it is impossible to transform the array accordingly, output -1
.
Note: All operations should bring the current element closer to the required target value given by the formula \(a_i = 2 \times a_{i-1}\).
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer T, the number of test cases.
- For each test case, the first line contains an integer N, the number of elements in the array.
- The next line contains N space-separated integers representing the array.
outputFormat
For each test case, output a single integer on a new line: the minimum number of operations required to transform the array into one satisfying \(a_i = 2 \times a_{i-1}\) for every i > 0. If it is impossible, output -1
.
2
3
4 8 16
4
1 2 8 16
0
2
</p>