Last Updated: 22 Aug, 2025

Uniform K-Subarray Sums

Moderate
Asked in company
Amdocs

Problem statement

You are given an array 'arr' of N positive integers and an integer 'K'.

Your task is to find the minimum number of array elements you need to replace to make the sum of every contiguous subarray of length 'K' equal. You can replace an element with any integer.


Input Format:
The first line of input contains two space-separated integers, 'N' and 'K'.

The second line contains 'N' space-separated integers, representing the elements of the array 'arr'.


Output Format:
Print a single integer representing the minimum number of replacements required.


Note:
The condition that all K-length subarray sums are equal implies a periodic property. If the sum of {a_i, ..., a_{i+K-1}} must equal the sum of {a_{i+1}, ..., a_{i+K}}, it forces a_i to be equal to a_{i+K}. This means all elements at indices j, j+K, j+2K, ... must be the same for the sums to be equal with minimal changes.