A Seven Segment Display can be used to display numbers. Given an array of n natural numbers. The task is to find the number in the array which is using minimum segments to display number. If multiple numbers have a minimum number of segments, output the number having the smallest index.

Examples :
Input : arr[] = { 1, 2, 3, 4, 5 }.
Output : 1
1 uses on 2 segments to display.
Input : arr[] = { 489, 206, 745, 123, 756 }.
Output : 745
Precompute the number of segment used by digits from 0 to 9 and store it. Now for each element of the array sum the number of segment used by each digit. Then find the element which is using the minimum number of segments.
The number of segment used by digit:
0 -> 6
1 -> 2
2 -> 5
3 -> 5
4 -> 4
5 -> 5
6 -> 6
7 -> 3
8 -> 7
9 -> 6
Below is the implementation of this approach:
C++
#include<bits/stdc++.h>
using namespace std;
const int seg[10] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int computeSegment( int x)
{
if (x == 0)
return seg[0];
int count = 0;
while (x)
{
count += seg[x%10];
x /= 10;
}
return count;
}
int elementMinSegment( int arr[], int n)
{
int minseg = computeSegment(arr[0]);
int minindex = 0;
for ( int i = 1; i < n; i++)
{
int temp = computeSegment(arr[i]);
if (temp < minseg)
{
minseg = temp;
minindex = i;
}
}
return arr[minindex];
}
int main()
{
int arr[] = {489, 206, 745, 123, 756};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << elementMinSegment(arr, n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int []seg = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 };
static int computeSegment( int x)
{
if (x == 0 )
return seg[ 0 ];
int count = 0 ;
while (x > 0 )
{
count += seg[x % 10 ];
x /= 10 ;
}
return count;
}
static int elementMinSegment( int []arr, int n)
{
int minseg = computeSegment(arr[ 0 ]);
int minindex = 0 ;
for ( int i = 1 ; i < n; i++)
{
int temp = computeSegment(arr[i]);
if (temp < minseg)
{
minseg = temp;
minindex = i;
}
}
return arr[minindex];
}
static public void main (String[] args)
{
int []arr = { 489 , 206 , 745 , 123 , 756 };
int n = arr.length;
System.out.println(elementMinSegment(arr, n));
}
}
|
Python3
seg = [ 6 , 2 , 5 , 5 , 4 ,
5 , 6 , 3 , 7 , 6 ]
def computeSegment(x):
if (x = = 0 ):
return seg[ 0 ]
count = 0
while (x):
count + = seg[x % 10 ]
x = x / / 10
return count
def elementMinSegment(arr, n):
minseg = computeSegment(arr[ 0 ])
minindex = 0
for i in range ( 1 , n):
temp = computeSegment(arr[i])
if (temp < minseg):
minseg = temp
minindex = i
return arr[minindex]
arr = [ 489 , 206 , 745 , 123 , 756 ]
n = len (arr)
print (elementMinSegment(arr, n))
|
C#
using System;
class GFG{
static int []seg = new int [10]{ 6, 2, 5, 5, 4,
5, 6, 3, 7, 6};
static int computeSegment( int x)
{
if (x == 0)
return seg[0];
int count = 0;
while (x > 0)
{
count += seg[x % 10];
x /= 10;
}
return count;
}
static int elementMinSegment( int []arr, int n)
{
int minseg = computeSegment(arr[0]);
int minindex = 0;
for ( int i = 1; i < n; i++)
{
int temp = computeSegment(arr[i]);
if (temp < minseg)
{
minseg = temp;
minindex = i;
}
}
return arr[minindex];
}
static public void Main()
{
int []arr = {489, 206, 745, 123, 756};
int n = arr.Length;
Console.WriteLine(elementMinSegment(arr, n));
}
}
|
PHP
<?php
$seg = array (6, 2, 5, 5, 4,
5, 6, 3, 7, 6);
function computeSegment( $x )
{
global $seg ;
if ( $x == 0)
return $seg [0];
$count = 0;
while ( $x )
{
$count += $seg [ $x % 10];
$x = (int) $x / 10;
}
return $count ;
}
function elementMinSegment( $arr , $n )
{
$minseg = computeSegment( $arr [0]);
$minindex = 0;
for ( $i = 1; $i < $n ; $i ++)
{
$temp = computeSegment( $arr [ $i ]);
if ( $temp < $minseg )
{
$minseg = $temp ;
$minindex = $i ;
}
}
return $arr [ $minindex ];
}
$arr = array (489, 206, 745, 123, 756);
$n = sizeof( $arr );
echo elementMinSegment( $arr , $n ) , "\n" ;
?>
|
Javascript
<script>
let seg = [ 6, 2, 5, 5, 4, 5, 6, 3, 7, 6];
function computeSegment(x)
{
if (x == 0)
return seg[0];
let count = 0;
while (x > 0)
{
count += seg[x % 10];
x = parseInt(x / 10, 10);
}
return count;
}
function elementMinSegment(arr, n)
{
let minseg = computeSegment(arr[0]);
let minindex = 0;
for (let i = 1; i < n; i++)
{
let temp = computeSegment(arr[i]);
if (temp < minseg)
{
minseg = temp;
minindex = i;
}
}
return arr[minindex];
}
let arr = [ 489, 206, 745, 123, 756 ];
let n = arr.length;
document.write(elementMinSegment(arr, n));
</script>
|
Time Complexity: O(n * log10n)
Auxiliary Space: O(10)
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.