Find Greatest Common Divisor of Array - Problem

Given an integer array nums, return the greatest common divisor of the smallest number and largest number in nums.

The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers.

Input & Output

Example 1 — Basic Array
$ Input: nums = [2,5,6,9,10]
Output: 2
💡 Note: The smallest number is 2, the largest is 10. GCD(2, 10) = 2 since 2 divides both 2 and 10 evenly.
Example 2 — Small Array
$ Input: nums = [7,5,6,8,3]
Output: 1
💡 Note: The smallest number is 3, the largest is 8. GCD(3, 8) = 1 since they share no common divisors other than 1.
Example 3 — Two Elements
$ Input: nums = [3,3]
Output: 3
💡 Note: Both minimum and maximum are 3. GCD(3, 3) = 3.

Constraints

  • 2 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Find Greatest Common Divisor of ArrayInput: [2, 5, 6, 9, 10]256910MINMAXGCD(2, 10)= 2Output: 22 is the largest number that divides both 2 and 10
Understanding the Visualization
1
Input Array
Given array with various integers
2
Find Min/Max
Identify smallest and largest values
3
Calculate GCD
Return GCD of min and max values
Key Takeaway
🎯 Key Insight: Only the minimum and maximum elements matter for the GCD calculation
Asked in
Google 12 Microsoft 8 Amazon 6
23.0K Views
Medium Frequency
~10 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen