CC--InterviewProblem icon indicating copy to clipboard operation
CC--InterviewProblem copied to clipboard

just a small mistake in the second while loop

Open riyavishwa981 opened this issue 5 years ago • 0 comments

#include #include #include #include #include #include #include using namespace std; const int N = 111000; int n, q, d; int a[N]; deque dq;

int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ scanf("%d%d",&n,&q); for(int i = 0; i < n; ++ i) scanf("%d",&a[i]); while(q --){ scanf("%d",&d); dq.clear(); int ans = INT_MAX; for(int i = 0; i < n; ++ i){ while(dq.size() && a[dq.back()] < a[i]) dq.pop_back(); dq.push_back(i); while(dq.size() && dq.front() <= i - d) **// here you have to compare the index and not the value ** dq.pop_front(); if(i >= d - 1 ){ ans = min(ans, a[dq.front()]); } } printf("%d\n",ans); } return 0; }

//the rest of the code is fine

riyavishwa981 avatar Mar 02 '21 12:03 riyavishwa981