You are given an array nums
consisting of positive integers.
You are also given an integer array queries
of size m
. For the ith
query, you want to make all of the elements of nums
equal to queries[i]
. You can perform the following operation on the array any number of times:
1
.Return an array answer
of size m
where answer[i]
is the minimum number of operations to make all elements of nums
equal to queries[i]
.
Note that after each query the array is reset to its original state.
Example 1:
Input: nums = [3,1,6,8], queries = [1,5] Output: [14,10] Explanation: For the first query we can do the following operations: - Decrease nums[0] 2 times, so that nums = [1,1,6,8]. - Decrease nums[2] 5 times, so that nums = [1,1,1,8]. - Decrease nums[3] 7 times, so that nums = [1,1,1,1]. So the total number of operations for the first query is 2 + 5 + 7 = 14. For the second query we can do the following operations: - Increase nums[0] 2 times, so that nums = [5,1,6,8]. - Increase nums[1] 4 times, so that nums = [5,5,6,8]. - Decrease nums[2] 1 time, so that nums = [5,5,5,8]. - Decrease nums[3] 3 times, so that nums = [5,5,5,5]. So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10.
Example 2:
Input: nums = [2,9,6,3], queries = [10] Output: [20] Explanation: We can increase each value in the array to 10. The total number of operations will be 8 + 1 + 4 + 7 = 20.
Constraints:
n == nums.length
m == queries.length
1 <= n, m <= 105
1 <= nums[i], queries[i] <= 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute-force method solves this by testing every possible number to see if it's the optimal target value. For each potential target value, we calculate the number of changes needed to make all other numbers equal to it. Finally, we select the target number requiring the fewest changes.
Here's how the algorithm would work step-by-step:
def min_operations_brute_force(numbers):
# Find potential targets by only using distinct array elements
unique_numbers = set(numbers)
minimum_operations = float('inf')
for potential_target in unique_numbers:
operations_count = 0
# Count operations required for current target.
for number in numbers:
if number != potential_target:
operations_count += 1
# Determine if we have a new minimum
minimum_operations = min(minimum_operations, operations_count)
return minimum_operations
The most efficient way to solve this is to realize that all numbers must become equal to one particular value that already exists in the original set. We want to quickly figure out which of the existing values is the best 'target' and how many changes are needed to reach it.
Here's how the algorithm would work step-by-step:
def minimum_operations_to_equal(numbers):
frequency_of_numbers = {}
for number in numbers:
if number in frequency_of_numbers:
frequency_of_numbers[number] += 1
else:
frequency_of_numbers[number] = 1
# We need to find the most frequent number.
most_frequent_number_count = 0
for number in frequency_of_numbers:
if frequency_of_numbers[number] > most_frequent_number_count:
most_frequent_number_count = frequency_of_numbers[number]
# The result is the total count minus the highest frequency.
# This gives us the minimum operations needed.
return len(numbers) - most_frequent_number_count
Case | How to Handle |
---|---|
Null input array | Throw an IllegalArgumentException or return an empty list indicating invalid input. |
Empty input array | Return 0 operations, since all elements are already 'equal' in an empty array. |
Array with a single element | Return 0 operations because a single element array is already equal. |
All elements are already equal | Return 0 operations, as no changes are needed. |
Array contains negative numbers | The core logic should handle negative numbers correctly, so no specific handling is required if the 'equal' condition still applies after the transformation. |
Array contains zero | The algorithm should handle zero values correctly without special treatment. |
Integer overflow during intermediate calculations (e.g., sum or differences) | Use long data types for intermediate calculations to prevent integer overflow issues, or choose an algorithm that avoids large intermediate values. |
Large array size leading to potential memory issues | Ensure the algorithm's space complexity is reasonable (e.g., O(1) or O(log n)), or consider using an algorithm that processes the array in chunks if memory is a constraint. |