Open In App

Maximum number that can be display on Seven Segment Display using N segments

Improve
Improve
Like Article
Like
Save
Share
Report

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.

 The individual segments of a seven-segment display
Image Source: Wikipedia

 
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 segments
void 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 Code
int main()
{
    int n = 5;
 
    printMaxNumber(n);
 
    return 0;
}


Java




// Java implementation of above approach
import 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 segments
def 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 Code
n = 5;
printMaxNumber(n);
 
# This code contributed by Rajput-Ji


C#




// C# implementation of above approach
using 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 segments
function 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 Code
var n = 5;
printMaxNumber(n);
 
</script>


Output: 

71

 

Time Complexity: O(n), as there runs a loop.
Auxiliary Space: O(1), as no extra space has been taken.



Last Updated : 16 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads