Find element using minimum segments in Seven Segment Display | C ++ program to find minimum number of segments required ; Precomputed values of segment used by digit 0 to 9. ; Return the number of segments used by x . ; Finding sum of the segment used by each digit of a number . ; Initialising the minimum segment and minimum number index . ; Finding and comparing segment used by each number arr [ i ] . ; If arr [ i ] used less segment then update minimum segment and minimum number . ; Driven Program
#include <bits/stdc++.h> NEW_LINE 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 ; }