Given an integer N, the task is to find the smallest and largest N-digit numbers in Octal Number System.
Input: N = 4
Input: N = 2
Approach: The following steps can be followed to compute the required answer:
- Largest Number: To get the largest number, every digit of the number must be maximum. The maximum digit in the Octal number system is ‘7‘. Therefore:
1 Digit Largest Number: '7' 2 Digit Largest Number: '77' 3 Digit Largest Number: '777' . . . N Digit Largest Number: '777....(N) times'
- Smallest Number: The smallest number in Octal number is ‘0‘. The idea is that the first digit needs to be as minimum as possible other than 0 which is ‘1’ and the remaining digits needs to be 0. Therefore:
1 Digit Smallest Number: '1' 2 Digit Smallest Number: '10' 3 Digit Smallest Number: '100' . . . N Digit Smallest Number: '100....(N - 1) times'
Below is the implementation of the above approach:
Largest: 7777 Smallest: 1000
Time Complexity: O(N) where N is the length of the string.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Largest Even and Odd N-digit numbers in Octal Number System
- Smallest and Largest sum of two n-digit numbers
- Largest and Smallest N-digit Hexadecimal Numbers
- Sum and product of k smallest and k largest prime numbers in the array
- Sum and product of k smallest and k largest composite numbers in the array
- Largest N digit Octal number which is a Perfect square
- Count of Octal numbers upto N digits
- Smallest and Largest Palindrome with N Digits
- Largest and smallest digit of a number
- Lexicographically smallest and largest substring of size k
- Smallest and Largest N-digit perfect squares
- Smallest and Largest N-digit perfect cubes
- Program to find Smallest and Largest Word in a String
- Queries for the smallest and the largest prime number of given digit
- Split the number into N parts such that difference between the smallest and the largest part is minimum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.