FastPrepMaximum Easy Tasks After Rearranging

Maximum Easy Tasks After Rearranging

IBM logoIBM● MediumINTERNOA
Learn

Problem statement

You are given an integer array taskDifficulty. You may rearrange its elements in any order.

A task is easy when both conditions hold:

  • It has an immediate neighbor on its left and on its right, so the first and last positions cannot be easy.
  • Its difficulty is strictly less than the difficulties of both immediate neighbors.

Return the maximum possible number of easy tasks after rearranging the array.

Function

getMaximumTasks(taskDifficulty: int[]) → int

Examples

Example 1

taskDifficulty = [4,5,6,1,2]return = 2

One optimal order is [4,1,5,2,6]. The values 1 and 2 are each strictly smaller than both immediate neighbors, so two tasks are easy.

Constraints

  • 1 <= taskDifficulty.length <= 2 * 10^5.
  • 1 <= taskDifficulty[i] <= 10^9.

More IBM problems

See IBM hiring insights
public int getMaximumTasks(int[] taskDifficulty) {
    // Write your code here
}
taskDifficulty[4,5,6,1,2]
expected2
Checking account…