Minimum number of operations required to reduce N to 1 | Java implementation of above approach ; Function that returns the minimum number of operations to be performed to reduce the number to 1 ; Base cases ; Driver code
import java . util . * ; class GFG { public static int count_minimum_operations ( int n ) { if ( n == 2 ) { return 1 ; } else if ( n == 1 ) { return 0 ; } if ( n % 3 == 0 ) { return 1 + count_minimum_operations ( n / 3 ) ; } else if ( n % 3 == 1 ) { return 1 + count_minimum_operations ( n - 1 ) ; } else { return 1 + count_minimum_operations ( n + 1 ) ; } } public static void main ( String [ ] args ) { int n = 4 ; int ans = count_minimum_operations ( n ) ; System . out . println ( ans ) ; } }