Problem · Array

Productive Worker Teams

Learn this problem
MediumAmazonNEW GRADOA
See Amazon hiring insights

Problem statement

The manager of an Amazon warehouse wants to create a productive team of four workers for their latest order.

There are n workers, where the level of the jth worker is represented by the array level[j], and the level of each worker is a unique integer from 1 to n. The higher the level of a worker, the more skillful and experienced the worker is.

A team of four workers [x, y, z, w], where 1 <= x < y < z < w <= n, is called productive if it satisfies the following conditions:

  • level[x] < level[z]
  • level[y] > level[w]
  • The order of workers in the team must follow their order in the level array, i.e. the team must be formed by choosing four distinct workers with increasing indices.

The manager wants to know the number of choices for creating a productive team. Given n workers and an array level, find the number of choices for creating a productive team.

Note:

  • The array level is a permutation. A permutation is one in which each element from 1 to n appears exactly once.
  • Two tuples [x1, y1, z1, w1] and [x2, y2, z2, w2] are considered to be different if x1 != x2 or y1 != y2 or z1 != z2 or w1 != w2.

Function

countProductiveTeams(n: int, level: int[]) → long

Examples

Example 1

n = 5level = [11, 14, 12, 13, 15]return = 1

The productive team is formed by worker positions [1, 2, 3, 4]. It satisfies level[1] < level[3] because 11 < 12, and level[2] > level[4] because 14 > 13.

Source note (June 29, 2026): The original source image did not include the output for this example. FastPrep copied the example input exactly from the source image and worked out the output from the stated tuple conditions. If you have the original output or notice something wrong, please let us know and we'll fix it. If we find a fuller source later, we'll come back and update this too. 🐣

More Amazon problems

drafts saved locally
public long countProductiveTeams(int n, int[] level) {
  // write your code here
}
n5
level[11, 14, 12, 13, 15]
expected1
checking account