Tap here to switch tabs
Problem
Submissions

Problem: Minimum Operations to Make Subarray Elements Equal

Minimum Operations to Make Subarray Elements Equal asks for the fewest increments or decrements in an integer array to make a contiguous subarray of length k equal.

Statement

You are given an integer array nums and an integer k.

In a single operation, you can choose any element in nums and increment or decrement its value by 11.

Return the minimum number of operations required to make all elements equal in at least one contiguous subarray of length k.

Constraints:

  • 2≤2 \leq nums.length ≤105\leq 10^5

  • −106≤-10^6 \leq nums[i] ≤106\leq 10^6

  • 2≤2 \leq k ≤\leq nums.length

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Operations to Make Subarray Elements Equal

Minimum Operations to Make Subarray Elements Equal asks for the fewest increments or decrements in an integer array to make a contiguous subarray of length k equal.

Statement

You are given an integer array nums and an integer k.

In a single operation, you can choose any element in nums and increment or decrement its value by 11.

Return the minimum number of operations required to make all elements equal in at least one contiguous subarray of length k.

Constraints:

  • 2≤2 \leq nums.length ≤105\leq 10^5

  • −106≤-10^6 \leq nums[i] ≤106\leq 10^6

  • 2≤2 \leq k ≤\leq nums.length