Smallest multiple of 3 which consists of three given non | Function to return the minimum number divisible by 3 formed by the given digits ; Sort the given array in ascending ; Check if any single digit is divisible by 3 ; Check if any two digit number formed by the given digits is divisible by 3 starting from the minimum ; Generate the two digit number ; If none of the above is true , we can form three digit number by taking a [ 0 ] three times . ; Driver code
< ? php function printSmallest ( $ a ) { sort ( $ a ) ; for ( $ i = 0 ; $ i < 3 ; $ i ++ ) { if ( $ a [ $ i ] % 3 == 0 ) return $ a [ $ i ] ; } for ( $ i = 0 ; $ i < 3 ; $ i ++ ) { for ( $ j = 0 ; $ j < 3 ; $ j ++ ) { $ num = ( $ a [ $ i ] * 10 ) + $ a [ $ j ] ; if ( $ num % 3 == 0 ) return $ num ; } } return $ a [ 0 ] * 100 + $ a [ 0 ] * 10 + $ a [ 0 ] ; } $ arr = array ( 7 , 7 , 1 ) ; echo printSmallest ( $ arr ) ; ? >