Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find GCD of Multiple Numbers

Basic Acceptance 98.77% Points 10.00

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] = 10?
Companies
Samsung ServiceNow Morgan Stanly
Topics
Data Structures Basics Array Basics
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions