Given two positive integers N and M, the task is to find the number of different GCDs which can be formed by adding an integer K to both N and M, where K ≥ 0.
Examples:
Input: N = 8, M = 4
Output: 3
Explanation: If K = 0, then GCD(8, 4) = 4,
If K = 1, then GCD(9, 5) = 1,
If K = 2, then GCD(10, 6) = 2Input: N = 7, M = 10
Output: 2
Explanation: If K = 0, then GCD(7, 10) = 1,
If K = 2, then GCD(9, 12) = 3
Approach: The problem can be solved based on the following mathematical idea:
The maximum value of GCD formed after adding any value of K will be abs(N - M).
Other than the above result if any other GCD is formed, that will be a perfect divisor of abs(N - M).
Follow the steps below to implement the above idea:
- Find the absolute difference between N and M (say X).
- Find the number of unique divisors of the X.
- Return this value as the required answer.
Below is the implementation of the above approach:
#include <bits/stdc++.h>
using namespace std;
int distinctGCDs(int N, int M)
{
// hs contains different results
// of GCD can formed
set<int> hs;
int diff = abs(N - M);
// Finding perfect divisor
// for diff variable
for (int i = 1; i * i <= diff; i++) {
// If we found any perfect divisor
// we will add it in our Set
if (diff % i == 0) {
hs.insert(i);
hs.insert(diff / i);
}
}
// Returning number of distinct GCD's
// which can be formed
return hs.size();
}
int main() {
int N = 8;
int M = 4;
// Function call
cout << distinctGCDs(N, M);
return 0;
}
// This code is contributed by satwik4409.
// Java code to implement the approach
import java.util.*;
class GFG {
// Driver Code
public static void main(String[] args)
{
int N = 8;
int M = 4;
// Function call
System.out.println(distinctGCDs(N, M));
}
// Function to find distinct numbers
// of GCD's can formed by adding N
public static int distinctGCDs(int N, int M)
{
// hs contains different results
// of GCD can formed
Set<Integer> hs = new HashSet<>();
int diff = Math.abs(N - M);
// Finding perfect divisor
// for diff variable
for (int i = 1; i * i <= diff; i++) {
// If we found any perfect divisor
// we will add it in our Set
if (diff % i == 0) {
hs.add(i);
hs.add(diff / i);
}
}
// Returning number of distinct GCD's
// which can be formed
return hs.size();
}
}
# python3 code to implement the above approach
def distinctGCDs(N, M):
# hs contains different results
# of GCD can formed
hs = set()
diff = abs(N - M)
# Finding perfect divisor
# for diff variable
i = 1
while i*i <= diff :
# If we found any perfect divisor
# we will add it in our Set
if diff % i == 0 :
hs.add(i)
hs.add(int(diff / i))
i+=1
# Returning number of distinct GCD's
# which can be formed
return len(hs)
# Driver Code
if __name__ == "__main__" :
N = 8
M = 4
# Function call
print(distinctGCDs(N, M))
# this code is contributed by aditya942003patil
// C# code to implement the approach
using System;
using System.Collections.Generic;
public class GFG {
// Driver Code
public static void Main(string[] args)
{
int N = 8;
int M = 4;
// Function call
Console.WriteLine(distinctGCDs(N, M));
}
// Function to find distinct numbers
// of GCD's can formed by adding N
public static int distinctGCDs(int N, int M)
{
// hs contains different results
// of GCD can formed
HashSet<int> hs = new HashSet<int>();
int diff = Math.Abs(N - M);
// Finding perfect divisor
// for diff variable
for (int i = 1; i * i <= diff; i++) {
// If we found any perfect divisor
// we will add it in our Set
if (diff % i == 0) {
hs.Add(i);
hs.Add(diff / i);
}
}
// Returning number of distinct GCD's
// which can be formed
return hs.Count;
}
}
// This code is contributed by AnkThon
<script>
// JavaScript code to implement the approach
const distinctGCDs = (N, M) => {
// hs contains different results
// of GCD can formed
let hs = new Set();
let diff = Math.abs(N - M);
// Finding perfect divisor
// for diff variable
for (let i = 1; i * i <= diff; i++) {
// If we found any perfect divisor
// we will add it in our Set
if (diff % i == 0) {
hs.add(i);
hs.add(diff / i);
}
}
// Returning number of distinct GCD's
// which can be formed
return hs.size;
}
let N = 8;
let M = 4;
// Function call
document.write(distinctGCDs(N, M));
// This code is contributed by rakeshsahni
</script>
Output
3
Time Complexity: O(sqrt(abs(N - M)))
Auxiliary Space: O(sqrt(abs(N - M)))