Minimum Operations To Reduce To Zero
Updated
Problem statement
Given a positive integer n, you can either add or subtract 2^i in a single operation where i >= 0. Determine the minimum number of operations required to reduce n to 0.
Find the minimum number of operations required to convert n to 0.
- Constraints
1 <= n < 2^40
Function
getMinOperations(n: long) → intExamples
Example 1
n = 6return = 2n can be reduced to 0 using two operations:
- Choose
i = 1and subtract2^1from6, so6 - 2 = 4. - Choose
i = 2and subtract2^2from4, so4 - 4 = 0.
The answer is 2.
Example 2
n = 21return = 3One sequence of operations is:
- Choose
i = 0, subtract2^0 = 1from21, converting it to20. - Choose
i = 2, subtract2^2 = 4from20, converting it to16. - Choose
i = 4, subtract2^4 = 16from16, converting it to0.
More Uber Online Assessment questions
- Count Palindromic Ancestor PathsOA · Seen Aug 2026
- Jump Game with Prime-3 StepsOA · Seen Aug 2026
- Last Truck to Leave the LaneOA · Seen Jul 2026
- Chain of CommandOA · Seen Jul 2026
- Total Palindrome Substring CostOA · Seen Jun 2026
- Jump Game with Prime-Step RuleOA · Seen May 2026
- Pipeline Throughput Maximization Under BudgetOA · Seen May 2026
- Balanced Prefix Sets in a PermutationOA · Seen Apr 2026
- Minimum Edge Reversals to Root a TreeOA · Seen Apr 2026
- Maximum Riders in CarOA · Seen Apr 2026
Similar questions at other companies
- Minimum Halvings for Alternating ParityIBM · OAIBM interview questions
- XOR MultiplicationMicrosoft · OAMicrosoft interview questions
- Get Minimum OperationsJP Morgan Chase · OAJP Morgan Chase interview questions
- Minimum Power-of-Two AdjustmentsNVIDIA · OANVIDIA interview questions
- Match an XOR-Adjusted SumSquarepoint Capital · OASquarepoint Capital interview questions
- Integer BreakAmazon · ONSITE INTERVIEW
- Minimum Cost for a Target WeightPace Stock Broking Services · PHONE SCREEN
- IP Address to CIDR BlocksOpenAI · PHONE SCREENOpenAI interview questions
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Server Upgrade PlanningWells Fargo · OAWells Fargo interview questions