Page Contents

**Count Maximum Teams Amazon OA Solution**

Amazon is hosting a team hackathon.

- Each team will have exactly teamSize developers.
- A developer’s skill level is denoted by skill[i].
- The difference between the maximum and minimum skill levels within a team cannot exceed a threshold, maxDiff.

Determine the maximum number of teams that can be formed from the contestants.

E**xample**

skill = [3, 4, 3, 1, 6, 5]

team5ize = 3

maxDiff = 2

At most, 2 teams can be formed: [3, 3, 1] and [4, 6, 5].

The difference between the maximum and minimum skill levels is 2 in each case,

which does not exceed the threshold value oft.

**Function Description**

Complete the function countMaximumTeams in the editor below.

countMaximumTeams has the following parameter(s):

- int skill[n]: the developers’ skill levels
- int teamSize: the number of developers to make up a team
- int maxDiff: the threshold value

**Returns:**

- int: the maximum number of teams that can be formed at one time

**SOLUTION**

**Program:** **Count Maximum Teams Amazon OA Solution** in C++

```
#include<bits/stdc++.h>
using namespace std;
int main() {
int n; cin>>n; vector<int> a(n);
int team, diff; cin>>team>>diff;
sort(a.begin(),a.end());
int i=0, j=team-1, cnt=0;
while(j<n){
if(a[j]-a[i]<=diff){
cnt++;
i=j+1;
j=j+team;
}
else{
i++;
j++;
}
}
cout<<cnt;
return 0;
}
```

**Program:****Count Maximum Teams Amazon OA Solution** in Java

```
static int countMaximumTeams(List<int> skill, int teamSize, int maxDiff)
{
skill.Sort();
int i = 0;
int j = teamSize - 1;
int count = 0;
while (j < skill.Count)
{
if (skill[j] - skill[i] <= maxDiff)
{
count++;
i = j + 1;
j = i + 2;
}
else
{
i++;
j++;
}
}
return count;
}
```

**Program:** **Count Maximum Teams Amazon OA Solution** in Python

```
def countMaximumTeams(skill, teamSize, maxDiff):
teams = 0
n = len(skill)
if n < teamSize:
return teams
skill.sort()
for i in range(n):
if i + teamSize - 1 < n:
diff = skill[i + teamSize - 1] - skill[i]
if diff <= maxDiff:
teams += 1
return teams
```

*Related: Amazon OA Online Assessment 2021 Questions and Answers*