
Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Notice that the order of merging two numbers into their LCM does not matter so we can greedily merge elements to its left if possible.
If a new value is formed, we should recursively check if it can be merged with the value to its left.
To simulate the merge efficiently, we can maintain a stack that stores processed elements. When we iterate through the array, we only compare with the top of the stack (which is the value to its left).