VOOZH about

URL: https://www.geeksforgeeks.org/dsa/cses-solutions-sliding-window-cost/

⇱ CSES Solutions - Sliding Window Cost - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CSES Solutions - Sliding Window Cost

Last Updated : 16 Mar, 2024

You are given an array arr[] of N integers. Your task is to calculate for each window of K elements, from left to right, the minimum total cost of making all elements equal. You can increase or decrease each element with cost X, where X is the difference between the new and the original value. The total cost is the sum of such costs.

Examples:

Input: N = 4, K = 3, arr[] = {8, 4, 2, 4}
Output: 6 2 
Explanation:

  • The first window: {8, 4, 2}, make all the elements equal to 4, hence the total cost will be abs(8 - 4) + abs(4 - 4) + abs(2 - 4) = 6.
  • The second window: {4, 2, 4}, make all the elements equal to 4, hence the total cost will be abs(4 - 4) + abs(2 - 4) + abs(4 - 4) = 2.

Input: N = 8, K = 3, arr[] = {2, 4, 3, 5, 8, 1, 2, 1}
Output: 2 2 5 7 7 1
Explanation:

  • The first window: {2, 4, 3}, make all the elements equal to 3, hence the total cost will be abs(2 - 3) + abs(4 - 3) + abs(3 - 3) = 0.
  • The second window: {4, 3, 5}, make all the elements equal to 4, hence the total cost will be abs(4 - 4) + abs(3 - 4) + abs(5 - 4) = 2.
  • The third window: {3, 5, 8}, make all the elements equal to 5, hence the total cost will be abs(3 - 5) + abs(5 - 5) + abs(8 - 5) = 5.
  • The fourth window: {5, 8, 1}, make all the elements equal to 5, hence the total cost will be abs(5 - 5) + abs(8 - 5) + abs(1 - 5) = 7.
  • The fifth window: {8, 1, 2}, make all the elements equal to 2, hence the total cost will be abs(8 - 2) + abs(1 - 2) + abs(2 - 2) = 7.
  • The sixth window: {1, 2, 1}, make all the elements equal to 1, hence the total cost will be abs(1 - 1) + abs(2 - 1) + abs(1 - 1) = 1.

Approach: To solve the problem, follow the below idea:

The idea is to make all the elements equal to the median of the K size window. For determining the median of each window of size K, we will use two multisets. For a k size window, the first multiset will contain ceil(K/2) smallest elements and second multiset will contain K/2 largest elements of the window. By this way the median of the window will be equal to the largest element of the first multiset.

Now the cost to make all the elements equal will be:

= Cost of making elements of first multiset equal to Median + Cost of making elements of second multiset equal to Median

= , for each i from 1 to ceil(k/2) and for each j from 1 to k/2, where Median is the Median of the current window, lwr and upr contains the elements of first and second multisets respectively.

The equation can be further simplified, making the total cost equal to

= (size of first multiset) * Median - (Sum of elements of first multiset) +

(Sum of elements of second multiset) - (size of second multiset) * Median

Follow the steps to solve the problem:

  • Use two multisets (lwr and upr) to maintain the smallest and largest elements of the current window. Initialize variables sum1 and sum2 to keep track of the sum of elements in each multiset.
  • Iterate through the array using a loop.
    • For each ith element, add it to the appropriate multiset and update the corresponding sum.
    • To ensure that lwr multiset contains the smallest elements and upr multiset contains the largest elements, we do the following:
      • Find the maximum from the multiset (lwr) and the minimum from the multiset (upr).
      • If the maximum element from lwr is greater than the minimum element from upr, swap them.
    • Calculate the cost using the given formula:
      ((size of lwr) * Median - (sum1)) + ((sum2) - (size of upr) * Median)
    • Output the calculated cost for each window.

Below is the implementation of above approach:


Output
2 2 5 7 7 1 

Time Complexity: O(N*logK), where N is the size of array arr[] and K is the size of window.
Auxiliary Space: O(K)

Comment
Article Tags: