Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find LCM of Multiple Numbers

Basic Acceptance 87.89% Points 10.00

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³
Companies
Netflix Infosys
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