class MainActivity: def main(self): # Read the data n, k = map(int, filter(lambda x: len(x) > 0, input().split(' '))) nums = list(map(int, filter(lambda x: len(x) > 0, input().split(' ')))) # Initialization nums.sort() leftPtr = 0 rightPtr = 1 result = 0 # Traverse if nums[-1] - nums[0] <= k: print(n) return while rightPtr < n: num = nums[rightPtr] while num - nums[leftPtr] > k and leftPtr < rightPtr: leftPtr += 1 result = max(result, rightPtr - leftPtr + 1) rightPtr += 1 print(result) if __name__ == '__main__': M = MainActivity() M.main()
def func(n, k, alist): if n <= 0: return 0 res = 0 lp = 0 alist.sort() for rp in range(n): if lp < n and alist[rp] - alist[lp] > k: lp += 1 res = max(res, rp-lp+1) return res n, k = map(int, input().strip().split()) alist = list(map(int, input().strip().split())) print(func(n, k, alist))
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); in.nextLine(); int nums[] = new int[n]; for(int i = 0;i < n;i++){ nums[i] = in.nextInt(); } Arrays.sort(nums); // 快慢指针定义 int slow = 0; int fast = 0; int res = 0; while(fast < n){ if(nums[fast] - nums[slow] > k){ res = Math.max(res,fast - slow); slow++; continue; } fast++; } res = Math.max(res,fast - slow); System.out.println(res); } }
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n, k; cin>>n>>k; vector<int> arr(n); for(int i=0; i<n; i++){ cin>>arr[i]; } sort(arr.begin(),arr.end()); int l=0, r=1, max_value=0; while(r<n){ if(arr[r]-arr[l]<=k){ max_value = max(max_value, r-l+1); r++; }else{ l++; } } max_value = max(max_value,r-l); cout<<max_value; return 0; }
public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } Arrays.sort(arr); int a = 0, b = 0; int res = 1; while (b < n) { while (b < n && arr[b] - arr[a] <= k) b++; res = Math.max(res, b - a); if(b == n) break; while (a<b && arr[b] - arr[a] > k) a++; } System.out.println(res); }
package main import ( "fmt" "os" "bufio" "sort" ) var in=bufio.NewReader(os.Stdin) func main() { var n,k int fmt.Scan(&n,&k) arr:=make([]int,n) for i:=0;i<n;i++{ fmt.Fscan(in,&arr[i]) } sort.Ints(arr) ans:=0 for l,r:=0,0;r<len(arr);r++{ for arr[r]-arr[l]>k{ l++ } if r-l+1>ans{ ans=r-l+1 } } fmt.Print(ans) }
#include <iostream> #include <algorithm> int main() { int n, k; std::cin >> n >> k; int nums[n]; for(int i = 0; i < n; ++i) std::cin >> nums[i]; std::sort(nums, nums + n); int left = 0, right = 1; int maxCnt = 0; while(right < n) { if (nums[right] - nums[left] <= k) maxCnt = std::max(maxCnt, right - left + 1); else ++left; ++right; } std::cout << maxCnt; }
#include <iostream> #include<bits/stdc++.h> using namespace std; int main() { long long n,k; cin>>n>>k; long long a[n]; for(long long i=0;i<n;i++) {cin>>a[i]; } long long max=0; sort(a,a+n); for(long long j=0;j<n;j++) {long long t; for(long long i=n-1;i>j;i--) {if(a[i]-a[j]<=k) {t=i;break;} else continue;} if(t-j>max) { max=t-j+1; } } cout<<max; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc= new Scanner(System.in); int n = sc.nextInt();//输入整数个数 int k = sc.nextInt();//间隔k ArrayList<Integer> list = new ArrayList<>(); for(int i=1;i<=n;i++){ list.add(sc.nextInt()); } Collections.sort(list);//将输入的整数从小到大排序 int max = 0; int len = list.size(); int start,end; for(start=0,end=0;end<len;){ if(list.get(end)-list.get(start)<=k) end++; else{ max = max<end-start?end-start:max; start++; } } max = max<end-start?end-start:max; System.out.println(max); } }
def func(n, arr, k): arr = sorted(arr) ans = 0 l = r = 0 while r < n: if arr[r] - arr[l] <= k: ans = max(ans, r - l + 1) else: l += 1 r += 1 return ans if __name__ == "__main__": n, k = [int(x) for x in input().split()] arr = [int(x) for x in input().split()] ans = func(n, arr, k) print(ans)