Find the index which is the last to be reduced to zero after performing a given operation | Function that returns the index which will be the last to become zero after performing given operation ; Initialize the result ; Finding the ceil value of each index ; Finding the index with maximum ceil value ; Driver code
function findIndex ( a , n , k ) { var index = - 1 , max_ceil = Number . MIN_VALUE ; for ( i = 0 ; i < n ; i ++ ) { a [ i ] = ( a [ i ] + k - 1 ) / k ; } for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= max_ceil ) { max_ceil = a [ i ] ; index = i ; } } return index ; } var arr = [ 31 , 12 , 25 , 27 , 32 , 19 ] ; var K = 5 ; var N = arr . length ; document . write ( findIndex ( arr , N , K ) ) ;