Given an integer element ‘N’, the task is to find the minimum number of operations that need to be performed to make ‘N’ equal to 1.

The allowed operations to be performed are:

- Decrement N by 1.
- Increment N by 1.
- If N is a multiple of 3, you can divide N by 3.

**Examples:**

Input:N = 4

Output:2

4 – 1 = 3

3 / 3 = 1

The minimum number of operations required is 2.

Input:N = 8

Output:3

8 + 1 = 9

9 / 3 = 3

3 / 3 = 1

The minimum number of operations required is 3.

**Approach:**

- If the number is a multiple of 3, divide it by 3.
- If the number modulo 3 is 1, decrement it by 1.
- If the number modulo 3 is 2, increment it by 1.
- There is an exception when the number is equal to 2, in this case the number should be decremented by 1.
- Repeat the above steps until the number is greater than 1 and print the count of operations performed in the end.

Below is the implementation of the above approach:

## C++

`// CPP implementation of above approach ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns the minimum ` `// number of operations to be performed ` `// to reduce the number to 1 ` `int` `count_minimum_operations(` `long` `long` `n) ` `{ ` ` ` ` ` `// To stores the total number of ` ` ` `// operations to be performed ` ` ` `int` `count = 0; ` ` ` `while` `(n > 1) { ` ` ` ` ` `// if n is divisible by 3 ` ` ` `// then reduce it to n / 3 ` ` ` `if` `(n % 3 == 0) ` ` ` `n /= 3; ` ` ` ` ` `// if n modulo 3 is 1 ` ` ` `// decrement it by 1 ` ` ` `else` `if` `(n % 3 == 1) ` ` ` `n--; ` ` ` `else` `{ ` ` ` `if` `(n == 2) ` ` ` `n--; ` ` ` ` ` `// if n modulo 3 is 2 ` ` ` `// then increment it by 1 ` ` ` `else` ` ` `n++; ` ` ` `} ` ` ` ` ` `// update the counter ` ` ` `count++; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `long` `long` `n = 4; ` ` ` `long` `long` `ans = count_minimum_operations(n); ` ` ` `cout<<ans<<endl; ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` ` ` `class` `GFG { ` ` ` ` ` `// Function that returns the minimum ` ` ` `// number of operations to be performed ` ` ` `// to reduce the number to 1 ` ` ` `static` `int` `count_minimum_operations(` `long` `n) ` ` ` `{ ` ` ` ` ` `// To stores the total number of ` ` ` `// operations to be performed ` ` ` `int` `count = ` `0` `; ` ` ` `while` `(n > ` `1` `) { ` ` ` ` ` `// if n is divisible by 3 ` ` ` `// then reduce it to n / 3 ` ` ` `if` `(n % ` `3` `== ` `0` `) ` ` ` `n /= ` `3` `; ` ` ` ` ` `// if n modulo 3 is 1 ` ` ` `// decrement it by 1 ` ` ` `else` `if` `(n % ` `3` `== ` `1` `) ` ` ` `n--; ` ` ` `else` `{ ` ` ` `if` `(n == ` `2` `) ` ` ` `n--; ` ` ` ` ` `// if n modulo 3 is 2 ` ` ` `// then increment it by 1 ` ` ` `else` ` ` `n++; ` ` ` `} ` ` ` ` ` `// update the counter ` ` ` `count++; ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `long` `n = ` `4` `; ` ` ` `long` `ans = count_minimum_operations(n); ` ` ` `System.out.println(ans); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of above approach ` ` ` `# Function that returns the minimum ` `# number of operations to be performed ` `# to reduce the number to 1 ` `def` `count_minimum_operations(n): ` ` ` ` ` `# To stores the total number of ` ` ` `# operations to be performed ` ` ` `count ` `=` `0` ` ` `while` `(n > ` `1` `) : ` ` ` ` ` `# if n is divisible by 3 ` ` ` `# then reduce it to n / 3 ` ` ` `if` `(n ` `%` `3` `=` `=` `0` `): ` ` ` `n ` `/` `/` `=` `3` ` ` ` ` `# if n modulo 3 is 1 ` ` ` `# decrement it by 1 ` ` ` `elif` `(n ` `%` `3` `=` `=` `1` `): ` ` ` `n ` `-` `=` `1` ` ` `else` `: ` ` ` `if` `(n ` `=` `=` `2` `): ` ` ` `n ` `-` `=` `1` ` ` ` ` `# if n modulo 3 is 2 ` ` ` `# then increment it by 1 ` ` ` `else` `: ` ` ` `n ` `+` `=` `1` ` ` ` ` `# update the counter ` ` ` `count ` `+` `=` `1` ` ` ` ` `return` `count ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `n ` `=` `4` ` ` `ans ` `=` `count_minimum_operations(n) ` ` ` `print` `(ans) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` ` ` `// Function that returns the minimum ` ` ` `// number of operations to be performed ` ` ` `// to reduce the number to 1 ` ` ` `static` `int` `count_minimum_operations(` `long` `n) ` ` ` `{ ` ` ` ` ` `// To stores the total number of ` ` ` `// operations to be performed ` ` ` `int` `count = 0; ` ` ` `while` `(n > 1) { ` ` ` ` ` `// if n is divisible by 3 ` ` ` `// then reduce it to n / 3 ` ` ` `if` `(n % 3 == 0) ` ` ` `n /= 3; ` ` ` ` ` `// if n modulo 3 is 1 ` ` ` `// decrement it by 1 ` ` ` `else` `if` `(n % 3 == 1) ` ` ` `n--; ` ` ` `else` `{ ` ` ` `if` `(n == 2) ` ` ` `n--; ` ` ` ` ` `// if n modulo 3 is 2 ` ` ` `// then increment it by 1 ` ` ` `else` ` ` `n++; ` ` ` `} ` ` ` ` ` `// update the counter ` ` ` `count++; ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main (){ ` ` ` ` ` `long` `n = 4; ` ` ` `long` `ans = count_minimum_operations(n); ` ` ` `Console.WriteLine(ans); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of above approach ` ` ` `// Function that returns the minimum ` `// number of operations to be performed ` `// to reduce the number to 1 ` `function` `count_minimum_operations(` `$n` `) ` `{ ` ` ` ` ` `// To stores the total number of ` ` ` `// operations to be performed ` ` ` `$count` `= 0; ` ` ` `while` `(` `$n` `> 1) ` ` ` `{ ` ` ` ` ` `// if n is divisible by 3 ` ` ` `// then reduce it to n / 3 ` ` ` `if` `(` `$n` `% 3 == 0) ` ` ` `$n` `/= 3; ` ` ` ` ` `// if n modulo 3 is 1 ` ` ` `// decrement it by 1 ` ` ` `else` `if` `(` `$n` `% 3 == 1) ` ` ` `$n` `--; ` ` ` `else` ` ` `{ ` ` ` `if` `(` `$n` `== 2) ` ` ` `$n` `--; ` ` ` ` ` `// if n modulo 3 is 2 ` ` ` `// then increment it by 1 ` ` ` `else` ` ` `$n` `++; ` ` ` `} ` ` ` ` ` `// update the counter ` ` ` `$count` `++; ` ` ` `} ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver code ` `$n` `= 4; ` ` ` `$ans` `= count_minimum_operations(` `$n` `); ` `echo` `$ans` `, ` `"\n"` `; ` ` ` `// This code is contributed by akt_mit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum number of given operations required to reduce the array to 0 element
- Minimum number of operations required to reduce N to 0
- Count the number of operations required to reduce the given number
- Count operations of the given type required to reduce N to 0
- Reduce N to 1 with minimum number of given operations
- Minimum steps required to reduce all the elements of the array to zero
- Minimum Decrements on Subarrays required to reduce all Array elements to zero
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Minimum steps to reduce N to 0 by given operations
- Reduce a number to 1 by performing given operations | Set 2
- Min number of operations to reduce N to 0 by subtracting any digits from N
- Min operations to reduce N by multiplying by any number or taking square root
- Reduce a given number to form a key by the given operations
- Minimum number of operations required to delete all elements of the array
- Minimum number operations required to convert n to m | Set-2
- Find the minimum number of operations required to make all array elements equal
- Minimum number of operations required to obtain a given Binary String
- Minimum number of steps required to obtain the given Array by the given operations
- Minimize subarray increments/decrements required to reduce all array elements to 0
- Find maximum operations to reduce N to 1

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.