Company: JPMorgan Chase
Difficulty: easy
Minimum Operations To Reach Each Value A counter starts at the value 0 . In a single operation you may do exactly one of the following: add 1 to the current value, or multiply the current value by 2 . You are given a list kValues of n independent targets. For every target you restart the counter at 0 and ask the same question: what is the minimum number of operations needed to turn 0 into that target? Return an array holding the answers to the queries in the given order. For example, with the target 8 you can go 0 -> (add 1) -> 1 -> (multiply by 2) -> 2 -> (multiply by 2) -> 4 -> (multiply by 2) -> 8 . That is 4 operations, and no shorter sequence reaches 8 , so 4 is stored in index 0 of the returned array. Function getMinOperations(kValues: int[]) -> int[] 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