Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import java.math.*;
- /* Name of the class has to be "Main" only if the class is public. */
- class Codechef {
- static int n;
- static long k;
- static Long a[];
- static StringBuilder str = new StringBuilder("");
- static void solve()
- {
- Arrays.sort(a);
- int ans=0;
- for(int i=0;i<n;i++)
- {
- int l,r;
- long high=a[i]+k;
- long low=a[i]-k;
- int hi=-1,li=-1;
- l=i+1;r=n-1;
- while(l<=r)
- {
- int mid=l+(r-l)/2;
- if(a[mid]<=high)
- {
- hi=mid;
- l=mid+1;
- }
- else
- r=mid-1;
- }
- l=0;r=i-1;
- while(l<=r)
- {
- int mid=l+(r-l)/2;
- if(a[mid]>=low)
- {
- li=mid;
- r=mid-1;
- }
- else
- l=mid+1;
- }
- if(li!=-1 || hi!=-1)
- ans++;
- }
- System.out.println(ans);
- }
- public static void main(String[] args) throws java.lang.Exception {
- BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
- String s[]=bf.readLine().trim().split("\\s+");
- n=Integer.parseInt(s[0]);
- k=Long.parseLong(s[1]);
- s=bf.readLine().trim().split("\\s+");
- a=new Long[n];
- for(int i=0;i<n;i++)
- a[i]=Long.parseLong(s[i]);
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment