Count unique numbers that can be generated from N by adding one and removing trailing zeros | C ++ implementation of the approach ; Function to count the unique numbers ; If the number has already been visited ; Insert the number to the set ; First step ; Second step remove trailing zeros ; Recur again for the new number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void count_unique ( unordered_set < int > & s , int n ) { if ( s . count ( n ) ) return ; s . insert ( n ) ; n += 1 ; while ( n % 10 == 0 ) { n = n / 10 ; } count_unique ( s , n ) ; } int main ( ) { int n = 10 ; unordered_set < int > s ; count_unique ( s , n ) ; cout << s . size ( ) ; return 0 ; }