You are given an array of positive integers nums. Your task is to return the greatest common divisor (GCD) of all the elements in the array. The GCD is the largest positive integer that divides every element without leaving a remainder.
Input: nums = [12, 18, 24]
Output: 6
Explanation: The largest positive integer that divides 12, 18, and 24 exactly is 6.
Input: nums = [7, 13, 5]
Output: 1
Explanation: The numbers share no common factor greater than 1. Therefore, their GCD is 1.
Input: nums = [10]
Output: 10
Explanation: Since the array contains only one number, its GCD is the number itself: 10.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
12 18
6
8 12 16
4