Problem · Array

Find Minimum Groups

Learn this problem
MediumAmazonOA
See Amazon hiring insights

Problem statement

A financial services company has requested AWS for a private deployment of its cloud network. Considering the sensitive nature of the company's business, AWS has also advised them to add a specific type of security system.

Overall, there are n servers in the network where the security needs of the i-th server are represented by security[i], where each element represents the grade of security needed for a server.

To ensure the highest possible protection, the AWS security team has recommended the following rule to be followed while designing the security system: all servers in a security group must have the same grade of security needs, and the number of servers in any two security groups should not differ by more than 1.

Given an integer array security, find the minimum number of security levels needed to ensure the protection of the network.

Function

findMinimumGroups(security: int[]) → int

Complete the function findMinimumGroups in the editor below.

findMinimumGroups has the following parameter:

  • int security[n]: an integer array denoting the security grade of the devices

Returns

int: an integer denoting the minimum number of groups required

Examples

Example 1

security = [2, 3, 3, 3, 2, 1]return = 4
Example 1 illustration

Consider n = 6 and security = [2, 3, 3, 3, 2, 1].

Then, the elements can be grouped as follows:

  • Group 1: 2 devices of vulnerability 2.
  • Group 2: 2 devices of vulnerability 3.
  • Group 3: 1 device of vulnerability 3.
  • Group 4: 1 device of vulnerability 1.

It requires 4 groups.

More Amazon problems

drafts saved locally
public int findMinimumGroups(int[] security) {
  // write your code here
}
security[2, 3, 3, 3, 2, 1]
expected4
checking account