Skip to main content
Back to problems
Leetcode
Medium
Arrays
Math
Number Theory
Find Greatest Common Divisor Of Array

Given an integer array, find the greatest common divisor of its largest and smallest elements.

Acceptance 0%
Problem Statement

Problem

You are given an integer array nums. Your task is to compute the greatest common divisor (GCD) of the smallest and largest values in the array.

In other words, let:

  • mn be the minimum value in nums
  • mx be the maximum value in nums

Return gcd(mn, mx).

Notes

  • The GCD of two integers is the largest positive integer that divides both numbers without remainder.
  • The array contains at least one integer.

Input Format

  • An integer array nums.

Output Format

  • Return a single integer: the GCD of the minimum and maximum elements of nums.

Constraints

  • nums.length >= 1
  • Elements are integers.
  • The problem is intended to be solved in linear time by scanning for the minimum and maximum values, then computing their GCD.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [2,5,6,9,10]

Output

2

Explanation

The minimum is 2 and the maximum is 10. Their GCD is 2.

Example 2

Input

nums = [7,5,6,8,3]

Output

1

Explanation

The minimum is 3 and the maximum is 8. Their GCD is 1.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.