Skip to content
Related Articles

Related Articles

Count unique numbers that can be generated from N by adding one and removing trailing zeros
  • Difficulty Level : Basic
  • Last Updated : 02 May, 2019

Given a number N. Add one to the number in the first step and if the number has trailing zeros, remove all the trailing zeros in the second step. Continue the process for the next generated number. The task is to count the number of unique numbers that can be generated from these operations.

Examples:

Input: N = 5
Output: 9
5 -> 6 -> 7 -> 8 -> 9 -> 1 -> 2 -> 3 -> 4 -> 5 (same sequence repeats)
Note that 10 is not included as it contained trailing zero
and removing the zero gave 1 as the next element.

Input: N = 28
Output: 11

Approach: The problem can be solved using recursion. Use a unordered_set to store all the unique numbers. In case a number is reached twice, we end the recursion as the same sequence will be repeated and we will not be getting any more unique numbers. Else insert the number to the set and in the first step increase the number by 1 and remove all trailing zeros in the next step if there are any.



Below is the implementation of the above approach:

C++




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

Java




// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
// Function to count the unique numbers
static void count_unique(HashSet<Integer>s, int n)
{
  
    // If the number has
    // already been visited
    if (s.contains(n))
        return;
  
    // Insert the number to the set
    s.add(n);
  
    // First step
    n += 1;
  
    // Second step
    // remove trailing zeros
    while (n % 10 == 0)
    {
        n = n / 10;
    }
  
    // Recur again for the new number
    count_unique(s, n);
}
  
// Driver code
public static void main(String[] args)
{
    int n = 10;
    HashSet<Integer>s = new HashSet<>();
    count_unique(s, n);
    System.out.println(s.size());
}
}
  
// This code has been contributed by 29AjayKumar

Python3




# Python3 implementation of the approach 
  
# Function to count the unique numbers 
def count_unique(s, n) : 
  
    # If the number has 
    # already been visited 
    if (s.count(n)) :
        return;
  
    # Insert the number to the set 
    s.append(n); 
  
    # First step 
    n += 1
  
    # Second step 
    # remove trailing zeros 
    while (n % 10 == 0) : 
        n = n // 10
  
    # Recur again for the new number 
    count_unique(s, n);
  
  
# Driver code 
if __name__ == "__main__" :
  
    n = 10
    s = []
      
    count_unique(s, n)
      
    print(len(s))
  
# This code is contributed by Ryuga

C#




// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG 
{
  
// Function to count the unique numbers
static void count_unique(HashSet<int>s, int n)
{
  
    // If the number has
    // already been visited
    if (s.Contains(n))
        return;
  
    // Insert the number to the set
    s.Add(n);
  
    // First step
    n += 1;
  
    // Second step
    // remove trailing zeros
    while (n % 10 == 0)
    {
        n = n / 10;
    }
  
    // Recur again for the new number
    count_unique(s, n);
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 10;
    HashSet<int>s = new HashSet<int>();
    count_unique(s, n);
    Console.WriteLine(s.Count);
}
}
  
// This code contributed by Rajput-Ji
Output:
19

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :