Find element using minimum segments in Seven Segment Display | 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 . ; Driver code
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 ) ) ;