#include #include int cmp_ll (const void *ap, const void *bp) { long long a = *(long long *)ap; long long b = *(long long *)bp; if (a > b) { return 1; } if (a < b) { return -1; } return 0; } int main () { int n = 0; long long k = 0LL; long long l = 0LL; long long u = 0LL; long long a[200000] = {}; int res = 0; long long ans = 1000000000000000000LL; int idx = 0; long long b[200000] = {}; res = scanf("%d", &n); res = scanf("%lld", &k); res = scanf("%lld", &l); res = scanf("%lld", &u); for (int i = 0; i < n; i++) { res = scanf("%lld", a+i); } qsort(a, n, sizeof(long long), cmp_ll); for (int i = 0; i < n; i++) { while (idx < n && a[i]+u-l >= a[idx]) { idx++; } if (i == n-idx || i+1 == n-idx || i == n-idx+1) { long long tmp = 0LL; int tidx = 0; for (int j = 0; j < n; j++) { if (a[j] < a[i]) { tmp += 1LL+(a[i]-a[j]-1LL)/k; } else if (a[j] > a[i]+u-l) { tmp += 1LL+(a[j]-a[i]-u+l-1LL)/k; } } if (tmp < ans) { ans = tmp; } for (int j = 0; j < n; j++) { if (a[j] < a[i]) { b[j] = ((a[i]-a[j]-1LL)/k)*k+a[j]; } else if (a[j] > a[i]+u-l) { b[j] = a[j]-(1LL+(a[j]-a[i]-u+l-1LL)/k)*k; } else { b[j] = a[j]; } } qsort(b, n, sizeof(long long), cmp_ll); for (int j = 0; j < i; j++) { while (tidx < n && b[j]+u-l >= b[tidx]) { tidx++; } if (tmp-((long long)(i-j-n-tidx)) < ans) { ans = tmp-((long long)(i-j-n-tidx)); } } for (int j = 0; j < n; j++) { if (a[j] < a[i]) { b[j] = (1LL+(a[i]-a[j]-1LL)/k)*k+a[j]; } else if (a[j] > a[i]+u-l) { b[j] = a[j]-((a[j]-a[i]-u+l-1LL)/k)*k; } else { b[j] = a[j]; } } qsort(b, n, sizeof(long long), cmp_ll); tidx = n-1; for (int j = n-1; j >= idx; j--) { while (tidx >= 0 && b[j]-u+l <= b[tidx]) { tidx--; } if (tmp-((long long)(j+1-idx-tidx-1)) < ans) { ans = tmp-((long long)(j+1-idx-tidx-1)); } } } } idx = n-1; for (int i = n-1; i >= 0; i--) { while (idx >= 0 && a[i]-u+l <= a[idx]) { idx--; } if (n-i-1 == idx+1 || n-i == idx+1 || n-i-1 == idx+2) { long long tmp = 0LL; int tidx = 0; for (int j = 0; j < n; j++) { if (a[j] > a[i]) { tmp += 1LL+(a[j]-a[i]-1LL)/k; } else if (a[j] < a[i]-u+l) { tmp += 1LL+(a[i]-u+l-a[j]-1LL)/k; } } if (tmp < ans) { ans = tmp; } for (int j = 0; j < n; j++) { if (a[j] > a[i]) { b[j] = a[j]-((a[j]-a[i]-1LL)/k)*k; } else if (a[j] < a[i]-u+l) { b[j] = a[j]+(1LL+(a[i]-a[j]-u+l-1LL)/k)*k; } else { b[j] = a[j]; } } qsort(b, n, sizeof(long long), cmp_ll); tidx = n-1; for (int j = n-1; j >= idx; j--) { while (tidx >= 0 && b[j]-u+l <= b[tidx]) { tidx--; } if (tmp-((long long)(j+1-idx-tidx-1)) < ans) { ans = tmp-((long long)(j+1-idx-tidx-1)); } } for (int j = 0; j < n; j++) { if (a[j] > a[i]) { b[j] = a[j]-(1LL+(a[i]-a[j]-1LL)/k)*k; } else if (a[j] < a[i]-u+l) { b[j] = a[j]+((a[i]-a[j]-u+l-1LL)/k)*k; } else { b[j] = a[j]; } } qsort(b, n, sizeof(long long), cmp_ll); tidx = 0; for (int j = 0; j < i; j++) { while (tidx < n && b[j]+u-l >= b[tidx]) { tidx++; } if (tmp-((long long)(i-j-n-tidx)) < ans) { ans = tmp-((long long)(i-j-n-tidx)); } } } } printf("%lld\n", ans); return 0; }