GCD of Array

EasyMathArray

Description

Given a non-empty array of positive integers, return the greatest common divisor (GCD) of all elements.

Examples

Input:nums = [12,18,24]
Output:6
Explanation:

gcd(12,18)=6, gcd(6,24)=6.

Input:nums = [7]
Output:7
Explanation:

Single element returns itself.

Input:nums = [5,11,13]
Output:1
Explanation:

Coprime numbers yield 1.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 10⁹

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.