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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach
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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
  
?>

chevron_right


Output:

71


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.