## DEV Community # 2244. Minimum Rounds to Complete All Tasks

You are given a 0-indexed integer array tasks, where tasks[i] represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the same difficulty level.

Return the minimum rounds required to complete all the tasks, or -1 if it is not possible to complete all the tasks.

Example 1:

Output: 4
Explanation: To complete all the tasks, a possible plan is:

• In the first round, you complete 3 tasks of difficulty level 2.
• In the second round, you complete 2 tasks of difficulty level 3.
• In the third round, you complete 3 tasks of difficulty level 4.
• In the fourth round, you complete 2 tasks of difficulty level 4. It can be shown that all the tasks cannot be completed in fewer than 4 rounds, so the answer is 4.

Example 2:

Output: -1
Explanation: There is only 1 task of difficulty level 2, but in each round, you can only complete either 2 or 3 tasks of the same difficulty level. Hence, you cannot complete all the tasks, and the answer is -1.

Constraints:

``````public class Solution {

HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();// Creating HashMap
int a = 0;
int c = 0;
for (int i = 0; i < tasks.length; i++)// Traversing array
{
// Checking if the element is present
a = 0;
} else // If element is not present then add it to the HashMap
{
}
}
// Iterating over the HashMap to count the minimum number of rounds or return -1
// if it is not possible to complete all the tasks.
for (int z : hm.keySet()) {
int d = hm.get(z);
if (d == 1) {
return -1;
} else if (d == 2) {
c += 1;
} else if (d % 3 == 0) {
c += d / 3;
} else {
c += d / 3 + 1;
}
}
return c;// returning the minimum number of rounds.
// Time Complexity: O(n)
// Space Complexity: O(n)
// End of the program.
}

}

``````