Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find array of length k from given array whose unfairness is minimum in python
Suppose we have an array A and another value k. We have to form an array of size k by taking elements from A and minimize the unfairness. Here the unfairness is calculated by this formula −
(??????? ?? ???) − (??????? ?? ???)
So, if the input is like A = [25, 120, 350, 150, 2500, 25, 35] and k = 3, then the output will be 10, because we can take elements [25, 25, 35] so max(arr) = 35 and min(arr) = 25. So their difference is 10.
Approach
The key insight is that to minimize unfairness, we need to select k consecutive elements from a sorted array. This ensures the difference between maximum and minimum is as small as possible.
To solve this, we will follow these steps −
- Sort the array A to group similar values together
- Use a sliding window of size k to find the minimum unfairness
- For each window, calculate the difference between last and first elements
- Return the minimum difference found
Example
Let us see the following implementation to get better understanding −
def solve(A, k):
# Sort the array to group similar values
A.sort()
n = len(A)
# Initialize minimum unfairness with maximum possible value
min_unfairness = A[n-1] - A[0]
# Use sliding window to find minimum unfairness
for i in range(n - k + 1):
# Calculate unfairness for current window
current_unfairness = A[i + k - 1] - A[i]
min_unfairness = min(min_unfairness, current_unfairness)
return min_unfairness
# Test the function
A = [25, 120, 350, 150, 2500, 25, 35]
k = 3
result = solve(A, k)
print(f"Array: {A}")
print(f"k: {k}")
print(f"Minimum unfairness: {result}")
Array: [25, 120, 350, 150, 2500, 25, 35] k: 3 Minimum unfairness: 10
How It Works
Let's trace through the example step by step ?
def solve_with_trace(A, k):
print(f"Original array: {A}")
A.sort()
print(f"Sorted array: {A}")
n = len(A)
min_unfairness = float('inf')
best_subarray = []
print(f"\nChecking all subarrays of length {k}:")
for i in range(n - k + 1):
subarray = A[i:i+k]
unfairness = A[i + k - 1] - A[i]
print(f"Subarray {subarray}: unfairness = {unfairness}")
if unfairness < min_unfairness:
min_unfairness = unfairness
best_subarray = subarray
print(f"\nBest subarray: {best_subarray}")
print(f"Minimum unfairness: {min_unfairness}")
return min_unfairness
# Test with trace
A = [25, 120, 350, 150, 2500, 25, 35]
k = 3
solve_with_trace(A, k)
Original array: [25, 120, 350, 150, 2500, 25, 35] Sorted array: [25, 25, 35, 120, 150, 350, 2500] Checking all subarrays of length 3: Subarray [25, 25, 35]: unfairness = 10 Subarray [25, 35, 120]: unfairness = 95 Subarray [35, 120, 150]: unfairness = 115 Subarray [120, 150, 350]: unfairness = 230 Subarray [150, 350, 2500]: unfairness = 2350 Best subarray: [25, 25, 35] Minimum unfairness: 10
Key Points
- Sorting is crucial: It ensures consecutive elements in the sliding window have minimal difference
- Sliding window approach: We check all possible subarrays of length k
- Time complexity: O(n log n) due to sorting, where n is the array length
- Space complexity: O(1) if we ignore the space used by sorting
Conclusion
To find the minimum unfairness subarray, sort the original array and use a sliding window approach to check all consecutive subarrays of length k. The minimum difference between the last and first elements of any window gives the optimal solution.
