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;
void printMaxNumber( int n)
{
if (n & 1) {
cout << "7" ;
for ( int i = 0; i < (n - 3) / 2; i++)
cout << "1" ;
}
else {
for ( int i = 0; i < n / 2; i++)
cout << "1" ;
}
}
int main()
{
int n = 5;
printMaxNumber(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void printMaxNumber( int n)
{
if (n % 2 != 0 ) {
System.out.print( "7" );
for ( int i = 0 ; i < (n - 3 ) / 2 ; i++)
System.out.print( "1" );
}
else {
for ( int i = 0 ; i < n / 2 ; i++)
System.out.print( "1" );
}
}
public static void main(String[] args)
{
int n = 5 ;
printMaxNumber(n);
}
}
|
Python3
def printMaxNumber(n):
if (n % 2 = = 1 ):
print ( "7" ,end = "");
for i in range ( int ((n - 3 ) / 2 )):
print ( "1" ,end = "");
else :
for i in range (n / 2 ):
print ( "1" ,end = "");
n = 5 ;
printMaxNumber(n);
|
C#
using System;
class GFG
{
public static void printMaxNumber( int n)
{
if (n % 2 != 0)
{
Console.Write( "7" );
for ( int i = 0; i < (n - 3) / 2; i++)
Console.Write( "1" );
}
else
{
for ( int i = 0; i < n / 2; i++)
Console.Write( "1" );
}
}
public static void Main(String[] args)
{
int n = 5;
printMaxNumber(n);
}
}
|
PHP
<?php
function printMaxNumber( $n )
{
if ( $n & 1)
{
echo "7" ;
for ( $i = 0; $i < ( $n - 3) / 2; $i ++)
echo "1" ;
}
else
{
for ( $i = 0; $i < $n / 2; $i ++)
echo "1" ;
}
}
$n = 5;
printMaxNumber( $n );
?>
|
Javascript
<script>
function printMaxNumber(n)
{
if (n & 1) {
document.write( "7" );
for ( var i = 0; i < (n - 3) / 2; i++)
document.write( "1" );
}
else {
for ( var i = 0; i < n / 2; i++)
document.write( "1" );
}
}
var n = 5;
printMaxNumber(n);
</script>
|
Time Complexity: O(n), as there runs a loop.
Auxiliary Space: O(1), as no extra space has been taken.