P20 · 1 second · 256 MB
GCD of an Array
Return the greatest common divisor of every element of `a`. Use the convention `gcd(0, x) = |x|` and `gcd` of an empty array is `0`. Elements may be negative; the gcd is non-negative.
function gcdArray(a) → number
CONSTRAINTS
- 0 ≤ a.length ≤ 2000
- -10⁹ ≤ a[i] ≤ 10⁹
Input: a = [12, 18, 24]
Output: 6
Input: a = [0, 0, 5]
Output: 5
Input: a = []
Output: 0
1
2
3
4
5
6
7
8
Arena judges JavaScript in a Web Worker (1s wall-clock, then TLE). Samples are public; submit runs hidden tests too. C++/Python is a later swarm package.