# Minimize given flips required to reduce N to 0

Given an integer **N**, the task is to reduce the value of **N** to **0** by performing the following operations minimum number of times:

- Flip the rightmost
**(0**bit in the binary representation of^{th})**N**. - If
**(i – 1)**bit is set, then flip the^{th}**i**bit and clear all the bits from^{th}**(i – 2)**to^{th}**0**bit.^{th}

**Examples:**

Input:N = 3Output:2Explanation:

The binary representation of N (= 3) is 11

Since 0^{th}bit in binary representation of N(= 3) is set, flipping the 1^{st}bit of binary representation of N modifies N to 1(01).

Flipping the rightmost bit of binary representation of N(=1) modifies N to 0(00).

Therefore, the required output is 2

Input:N = 4Output:7

**Approach:** The problem can be solved based on the following observations:

1 -> 0 => 1

10 -> 11 ->01-> 00 => 2 + 1 = 3

100 -> 101 -> 111 -> 110 ->010-> … => 4 + 2 + 1 = 7

1000 -> 1001 -> 1011 -> 1010 -> 1110 -> 1111 -> 1101 -> 1100 ->0100-> … => 8 + 7 = 15

Therefore, for N = 2^{N}total (2^{(N + 1)}– 1) operations required.

If N is not a power of 2, then the recurrence relation is:

MinOp(N) = MinOp((1 << cntBit) – 1) – MinOp(N – (1 << (cntBit – 1)))

cntBit = total count of bits in binary representation of N.

MinOp(N) denotes minimum count of operations required to reduce N to 0.

Follow the steps below to solve the problem:

- Calculate the count of bits in binary representation of
**N**using**log**._{2}(N) + 1 - Use the above recurrence relation and calculate the minimum count of operations required to reduce
**N**to**0**.

Below is the implementation of the above approach.

## C++

`// C++ program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the minimum count of` `// operations required to Reduce N to 0` `int` `MinOp(` `int` `N)` `{` ` ` `if` `(N <= 1)` ` ` `return` `N;` ` ` `// Stores count of` ` ` `// bits in N` ` ` `int` `bit = log2(N) + 1;` ` ` `// Recurrence relation` ` ` `return` `((1 << bit) - 1)` ` ` `- MinOp(N - (1 << (bit - 1)));` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 4;` ` ` `cout << MinOp(N);` ` ` `return` `0;` `}` |

## Java

`// Java program to implement` `// the above approach` `class` `GFG{` ` ` `// Function to find the minimum count of` `// operations required to Reduce N to 0` `public` `static` `int` `MinOp(` `int` `N)` `{` ` ` `if` `(N <= ` `1` `)` ` ` `return` `N;` ` ` ` ` `// Stores count of` ` ` `// bits in N` ` ` `int` `bit = (` `int` `)(Math.log(N) /` ` ` `Math.log(` `2` `)) + ` `1` `;` ` ` ` ` `// Recurrence relation` ` ` `return` `((` `1` `<< bit) - ` `1` `) - MinOp(` ` ` `N - (` `1` `<< (bit - ` `1` `)));` `}` `// Driver code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `N = ` `4` `;` ` ` ` ` `System.out.println(MinOp(N));` `}` `}` `// This code is contributed by divyeshrabadiya07` |

## Python3

`# Python program to implement` `# the above approach` `# Function to find the minimum count of` `# operations required to Reduce N to 0` `import` `math` `def` `MinOp(N):` ` ` `if` `(N <` `=` `1` `):` ` ` `return` `N;` ` ` `# Stores count of` ` ` `# bits in N` ` ` `bit ` `=` `(` `int` `)(math.log(N) ` `/` `math.log(` `2` `)) ` `+` `1` `;` ` ` `# Recurrence relation` ` ` `return` `((` `1` `<< bit) ` `-` `1` `) ` `-` `MinOp(N ` `-` `(` `1` `<< (bit ` `-` `1` `)));` `# Driver code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `N ` `=` `4` `;` ` ` `print` `(MinOp(N));` `# This code is contributed by 29AjayKumar` |

## C#

`// C# program to implement` `// the above approach ` `using` `System;` `class` `GFG{` ` ` `// Function to find the minimum count of` `// operations required to Reduce N to 0` `public` `static` `int` `MinOp(` `int` `N)` `{` ` ` `if` `(N <= 1)` ` ` `return` `N;` ` ` ` ` `// Stores count of` ` ` `// bits in N` ` ` `int` `bit = (` `int` `)(Math.Log(N) /` ` ` `Math.Log(2)) + 1;` ` ` ` ` `// Recurrence relation` ` ` `return` `((1 << bit) - 1) - MinOp(` ` ` `N - (1 << (bit - 1)));` `}` ` ` `// Driver code` `public` `static` `void` `Main()` `{` ` ` `int` `N = 4;` ` ` ` ` `Console.WriteLine(MinOp(N));` `}` `}` `// This code is contributed by sanjoy_62` |

## Javascript

`<script>` `// javascript program to implement` `// the above approach` `// Function to find the minimum count of` `// operations required to Reduce N to 0` `function` `MinOp(N)` `{` ` ` `if` `(N <= 1)` ` ` `return` `N;` ` ` ` ` `// Stores count of` ` ` `// bits in N` ` ` `let bit = (Math.log(N) /` ` ` `Math.log(2)) + 1;` ` ` ` ` `// Recurrence relation` ` ` `return` `((1 << bit) - 1) - MinOp(` ` ` `N - (1 << (bit - 1)));` `}` `// Driver code` ` ` `let N = 4;` ` ` `document.write(MinOp(N));` ` ` ` ` `// This code is contributed by souravghosh0416.` `</script>` |

**Output:**

7

**Time Complexity:** O(log(N))**Auxiliary Space:** O(1)