数轴覆盖

package week03;

import java.util.Scanner;

public class 数轴覆盖 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int m = sc.nextInt();
        int []a = new int[n];
/*
        System.out.println(n);
        System.out.println(m);*/
        for(int i = 0;  i < n; i++)
        {
            a[i] = sc.nextInt();
        }
/*        for(int j = 0;  j < n ; j++)
        {
            System.out.println(a[j]);
        }*/
        int l = 0,r = 0;
        int res = 0;
        while(l < a.length)
        {
            while(r < a.length && a[r] - a[l] < m)
            {
                r++;
            }
            //l++的意思是进行下一个
            //刚才我没有经行l++直接死循环了哈哈
            res = Math.max(res,r - (l++));
        }
        System.out.println(res);
    }
    //本题双指针问题,一定注意循环完毕后进行指针的移动!!
}

  

posted @ 2022-01-10 21:00  爽爽子的秃头生活  阅读(37)  评论(0)    收藏  举报