51. OA - Count Productive Teams

Count Productive Teams
You are given an integer array level of size n that is a permutation (i.e., all values are distinct, typically it contains 1..n). Count how many 4-person teams can be formed that satisfy a specific “productive” pattern.

Definitions

  • Permutation: level contains n distinct integers (commonly the numbers 1..n exactly once).
  • Team (size 4): choose indices (x, y, z, w) such that x < y < z < w (indices are 0-based).
  • Productive team condition: level[x] < level[z] and level[y] > level[w].

Task

Return the total number of productive teams (x, y, z, w) that satisfy:

  • x < y < z < w
  • level[x] < level[z]
  • level[y] > level[w]

Input / Output

  • Input: integer array level (a permutation), length n.
  • 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 ≤ 3000
  • level is a permutation (all elements are distinct; commonly 1..n)




Please use Laptop/Desktop or any other large screen to add/edit code.