Problem · Math
Get Minimum Operations
Starting from 0, add 1, then multiply by 2 three times. It takes a minimum of 4 operations to get to 8, so store 4 in index 0 of the return array.
Complete the function getMinOperations in the editor with the following parameter(s):
kValues[n]: the values to match
Returns
int[n]: answers to a list of queries in the given order
Constraints
- 1 ≤ n ≤ 10^4
- 0 ≤ kValues ≤ 10^16
Deepest thanks to an old friend for their kind help, with sincere, boundless gratitude. 🌷
Examples
01 · Example 1
kValues = [5, 3] return = [4, 3]
0. To get from 0 to
kValues[0] = 5, ADD_1 → MULTIPLY_2 → MULTIPLY_2 → ADD_1 to get 0 + 1 → 1 × 2 → 2 × 2 → 4 + 1 = 5. Because it took four operations, store 4 in index 0 of the return array.
1. To get from 0 to kValues[1] = 3, ADD_1 → MULTIPLY_2 → ADD_1 to get 0 + 1 → 1 × 2 → 2 + 1 = 3. Because it took three operations, store 3 in index 1 of the return array.More JPMorgan Chase problems
public int[] getMinOperations(int[] kValues) {
// write your code here
}
kValues[5, 3]
expected[4, 3]
sign in to submit