Minimum operations to make two numbers equal
Last Updated :
22 Feb, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int n, int m)
{
int a = 0, k = 1;
int p = max(n, m);
while (n != m) {
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;
}
int main()
{
int n = 1, m = 3;
cout << minOperations(n, m);
return 0;
}
|
Java
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));
}
}
|
Python3
from math import sqrt, floor
def minOperations( n, m) :
a = 0 ; k = 1 ;
p = max (n, m);
while (n ! = m) :
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;
if __name__ = = "__main__" :
n = 1 ; m = 3 ;
print (minOperations(n, m));
|
C#
using System;
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()
{
int n = 1, m = 3;
Console.Write(minOperations(n, m));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...