Back to Month
EASY 18 Jul 2026 View on LeetCode

1979. Find Greatest Common Divisor of Array

</> Solution

class Solution {
    public int findGCD(int[] nums) {
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        for (int num : nums) {
            min = Math.min(min, num);
            max = Math.max(max, num);
        }

        return gcd(min, max);
    }
    private int gcd(int a, int b) {
        while (b != 0) {
            int temp = a % b;
            a = b;
            b = temp;
        }
        return a;
    }
}

TIME COMPLEXITY

O(n + log(min))

SPACE COMPLEXITY

O(1)

TOPICS

Array Math