Largest Subset with GCD 1Given n integers, we need to find size of the largest subset with GCD equal to 1. Input Constraint : n <= 10^5, A[i] <= 10^5Examples: Input : A = {2, 3, 5}Output : 3Explanation: The largest subset with a GCD greater than 1 is {2, 3, 5}, and the GCD of all the elements in the subset is 3.Input
6 min read
Summation of GCD of all the pairs up to nGiven a number n, find sum of all GCDs that can be formed by selecting all the pairs from 1 to n. Examples: Input : n = 4Output : 7Explanation: Numbers from 1 to 4 are: 1, 2, 3, 4Result = gcd(1,2) + gcd(1,3) + gcd(1,4) + gcd(2,3) + gcd(2,4) + gcd(3,4) = 1 + 1 + 1 + 1 + 2 + 1 = 7Input : n = 12Output
10 min read
GCD of more than two (or array) numbersGiven an array arr[] of non-negative numbers, the task is to find GCD of all the array elements. In a previous post we find GCD of two number.Examples:Input: arr[] = [1, 2, 3]Output: 1Input: arr[] = [2, 4, 6, 8]Output: 2Using Recursive GCDThe GCD of three or more numbers equals the product of the pr
11 min read
GCD of digits of a given numberGiven a number n, find GCD of its digits. Examples : Input : 345 Output : 1 GCD of 3, 4 and 5 is 1. Input : 2448 Output : 2 GCD of 2, 4, 4 and 8 is 2 We traverse the digits of number one by one using below loop digit = n mod 10; n = n / 10; While traversing digits, we keep track of current GCD and k
4 min read
Maximize GCD of all possible pairs from 1 to NGiven an integer N (> 2), the task is to find the maximum GCD among all pairs possible by the integers in the range [1, N]. Example: Input: N = 5 Output: 2 Explanation : GCD(1, 2) : 1 GCD(1, 3) : 1 GCD(1, 4) : 1 GCD(1, 5) : 1 GCD(2, 3) : 1 GCD(2, 4) : 2 GCD(2, 5) : 1 GCD(3, 4) : 1 GCD(3, 5) : 1 G
3 min read
GCD of two numbers formed by n repeating x and y timesGiven three positive integer n, x, y. The task is to print Greatest Common Divisor of numbers formed by n repeating x times and number formed by n repeating y times. 0 <= n, x, y <= 1000000000.Examples : Input : n = 123, x = 2, y = 3. Output : 123 Number formed are 123123 and 123123123. Greate
6 min read
Find the GCD that lies in given rangeGiven two positive integer a and b and a range [low, high]. The task is to find the greatest common divisor of a and b which lie in the given range. If no divisor exist in the range, print -1.Examples: Input : a = 9, b = 27, low = 1, high = 5 Output : 3 3 is the highest number that lies in range [1,
7 min read
Find the maximum GCD of the siblings of a Binary TreeGiven a 2d-array arr[][] which represents the nodes of a Binary tree, the task is to find the maximum GCD of the siblings of this tree without actually constructing it. Example:Â Â Input: arr[][] = {{4, 5}, {4, 2}, {2, 3}, {2, 1}, {3, 6}, {3, 12}}Â Output: 6Â Explanation:Â Â For the above tree, the maxi
11 min read