Minimum replacements required to make sum of all K | 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 ; Given Input ; Function Call
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 ) ;