VOOZH about

URL: https://www.geeksforgeeks.org/dsa/all-possible-numbers-of-n-digits-and-base-b-without-leading-zeros/

⇱ All possible numbers of N digits and base B without leading zeros - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

All possible numbers of N digits and base B without leading zeros

Last Updated : 16 Aug, 2022

Given a number of digits 'N' and base 'B', the task is to count all the 'N' digit numbers without leading zeros that are in base 'B'

Examples:  

Input: N = 2, B = 2
Output: 2
All possible numbers without 
leading zeros are 10 and 11.

Input: N = 5, B = 8
Output: 28672

Approach:  

  • If the base is 'B' then every digit of the number can take any value within the range [0, B-1].
  • So, B'N' digit numbers are possible with base 'B' (including the numbers with leading zeros).
  • And, if we fix the first digit as '0' then the rest of the 'N-1' digits can form a total of Bnumbers.
  • So, the total number of 'N' digit numbers with base 'B' possible without leading zeros are B- B.

Below is the implementation of the above approach: 


Output: 
3072

 


Time Complexity: O(logn), since pow function takes logn time to find the power of a number to base n.

Auxiliary Space: O(1), since no extra space has been taken.

Comment
Article Tags: