Minimum Operations to Reduce an Integer to Zero
Updated
Problem statement
Given a positive integer n, return the minimum number of operations needed to reduce it to zero.
In one operation, you may add or subtract 2^b for any nonnegative integer b. Intermediate values may be larger than the starting value. Count only these additions and subtractions; rewriting a number in binary is not an operation.
Function
minOperations(n: int) → intExamples
Example 1
n = 39return = 3One optimal sequence is 39 - 32 = 7, 7 + 1 = 8, then 8 - 8 = 0, using 3 operations.
Example 2
n = 54return = 3Use 54 + 2 = 56, 56 + 8 = 64, then 64 - 64 = 0.
Example 3
n = 8return = 1Subtract 2^3 = 8 once.
Constraints
- For this exercise, assume
1 <= n <= 10^5. - Each operation adds or subtracts one power of two.
More Microsoft Online Assessment questions
- Minimum Cost to Process Priority TasksOA · Seen Nov 2026
- Most Visited Sprint MarkerOA · Seen Nov 2026
- Maximum Escape Game ScoreOA · Seen Nov 2026
- Count Subarrays with a Perfect-Square ProductOA · Seen Nov 2026
- Maximum Interactive Team SizeOA · Seen Nov 2026
- Maximize Pipeline Throughput Under a Scaling BudgetOA · Seen Nov 2026
- Minimum Cost K-Capable ModelsOA · Seen Nov 2026
- Find Available Meeting RoomsOA · Seen Oct 2026
- Maximum Sum of a K-StarOA · Seen Oct 2026
- Capacity to Ship Packages Within DaysOA · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Minimum No-Repeat Segments After One Character RemovalSalesforce · OASalesforce interview questions
- Minimize the Maximum Array ElementChubb · OAChubb interview questions
- Minimum Halvings for Alternating ParityIBM · OAIBM interview questions
- Server SelectionPoint72 · OAPoint72 interview questions
- Minimum Servers for an Exact LoadJP Morgan Chase · OAJP Morgan Chase interview questions
- Minimum Operations To Reduce To ZeroUber · OAUber interview questions
- Maximize PartitionsAmazon · OA
- Maximum XOR SuffixTikTok · OATikTok interview questions
- Minimum Power-of-Two AdjustmentsNVIDIA · OANVIDIA interview questions
- Fewest Steps to Build an Array from ZerosExpedia · OAExpedia interview questions