Find Greatest Common Divisor of Array

Given an integer array nums, return the greatest common divisor of the smallest number and the largest number in nums. The greatest common divisor of two numbers is the largest positive integer that evenly divides both.

30:00

Find Greatest Common Divisor of Array
easy
Free written solution
Topics
Companies

Given an integer array nums, return the greatest common divisor of the smallest number and the largest number in nums. The greatest common divisor of two numbers is the largest positive integer that evenly divides both.

Example 1:
Input: {"nums":[2,5,6,9,10]}
Output: 2
Constraints:
  • 2≤nums.length≤10002 \leq \text{nums.length} \leq 1000

  • 1≤nums[i]≤10001 \leq \text{nums}[i] \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[2,5,6,9,10]}

Only the smallest and largest of the 5 numbers matter. Find them first.

nums (blue = min, amber = max)

2
5
6
9
10

min = ?

max = ?

Variables
VariableValue
n5
DepthFunction Call
Stack empty
0/10