You are given an array A of length N (where N is as large as 2×105). All elements of the array are positive integers less than or equal to N. Determine the count of subarrays (contiguous subsequences) of A which contain their length as an element.
The time limit on CodeChef is 1 second. My code exceeds the time limit.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T,N;
cin>>T; // the number of test cases
for(int i=0;i<T;i++)
{vector<int> A;
cin>>N; // length of array
for(int i=0;i<N;i++)
{
int x;
cin>>x; // array elements
A.push_back(x);
}
int c=0;
for(int i=0;i<N;i++)
{
for(int j=i;j<N;j++)
{
for(int k=i;k<=j;k++)
{
if(A[k]==j-i+1) {/*j-i+1 is length of the subarray that starts at
c++; index i and ends at index j.*/
break; }
}
}
}
cout<<c<<endl;
}
return 0;
}