Count unique numbers that can be generated from N by adding one and removing trailing zeros | Java 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
import java . util . * ; class GFG { static void count_unique ( HashSet < Integer > s , int n ) { if ( s . contains ( n ) ) return ; s . add ( n ) ; n += 1 ; while ( n % 10 == 0 ) { n = n / 10 ; } count_unique ( s , n ) ; } public static void main ( String [ ] args ) { int n = 10 ; HashSet < Integer > s = new HashSet < > ( ) ; count_unique ( s , n ) ; System . out . println ( s . size ( ) ) ; } }