Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find GCD of Multiple Numbers

Basic Acceptance 98.77% Points 10

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.

Examples
Example 1

Input: nums = [12, 18, 24]
Output: 6
Explanation: The largest positive integer that divides 12, 18, and 24 exactly is 6.

Example 2

Input: nums = [7, 13, 5]
Output: 1
Explanation: The numbers share no common factor greater than 1. Therefore, their GCD is 1.

Example 3

Input: nums = [10]
Output: 10
Explanation: Since the array contains only one number, its GCD is the number itself: 10.

Constraints
  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109
Topics
Data Structures Basics Array Basics
Companies
Samsung ServiceNow Morgan Stanly
Added 31 Oct 2026 98.77% acceptance
Ln 1, Col 1Sign in to save your code
12 18
6