Advertisement

Minimum Operations to Reduce an Integer to 0 - LeetCode 2571 Solution

Minimum Operations to Reduce an Integer to 0 - Complete Solution Guide

Minimum Operations to Reduce an Integer to 0 is LeetCode problem 2571, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given a positive integer n , you can do the following operation any number of times: Add or subtract a power of 2 from n . Return the minimum number of operations to make n equal to 0 . A number x is power of 2 if x == 2 i where i >= 0 . Example 1: Input: n = 39 Output: 3 Explanation: We can do the following operations: - Add 2 0 = 1 to n, so now n = 40. - Subtract 2 3 = 8 from n, so now n = 32. - Subtract 2 5 = 32 from n, so now n = 0. It can be shown that 3 is the minimum number of ope

Detailed Explanation

The problem asks to find the minimum number of operations needed to reduce a given positive integer 'n' to 0. The only allowed operation is to add or subtract a power of 2 from 'n'. We want to find the fewest such additions and subtractions to achieve n = 0. For example, if n = 39, we can add 1 to get 40, subtract 8 to get 32, and subtract 32 to get 0, requiring 3 operations. The constraint is 1 <= n <= 10^5.

Solution Approach

The solution uses a greedy approach based on bit manipulation. It iteratively reduces 'n' to 0. In each iteration, it checks the least significant bit (LSB). If the LSB is 1, it increments the operation count and decides whether to add or subtract based on the next bit. Specifically, if the two least significant bits are '11' (n & 3 == 3), it's better to add 1 to 'n' because it sets these bits to '00', potentially leading to faster convergence. Otherwise, if the last two bits are '01', it subtracts 1 from 'n'. If the LSB is 0, it right-shifts 'n' by 1 (equivalent to dividing by 2). This process continues until 'n' becomes 0.

Step-by-Step Algorithm

  1. Step 1: Initialize a variable 'ops' to 0. This variable will store the number of operations.
  2. Step 2: While 'n' is greater than 0, repeat the following steps:
  3. Step 3: Check if the least significant bit of 'n' is 1 (n & 1 == 1).
  4. Step 4: If the LSB is 1, increment 'ops' by 1.
  5. Step 5: If the two least significant bits of 'n' are '11' (n & 3 == 3), add 1 to 'n'. This prepares for the next right shift by setting the two LSBs to 00.
  6. Step 6: Otherwise (if the two least significant bits are '01'), subtract 1 from 'n'.
  7. Step 7: If the LSB is 0, right-shift 'n' by 1 (n >>= 1). This is equivalent to dividing 'n' by 2 and moving to the next bit.
  8. Step 8: Return the value of 'ops'.

Key Insights

  • Insight 1: Each operation either adds or subtracts a power of 2. The goal is to efficiently represent the number as a sum or difference of powers of 2.
  • Insight 2: When the last two bits of 'n' are '11', adding 1 reduces the number of operations compared to subtracting 1. This is because adding 1 turns '11' into '00' and carries over, potentially eliminating several 1s at once, reducing subsequent operations. Similarly, if last two bits are '01' subtracting 1 reduces the number of operations.
  • Insight 3: We can iteratively process the number from the least significant bit, deciding whether to add or subtract a power of 2 based on the last two bits.

Complexity Analysis

Time Complexity: O(log n)

Space Complexity: O(1)

Topics

This problem involves: Dynamic Programming, Greedy, Bit Manipulation.

Companies

Asked at: IBM, Nvidia, Two Sigma.