Back to July Back to Year 2026
EASY 18 Jul 2026 View on LeetCode

1979. Find Greatest Common Divisor of Array

</> Solution

/**
 * Problem: LeetCode 1979 - Find Greatest Common Divisor of Array
 * Difficulty: Easy
 * Language: Java
 * Author: CodeByTushu
 */
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 A))

SPACE COMPLEXITY

O(1)

TOPICS

Array Math Number Theory