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.
Input: nums = [4, 6, 8]
Output: 24
Explanation: The smallest positive integer divisible by 4, 6, and 8 is 24.
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.
Input: nums = [7]
Output: 7
Explanation: Since the array contains only one number, its LCM is the number itself: 7.
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.
2 3
6
4 6
12