Teaching Kids Programming – Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof)


Teaching Kids Programming: Videos on Data Structures and Algorithms

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 == 2i where i >= 0.

Example 1:
Input: n = 39
Output: 3
Explanation: We can do the following operations:
– Add 20 = 1 to n, so now n = 40.
– Subtract 23 = 8 from n, so now n = 32.
– Subtract 25 = 32 from n, so now n = 0.
It can be shown that 3 is the minimum number of operations we need to make n equal to 0.

Example 2:
Input: n = 54
Output: 3
Explanation: We can do the following operations:
– Add 21 = 2 to n, so now n = 56.
– Add 23 = 8 to n, so now n = 64.
– Subtract 26 = 64 from n, so now n = 0.
So the minimum number of operations is 3.

Constraints:
1 <= n <= 10^5

Minimum Operations to Reduce an Integer to 0 (Math Analysis + Another Recursion)

Let tex_878f71de1e4f1a6da961d533f43d026e Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video be the minimum number of operations to bring tex_07baee26b11d17fadc32609a4e1b0565 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video down to zero. With every operation, we can add or subtract any power of two tex_35d16fe69cc36a7681eec99584d47087 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video . Let’s rewrite it:

tex_7921ce3dcaa63977989d51184e90a6ff Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video where tex_f251a37d753540e4666f2a80d17d9aab Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video

Then the number of operations equals tex_db07ca3eb60111947c6b6a516c13b5a0 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video

f(n) when n is even number

If tex_07baee26b11d17fadc32609a4e1b0565 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video is even number, i.e. tex_9cf50f9b06802a41f633676ca1c8e631 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video

Then, tex_ef4972814950c3bcc2085702217b8ee4 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video

If we divide two on both sides tex_1a8e8726bea0de7b1c62990c418efaab Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video

so tex_b92d7c7ea47d4a58010a9cb2f037fa9d Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video for any given integer n’ because we have a one-to-one mapping between tex_03d2f292a1eeb096e1a6aef633014d5a Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video and tex_6c2f32c59f7d4ca2e18aa15d668f5046 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video so the tex_878f71de1e4f1a6da961d533f43d026e Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video must be optimal as tex_6c2f32c59f7d4ca2e18aa15d668f5046 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video otherwise we could just optimize it.

f(n) when n is odd number

If tex_07baee26b11d17fadc32609a4e1b0565 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video is odd number, i.e. tex_a0cca47ff86c78a660f3692399828acd Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video

Then, tex_a500c86063062f768cd28c0e10a9da50 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video or tex_ae9b2e2a6796ab24ce139fee6685044b Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video

if tex_9f9c12f8ca2ab202d89426c52cb14222 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video , we can subtract one and divide by two on both sides: tex_5449c3efe24dc2185c224dc765e51824 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video
if tex_4eef49a7f43a9f64047df381ff0b1964 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video , we can add one and divide by two on both sides: tex_2fb76d6d6d97e87931d2253a25a35c56 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video

So, for tex_878f71de1e4f1a6da961d533f43d026e Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video we must search in tex_6c2f32c59f7d4ca2e18aa15d668f5046 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video and tex_5acf8c811741d9c1353b463d188847a6 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video .

Thus we have a recursive formula for tex_878f71de1e4f1a6da961d533f43d026e Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video where we can reduce n by half:

tex_a7b4f7615bc709e4eee06d7660728870 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video when tex_7d7acf6300319c49becd64dde4e59ef2 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video is even and:
tex_e7f767c65adb60c19f71eccdc21f4302 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video when tex_509b82a35348f253da185b2d368c6886 Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video is odd.

We can then implement the above formula using Top Down Dynamic Programming (Recursion with Memoization):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
    def minOperations(self, n: int) -> int:
        @cache
        def f(x):
            if x == 0:
                return 0
            if x == 1:
                return 1            
            y = x // 2
            if x & 1:                
                return min(f(y), f(y + 1)) + 1
            return f(y)
 
        return f(n)
class Solution:
    def minOperations(self, n: int) -> int:
        @cache
        def f(x):
            if x == 0:
                return 0
            if x == 1:
                return 1            
            y = x // 2
            if x & 1:                
                return min(f(y), f(y + 1)) + 1
            return f(y)

        return f(n)

The time/space complexity is O(LogN).

The following are easy to understand/observe:

  • The order of operations do not matter, for example, 7=-1+8 or 7=8-1
  • We can not have additions of same power of twos, for example: +4+4=+8, the former needs two steps, and the latter needs one step. tex_a25ca5105c6bd41e9d8935de2d4a600f Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video
  • Similarly, we cannot have subtraction off the same power of twos.
  • And, also we cannot have addition and subtraction of the same power of twos, since the sum will be zero.

From the definition, we can easily prove that:
tex_696b39cfa875f75619ac7bed8a17685a Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof) algorithms Dynamic Programming math programming languages Python python Recursion teaching kids programming youtube video

For example, to transform x to x+1, we need one step.

Minimum Operations to Reduce an Integer to 0

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
1823 words
Last Post: Teaching Kids Programming - Minimum Operations to Reduce an Integer to 0 (Greedy/Iterative Algorithm)
Next Post: Starting a Business with Telegram Bots: Creative Ideas to Inspire You

The Permanent URL is: Teaching Kids Programming – Minimum Operations to Reduce an Integer to 0 (Recursion with Math Proof)

Leave a Reply