 # Chef and Frogs | CodeChef

#include<bits/stdc++.h>
using namespace std;

int main(){
int n, k, p, i, x, y;
cin >> n >> k >> p;
vector<pair<int, int>> a(n);
for(i = 0; i < n; i++){
cin >> x;
a[i] = {x, i}; //Store both distance with their respective entry number(i) with it
}

``````sort(a.begin(), a.end()); //sort the array
int dp[n], pos[n] = {0}; //pos array to store new position in terms of original entry number
dp = 0;
pos[a.second] = 0;
for(i = 1; i < n; i++){
if(a[i].first <= (a[i-1].first + k))
//increment 1 when "if" condition is true i.e i can be reach by (i-1)
//and all numbers from which we can reach (i-1)
dp[i] = dp[i-1] + 1;
else
//if we can't able to go from (i-1) to i
dp[i] = 0;
//Store the new position(i) in terms of original entry position(a[i].second)
pos[a[i].second] = i;
}
for(i = 0; i < p; i++){
cin >> x >> y;
//find the position in sorted aaray from original entry position(x, y)
x = pos[x-1];
y = pos[y-1];
//if distance between x and y(in sorted array) is same as dp[x] and dp[y]
if((y - x) == (dp[y] - dp[x]))
cout<<"YES\n";
else
cout<<"NO\n";
}
``````

}
// 0 1 2 3 4 //index(base 0)
// 0 3 8 5 12 //original
// 0 3 5 8 12 //sorted
// 0 1 2 3 0 //dp
// 0 1 3 2 4 //pos

Is there any error in my logic
Also can someone suggest the test case for failure of logic