Skip to the code
cf.
+25 XP

#118 Count Triples Under a Cap

medium

A server can run three background jobs at once only if their combined memory use is strictly below cap. The memory use of each job is in loads.

Write countTriples(loads, cap) that returns how many groups of three different jobs (three different indexes i < j < k) have loads[i] + loads[j] + loads[k] < cap.

Example 1

Input: loads = [-2,0,1,3], cap = 2
Output: 2

-2 + 0 + 1 = -1 and -2 + 0 + 3 = 1 are below 2. The other two triples total 2 and 4.

Example 2

Input: loads = [5,5,5], cap = 15
Output: 0

The only triple totals exactly 15, which is not strictly below.

Constraints

  • 0 <= loads.length <= 2000
  • -100 <= loads[i] <= 100
  • -300 <= cap <= 300
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).