The Minimum Total Cost to Make Arrays Unequal problem gives you two arrays of equal length. You can swap elements within arr1 (at a cost equal to the element's index). The goal is to make arr1 and arr2 have no index where arr1[i] == arr2[i]. Find the minimum total cost of swaps needed. This hard coding problem combines greedy thinking with frequency counting and the majority element concept.
Flipkart and Razorpay ask this hard problem because it requires the insight that you need to gather all "problem" elements (where arr1[i] == arr2[i]) and redistribute them, paying attention to whether one value appears so many times that it must be spread to more positions. The array, hash table, counting, and greedy interview pattern is central, and the majority element detection within the swapping pool drives the answer.
Greedy with majority element detection. First collect all indices where arr1[i] == arr2[i] — these must be swapped. Sum their costs (indices). The elements at these positions form a pool. If any one value appears more than half the total swapped elements, it can't be redistributed without dragging in more indices from outside the problem set — include additional indices (cheapest first) until no majority element exists. The total cost is the sum of all included indices.
arr1 = [2,2,2,1,3], arr2 = [1,2,2,3,3].
Hard greedy problems with "majority element" constraints require recognizing when one value's frequency exceeds half the total pool size. The Boyer-Moore majority vote algorithm is useful here. Practice majority element detection in dynamic pools — scenarios where elements are added incrementally and you need to track whether any element dominates. This greedy-plus-majority pattern appears in problems involving frequency balancing, equalization, and redistribution.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Equal Sum Arrays With Minimum Number of Operations | Medium | Solve | |
| Minimum Number of Operations to Make Array Empty | Medium | Solve | |
| Minimum Operations to Make the Array Alternating | Medium | Solve | |
| Minimum Rounds to Complete All Tasks | Medium | Solve | |
| Largest Values From Labels | Medium | Solve |