Minimizing numbers through subtraction operations
Last Updated :
07 Nov, 2023
Given two numbers given as P and Q, the task is to make both numbers equal in the minimum number of operations, where you are allowed to perform the below operations any number of times:
- If P > Q, replace P with P − Q;
- If Q < P, replace Q with Q − P.
Examples:
Input: P = 100001, Q = 100001
Output: 0
Explanation: P and Q are equal so no steps are required.
Input: P = 10, Q = 18
Output: 5
Explanation: Initially, P=10 and Q=18. You repeat the operation as follows:
- P < Q, so replace Q with Q − P = 8, making P = 10 and Q = 8.
- Q < A, so replace P with P − Q = 2, making P = 2 and Q = 8.
- Q > P, so replace Q with Q − P = 6, making P = 2 and Q = 6.
- Q > P, so replace Q with Q − P = 4, making P = 2 and Q = 4.
- Q > P, so replace Q with Q − P = 2, making P = 2 and Q = 2
Thus, you repeat it five times.
Approach: This can be solved with the following idea:
When Q > P, it means Q/P number of steps would be performed in order to make it equal and vice versa.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void minimumSteps( long long P, long long Q)
{
long long number_of_steps = 0;
if (P == Q) {
cout << 0 << endl;
return ;
}
while (P != 0 && Q != 0) {
if (P > Q) {
number_of_steps += P / Q;
P = P % Q;
}
else if (Q > P) {
number_of_steps += Q / P;
Q = Q % P;
}
}
cout << number_of_steps - 1 << endl;
}
int main()
{
long long P, Q;
P = 10;
Q = 18;
minimumSteps(P, Q);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static void minimumSteps( long P, long Q)
{
long number_of_steps = 0 ;
if (P == Q) {
System.out.println( 0 );
return ;
}
while (P != 0 && Q != 0 ) {
if (P > Q) {
number_of_steps += P / Q;
P = P % Q;
}
else if (Q > P) {
number_of_steps += Q / P;
Q = Q % P;
}
}
System.out.println(number_of_steps - 1 );
}
public static void main(String[] args)
{
long P, Q;
P = 10 ;
Q = 18 ;
minimumSteps(P, Q);
}
}
|
Python3
def minimumSteps(P, Q):
number_of_steps = 0
if P = = Q:
print ( 0 )
return
while P ! = 0 and Q ! = 0 :
if P > Q:
number_of_steps + = P / / Q
P = P % Q
elif Q > P:
number_of_steps + = Q / / P
Q = Q % P
print (number_of_steps - 1 )
if __name__ = = '__main__' :
P = 10
Q = 18
minimumSteps(P, Q)
|
C#
using System;
public class GFG {
static void MinimumSteps( long P, long Q)
{
long number_of_steps = 0;
if (P == Q) {
Console.WriteLine(0);
return ;
}
while (P != 0 && Q != 0) {
if (P > Q) {
number_of_steps += P / Q;
P = P % Q;
}
else if (Q > P) {
number_of_steps += Q / P;
Q = Q % P;
}
}
Console.WriteLine(number_of_steps - 1);
}
static void Main( string [] args)
{
long P, Q;
P = 10;
Q = 18;
MinimumSteps(P, Q);
}
}
|
Javascript
<script>
function minimumSteps(P, Q) {
let number_of_steps = 0;
if (P == Q) {
document.write(0);
return ;
}
while (P != 0 && Q != 0) {
if (P > Q) {
number_of_steps += Math.floor(P / Q);
P = P % Q;
}
else if (Q > P) {
number_of_steps += Math.floor(Q / P);
Q = Q % P;
}
}
document.write(number_of_steps - 1);
}
let P, Q;
P = 10;
Q = 18;
minimumSteps(P, Q);
</script>
|
Time Complexity: O(Log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...