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

C++Server Side ProgrammingProgramming

<p style="">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 &minus;</p><ul class="list"><li><p>Add 1 to number</p></li><li><p>Remove trailing zeros from the generated number, if any</p></li></ul><p>If N is 8 then numbers generated will be</p><p>Applying step 1&minus; 8 &rarr; 9 &rarr;</p><p>Applying step 2&minus; 1 &rarr; ( removed 0 from 10 )</p><p>Applying step 1: 2 &rarr; 3 &rarr; 4 &rarr; 5 &rarr; 6 &rarr; 7 &rarr; 8 ( same sequence )</p><p>Count of unique numbers will be 9.</p><p><strong>For Example</strong></p><h2>Input</h2><pre class="result notranslate">N=21</pre><h2>Output</h2><pre class="result notranslate">Count of unique numbers that can be generated from N by adding one and removing trailing zeros are: 18</pre><h2>Explanation</h2><pre class="result notranslate">Numbers will be: 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3 &minus;&minus;&minus;now same sequence Unique numbers are: 18</pre><h2>Input</h2><pre class="result notranslate">N=38</pre><h2>Output</h2><pre class="result notranslate">Count of unique numbers that can be generated from N by adding one and removing trailing zeros are: 11</pre><h2>Explanation</h2><pre class="result notranslate">Numbers will be: 38, 39, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4 &minus;&minus;&minus;now same sequence Unique numbers are: 11</pre><p><strong>Approach used in the below program is as follows</strong> &minus;</p><p>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.</p><ul class="list"><li><p>Take the number N as integer.</p></li><li><p>Take the unordered_set&lt;int&gt; U_S for inserting generated numbers.</p></li><li><p>Function unique_N(unordered_set&lt;int&gt;&amp; U_S, int N) takes the set and N and adds numbers to the set U_S until all numbers are unique in it.</p></li><li><p>If U_S.count(N) returns 1 that means N already exists in the set. So the numbers would repeat, return from the function.</p></li><li><p>Otherwise insert N to the set and apply operation 1 ( increment by 1).</p></li><li><p>Check if number N has trailing zeroes (is multiple of 10).</p></li><li><p>If N % 10 is 0, then remove trailing zero by dividing it by 10.</p></li><li><p>Call function unique_N() with updated N.</p></li><li><p>After returning from the function, take count as the size of the set U_S.</p></li><li><p>Print result as count.</p></li></ul><h2>Example</h2><p><a class="demo" href="http://tpcg.io/r9lfrSGa" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate" style="">#include &lt;bits/stdc++.h&gt; using namespace std; void unique_N(unordered_set&lt;int&gt;&amp; U_S, int N){ &nbsp; &nbsp;if (U_S.count(N)){ &nbsp; &nbsp; &nbsp; return; &nbsp; &nbsp;} &nbsp; &nbsp;U_S.insert(N); &nbsp; &nbsp;N = N + 1; &nbsp; &nbsp;while (N % 10 == 0){ &nbsp; &nbsp; &nbsp; N = N / 10; &nbsp; &nbsp;} &nbsp; &nbsp;unique_N(U_S, N); } int main(){ &nbsp; &nbsp;int N = 7; &nbsp; &nbsp;unordered_set&lt;int&gt; U_S; &nbsp; &nbsp;unique_N(U_S, N); &nbsp; &nbsp;int count = U_S.size(); &nbsp; &nbsp;cout&lt;&lt;&quot;Count of unique numbers that can be generated from N by adding one and removing &nbsp; &nbsp; &nbsp; trailing zeros are: &quot;&lt;&lt;count; &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><p>If we run the above code it will generate the following output &minus;</p><pre class="result notranslate">Count of unique numbers that can be generated from N by adding one and removing trailing zeros are: 9</pre>