https://www.codechef.com/LRNDSA07/problems/FROGV

https://www.codechef.com/LRNDSA07/problems/FROGV

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define mod 1e9+7
#define ff first
#define ss second
#define ins insert
#define endl "\n"
#define pie_op ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define tc int t;cin>>t;while(t--)
#define pb push_back
#define input(n) int arr[n];for(int i1=0;i1<n;i1++)cin>>arr[i1]

int main()
{
	pie_op;
	ll n,p,k;cin>>n>>p>>k;
	vector<ll> arr(n);
	set<ll> st;
	for(ll i=0;i<n;i++){
		cin>>arr[i];
		st.ins(arr[i]);
	}
	arr.clear();
	for(auto x: st){
		arr.pb(x);
	}
	vector<ll> v(arr.size(),0);
	
	for(ll i=1;i<arr.size();i++){
		if(arr[i]-arr[i-1]<=k){
			v[i]+=v[i-1]+1;		
		}
		else{
			v[i]=0;
		}
	}

	while(p--){
		ll a,b;cin>>a>>b;
		if(v[b-1]>=(b-a)) cout<<"YES";
		else cout<<"NO";
		cout<<endl;
	}
	return 0;
}

Can anyone tell me what i am doing wrong?

Input is given as N K P
and you have taken input as N P K.
Sample will give same answer as P and K are equal in sample.
And still I think there is logical error.
This code will give segmentation fault on ( as your v size will be 1 and you will accessing index 4 while answering query ).
5 1 1
1 1 1 1 1
4 5

1 Like

Can you help me with right code

Code:-
Coding Blocks IDE
Just sort the given array by x coordinates and now if consecutive nodes have distance less than equal to k then color both of them with the same color otherwise change the color.
Now you can see the people which can communicate will have same color.

2 Likes