Ctrl K

Three Addends and a Later Total

Easy
You are given an array of positive integers values. Count the index quadruples a < b < c < d such that values[a] + values[b] + values[c] == values[d]. In other words, three elements add up to some element that sits to the right of all three. Different sets of indices count as different quadruples even when their values are the same.
Example 1
values=[2, 4, 1, 7] → 1

2 + 4 + 1 = 7, so the index quadruple (0, 1, 2, 3) is the only one that works.

Example 2
values=[1, 2, 1, 4, 4] → 2

1 + 2 + 1 = 4, and either 4 can serve as the last element: (0, 1, 2, 3) and (0, 1, 2, 4). No other triple sums to 4.

Example 3
values=[9, 1, 1, 1] → 0

The total must come after the three addends, and 9 + 1 + 1 is not 1.

Constraints
  • 4 ≤ len(values) ≤ 50
  • 1 ≤ values[i] ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEven Numbers From Digit Cards EasyEvenly Spaced Triples EasySmallest Number From Two Digit Sets EasyEven Codes From Digit Cards EasyBalanced Cuts After One Replacement Hard
loading Python…
Input
values=[2, 4, 1, 7]
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 92 tests on the server, hidden ones included.