- 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

# Count unique numbers that can be generated from N by adding one and removing trailing zeros in C++

We are given a number N as input. Perform two operations on N and identify the count of unique numbers generated in the process. Steps will −

Add 1 to number

Remove trailing zeros from the generated number, if any

If N is 8 then numbers generated will be

Applying step 1− 8 → 9 →

Applying step 2− 1 → ( removed 0 from 10 )

Applying step 1: 2 → 3 → 4 → 5 → 6 → 7 → 8 ( same sequence )

Count of unique numbers will be 9.

**For Example**

## Input

N=21

## Output

Count of unique numbers that can be generated from N by adding one and removing trailing zeros are: 18

## Explanation

Numbers will be: 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3 −−−now same sequence Unique numbers are: 18

## Input

N=38

## Output

Count of unique numbers that can be generated from N by adding one and removing trailing zeros are: 11

## Explanation

Numbers will be: 38, 39, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4 −−−now same sequence Unique numbers are: 11

**Approach used in the below program is as follows** −

In this approach we will create an unordered set which will contain all unique numbers generated after applying steps 1 and 2. In case the numbers repeat, then we will stop the iteration. The size of the set will give us the count of unique numbers generated in the process.

Take the number N as integer.

Take the unordered_set<int> U_S for inserting generated numbers.

Function unique_N(unordered_set<int>& U_S, int N) takes the set and N and adds numbers to the set U_S until all numbers are unique in it.

If U_S.count(N) returns 1 that means N already exists in the set. So the numbers would repeat, return from the function.

Otherwise insert N to the set and apply operation 1 ( increment by 1).

Check if number N has trailing zeroes (is multiple of 10).

If N % 10 is 0, then remove trailing zero by dividing it by 10.

Call function unique_N() with updated N.

After returning from the function, take count as the size of the set U_S.

Print result as count.

## Example

#include <bits/stdc++.h> using namespace std; void unique_N(unordered_set<int>& U_S, int N){ if (U_S.count(N)){ return; } U_S.insert(N); N = N + 1; while (N % 10 == 0){ N = N / 10; } unique_N(U_S, N); } int main(){ int N = 7; unordered_set<int> U_S; unique_N(U_S, N); int count = U_S.size(); cout<<"Count of unique numbers that can be generated from N by adding one and removing trailing zeros are: "<<count; return 0; }

## Output

If we run the above code it will generate the following output −

Count of unique numbers that can be generated from N by adding one and removing trailing zeros are: 9

- Related Articles
- Count of distinct sums that can be obtained by adding prime numbers from given arrays in C++
- C++ Program to find out the number of unique matrices that can be generated by swapping rows and columns
- Remove Trailing Zeros from string in C++
- Count trailing zeros in factorial of a number in C++
- C Program to count trailing and leading zeros in a binary number
- Print all distinct integers that can be formed by K numbers from a given array of N numbers in C++
- Count number of trailing zeros in product of array in C++
- Program to find trailing zeros in factorial of n in C++?\n
- Program to check one string can be converted to another by removing one element in Python
- Count the total number of squares that can be visited by Bishop in one move in C++
- Program to find island count by adding blocks into grid one by one in Python
- Adding base n numbers
- How to Add Trailing Zeros to a Column of Numbers in Excel?
- Name one disease that can be prevented by vaccination.
- Count Triplets such that one of the numbers can be written as sum of the other two in C++