Minimum replacements required to make sum of all K-length subarrays equal
Given an array arr[] consisting of N positive integers and an integer K, the task is to make the sum of all K-length subarrays equal by replacing minimum number of array elements with any integer.
Examples:
Input: arr[] = {3, 4, 3, 5, 6}, K = 2
Output: 2
Explanation:
Operation 1: Replacing arr[3] by 4 modifies arr[] to {3, 4, 3, 4, 6}.
Operation 2: Replacing arr[4] by 3 modifies arr[] to {3, 4, 3, 4, 3}.
All subarrays of length 2 are {{3, 4}, {4, 3}, {3, 4}, {4, 3}}. Sum of all these subarrays is 7. Therefore, the minimum number of operations required is 2.
Input: arr[] = {1, 2, 3, 1, 2}, K = 3
Output: 0
Explanation: All subarrays of length 3 are {{1, 2, 3}, {2, 3, 1}, {3, 1, 2}}. Since all these subarrays have sum 6, the number of operations required is 0.
Approach: The idea is based on the observation that all subarrays will have equal sum, when all elements separated by distance K are equal.
Therefore, the problem can be solved by counting the frequency of elements separated by a distance K and find the number which appears maximum times. Follow the steps below to solve the problem:
- Initialize a variable ans to store the required result.
- Iterate in the range [0, K-1] using the variable i
- Create a map, freq to store the frequency of elements separated by a distance K starting from i.
- Traverse the map and find the element which occurs the maximum number of times.
- Again, traverse the map and if the element is not equal to the maximum occurring element then add its frequency to the ans.
- Print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMinOperations( int arr[],
int N, int K)
{
int operations = 0;
for ( int i = 0; i < K; i++) {
unordered_map< int , int > freq;
for ( int j = i; j < N; j += K)
freq[arr[j]]++;
int max1 = 0, num;
for ( auto x : freq) {
if (x.second > max1) {
max1 = x.second;
num = x.first;
}
}
for ( auto x : freq) {
if (x.first != num)
operations += x.second;
}
}
cout << operations;
}
int main()
{
int arr[] = { 3, 4, 3, 5, 6 };
int K = 2;
int N = sizeof (arr) / sizeof (arr[0]);
findMinOperations(arr, N, K);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG
{
static void findMinOperations( int arr[],
int N, int K)
{
int operations = 0 ;
for ( int i = 0 ; i < K; i++) {
Map<Integer, Integer> freq= new HashMap<>();
for ( int j = i; j < N; j += K)
freq.put(arr[j], freq.getOrDefault(arr[j], 0 )+ 1 );
int max1 = 0 , num=- 1 ;
for (Map.Entry<Integer,Integer> x : freq.entrySet()) {
if (x.getValue() > max1) {
max1 = x.getValue();
num = x.getKey();
}
}
for ( Map.Entry<Integer,Integer> x : freq.entrySet()) {
if (x.getKey() != num)
operations += x.getValue();
}
}
System.out.print(operations);
}
public static void main(String[] args)
{
int arr[] = { 3 , 4 , 3 , 5 , 6 };
int K = 2 ;
int N = arr.length;
findMinOperations(arr, N, K);
}
}
|
Python3
def findMinOperations(arr, N, K):
operations = 0
for i in range (K):
freq = {}
for j in range (i,N,K):
if arr[j] in freq:
freq[arr[j]] + = 1
else :
freq[arr[j]] = 1
max1 = 0
num = 0
for key,value in freq.items():
if (value > max1):
max1 = value
num = key
for key,value in freq.items():
if (key ! = num):
operations + = value
print (operations)
if __name__ = = '__main__' :
arr = [ 3 , 4 , 3 , 5 , 6 ]
K = 2
N = len (arr)
findMinOperations(arr, N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findMinOperations( int []arr,
int N, int K)
{
int operations = -1;
for ( int i = 0; i < K; i++)
{
Dictionary< int ,
int > freq = new Dictionary< int ,
int >();
for ( int j = i; j < N; j += K)
{
if (freq.ContainsKey(arr[j]))
freq[arr[j]]++;
else
freq.Add(arr[j], 1);
}
int max1 = -1, num = 0;
foreach (KeyValuePair< int , int > entry in freq)
{
if (entry.Key > max1)
{
max1 = entry.Value;
num = entry.Key;
}
}
foreach (KeyValuePair< int , int > entry in freq)
{
if (entry.Key != num)
operations += entry.Value;
}
}
Console.Write(operations);
}
public static void Main()
{
int []arr = { 3, 4, 3, 5, 6 };
int K = 2;
int N = arr.Length;
findMinOperations(arr, N, K);
}
}
|
Javascript
<script>
function findMinOperations(arr, N, K)
{
var operations = 0;
var i,j;
for (i = 0; i < K; i++) {
var freq = new Map();
for (j = i; j < N; j += K){
if (freq.has(arr[j]))
freq.set(arr[j], freq.get(arr[j])+1);
else
freq.set(arr[j],1);
}
var max1 = 0, num;
for (const [key, value] of freq.entries()) {
if (value > max1) {
max1 = value;
num = key;
}
}
for (const [key, value] of freq.entries()) {
if (key != num)
operations += value;
}
}
document.write(operations);
}
var arr = [3, 4, 3, 5, 6];
var K = 2;
var N = arr.length;
findMinOperations(arr, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
13 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...