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
< ? 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 ) , " " ; ? >