Javascript reduce()
const gcd = (a,b) =>{ return a%b === 0 ? b:gcd(b,a%b); } const lcm = (a,b) =>{ return a*b/gcd(a,b); } function solution (arr) { return arr.reduce((res, cur) => (res * cur) / gcd(res, cur)); }
C++
int gcd(int x, int y){ return x%y== 0 ? y:gcd(y,x%y);} int lcm(int x, int y) { return x * y / gcd(x, y); } int solution(vector<int> arr) { int answer = arr[0]; for(int i=1;i<arr.size();i++){ answer = lcm(answer,arr[i]); } return answer; }