Skip to content

2561. Rearranging Fruits #2001

Answered by mah-shamim
mah-shamim asked this question in Q&A
Aug 2, 2025 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

We need to determine the minimum cost required to make two fruit baskets equal by swapping fruits between them. The cost of each swap is defined as the minimum of the costs of the two fruits being swapped. If it's impossible to make the baskets equal, we should return -1.

Approach

  1. Frequency Analysis:

    • First, we calculate the total frequency of each fruit across both baskets. If any fruit has an odd total frequency, it's impossible to split them equally between the two baskets, so we return -1 immediately.
  2. Global Minimum Fruit Cost:

    • Identify the global minimum fruit cost (m) from both baskets. This value is crucial as it can be used to minimize the swap cost by acting as an intermedi…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Aug 2, 2025
Maintainer Author

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Aug 2, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested hard Difficulty
2 participants