Problem · Math

Let's Play Crush Fruit Together :)

Learn this problem
EasyAmazonFULLTIMEOA
See Amazon hiring insights

Problem statement

Amazon launched a game called Fruit Crush. You are given an array fruitsArray of length n, where each integer represents a type of fruit.

In one operation, choose any two fruits with different types and remove both of them from the array.

You may perform the operation any number of times while possible. Return the minimum possible number of fruits left after all operations.

Function

getMinimumFruits(fruitsArray: int[]) → int

Examples

Example 1

fruitsArray = [3, 3, 1, 1, 2]return = 1
Example 1 illustration
We can first crush fruit type 1 (e.g., banana) with fruit type 2 (e.g., pineapple). Next, we crush the remaining fruit type 1 (banana) with one fruit type 3 (orange). Now, only one fruit type 3 (orange) remains in the array. Since there are no distinct fruit types left to crush, the minimum number of remaining fruits is 1.

Example 2

fruitsArray = [1, 2, 5, 6]return = 0
We can first crush fruit type 1 with fruit type 2. Next, we can crush fruit type 5 with fruit type 6. Since all fruit types have been successfully removed through crushing, the final number of fruits left is 0.

Constraints

  • 1 <= n <= 105
  • 1 <= fruitsArray[i] <= 109

More Amazon problems

drafts saved locally
public int getMinimumFruits(int[] fruitsArray) {
  // write your code here
}
fruitsArray[3, 3, 1, 1, 2]
expected1
checking account