Minimum operations to make two numbers equal | Java implementation of the above approach ; 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
import java . util . * ; class GFG { static int minOperations ( int n , int m ) { int a = 0 , k = 1 ; int p = Math . max ( n , m ) ; while ( n != m ) { float s = ( float ) ( p - n + p - m ) ; float q = ( float ) ( ( - 1 + Math . sqrt ( 8 * s + 1 ) ) / 2 ) ; if ( q - Math . floor ( q ) == 0 ) { a = ( int ) q ; n = m ; } p = p + 1 ; } return a ; } public static void main ( String [ ] args ) { int n = 1 , m = 3 ; System . out . print ( minOperations ( n , m ) ) ; } }