Given two numbers n and m. Find the biggest integer a(gcd), such that all integers n, n + 1, n + 2, …, m are divisible by a.

Examples:

Input : n = 1, m = 2 Output: 1 Explanation: Here, series become 1, 2. So, the greatest no which divides both of them is 1. Input : n = 475, m = 475 Output : 475 Explanation: Here, series has only one term 475. So, greatest no which divides 475 is 475.

Here, We have to examine only two cases:

- if a = b : the segment consists of a single number, hence the answer is a.
- if a < b : we have gcd(n, n + 1, n?+ 2, …, m) = gcd(gcd(n, n + 1), n + 2, …, m) = gcd(1, n + 2, …, n) = 1.
- Minimum elements to be added in a range so that count of elements is divisible by K
- XOR of all the elements in the given range [L, R]
- Sum of elements of an AP in the given range
- XOR of a subarray (range of elements) | Set 2
- Sum of elements of a Geometric Progression (GP) in a given range
- Sum of multiples of Array elements within a given range [L, R]
- Number of elements with even factors in the given range
- Find the XOR of the elements in the given range [L, R] with the value K for a given set of queries
- Find elements in a given range having at least one odd divisor
- Number of elements with odd factors in given range
- Queries for GCD of all numbers of an array except elements in a given range
- Count of elements which are power of 2 in a given range subarray for Q queries
- Count elements in the given range which have maximum number of divisors
- Check if product of Array elements in given range are M-th root or not
- Count numbers in a range that are divisible by all array elements
- Number of Co-prime pairs obtained from the sum of digits of elements in the given range
- Count of elements having odd number of divisors in index range [L, R] for Q queries
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]

Below is the code for above approach.

## C++

`// GCD of given range ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `rangeGCD(` `int` `n, ` `int` `m) ` `{ ` ` ` `return` `(n == m)? n : 1; ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `n = 475; ` ` ` `int` `m = 475; ` ` ` `cout << rangeGCD(n, m); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// GCD of given range ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `rangeGCD(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `return` `(n == m) ? n : ` `1` `; ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `475` `; ` ` ` `int` `m = ` `475` `; ` ` ` ` ` `System.out.println(rangeGCD(n, m)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ajit. ` |

*chevron_right*

*filter_none*

## Python3

` ` `# GCD of given range ` ` ` `def` `rangeGCD(n, m): ` ` ` `return` `n ` `if` `(n ` `=` `=` `m) ` `else` `1` ` ` `# Driver code ` `n, m ` `=` `475` `, ` `475` `print` `(rangeGCD(n, m)) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// GCD of given range ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `rangeGCD(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `return` `(n == m) ? n : 1; ` ` ` `} ` ` ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 475; ` ` ` `int` `m = 475; ` ` ` ` ` `Console.WriteLine(rangeGCD(n, m)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for ` `// GCD of given range ` ` ` `// function returns the GCD ` `function` `rangeGCD(` `$n` `, ` `$m` `) ` `{ ` ` ` `return` `(` `$n` `== ` `$m` `)? ` `$n` `: 1; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 475; ` ` ` `$m` `= 475; ` ` ` `echo` `rangeGCD(` `$n` `, ` `$m` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

Output:

475

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.