Minimum replacements required to make sum of all K | C # program for the above approach ; Function to find minimum number of operations required to make sum of all subarrays of size K equal ; Stores number of operations ; Iterate in the range [ 0 , K - 1 ] ; Stores frequency of elements separated by distance K ; Stores maximum frequency and corresponding element ; Find max frequency element and its frequency ; Update the number of operations ; Print the result ; Driver Code ; Given Input ; Function Call
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 ) ; } }