Minimum Power-of-Two Adjustments
Updated
Problem statement
Given a positive integer n, return the minimum number of operations needed to reduce it to zero. In one operation, add or subtract 2^i for any integer i >= 0. Each addition or subtraction costs one operation.
For this exercise, intermediate values are nonnegative and may exceed the starting value. Removing factors of two in an algorithm is a reasoning step, not an allowed free operation on the actual number.
Function
minPowerAdjustments(n: int) → intExamples
Example 1
n = 14return = 2Add 2 to obtain 16, then subtract 16.
Example 2
n = 27return = 3Add 4 to obtain 31, add 1 to obtain 32, then subtract 32.
Example 3
n = 1return = 1Subtract 1 to reach zero.
Constraints
1 <= n <= 65536.- Use integer arithmetic; powers may exceed
n.
More NVIDIA Online Assessment questions
- Query a Header-Encoded MatrixOA · Seen Oct 2026
- Maximum ScoreOA · Seen Oct 2026
- Address on Same PageOA · Seen Oct 2026
- Minimize Task Resource ConsumptionOA · Seen Oct 2026
- Minimum Project Bid CostOA · Seen Oct 2026
- Count Ideal Numbers in a RangeOA · Seen Oct 2026
- Generate Bit MaskOA · Seen Oct 2026
- Reconstruct an Array from Prefix XOROA · Seen Oct 2026
- Sum of Subarray MinimumsOA · Seen Oct 2026
- Unique Integers That Sum to ZeroOA · Seen Oct 2026
Similar questions at other companies
- Minimum Halvings for Alternating ParityIBM · OAIBM interview questions
- XOR MultiplicationMicrosoft · OAMicrosoft interview questions
- Minimum Operations To Reduce To ZeroUber · OAUber interview questions
- IP Address to CIDR BlocksOpenAI · PHONE SCREENOpenAI interview questions
- IP Range to CIDR BlocksAirbnb · PHONE SCREENAirbnb interview questions
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Server Upgrade PlanningWells Fargo · OAWells Fargo interview questions
- Knights, Knaves and Their StatementsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Minimum No-Repeat Segments After One Character RemovalSalesforce · OASalesforce interview questions
- Repeated Mirror XORMathWorks · OAMathWorks interview questions