Given an integer N, the task is to find the largest number that can be shown with the help of N segments using any number of 7 segment displays.
Input: N = 7
Largest number that can be displayed by turning on seven segments is 711 with the help of 3 segments display set.
If the problem is observed carefully, then the number N can be of two types that is even or odd and each of them should be solved separately as follows:
- For Even: As in the above image, There are 6 numbers that can be displayed using even number of segments which is
0 - 6 1 - 2 2 - 5 4 - 4 6 - 6 9 - 6
As it is observed number 1 uses the minimum count of segments to display a digit. Then, even the number of segments can be displayed using 1 with 2 counts of segments in each digit.
- For Odd: As in the above image, there are 5 numbers that can be displayed using an odd number of segments which is
3 - 5 5 - 5 7 - 3 8 - 7
As it is observed number 7 uses the minimum number of odd segments to display a digit. Then an odd number of segments can be displayed using 7 with 3 counts of segments in each digit.
- If the given number N is 0 or 1, then any number cannot be displayed with this much of bits.
- If the given number N is odd then the most significant digit will be 7 and the rest of the digits can be displayed with the help of the (N – 3) segments because to display 7 it takes 3 segments.
- If given number N is even then the most significant digit will be 1 and the rest of the digits can be displayed with the help of the (N – 2) segments because to display 1, it takes 2 segments only.
- The number N is processed digit by digit recursively.
Explanation with Example:
Given number N be – 11
|Digit (from MSB to LSB)||N||Largest number from N using segment||Segments used||Segments remaining|
Then, the largest number will be 71111.
Below is the implementation of the above approach:
- Time Complexity: As in the above approach, there is recursive call which takes O(N) time in worst case, Hence the Time Complexity will be O(N).
- Auxiliary Space Complexity: As in the above approach, taking consideration of the stack space used in recursive call then the auxiliary space complexity will be O(N)
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.
- Maximum number that can be display on Seven Segment Display using N segments
- Find element using minimum segments in Seven Segment Display
- Maximum number of parallelograms that can be made using the given length of line segments
- Maximum length of segments of 0's and 1's
- Partition an array such into maximum increasing segments
- Recursive Programs to find Minimum and Maximum elements of array
- Count of ways to split a given number into prime segments
- Minimum splits required to convert a number into prime segments
- Recursive program for prime number
- Recursive program to find all Indices of a Number
- Floor value Kth root of a number using Recursive Binary Search
- Recursive Program to print multiplication table of a number
- Find the largest number smaller than integer N with maximum number of set bits
- Divide the array in K segments such that the sum of minimums is maximized
- Find M such that GCD of M and given number N is maximum
- Find a number M < N such that difference between their XOR and AND is maximum
- Maximum number of candies that can be bought
- How to print maximum number of A's using given four keys
- Maximum number of customers that can be satisfied with given quantity
- Maximum number of elements without overlapping in a Line
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.