Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
Example 1:
Input: nums = [2,5,6,9,10] Output: 2 Explanation: The smallest number in nums is 2. The largest number in nums is 10. The greatest common divisor of 2 and 10 is 2.
Example 2:
Input: nums = [7,5,6,8,3] Output: 1 Explanation: The smallest number in nums is 3. The largest number in nums is 8. The greatest common divisor of 3 and 8 is 1.
Example 3:
Input: nums = [3,3] Output: 3 Explanation: The smallest number in nums is 3. The largest number in nums is 3. The greatest common divisor of 3 and 3 is 3.
Constraints:
2 <= nums.length <= 10001 <= nums[i] <= 1000Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[2,5,6,9,10]
[7,5,6,8,3]
[3,3]
greatest-common-divisor-of-strings)number-of-different-subsequences-gcds)three-divisors)smallest-even-multiple)number-of-subarrays-with-gcd-equal-to-k)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1979: Find Greatest Common Divisor of Array
class Solution {
public int findGCD(int[] nums) {
int a = 1, b = 1000;
for (int x : nums) {
a = Math.max(a, x);
b = Math.min(b, x);
}
return gcd(a, b);
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
// Accepted solution for LeetCode #1979: Find Greatest Common Divisor of Array
func findGCD(nums []int) int {
a, b := slices.Max(nums), slices.Min(nums)
return gcd(a, b)
}
func gcd(a, b int) int {
if b == 0 {
return a
}
return gcd(b, a%b)
}
# Accepted solution for LeetCode #1979: Find Greatest Common Divisor of Array
class Solution:
def findGCD(self, nums: List[int]) -> int:
return gcd(max(nums), min(nums))
// Accepted solution for LeetCode #1979: Find Greatest Common Divisor of Array
impl Solution {
pub fn find_gcd(nums: Vec<i32>) -> i32 {
let min_val = *nums.iter().min().unwrap();
let max_val = *nums.iter().max().unwrap();
gcd(min_val, max_val)
}
}
fn gcd(mut a: i32, mut b: i32) -> i32 {
while b != 0 {
let temp = b;
b = a % b;
a = temp;
}
a
}
// Accepted solution for LeetCode #1979: Find Greatest Common Divisor of Array
function findGCD(nums: number[]): number {
const min = Math.min(...nums);
const max = Math.max(...nums);
return gcd(min, max);
}
function gcd(a: number, b: number): number {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.