diff --git a/3025. Find the Number of Ways to Place People I 1 1 b/3025. Find the Number of Ways to Place People I 1 1 new file mode 100644 index 0000000..8040b53 --- /dev/null +++ b/3025. Find the Number of Ways to Place People I 1 1 @@ -0,0 +1,40 @@ +#include +using namespace std; + +class Solution { +public: + vector replaceNonCoprimes(vector& nums) { + vector stack; + + for (int num : nums) { + stack.push_back(num); + + // Merge while top two are non-coprime + while (stack.size() > 1) { + int a = stack.back(); stack.pop_back(); + int b = stack.back(); stack.pop_back(); + int g = gcd(a, b); + + if (g > 1) { + long long l = (1LL * a / g) * b; // lcm + stack.push_back((int)l); + } else { + stack.push_back(b); + stack.push_back(a); + break; + } + } + } + return stack; + } + +private: + int gcd(int a, int b) { + while (b != 0) { + int temp = b; + b = a % b; + a = temp; + } + return a; + } +};