GCD (Greatest Common Divisor) Practice Problems for Competitive Programming Last Updated : 10 Nov, 2023 Improve Improve Like Article Like Save Share Report GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest positive integer that divides both of the numbers. GCD (Greatest Common Divisor) Fastest way to compute GCD The fastest way to find the Greatest Common Divisor (GCD) of two numbers is by using the Euclidean algorithm. The Euclidean algorithm is an efficient and widely used method for GCD calculation. It used to compute GCD of two numbers in O(log(min(a,b)). It uses recursion to repeatedly replace a with b and b with the remainder of a divided by b until b becomes zero, returning a as the GCD. This approach is concise and efficient for GCD calculation. Below is the code snippet for above algorithm: C++ int gcd (int a, int b) { if (b == 0) return a; else return gcd (b, a % b); } Java public static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } Python3 def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) C# using System; int Gcd(int a, int b) { if (b == 0) return a; else return Gcd(b, a % b); } Javascript function gcd(a, b) { if (b === 0) { return a; } else { return gcd(b, a % b); } } Problems identification that involve GCD (Greatest Common Divisor) GCD (Greatest Common Divisor) problems can vary in complexity and application, but they generally involve finding the greatest common divisor of one or more integers or applying GCD properties to solve a specific problem. Here are some common types of problems that involve GCD: Problems that require to determine if one number is divisible by another may involve GCD, as GCD is related to the greatest common factor of two numbers. Many number theory problems which related to coprime numbers, relatively prime numbers, or Euler’s totient function, involve GCD calculation. Consider whether the problem hints about the need to factorize numbers into their prime factors. GCD problems sometimes involve prime factorization to find common factors efficiently. In graph theory, GCD can be used in problems related to connected components, in which a edge between the nodes will only exist if they are not co-prime. Here is a list of the Top GCD Problems for practice. Problems in this Article are divided into three Levels to practice according to the difficulty level step by step. Easy Level Problems on GCD Program to Find GCD or HCF of Two Numbers Check if two numbers are co-prime or not GCD of more than two (or array) numbers Program to find LCM of two numbers LCM of given array elements Find the other number when LCM and HCF given Minimum insertions to make a Co-prime array Find the minimum possible health of the winning player Minimum squares to evenly cut a rectangle Minimum operations to make GCD of array a multiple of k Medium Level Problems on GCD LCM of First n Natural Numbers Given GCD G and LCM L, find number of possible pairs (a, b) Count numbers in range 1 to N which are divisible by X but not by Y Minimum possible final health of the last monster in a game Find the maximum GCD of the siblings of a Binary Tree Check if possible to move from given coordinate to desired coordinate Program to find Greatest Common Divisor (GCD) of N strings Largest subsequence having GCD greater than 1 Subsequence of size k with maximum possible GCD Minimum gcd operations to make all array elements one GCD of elements in a given range Count pairs of natural numbers with GCD equal to given number Maximum GCD of all subarrays of length at least 2 Queries for GCD of all numbers of an array except elements in a given range Minimum operations required to make two numbers equal GCD of two numbers formed by n repeating x and y times Hard Level Problems on GCD Remove an element to maximize the GCD of the given array Common divisors of N numbers Find the number of pairs such that their gcd is equals to 1 Program to find Nth term divisible by a or b Largest number that divides x and is co-prime with y Summation of GCD of all the pairs up to N Calculate the Sum of GCD over all subarrays Sum of LCM(1, n), LCM(2, n), LCM(3, n), … , LCM(n, n) Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B Find original numbers from gcd() every pair Count number of sub-sequences with GCD 1 Split array into minimum number of subarrays having GCD of its first and last element exceeding 1 Like Article Suggest improvement Next Program to Find GCD or HCF of Two Numbers Share your thoughts in the comments Add Your Comment Please Login to comment...