Menu

3505. Minimum Operations to Make Elements Within K Subarrays Equal
📰

3505. Minimum Operations to Make Elements Within K Subarrays Equal

DEV Community·MD ARIFUL HAQUE·6 days ago
#RESR6GDz
#dev#nums#output#median#example
Reading 0:00
15s threshold

3505. Minimum Operations to Make Elements Within K Subarrays Equal

Difficulty: Hard

Topics: Principal , Array , Hash Table , Math , Dynamic Programming , Sliding Window , Heap (Priority Queue) , Weekly Contest 443

You are given an integer array nums and two integers, x and k . You can perform the following operation any number of times ( including zero ):

Increase or decrease any element of nums by 1.

Return the minimum number of operations needed to have at least k non-overlapping subarrays 1 of size exactly x in nums , where all elements within each subarray are equal.

Example 1:

Input: nums = [5,-2,1,3,7,3,6,4,-1], x = 3, k = 2

Output: 8

Explanation:

Use 3 operations to add 3 to nums[1] and use 2 operations to subtract 2 from nums[3] . The resulting array is [5, 1, 1, 1, 7, 3, 6, 4, -1] .

Use 1 operation to add 1 to nums[5] and use 2 operations to subtract 2 from nums[6] .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.