The Wayback Machine - https://web.archive.org/web/20240703201411/https://www.geeksforgeeks.org/find-element-using-minimum-segments-seven-segment-display/
Open In App

Find element using minimum segments in Seven Segment Display

Last Updated : 12 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Seven Segment Display

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
Recommended Practice

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




// 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 Program
int main()
{
    int arr[] = {489, 206, 745, 123, 756};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << elementMinSegment(arr, n) << endl;
    return 0;
}


Java




// Java program to find minimum
// number of segments required
import 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.


Python3




# 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 index
def 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 Code
arr = [489, 206, 745, 123, 756]
n = len(arr)
  
# function print required answer
print(elementMinSegment(arr, n))
  
# This code is contributed by
# Sanjit_Prasad


C#




// C# program to find minimum 
// number of segments required
using 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
// 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
?>


Javascript




<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 code
let 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)



Previous Article
Next Article

Similar Reads

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.
5 min read
Check if mirror image of a number is same if displayed in seven segment display
Given a positive number n. The task is to check if the mirror image of the number is equal to given number or not if displayed on Seven Line Segment. A mirror image of a number is a reflected duplication of the number that appears almost identical but is reversed in the direction perpendicular to the mirror surface.Examples: Input : n = 101 Output:
6 min read
Minimize segments required to be removed such that at least one segment intersects with all remaining segments
Given an array arr[] consisting of N pairs [L, R], where L and R denotes the start and end indices of a segment, the task is to find the minimum number of segments that must be deleted from the array such that the remaining array contains at least one segment which intersects with all other segments present in the array. Examples: Input: arr[] = {{
10 min read
Check if number can be displayed using seven segment led
Given a string str representing an integer and an integer led which is the count of LEDs available. The task is to check if it is possible to display the number using the given LEDs. Note that a digit will be displayed as it is displayed on a 7 segment LED. If its possible to display the number then print Yes, otherwise print No. Here's an example
5 min read
Given n line segments, find if any two segments intersect
Given n line segments (p1, q1), (p2, q2), ... (pn, qn), find if the given line segments intersect with each other or not. Example: Input: {{1, 5}, {4, 5}}, {{2, 5}, {10, 1}},{{3, 2}, {10, 3}},{{6, 4}, {9, 4}},{{7, 1}, {8, 1}}Output: 2Explanation: Lines {{1, 5}, {4, 5}}, {{2, 5}, {10, 1}} are intersecting, as well as {{2, 5}, {10, 1}},{{3, 2}, {10,
10 min read
Minimum number of segments required such that each segment has distinct elements
Given an array of integers, the task is to find the minimum number of segments that the array elements can be divided into such that all the segments contain distinct elements. Examples: Input: n = 6; Array: 1, 7, 4, 3, 3, 8 Output: 2 Explanation: Optimal way to create segments here is {1, 7, 4, 3} {3, 8} Clearly, the answer is the maximum frequenc
6 min read
Maximum sum of segments among all segments formed in array after Q queries
Given two arrays arr[](1-based indexing) and queries[] consisting of N integers and queries[] contains a permutation of the first N natural numbers, the task is to perform the query on the array and find the maximum sum of segments among all segments formed such that in each query queries[i] the array element at index queries[i] is deleted and that
15 min read
Find the segment that overlaps with maximum number of segments
Given a 2D array segments[][] where each segment is of the form [L, R] representing (X, Y) co-ordinates, the task is to find a segment that overlaps with maximum number of segments. Examples: Input: segments[][] = {{1, 4}, {2, 3}, {3, 6}}Output: {3, 6}Explanation: Every segment overlaps with all other segments. Therefore, print any one of them. Inp
9 min read
Partition the array in K segments such that bitwise AND of individual segment sum is maximized
Given an array of size N and an integer K. The task is to partition the array in K segments such that bitwise AND of individual segment sum is maximized. Find the maximum value of bitwise AND which can be obtained. Examples: Input : a[] = { 2, 5, 6, 9, 1, 3, 10, 12 }, K = 3 Output : 8 Explanation : Maximum bitwise AND can be obtained by making cut
12 min read
Divide an array into k segments to maximize maximum of segment minimums
Given an array of n integers, divide it into k segments and find the maximum of the minimums of k segments. Output the maximum integer that can be obtained among all ways to segment in k subarrays. Examples: Input : arr[] = {1, 2, 3, 6, 5} k = 2 Output: 5 Explanation: There are many ways to create two segments. The optimal segments are (1, 2, 3) an
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg