Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find LCM of Multiple Numbers

Basic Acceptance 87.89% Points 10

You are given an array of positive integers nums. Your task is to return the least common multiple (LCM) of all the elements in the array. The LCM is the smallest positive integer that is divisible by every element without leaving a remainder.

Examples
Example 1

Input: nums = [4, 6, 8]
Output: 24
Explanation: The smallest positive integer divisible by 4, 6, and 8 is 24.

Example 2

Input: nums = [2, 3, 5]
Output: 30
Explanation: Since 2, 3, and 5 are pairwise coprime, their LCM is their product: 2 × 3 × 5 = 30.

Example 3

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

Constraints
  • 1 <= nums.length <= 10³
  • 1 <= nums[i] <= 10³
Topics
Data Structures Basics Array Basics
Companies
Netflix Infosys
Added 31 Oct 2026 87.89% acceptance
Ln 1, Col 1Sign in to save your code
2 3
6