You are given the following: Integers n, c, d Arr...

Created on: September 2, 2025

Answered using GPT-5 Thinking by Chat01

Question

You are given the following:

Integers n, c, d
Array a as {a1, a2, ..., an} of length n
Array b as {b1, b2, ..., bn} of length n

Determine the number of pairs i, j (1 <= i < j <= n), satisfying the inequality
ai - aj + c <= bi - bj + d

code this in java

Thought

Refining pair count approach

Alright, I need to compute the number of pairs (i < j) where ai - aj + c <= bi - bj + d. After rearranging it, it becomes about counting pairs where xi - xj <= k with k = d - c. I'll explore sorting xi and using pointers or binary indexed trees. Sorting should help find pairs more efficiently, and the "i < j" condition holds since I'm only using the values, not positions.

Clarifying pair conditions

Hmm, I'm wondering if the inequality's symmetry could cause some confusion. Since xi - xj is symmetric, counting unordered pairs could lead to the same result as counting ordered pairs. But after sorting xi in ascending order, it gets tricky since sorting changes the original index order, and I need to respect i < j.

Understanding index relevance

Sorting affects order, but the inequality depends on the direction of the pair (i < j). When counting pairs, flipping the indices gives the opposite difference, so sorting alone doesn't work. The solution requires maintaining original indices or using another way to scan pairs offline with proper structure.

Efficient counting approach

Since the condition requires i < j, I can iterate j, and for each j, count how many i values (where i < j) fulfill xi - xj <= k. This can be efficiently done with a binary indexed tree or a sorted list. By maintaining previous xi values and using upper bound to count satisfying pairs, I can achieve a faster solution.

Thought for 24s

Answer

Here’s an O(n log n) Java solution. It reduces the condition

ai − aj + c ≤ bi − bj + d
⟺ (ai − bi) − (aj − bj) ≤ (d − c)

Let xi = ai − bi and k = d − c. For each j (in index order), we need the count of previous i < j with xi ≤ xj + k. We maintain a Fenwick tree (Binary Indexed Tree) over compressed xi values to query “how many previous xi are ≤ threshold” in logarithmic time.

java
import java.io.*; import java.util.*; public class Main { // Fenwick (Binary Indexed) Tree for prefix sums static final class Fenwick { long[] bit; // 1-indexed Fenwick(int n) { bit = new long[n + 1]; } void add(int idx, long delta) { for (int i = idx; i < bit.length; i += i & -i) bit[i] += delta; } long sum(int idx) { long res = 0; for (int i = idx; i > 0; i -= i & -i) res += bit[i]; return res; } } // Fast input static final class FastScanner { private final InputStream in; private final byte[] buffer = new byte[1 << 16]; private int ptr = 0, len = 0; FastScanner(InputStream is) { in = is; } private int read() throws IOException { if (ptr >= len) { len = in.read(buffer); ptr = 0; if (len <= 0) return -1; } return buffer[ptr++]; } long nextLong() throws IOException { int c; do { c = read(); } while (c <= 32); int sign = 1; if (c == '-') { sign = -1; c = read(); } long val = 0; while (c > 32) { val = val * 10 + (c - '0'); c = read(); } return val * sign; } int nextInt() throws IOException { return (int) nextLong(); } } // Binary searches on sorted unique array static int lowerBound(long[] arr, long x) { int l = 0, r = arr.length; while (l < r) { int m = (l + r) >>> 1; if (arr[m] < x) l = m + 1; else r = m; } return l; } static int upperBound(long[] arr, long x) { int l = 0, r = arr.length; while (l < r) { int m = (l + r) >>> 1; if (arr[m] <= x) l = m + 1; else r = m; } return l; // number of elements <= x } public static void main(String[] args) throws Exception { FastScanner fs = new FastScanner(System.in); int n = fs.nextInt(); long c = fs.nextLong(); long d = fs.nextLong(); long[] a = new long[n]; long[] b = new long[n]; for (int i = 0; i < n; i++) a[i] = fs.nextLong(); for (int i = 0; i < n; i++) b[i] = fs.nextLong(); long k = d - c; long[] x = new long[n]; for (int i = 0; i < n; i++) x[i] = a[i] - b[i]; // Coordinate compress the x values (the only values we insert in Fenwick) long[] xs = x.clone(); Arrays.sort(xs); int m = 0; for (int i = 0; i < xs.length; i++) { if (i == 0 || xs[i] != xs[i - 1]) xs[m++] = xs[i]; } long[] uniq = Arrays.copyOf(xs, m); Fenwick fw = new Fenwick(uniq.length); long ans = 0; // Process j from left to right (respecting i < j) for (int j = 0; j < n; j++) { long threshold = x[j] + k; int r = upperBound(uniq, threshold); // number of unique xi <= threshold if (r > 0) ans += fw.sum(r); // count previous xi satisfying xi <= threshold int pos = lowerBound(uniq, x[j]) + 1; // 1-indexed for Fenwick fw.add(pos, 1); } System.out.println(ans); } }

Input format (expected)

  • First line: n c d
  • Second line: a1 a2 ... an
  • Third line: b1 b2 ... bn

The program prints the number of valid pairs (i, j) with 1 ≤ i < j ≤ n satisfying the inequality. Uses long everywhere to be safe for large values and returns in O(n log n) time.

Share this Q&A