text: Minimum operations to make all elements equal using the second array | Function to find the minimum operations required to make all elements of the array equal ; Minimum element of A ; Traverse through all final values ; Variable indicating whether all elements can be converted to x or not ; Total operations ; Traverse through all array elements ; All elements can 't  be converted to x ; Driver Code
code: function minOperations ( a , b , n ) { var minA = Math . max . apply ( Math , a ) ; ; for ( x = minA ; x >= 0 ; x -- ) { var check = true ; var operations = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( x % b [ i ] == a [ i ] % b [ i ] ) { operations += ( a [ i ] - x ) / b [ i ] ; } else { check = false ; break ; } } if ( check ) return operations ; } return - 1 ; } var N = 5 ; var A = [ 5 , 7 , 10 , 5 , 15 ] ; var B = [ 2 , 2 , 1 , 3 , 5 ] ; document . write ( minOperations ( A , B , N ) ) ;