Minimum Operations to Make Elements Within K Subarrays Equal - Problem

You're given an integer array nums and two integers x and k. Your mission is to transform this array into one that contains at least k non-overlapping subarrays of size exactly x, where all elements within each subarray are equal.

You can perform the following operation any number of times (including zero):

  • Increase or decrease any element of nums by 1

For example, if nums = [1, 3, 2, 4, 5], x = 2, and k = 2, you need to create at least 2 non-overlapping subarrays of size 2 where all elements in each subarray are equal. One possible solution is [2, 2, 2, 2, 5] with subarrays [2,2] and [2,2], requiring 3 operations total.

Return the minimum number of operations needed to achieve this goal.

Input & Output

example_1.py — Basic Case
$ Input: nums = [1, 3, 2, 4, 5], x = 2, k = 2
Output: 3
💡 Note: We need 2 non-overlapping subarrays of size 2. We can use [1,3] and [4,5]. To make [1,3] equal, we change to [2,2] (cost=2). To make [4,5] equal, we change to [4,4] (cost=1). Total cost = 3.
example_2.py — Impossible Case
$ Input: nums = [1, 2], x = 3, k = 1
Output: -1
💡 Note: We need 1 subarray of size 3, but the array only has 2 elements. This is impossible, so return -1.
example_3.py — Optimal Placement
$ Input: nums = [1, 1, 2, 2, 3, 3], x = 2, k = 3
Output: 0
💡 Note: We can use [1,1], [2,2], and [3,3] as our 3 subarrays. Each already has equal elements, so no operations needed. Total cost = 0.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 105
  • 1 ≤ x ≤ nums.length
  • 1 ≤ k ≤ nums.length / x
  • All subarrays must be non-overlapping and of size exactly x

Visualization

Tap to expand
Minimum Operations for K Equal Subarrays INPUT Array nums: 1 i=0 3 i=1 2 i=2 4 i=3 5 i=4 Parameters x = 2 (subarray size) k = 2 (num subarrays) Goal: Find 2 non-overlapping subarrays of size 2 with equal elements each ALGORITHM STEPS (DP) 1 Compute Subarray Costs cost[i] = ops to make nums[i:i+x] all equal (use median) Subarray Costs: [1,3]-->1 [3,2]-->1 [2,4]-->2 [4,5]-->1 2 Define DP State dp[i][j] = min cost using j subarrays ending at/before i 3 DP Transition dp[i][j] = min(dp[i-1][j], dp[i-x][j-1] + cost[i-x+1]) 4 Find Optimal Selection Select [1,3] and [4,5] Cost: 1+1 = 2? No overlap! Optimal: [1,3]-->[2,2] + [4,5]-->[4,4] or [3,2]-->[2,2] + [4,5]-->[5,5] FINAL RESULT Original Array: 1 3 2 4 5 Subarray 1 Subarray 2 After Operations: 2 2 2 5 5 Operations Breakdown: 1 --> 2: +1 op 3 --> 2: +1 op 4 --> 5: +1 op Total: 3 OUTPUT 3 [OK] Minimum operations Key Insight: Use Dynamic Programming to track minimum operations needed for selecting j non-overlapping subarrays up to position i. For each subarray, the optimal target value is the MEDIAN of its elements, which minimizes total operations. Time complexity: O(n * k) with preprocessing for subarray costs. TutorialsPoint - Minimum Operations to Make Elements Within K Subarrays Equal | DP Approach
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 24
54.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen