Largest number divisible by 50 that can be formed from a given set of N digits consisting of 0 s and 7 s only | Print the largest number divisible by 50 ; Counting number of 0 s and 7 s ; If count of 7 is divisible by 50 ; If count of 7 is less than 5 ; If count of 7 is not divisible by 50 ; Count of groups of 5 in which count of 7 s can be grouped ; Given array ; Size of the array
function printLargestDivisible ( arr , N ) { var i , count0 = 0 , count7 = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) count0 ++ ; else count7 ++ ; } if ( count7 % 50 == 0 ) { while ( count7 -- ) document . write ( 7 ) ; while ( count0 -- ) document . write ( 0 ) ; } else if ( count7 < 5 ) { if ( count0 == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } else { count7 = count7 - count7 % 5 ; while ( count7 -- ) document . write ( 7 ) ; while ( count0 -- ) document . write ( 0 ) ; } } var arr = [ 0 , 7 , 0 , 7 , 7 , 7 , 7 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 7 , 7 ] ; var N = arr . length ; printLargestDivisible ( arr , N ) ;