- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Reduce a number to 1 by performing given operations in C++

Given an integer Number as input. The goal is to find the minimum number of steps or operations required to reduce the input Number to 1. Operations that can be performed will be-:

If Number is even, then Divide it by 2.

If Number is odd, then increment or decrement it by 1.

## Examples

**Input **− Number=28

**Output **− Minimum steps to reduce 28 to 1: 6

**Explanation**−

28 is even - divide by 2 = 14

14 is even - divide by 2 = 7

7 is odd - increment by 1 = 8

8 is even - divide by 2 = 4

4 is even - divide by 2 = 2

2 is even - divide by 2 = 1

**Input **− Number=9

**Output **− Minimum steps to reduce 9 to 1: 4

**Explanation** −

9 is odd - decrement by 1 = 8

8 is even - divide by 2 = 4

4 is even - divide by 2 = 2

2 is even - divide by 2 = 1

## Approach used in the below program is as follows

In this approach use a recursive approach to check minimum operations required to reduce Number to 1. In case it is even simply divide by 2 else check recursively for minimum ways for Number+1 or Number-1, whichever is less.

Take the input Number as integer.

Function minWays(int num) takes num as input and returns the count of minimum operations required to reduce num to 1.

Take variables tmp1, tmp2 and min as integers.

If num is 0 then return 1.

If num%2==0 then it is even then set num=num/2

If num is odd then set tmp1=minWays(num-1) and tmp2=minWays(num+1).

Set min is minimum of tmp1 and tmp2.

Return 1+min.

At the end we will get the desired result.

Print result in main.

## Example

#include <iostream> using namespace std; int minWays(int num){ int tmp1,tmp2,min; if (num == 1){ return 0; } else if (num % 2 == 0){ tmp1=minWays(num/2); return (1 + tmp1); } else{ int tmp1=minWays(num - 1); int tmp2=minWays(num + 1); int min=tmp1<tmp2?tmp1:tmp2; return (1 + min); } } int main(){ int Number = 21; cout <<"Minimum steps to reduce "<<Number<<" to 1: "<<minWays(Number); return 0; }

## Output

If we run the above code it will generate the following Output

Minimum steps to reduce 21 to 1: 6

- Related Articles
- Program to check final answer by performing given stack operations in Python
- Count the number of operations required to reduce the given number in C++
- Final string after performing given operations in C++
- Program to find expected sum of subarrays of a given array by performing some operations in Python
- Find maximum operations to reduce N to 1 in Python
- Find maximum operations to reduce N to 1 in C++
- Maximum Possible Product in Array after performing given Operations in C++
- Python Program to Create a class performing Calculator Operations
- Count operations of the given type required to reduce N to 0 in C++
- Program to find maximum sum by performing at most k negate operations in Python
- Performing Bitwise Operations with BigInteger in Java
- Program to reduce list by given operation and find smallest remaining number in Python
- Maximum count of equal numbers in an array after performing given operations in C++
- Count number of step required to reduce N to 1 by following certain rule in C++
- Convert a number m to n using minimum number of given operations in C++