Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Practice › Arrays

Count Triplets with Sum Smaller than X

Medium · Arrays · Asked at Amazon, Google, Microsoft

Count the triplets of positions i < j < k with a[i] + a[j] + a[k] strictly less than X.

Input

First line: n X. Second line: n integers.

Output

The number of triplets.

Constraints

The answer does not depend on the order, so sort. Fix the smallest element i; with lo = i + 1 and hi = n − 1, if a[i] + a[lo] + a[hi] < X then every k in (lo, hi] works with lo — add hi − lo and move lo up; otherwise move hi down. O(n²) overall; the count can pass 2^31.

Example 1

Input

4 2
-2 0 1 3

Output

2

Example 2

Input

5 12
5 1 3 4 7

Output

4

Hints

  1. The count does not depend on the order of the elements — sort first.
  2. Fix the first element; for the other two use two pointers.
  3. If a[i] + a[lo] + a[hi] < X, then every index between lo and hi also works with lo.

Approach

After sorting, for each i let lo = i + 1, hi = n − 1. If the sum is below X, all pairs (lo, k) with lo < k ≤ hi are valid — add hi − lo and move lo up. Otherwise the largest element is too big, so move hi down. This counts every triplet exactly once in O(n²). Up to about 1.3 × 10^9 triplets exist for n = 2000, so use a 64-bit counter.

Complexity: O(n²) time · O(1) extra space

Solution

C++ solution

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    long long X;
    cin >> n >> X;
    vector<long long> a(n);
    for (auto &x : a) cin >> x;
    sort(a.begin(), a.end());
    long long count = 0;
    for (int i = 0; i + 2 < n; i++) {
        int lo = i + 1, hi = n - 1;
        while (lo < hi) {
            if (a[i] + a[lo] + a[hi] < X) {
                count += hi - lo;
                lo++;
            } else hi--;
        }
    }
    cout << count << "\n";
    return 0;
}

Python solution

import sys

data = list(map(int, sys.stdin.read().split()))
n, X = data[0], data[1]
a = sorted(data[2 : 2 + n])
count = 0
for i in range(n - 2):
    lo, hi = i + 1, n - 1
    need = X - a[i]
    while lo < hi:
        if a[lo] + a[hi] < need:
            count += hi - lo
            lo += 1
        else:
            hi -= 1
print(count)

Java solution

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int n = (int) in.nval;
        in.nextToken();
        long X = (long) in.nval;
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            in.nextToken();
            a[i] = (long) in.nval;
        }
        Arrays.sort(a);
        long count = 0;
        for (int i = 0; i + 2 < n; i++) {
            int lo = i + 1, hi = n - 1;
            while (lo < hi) {
                if (a[i] + a[lo] + a[hi] < X) {
                    count += hi - lo;
                    lo++;
                } else hi--;
            }
        }
        System.out.println(count);
    }
}

Solve Count Triplets with Sum Smaller than X online · All practice problems