Common Divisor Reduction Algorithm
Last Updated :
14 Sep, 2023
Given two integers x and y. In one step, we have to subtract the greatest common divisor of x and y from x and y each till x ≥ 1 and y ≥ 1. We have to find the minimum number of repetitions of this step.
Examples:
Input: x = 36, y = 16
Output: 4
Explanation: GCD of 36 and 16 is 4, so replace 36 and 16 with 32 and 12
GCD of 32 and 12 is 4, so replace 32 and 12 with 28 and 8
GCD of 28 and 8 is 4, so replace 28 and 8 with 24 and 4
GCD of 24 and 4 is 4, so replace 24 and 4 with 20 and 0
Now, as y < 1 so, process terminated. So steps repeated 4 times, so output will be 4.
Input: x = 60, y = 9
Output: 3
Recursive Approach: The approach is to calculate the greatest common divisor of the two input numbers, finds the maximum possible reduction in the two numbers, and calculate the amount of reduction needed. Then recursively calls itself with updated values of the two numbers after the reduction until the two numbers become less than 1.
Steps to implement the above approach:
- Define a recursive function "solve" which takes two integers a and b as input.
- Check if either a or b is zero, and return 0 if true.
- Check if a and b are equal, and return 1 if true.
- Find the greatest common divisor of a and b using the "__gcd" function.
- Swap a and b if a is greater than b.
- Calculate the values of x and y using the formulas: x = (b-a)/g and y = a/g, where g is the greatest common divisor of a and b.
- Find the maximum value of y/i * i for all factors i of x, and call this value mx.
- Calculate the reduction in a and b after performing the operation, and call it red. The formula for red is (y - mx) * g.
- If red is greater than or equal to a, return a/g.
- If red is zero, return a/g.
- Otherwise, recursively solve the problem on the new values of a and b after performing the operation. The formula for the return value is (y - mx) + solve(a - red, b - red).
Below is the implementation of the above approach:
C++
// C++ code to solve the problem
// "Common Divisor Reduction"
#include <bits/stdc++.h>
using namespace std;
// Recursive function to
// solve the problem
int solve(int a, int b)
{
// If either a or b is zero,
// we can't perform the operation
if (a == 0 || b == 0)
return 0;
// If a and b are equal, we can
// only perform the operation once
if (a == b)
return 1;
// Find the greatest common
// divisor of a and b
int g = __gcd(a, b);
// Swap a and b if a is
// greater than b
if (a > b)
swap(a, b);
// Calculate the values of x and y
int x = (b - a) / g, y = a / g;
// Find the maximum value of y/i * i
// for all factors i of x, and
// call this value mx
int mx = 0;
for (int i = 2; i * i <= x; i++) {
if (x % i != 0)
continue;
int j = x / i;
mx = max(mx, (y / i) * i);
mx = max(mx, (y / j) * j);
}
mx = max(mx, (y / x) * x);
// Calculate the reduction in a and b
// after performing the operation,
// and call it red
int red = (y - mx) * g;
// If red is greater than or equal to
// a, we can't perform the
// operation anymore
if (red >= a)
return a / g;
// If red is zero, we can only perform
// the operation once
if (red == 0)
return a / g;
// Otherwise, recursively solve the
// problem on the new values of a and
// b after performing the operation
return (y - mx) + solve(a - red, b - red);
}
// Driver Code
int main()
{
// Example usage
int x = 36, y = 16;
// Function Call
cout << solve(x, y);
return 0;
}
Java
// JAVA code to solve the problem
// "Common Divisor Reduction"
import java.util.*;
class GFG {
public static int solve(int a, int b)
{
// If either a or b is zero,
// we can't perform the operation
if (a == 0 || b == 0)
return 0;
// If a and b are equal, we can
// only perform the operation once
if (a == b)
return 1;
// Find the greatest common
// divisor of a and b
int g = gcd(a, b);
// Swap a and b if a is
// greater than b
if (a > b) {
int temp = a;
a = b;
b = temp;
}
// Calculate the values of x and y
int x = (b - a) / g, y = a / g;
// Find the maximum value of y/i * i
// for all factors i of x, and
// call this value mx
int mx = 0;
for (int i = 2; i * i <= x; i++) {
if (x % i != 0)
continue;
int j = x / i;
mx = Math.max(mx, (y / i) * i);
mx = Math.max(mx, (y / j) * j);
}
mx = Math.max(mx, (y / x) * x);
// Calculate the reduction in a and b
// after performing the operation,
// and call it red
int red = (y - mx) * g;
// If red is greater than or equal to
// a, we can't perform the
// operation anymore
if (red >= a)
return a / g;
// If red is zero, we can only perform
// the operation once
if (red == 0)
return a / g;
// Otherwise, recursively solve the
// problem on the new values of a and
// b after performing the operation
return (y - mx) + solve(a - red, b - red);
}
public static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static void main(String[] args)
{
int x = 36, y = 16;
System.out.println(solve(x, y));
}
}
// This code is contributed by shivamgupta310570
Python3
import math
# Recursive function to solve the problem
def solve(a, b):
# If either a or b is zero, we can't perform the operation
if a == 0 or b == 0:
return 0
# If a and b are equal, we can only perform the operation once
if a == b:
return 1
# Find the greatest common divisor of a and b
g = math.gcd(a, b)
# Swap a and b if a is greater than b
if a > b:
a, b = b, a
# Calculate the values of x and y
x = (b - a) // g
y = a // g
# Find the maximum value of y//i * i for all factors i of x, and call this value mx
mx = 0
for i in range(2, int(math.sqrt(x)) + 1):
if x % i != 0:
continue
j = x // i
mx = max(mx, (y // i) * i)
mx = max(mx, (y // j) * j)
mx = max(mx, (y // x) * x)
# Calculate the reduction in a and b after performing the operation, and call it red
red = (y - mx) * g
# If red is greater than or equal to a, we can't perform the operation anymore
if red >= a:
return a // g
# If red is zero, we can only perform the operation once
if red == 0:
return a // g
# Otherwise, recursively solve the problem on the new values of a and b after performing the operation
return (y - mx) + solve(a - red, b - red)
# Driver code
if __name__ == '__main__':
# Example usage
x = 36
y = 16
# Function call
print(solve(x, y))
# This code is contributed by shivamgupta310570
C#
using System;
class GFG
{
public static int Solve(int a, int b)
{
// If either a or b is zero,
// we can't perform the operation
if (a == 0 || b == 0)
return 0;
// If a and b are equal, we can
// only perform the operation once
if (a == b)
return 1;
// Find the greatest common
// divisor of a and b
int g = GCD(a, b);
// Swap a and b if a is
// greater than b
if (a > b)
{
int temp = a;
a = b;
b = temp;
}
// Calculate the values of x and y
int x = (b - a) / g, y = a / g;
// Find the maximum value of y/i * i
// for all factors i of x, and
// call this value mx
int mx = 0;
for (int i = 2; i * i <= x; i++)
{
if (x % i != 0)
continue;
int j = x / i;
mx = Math.Max(mx, (y / i) * i);
mx = Math.Max(mx, (y / j) * j);
}
mx = Math.Max(mx, (y / x) * x);
// Calculate the reduction in a and b
// after performing the operation,
// and call it red
int red = (y - mx) * g;
// If red is greater than or equal to
// a, we can't perform the
// operation anymore
if (red >= a)
return a / g;
// If red is zero, we can only perform
// the operation once
if (red == 0)
return a / g;
// Otherwise, recursively solve the
// problem on the new values of a and
// b after performing the operation
return (y - mx) + Solve(a - red, b - red);
}
public static int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
public static void Main(string[] args)
{
int x = 36, y = 16;
Console.WriteLine(Solve(x, y));
}
}
JavaScript
// Function to calculate the gcd
function gcd(a, b) {
if (b === 0) {
return a;
}
return gcd(b, a % b);
}
// Recursive function to
// solve the problem
function solve(a, b) {
// If either a or b is zero,
// we can't perform the operation
if (a === 0 || b === 0) {
return 0;
}
// If a and b are equal, we can
// only perform the operation once
if (a === b) {
return 1;
}
// Find the greatest common
// divisor of a and b
let g = gcd(a, b);
// Swap a and b if a is
// greater than b
if (a > b) {
[a, b] = [b, a];
}
// Calculate the values of x and y
let x = (b - a) / g;
let y = a / g;
// Find the maximum value of y/i * i
// for all factors i of x, and
// call this value mx
let mx = 0;
for (let i = 2; i * i <= x; i++) {
if (x % i !== 0) {
continue;
}
let j = x / i;
mx = Math.max(mx, (y / i) * i);
mx = Math.max(mx, (y / j) * j);
}
mx = Math.max(mx, (y / x) * x);
// Calculate the reduction in a and b
// after performing the operation,
// and call it red
let red = (y - mx) * g;
// If red is greater than or equal to
// a, we can't perform the
// operation anymore
if (red >= a) {
return a / g;
}
// If red is zero, we can only perform
// the operation once
if (red === 0) {
return a / g;
}
// Otherwise, recursively solve the
// problem on the new values of a and
// b after performing the operation
return (y - mx) + solve(a - red, b - red);
}
// Test case
let x = 36, y = 16;
console.log(solve(x, y));
Time Complexity: O(sqrt(x)log(x))
Auxiliary Space: O(1)
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read
Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read