Given two numbers n and m, the task is to find the minimum number of operations required to make them equal if the following operations can be performed on them.
- During the first operation, any of the two numbers can be increased by one.
- During the second operation, any of the two numbers can be increased by two.
- During the third operation, any of the two numbers can be increased by three and so on.
Examples:
Input : n = 1, m = 3 Output : 3 Explanation: Add 1 to n; n = 2 Add 2 to m; m = 5 Add 3 to n; n = 5 Both n and m are equal now N of operations = 3 Input : n = 30, m = 20 Output : 4
Approach:
The approach used to solve the problem is the sum of N terms in an AP.
It is given by the formula
S(n) = (n*(n+1))/2
So, the task is to find the difference between those two numbers and see if the difference can be achieved by adding first n elements. Therefore,
S(n) = max(m,n) - min(m,n)
On substituting this value of sum in the first equation;
we get the number of elements n given by
n=(-1+sqrt(1+8*S(n)))/2
If this n is a perfect integer, then it is our final answer.
Else, we increment our target value to reach by 1 and continue.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to find the minimum no of operations int minOperations(int n, int m) { int a = 0, k = 1; // find the maximum of two and store it in p int p = max(n, m); // increase it until it is achievable from // given n and m while (n != 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+sqrt(1+8*S(n)))/2 float s = (float)(p - n + p - m); float q = (-1 + sqrt(8 * s + 1)) / 2; if (q - floor(q) == 0) { a = q; n = m; } p = p + 1; } return a; } // Driver code int main() { int n = 1, m = 3; // Function calling cout << minOperations(n, m); return 0; } |
Java
// Java implementation of the above approach import java.util.*; class GFG { // Function to find the minimum no of operations static int minOperations(int n, int m) { int a = 0, k = 1; // find the maximum of two and store it in p int p = Math.max(n, m); // increase it until it is achievable from // given n and m while (n != 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 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; } // Driver code public static void main(String[] args) { int n = 1, m = 3; // Function calling System.out.print(minOperations(n, m)); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 implementation of # the above approach from math import sqrt, floor # Function to find the minimum # no. of operations def minOperations( n, m) : a = 0; k = 1; # find the maximum of two and # store it in p p = max(n, m); # increase it until it is achievable # from given n and m while (n != 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+sqrt(1+8*S(n)))/2 s = float(p - n + p - m); q = (-1 + sqrt(8 * s + 1)) / 2; if (q - floor(q) == 0) : a = q; n = m; p = p + 1; return a; # Driver code if __name__ == "__main__" : n = 1; m = 3; # Function calling print(minOperations(n, m)); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the above approach using System; class GFG { // Function to find the minimum no of operations static int minOperations(int n, int m) { int a = 0, k = 1; // find the maximum of two and store it in p int p = Math.Max(n, m); // increase it until it is achievable from // given n and m while (n != 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 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; } // Driver code public static void Main() { int n = 1, m = 3; // Function calling Console.Write(minOperations(n, m)); } } // This code is contributed by AnkitRai01 |
3
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum operations required to make two numbers equal
- Minimum multiplications with {2, 3, 7} to make two numbers equal
- Minimum operations required to make all the array elements equal
- Find the minimum number of operations required to make all array elements equal
- Minimum decrement operations to make Array elements equal by only decreasing K each time
- Minimum operations to make all elements equal using the second array
- Minimum Cost to make all array elements equal using given operations
- Find minimum number of merge operations to make an array palindrome
- Minimum gcd operations to make all array elements one
- Minimum operations to make GCD of array a multiple of k
- Minimum operations required to make all the elements distinct in an array
- Minimum operations required to make all Array elements divisible by K
- Minimum increment operations to make the array in increasing order
- Find minimum operations needed to make an Array beautiful
- Minimum Increment operations to make Array unique
- Minimum operations of the given type required to make a complete graph
- Minimum operations to make counts of remainders same in an array
- Make all the array elements odd with minimum operations of given type
- Minimum pair merge operations required to make Array non-increasing
- Maximum count of equal numbers in an array after performing given operations
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

