Can anyone help me solve this problem?
include <bits/stdc++.h>
using namespace std; typedef long long int lli; lli gcd(lli a, lli b) { if (a % b == 0) return b; return gcd(b, a % b); } class stk { stack<pair<lli, lli>> st;
public: stk() { } void push(lli val) { lli elem = st.empty() ? val : gcd(val, st.top().second); st.push({val, elem}); } void pop() { st.pop(); } pair<lli, lli> top() { return st.top(); } int size() { return st.size(); } };
void remove(stk &st1, stk &st2) { if (st2.size() == 0) { while (st1.size() > 0) { st2.push(st1.top().first); st1.pop(); } } st2.pop(); swap(st1,st2); }
int main() {
int n;
cin >> n;
vector<lli> arr(n);
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int l = 0;
int res = INT_MAX;
stk st1, st2;
for (int r = 0; r < n; r++)
{
st1.push(arr[r]);
if (arr[r] == 1)
{
res = 1;
break;
}
while (st1.top().second == 1)
{
res = min(res, r - l + 1);
remove(st1, st2);
l++;
}
}
if (res > n)
{
cout << -1;
}
else
{
cout << res;
}
return 0;}
This is my approach using sliding window and stack.







