Halve Values Until All Are at Most k
Updated
Problem statement
You are given an array nums of non-negative numbers, which may have fractional parts, and a positive number k. In one operation you pick any element of nums and replace it with half of its value, with no rounding (for example, 5 becomes 2.5). The same element may be picked again later.
Return the smallest number of operations after which every element of nums is at most k.
Function
minHalvings(nums: double[], k: double) → intExamples
Example 1
nums = [10.0,3.0,4.5]k = 2.5return = 410 needs two halvings (10, 5, 2.5) and 3 needs one (3, 1.5). 4.5 needs one (2.25). The total is 2 + 1 + 1 = 4.
Example 2
nums = [0.5,1.25,2.0]k = 2.0return = 0Every number is already at most 2 (2 equals k, which is allowed), so no halving is needed and the answer is 0.
Example 3
nums = [100.0]k = 0.75return = 8100 becomes 50, 25, 12.5, 6.25, 3.125, 1.5625 and then 0.78125, which is still above 0.75, so one more halving gives 0.390625. That is 8 halvings.
Constraints
1 <= nums.length <= 20000 <= nums[i] <= 10^9, with at most 6 digits after the decimal point0.001 <= k <= 10^9, with at most 3 digits after the decimal point
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Maximum Number of Events That Can Be AttendedByteDance · PHONE SCREENByteDance interview questions
- Greedy Person-Cake Matching DistanceSnowflake · PHONE SCREENSnowflake interview questions
- Defer Expenses to Preserve BalanceDRW · PHONE SCREENDRW interview questions
- Minimum Operations to Make Every Array Element OneOtter.ai · PHONE SCREENOtter.ai interview questions
- Maximize Adjacent Difference With One ReversalAmazon · PHONE SCREEN
- Minimize Array Sum After K Halving OperationsDocuSign · PHONE SCREENDocuSign interview questions
- IPOTekion · PHONE SCREENTekion interview questions
- Automation Pipeline, Part 3: Stage DurationPlaid · PHONE SCREENPlaid interview questions
Interview reports that mention this question
Adobe interview experiences
- Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
- Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
- Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
- Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)
- Adobe Product Intern Interviews: 15 Experiences (Part 3 of 4)