Minimum number of operations required to reduce N to 1
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++
#include<bits/stdc++.h>
using namespace std;
int count_minimum_operations( long long n)
{
int count = 0;
while (n > 1) {
if (n % 3 == 0)
n /= 3;
else if (n % 3 == 1)
n--;
else {
if (n == 2)
n--;
else
n++;
}
count++;
}
return count;
}
int main()
{
long long n = 4;
long long ans = count_minimum_operations(n);
cout<<ans<<endl;
return 0;
}
|
Java
class GFG {
static int count_minimum_operations( long n)
{
int count = 0 ;
while (n > 1 ) {
if (n % 3 == 0 )
n /= 3 ;
else if (n % 3 == 1 )
n--;
else {
if (n == 2 )
n--;
else
n++;
}
count++;
}
return count;
}
public static void main(String[] args)
{
long n = 4 ;
long ans = count_minimum_operations(n);
System.out.println(ans);
}
}
|
Python3
def count_minimum_operations(n):
count = 0
while (n > 1 ) :
if (n % 3 = = 0 ):
n / / = 3
elif (n % 3 = = 1 ):
n - = 1
else :
if (n = = 2 ):
n - = 1
else :
n + = 1
count + = 1
return count
if __name__ = = "__main__" :
n = 4
ans = count_minimum_operations(n)
print (ans)
|
C#
using System;
public class GFG{
static int count_minimum_operations( long n)
{
int count = 0;
while (n > 1) {
if (n % 3 == 0)
n /= 3;
else if (n % 3 == 1)
n--;
else {
if (n == 2)
n--;
else
n++;
}
count++;
}
return count;
}
static public void Main (){
long n = 4;
long ans = count_minimum_operations(n);
Console.WriteLine(ans);
}
}
|
PHP
<?php
function count_minimum_operations( $n )
{
$count = 0;
while ( $n > 1)
{
if ( $n % 3 == 0)
$n /= 3;
else if ( $n % 3 == 1)
$n --;
else
{
if ( $n == 2)
$n --;
else
$n ++;
}
$count ++;
}
return $count ;
}
$n = 4;
$ans = count_minimum_operations( $n );
echo $ans , "\n" ;
?>
|
Javascript
<script>
function count_minimum_operations(n)
{
let count = 0;
while (n > 1) {
if (n % 3 == 0)
n /= 3;
else if (n % 3 == 1)
n--;
else {
if (n == 2)
n--;
else
n++;
}
count++;
}
return count;
}
let n = 4;
let ans = count_minimum_operations(n);
document.write(ans);
</script>
|
Time Complexity: O(n), where n represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Recursive Approach: The recursive approach is similar to the approach used above.
Below is the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int count_minimum_operations( long long 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);
}
}
int main()
{
long long n = 4;
long long ans = count_minimum_operations(n);
cout << ans << endl;
return 0;
}
|
Java
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);
}
}
|
Python3
def count_minimum_operations(n):
if (n = = 2 ):
return 1
elif (n = = 1 ):
return 0
if (n % 3 = = 0 ):
return 1 + count_minimum_operations(n / 3 )
elif (n % 3 = = 1 ):
return 1 + count_minimum_operations(n - 1 )
else :
return 1 + count_minimum_operations(n + 1 )
n = 4
ans = count_minimum_operations(n)
print (ans)
|
C#
using System;
class GFG {
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);
}
}
static void Main() {
int n = 4;
int ans = count_minimum_operations(n);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function count_minimum_operations(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);
}
}
let n = 4;
let ans = count_minimum_operations(n);
document.write(ans);
</script>
|
Time Complexity: O(n), where n represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Another Method (Efficient):
DP using memoization(Top down approach)
We can avoid the repeated work done by storing the operations performed calculated so far. We just need to store all the values in an array.
C++
#include <bits/stdc++.h>
using namespace std;
int static dp[1001];
int count_minimum_operations( long long n)
{
if (n == 2) {
return 1;
}
if (n == 1) {
return 0;
}
if (dp[n] != -1)
{
return dp[n];
}
if (n % 3 == 0) {
dp[n] = 1 + count_minimum_operations(n / 3);
}
else if (n % 3 == 1) {
dp[n] = 1 + count_minimum_operations(n - 1);
}
else {
dp[n] = 1 + count_minimum_operations(n + 1);
}
return dp[n];
}
int main()
{
long long n = 4;
memset (dp, -1, sizeof (dp));
long long ans = count_minimum_operations(n);
cout << ans << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int []dp = new int [ 1001 ];
public static int count_minimum_operations( int n)
{
if (n == 2 )
{
return 1 ;
}
else if (n == 1 )
{
return 0 ;
}
if (dp[n] != - 1 )
{
return dp[n];
}
if (n % 3 == 0 ) {
dp[n] = 1 + count_minimum_operations(n / 3 );
}
else if (n % 3 == 1 ) {
dp[n] = 1 + count_minimum_operations(n - 1 );
}
else {
dp[n] = 1 + count_minimum_operations(n + 1 );
}
return dp[n];
}
public static void main(String []args)
{
int n = 4 ;
for ( int i = 0 ; i < 1001 ; i++) {
dp[i] = - 1 ;
}
int ans = count_minimum_operations(n);
System.out.println(ans);
}
}
|
Python
dp = [ - 1 for i in range ( 1001 )]
def count_minimum_operations(n):
if (n = = 2 ):
return 1
elif (n = = 1 ):
return 0
if (dp[n] ! = - 1 ):
return dp[n]
elif (n % 3 = = 0 ):
dp[n] = 1 + count_minimum_operations(n / 3 )
elif (n % 3 = = 1 ):
dp[n] = 1 + count_minimum_operations(n - 1 )
else :
dp[n] = 1 + count_minimum_operations(n + 1 )
return dp[n]
n = 4
ans = count_minimum_operations(n)
print (ans)
|
C#
using System;
class GFG
{
static int []dp = new int [1001];
public static int count_minimum_operations( int n)
{
if (n == 2)
{
return 1;
}
else if (n == 1)
{
return 0;
}
if (dp[n] != -1)
{
return dp[n];
}
if (n % 3 == 0) {
dp[n] = 1 + count_minimum_operations(n / 3);
}
else if (n % 3 == 1) {
dp[n] = 1 + count_minimum_operations(n - 1);
}
else {
dp[n] = 1 + count_minimum_operations(n + 1);
}
return dp[n];
}
public static void Main()
{
int n = 4;
for ( int i = 0; i < 1001; i++) {
dp[i] = -1;
}
int ans = count_minimum_operations(n);
Console.Write(ans);
}
}
|
Javascript
<script>
let dp = [];
function count_minimum_operations(n)
{
if (n == 2) {
return 1;
}
if (n == 1) {
return 0;
}
if (dp[n] != -1)
{
return dp[n];
}
if (n % 3 == 0) {
dp[n] = 1 + count_minimum_operations(n / 3);
}
else if (n % 3 == 1) {
dp[n] = 1 + count_minimum_operations(n - 1);
}
else {
dp[n] = 1 + count_minimum_operations(n + 1);
}
return dp[n];
}
let n = 4;
for (let i = 0; i < 1001; i++) {
dp[i] = -1;
}
let ans = count_minimum_operations(n);
document.write(ans);
</script>
|
Time Complexity: O(n), where n represents the given integer.
Auxiliary Space: O(n), since n extra space has been taken.
Last Updated :
23 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...