Find element using minimum segments in Seven Segment Display - GeeksforGeeks

08 Jun, 2021
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 -> 6Below is the implementation of this approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find minimum number of segments// required#include<bits/stdc++.h>using namespace std; // Precomputed values of segment used by digit 0 to 9.const int seg[10] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; // Return the number of segments used by x.int computeSegment(int x){    if (x == 0)        return seg[0];     int count = 0;     // Finding sum of the segment used by    // each digit of a number.    while (x)    {        count += seg[x%10];        x /= 10;    }     return count;} int elementMinSegment(int arr[], int n){    // Initialising the minimum segment and minimum    // number index.    int minseg = computeSegment(arr[0]);    int minindex = 0;     // Finding and comparing segment used    // by each number arr[i].    for (int i = 1; i < n; i++)    {        int temp = computeSegment(arr[i]);         // If arr[i] used less segment then update        // minimum segment and minimum number.        if (temp < minseg)        {            minseg   = temp;            minindex = i;        }    }     return arr[minindex];} // Driven Programint main(){    int arr[] = {489, 206, 745, 123, 756};    int n = sizeof(arr)/sizeof(arr[0]);    cout << elementMinSegment(arr, n) << endl;    return 0;}
// Java program to find minimum// number of segments requiredimport java.io.*; class GFG {     // Precomputed values of segment// used by digit 0 to 9.static int []seg = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; // Return the number of segments used by x.static int computeSegment(int x){    if (x == 0)        return seg[0];     int count = 0;     // Finding sum of the segment used by    // each digit of a number.    while (x > 0)    {        count += seg[x % 10];        x /= 10;    }     return count;} static int elementMinSegment(int []arr, int n){    // Initialising the minimum segment    // and minimum number index.    int minseg = computeSegment(arr[0]);    int minindex = 0;     // Finding and comparing segment used    // by each number arr[i].    for (int i = 1; i < n; i++)    {        int temp = computeSegment(arr[i]);         // If arr[i] used less segment then update        // minimum segment and minimum number.        if (temp < minseg)        {            minseg = temp;            minindex = i;        }    }     return arr[minindex];}     // Driver program    static public void main (String[] args)    {       int []arr = {489, 206, 745, 123, 756};       int n = arr.length;       System.out.println(elementMinSegment(arr, n));    }} //This code is contributed by vt_m.
# Python implementation of# the above approach # Precomputed values of segment# used by digit 0 to 9.seg = [6, 2, 5, 5, 4,       5, 6, 3, 7, 6] # Return the number of# segments used by x.def computeSegment(x):    if(x == 0):        return seg[0]     count = 0     # Finding sum of the segment    # used by each digit of a number.    while(x):        count += seg[x % 10]        x = x // 10     return count # function to return minimum sum indexdef elementMinSegment(arr, n):     # Initialising the minimum    # segment and minimum number index.    minseg = computeSegment(arr[0])    minindex = 0     # Finding and comparing segment    # used by each number arr[i].    for i in range(1, n):        temp = computeSegment(arr[i])         # If arr[i] used less segment        # then update minimum segment        # and minimum number.        if(temp < minseg):             minseg = temp            minindex = i     return arr[minindex] # Driver Codearr = [489, 206, 745, 123, 756]n = len(arr) # function print required answerprint(elementMinSegment(arr, n)) # This code is contributed by# Sanjit_Prasad
// C# program to find minimum// number of segments requiredusing System; class GFG{     // Precomputed values of segment// used by digit 0 to 9.static int []seg = new int[10]{ 6, 2, 5, 5, 4,                               5, 6, 3, 7, 6}; // Return the number of segments used by x.static int computeSegment(int x){    if (x == 0)        return seg[0];     int count = 0;     // Finding sum of the segment used by    // each digit of a number.    while (x > 0)    {        count += seg[x % 10];        x /= 10;    }     return count;} static int elementMinSegment(int []arr, int n){    // Initialising the minimum segment    // and minimum number index.    int minseg = computeSegment(arr[0]);    int minindex = 0;     // Finding and comparing segment used    // by each number arr[i].    for (int i = 1; i < n; i++)    {        int temp = computeSegment(arr[i]);         // If arr[i] used less segment then update        // minimum segment and minimum number.        if (temp < minseg)        {            minseg = temp;            minindex = i;        }    }     return arr[minindex];}     // Driver program    static public void Main()    {       int []arr = {489, 206, 745, 123, 756};       int n = arr.Length;       Console.WriteLine(elementMinSegment(arr, n));    }} //This code is contributed by vt_m.
<?php// PHP program to find minimum// number of segments required // Precomputed values of segment// used by digit 0 to 9. $seg = array(6, 2, 5, 5, 4,             5, 6, 3, 7, 6); // Return the number of// segments used by x.function computeSegment($x){    global $seg;    if ($x == 0)        return $seg[0];     $count = 0;     // Finding sum of the segment    // used by each digit of a number.    while ($x)    {        $count += $seg[$x % 10];        $x = (int)$x / 10;    }     return $count;} function elementMinSegment($arr, $n){    // Initialising the minimum segment    // and minimum number index.    $minseg = computeSegment($arr[0]);    $minindex = 0;     // Finding and comparing segment    // used by each number arr[i].    for ($i = 1; $i < $n; $i++)    {        $temp = computeSegment($arr[$i]);         // If arr[i] used less segment        // then update minimum segment        // and minimum number.        if ($temp < $minseg)        {            $minseg = $temp;            $minindex = $i;        }    }     return $arr[$minindex];} // Driver Code$arr = array (489, 206, 745, 123, 756);$n = sizeof($arr);echo elementMinSegment($arr, $n) ,"\n"; // This code is contributed by ajit?>
<script> // Javascript program to find minimum// number of segments required // Precomputed values of segment// used by digit 0 to 9.let seg = [ 6, 2, 5, 5, 4, 5, 6, 3, 7, 6]; // Return the number of segments used by x.function computeSegment(x){    if (x == 0)        return seg[0];     let count = 0;     // Finding sum of the segment used by    // each digit of a number.    while (x > 0)    {        count += seg[x % 10];        x = parseInt(x / 10, 10);    }    return count;} function elementMinSegment(arr, n){         // Initialising the minimum segment    // and minimum number index.    let minseg = computeSegment(arr[0]);    let minindex = 0;     // Finding and comparing segment used    // by each number arr[i].    for(let i = 1; i < n; i++)    {        let temp = computeSegment(arr[i]);         // If arr[i] used less segment then update        // minimum segment and minimum number.        if (temp < minseg)        {            minseg = temp;            minindex = i;        }    }    return arr[minindex];} // Driver codelet arr = [ 489, 206, 745, 123, 756 ];let n = arr.length; document.write(elementMinSegment(arr, n)); // This code is contributed by divyesh072019 </script>
Output : 
745
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.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
vt_m
jit_t
Sanjit_Prasad
Akanksha_Rai
subham348
divyesh072019
saurabh1990aror
number-digits
Arrays
Greedy
Mathematical
Arrays
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Introduction to Arrays
Dijkstra's shortest path algorithm | Greedy Algo-7
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Huffman Coding | Greedy Algo-3
Write a program to print all permutations of a given string