My code is exactly like yours but still giving WA. Can you point out what is wrong here.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m,p;
cin>>n>>m>>p;
int y[n];
pair<int,int>e[n];
for (int i=0;i<n;i++)
{
cin>>e[i].first;
e[i].second = i;
}
sort(e,e+n);
y[0] = 1;
int g = 1;
/for (int i=0;i<n;i++)
cout<<e[i].first<<" ";
cout<<endl;/
for (int i = 1;i<n;i++)
{
if (e[i].first-e[i-1].first <= m)
y[e[i].second] = g;
else
{
g++;
y[e[i].second] = g;
}
}
vectorv;
while(p–)
{
int a,b;
cin>>a>>b;
if (y[a-1] == y[b-1])
v.push_back(“Yes”);
else
v.push_back(“No”);
}
for (int i=0;i<v.size();i++)
cout<<v[i]<<endl;
}
Can’t figure out why I’m getting WA on my recursive solution, I am expecting TLE only.
https://www.codechef.com/viewsolution/40910014
The recurrence makes sense on paper, and I can’t figure out a contradiction.
Can someone help why i am getting NZEC while submitting the code
import java.util.;
import java.lang.;
import java.io.*;
/* Name of the class has to be “Main” only if the class is public. */
class frog
{
int dist,indic,point;
public frog(int dist,int indic,int point)
{
this.dist=dist;
this.indic=indic;
this.point=point;
}
}
class sort implements Comparator
{
public int compare(frog f,frog e)
{
return f.dist-e.dist;
}
}
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
int w=sc.nextInt();
frog arr[]=new frog[n];
frog arr1[]=new frog[n];
for(int i=0;i<n;i++)
{
int dist=sc.nextInt();
frog ff=new frog(dist,i,0);
arr[i]=ff;
}
Arrays.sort(arr,new sort());
int count=0;
for(int i=n-1;i>0;i–)
{
if(arr[i].dist-arr[i-1].dist<=k)
{
arr[i-1].point=arr[i].point;
}
else
{
arr[i].point=count;
arr[i-1].point=count+1;
count++;
}
arr1[arr[i].indic]=arr[i];
}
arr1[0]=arr[0];
for(int i=0;i<w;i++)
{
int l=sc.nextInt();
int h=sc.nextInt();
if(arr1[l-1].point==arr1[h-1].point)
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
}
Thinking for the same Issue.used same approach but giving WA on irtrating from starting Point to End Point
Used Same Approach but cannot why we Cant use arr[i+1] - arr[i] instead of arr[i-1]-arr[i])<=k): which gives WA …
facing Same Problem
I used dsu , by pushing the component which gave me distance<=3 and later searched , it can be even modified using path and rank compression but still got accepted
![]()
https://www.codechef.com/viewsolution/54609759