Minimum operations to make two numbers equal | Function to find the minimum no of operations ; find the maximum of two and store it in p ; increase it until it is achievable from given n and m ; Here value added to n and m will be S ( n ) = p - n + p - m ; check whether integer value of n exist by the formula n = ( - 1 + Math . sqrt ( 1 + 8 * S ( n ) ) ) / 2 ; Driver code ; Function calling
function minOperations ( n , m ) { var a = 0 , k = 1 ; var p = Math . max ( n , m ) ; while ( n != m ) { var s = ( p - n + p - m ) ; var q = ( ( - 1 + Math . sqrt ( 8 * s + 1 ) ) / 2 ) ; if ( q - Math . floor ( q ) == 0 ) { a = parseInt ( q ) ; n = m ; } p = p + 1 ; } return a ; } var n = 1 , m = 3 ; document . write ( minOperations ( n , m ) ) ;