Minimum Operations to Halve an Array Sum
Updated
Problem statement
Given a nonempty array of positive integers values, one operation chooses one position and replaces its current value x with floor(x / 2).
Let originalSum be the sum before any operations and currentSum the sum afterward. Return the minimum number of operations needed until 2 * currentSum <= originalSum. The same position may be chosen more than once.
Function
minimumHalvingOperations(values: int[]) → intExamples
Example 1
values = [7,4,8,1]return = 3The original sum is 20. Halving 8, then 7, then a current 4 reaches sum 10 in three operations.
Example 2
values = [10]return = 1Replacing 10 by 5 reaches exactly half the original sum.
Constraints
1 <= values.length <= 100000.1 <= values[i] <= 1000000000.
More Grab Phone Screen questions
Similar questions at other companies
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Maximum Number of Events That Can Be AttendedByteDance · PHONE SCREENByteDance interview questions
- Defer Expenses to Preserve BalanceDRW · PHONE SCREENDRW interview questions
- Minimize Array Sum After K Halving OperationsDocuSign · PHONE SCREENDocuSign interview questions
- Maximum Number of Events That Can Be AttendedSnowflake · PHONE SCREENSnowflake interview questions
- Automation Pipeline, Part 3: Stage DurationPlaid · PHONE SCREENPlaid interview questions
- Minimum Sum After K Ceiling HalvingsAdobe · PHONE SCREENAdobe interview questions
- Furthest Building with Bricks and LaddersAmazon · PHONE SCREEN
- Schedule Unit-Time Jobs for the Most ProfitScale AI · PHONE SCREENScale AI interview questions
- Minimum Meeting RoomsSentry · PHONE SCREENSentry interview questions