I'm struggling with a question on hackerrank deque-stl. I have implemented an algorithm which finds the maximum element in the windows and stores its index, then use this index of the maximum element in the previous window to find the maximum element in the next window only if the index of maximum element lies between the indexes of the next window. Using this algorithm and suggestions mentioned in the comments i've implemented this updated algorithm but i'm still getting Wrong Answer Error.
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int m,idx;
void maxinwindow(int arr[], int start, int end)
{
/*If the index of the maximum element in the previous window
is between the indexes of next windows then no need to compare
elements that were in previous window */
if(idx>=start)
{
if(arr[idx]>=arr[end])
{
m=arr[idx];
}
else
{
m=arr[end];
idx=end;
}
}
else
{
if(arr[start]>=arr[start+1])
m=arr[start];
else
m=arr[start+1];
for(int k=start+2;k<=end;k++)
{
if(arr[k]>=m)
{
m=arr[k];
idx=k;
}
}
}
}
int main()
{
int arr[100000];
int q;
cin>>q;
for(int i=1,size,ws;i<=q;i++)
{
m=0;
cin>>size; //Array size
cin>>ws; //Window Size
//Entering The Elements In The Array
for(int j=1;j<=size;j++)
{
cin>>arr[j];
}
//Boundary Condition i.e. Windows size is equal to 1
if(ws==1)
{
for(int j=1;j<=size;j++)
{
cout<<arr[j]<<" ";
}
}
else
{
for(int k=1;k<=ws;k++)
{
if(arr[k]>=m)
{
m=arr[k];
idx=k;
}
}
cout<<m<<" ";
for(int k=2,j;k<=(size-(ws-1));k++)
{
j=(k+(ws-1));
maxinwindow(arr,k,j);
cout<<m<<" ";
}
cout<<endl;
}
}
}