Given a positive integer N. The task is to find the maximum number that can be displayed on seven segment display using N segments.
Seven Segment Display: A seven-segment display (SSD), or seven-segment indicator, is a form of an electronic display device for displaying decimal numerals that is an alternative to the more complex dot matrix displays.
Â
Examples:Â
Input : N = 5 Output : 71 On 7-segment display, 71 will look like: _ | | | | Input : N = 4 Output : 11
Observe, the number having a greater number of digits than other numbers will be greater in value. So, we will try to make a number with maximum possible length (number of digits) using given ‘N’ segments.Â
Also observe, to increase the length of the number we will try to use less segment on each digit as possible. So, number ‘1’ use only 2 segments to represent a digit. No other digit use less than 2 segments.Â
So, in case N is even, the answer would be 1s N/2 number of time.Â
In case N is odd, we cannot use all segments if we make 1s N/2 number of time. Also, if we use 3 segments to make a digit of 7 and (N-3)/2 number of 1s, then the number formed will be greater in value than the number formed by N/2 number of 1s.Â
Below is the implementation of this approach:Â Â
C++
#include <bits/stdc++.h>using namespace std;Â
// Function to print maximum number that can be formed// using N segmentsvoid printMaxNumber(int n){    // If n is odd    if (n & 1) {        // use 3 three segment to print 7        cout << "7";Â
        // remaining to print 1        for (int i = 0; i < (n - 3) / 2; i++)            cout << "1";    }Â
    // If n is even    else {        // print n/2 1s.        for (int i = 0; i < n / 2; i++)            cout << "1";    }}Â
// Driver's Codeint main(){Â Â Â Â int n = 5;Â
    printMaxNumber(n);Â
    return 0;} |
Java
// Java implementation of above approachimport java.io.*;class GFG {Â
    // Function to print maximum number that    // can be formed using N segments    public static void printMaxNumber(int n)    {        // If n is odd        if (n % 2 != 0) {            // use 3 three segment to print 7            System.out.print("7");Â
            // remaining to print 1            for (int i = 0; i < (n - 3) / 2; i++)                System.out.print("1");        }Â
        // If n is even        else {Â
            // print n/2 1s.            for (int i = 0; i < n / 2; i++)                System.out.print("1");        }    }Â
    // Driver Code    public static void main(String[] args)    {        int n = 5;        printMaxNumber(n);    }}Â
// This code is contributed by princiraj1992 |
Python3
# Function to print maximum number that can be formed# using N segmentsdef printMaxNumber(n):         # If n is odd    if (n % 2 == 1):                 # use 3 three segment to print 7        print("7",end="");Â
        # remaining to print 1        for i in range(int((n - 3) / 2)):            print("1",end="");Â
    # If n is even    else:                 # print n/2 1s.        for i in range(n/2):            print("1",end="");Â
# Driver's Coden = 5;printMaxNumber(n);Â
# This code contributed by Rajput-Ji |
C#
// C# implementation of above approachusing System;Â
class GFG {Â
    // Function to print maximum number that    // can be formed using N segments    public static void printMaxNumber(int n)    {        // If n is odd        if (n % 2 != 0)         {            // use 3 three segment to print 7            Console.Write("7");Â
            // remaining to print 1            for (int i = 0; i < (n - 3) / 2; i++)                Console.Write("1");        }Â
        // If n is even        else        {Â
            // print n/2 1s.            for (int i = 0; i < n / 2; i++)                Console.Write("1");        }    }Â
    // Driver Code    public static void Main(String[] args)    {        int n = 5;        printMaxNumber(n);    }}Â
// This code has been contributed by 29AjayKumar |
PHP
<?php// PHP code implementation of above codeÂ
// Function to print maximum number that can be formed // using N segments function printMaxNumber($n) {     // If n is odd     if ($n & 1)     {         // use 3 three segment to print 7         echo "7"; Â
        // remaining to print 1         for ($i = 0; $i < ($n - 3) / 2; $i++)             echo "1";     } Â
    // If n is even     else    {         // print n/2 1s.         for ($i = 0; $i < $n / 2; $i++)             echo "1";     } } Â
// Driver's Code $n = 5; Â
printMaxNumber($n); Â
// This code is contributed by AnkitRai01Â
?> |
Javascript
<script>Â
Â
// Function to print maximum number that can be formed// using N segmentsfunction printMaxNumber(n){    // If n is odd    if (n & 1) {        // use 3 three segment to print 7        document.write( "7");Â
        // remaining to print 1        for (var i = 0; i < (n - 3) / 2; i++)            document.write( "1");    }Â
    // If n is even    else {        // print n/2 1s.        for (var i = 0; i < n / 2; i++)            document.write( "1");    }}Â
// Driver's Codevar n = 5;printMaxNumber(n);Â
</script> |
71
Â
Time Complexity: O(n), as there runs a loop.
Auxiliary Space: O(1), as no extra space has been taken.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
