Definitions
- Permutation:
levelcontainsndistinct integers (commonly the numbers1..nexactly once). - Team (size 4): choose indices
(x, y, z, w)such thatx < y < z < w(indices are 0-based). - Productive team condition:
level[x] < level[z]andlevel[y] > level[w].
Task
Return the total number of productive teams (x, y, z, w) that satisfy:
x < y < z < wlevel[x] < level[z]level[y] > level[w]
Input / Output
- Input: integer array
level(a permutation), lengthn. - Output: an integer = number of productive teams of size 4.
Examples
Example 1
level = [1, 4, 2, 3, 5]
Valid productive teams count = 1
Explanation (one productive team):
- Choose indices
(x, y, z, w) = (0, 1, 2, 3)→ values[level[0], level[1], level[2], level[3]] = [1, 4, 2, 3] level[x] < level[z]→1 < 2✅level[y] > level[w]→4 > 3✅
Example 2
level = [1, 5, 2, 4, 3]
Output: 3
Explanation (productive teams):
(0, 1, 2, 3)→ values[1, 5, 2, 4](0, 1, 2, 4)→ values[1, 5, 2, 3](0, 1, 3, 4)→ values[1, 5, 4, 3]
Example 3
level = [4, 3, 2, 1]
Output: 0
Explanation: the only possible quadruple is (0,1,2,3), but level[0] < level[2] is 4 < 2, which is false.
Constraints
4 ≤ n ≤ 3000levelis a permutation (all elements are distinct; commonly1..n)