DDSA Solutions

Two Pairs with Same Sum

Problem Overview

The task is four distinct indices forming two pairs with equal sums, and among all answers the lexicographically smallest index quadruplet wins.

Intuition

The task is four distinct indices forming two pairs with equal sums, and among all answers the lexicographically smallest index quadruplet wins. Enumerating pairs in order of (i, j) visits them from earliest to latest. Store the first pair for each sum. Any later pair with that sum is already later in lexicographic pair order, so if the four indices are distinct the candidate (first, current) is a valid ordered answer. Scanning every such candidate and keeping the minimum quadruplet yields the global lex best in quadratic time.

Algorithm

  1. 1Create a map from pair sum to the first index pair that produced it.
  2. 2For every i < j, compute sum = arr[i] + arr[j].
  3. 3If the sum is new, store (i, j) and continue.
  4. 4Otherwise let (a, b) be the stored pair. Skip when any index collides with i or j.
  5. 5Form the candidate [a, b, i, j] and keep it when it is lexicographically smaller than the best so far.
  6. 6Return the best candidate, or four minus ones when none exists.

Example Walkthrough

Input: arr = [3, 4, 7, 1, 2, 9, 8]

  1. 1. Pairs that sum to 10 include (0, 2) for 3 + 7 and (3, 5) for 1 + 9.
  2. 2. Those four indices are distinct, so [0, 2, 3, 5] is a valid candidate.
  3. 3. No lexicographically smaller valid quadruplet exists, so that is the answer.

Output: [0, 2, 3, 5]

Common Pitfalls

  • • Keep the lexicographically smallest result. Returning the first collision found can miss an earlier quadruplet from another sum.
  • • Require four distinct indices. Two pairs that share a vertex are invalid even when the sums match.
  • • Do not require b < c. Only the pair order (a, b) before (c, d) matters, and the scan already enforces that.
  • • Building every pair list and rescanning them can reach O(n^4). One stored pair per sum is enough for the expected method.
Two Pairs with Same Sum.java
Java
// Approach: Enumerate pairs (i, j) in lexicographic order and map each sum
// to the first pair that produced it. A later pair with the same sum and
// four distinct indices forms a candidate (first, current); keep the
// lexicographically smallest such quadruplet. Pair order already guarantees
// the stored pair is the earlier one.
// Complexity: O(n^2) time, O(n^2) extra space.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

class Solution {
    public ArrayList<Integer> findPairs(int[] arr) {
        int n = arr.length;
        HashMap<Integer, int[]> first = new HashMap<>();
        ArrayList<Integer> best = null;

        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                int sum = arr[i] + arr[j];
                int[] prev = first.get(sum);

                if (prev == null) {
                    first.put(sum, new int[] { i, j });
                    continue;
                }

                int a = prev[0], b = prev[1];
                if (a == i || a == j || b == i || b == j)
                    continue;

                ArrayList<Integer> cur = new ArrayList<>(Arrays.asList(a, b, i, j));
                if (best == null || less(cur, best))
                    best = cur;
            }
        }

        return best != null ? best
                : new ArrayList<>(Arrays.asList(-1, -1, -1, -1));
    }

    private boolean less(ArrayList<Integer> a, ArrayList<Integer> b) {
        for (int i = 0; i < 4; i++) {
            int x = a.get(i), y = b.get(i);
            if (x != y)
                return x < y;
        }
        return false;
    }
}
Was this solution helpful?