Time Limit Problem

I tried to rectify my code, but still, t is showing time-limit- exceeded.

Here Is The Code :

#include
using namespace std;

int main()
{
int n,i,k,j,max;

cin>>n;

int arr[n];

for(i=0;i<n;i++)
{
	cin>>arr[i];	
}

cin>>k;

for (int i = 0; i <= n - k; i++) 
{ 
	max = arr[i]; 

	for (j = 1; j < k; j++) 
	{ 
		if (arr[i + j] > max) 
		{
			max = arr[i + j];
		} 
	} 
	cout << max << " "; 
}

return 0; 

}

Hey @NKushal
code looks good
But not efficient
Efficient Approach - (Sliding Window Technique using Deque)
We create a Deque, Qi of capacity k, that stores only useful elements of current window of k elements. An element is useful if it is in current window and is greater than all other elements on left side of it in current window. We process all array elements one by one and maintain Qi to contain useful elements of current window and these useful elements are maintained in sorted order. The element at front of the Qi is the largest and element at rear of Qi is the smallest of current window.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.