Minimize increment/decrement of Array elements to make each modulo K equal
Last Updated :
15 Jan, 2023
Given an array arr[] of length N and an integer K. In each operation any element(say arr[i]) can be selected from the array and can be changed to arr[i] + 1 or arr[i] – 1. The task is to find the minimum number of operations required to perform on the array such that each value of the array modulo K remains the same.
Examples:
Input: arr[] = {4, 5, 8, 3, 12}, k =5
Output: 4
Explanation:
Operation 1: { 3, 5, 8, 3, 12 }, decrease 4 at index 0 by 1.
Operation 2: { 3, 4, 8, 3, 12 }, decrease 5 at index 1 by 1.
Operation 3: { 3, 3, 8, 3, 12 }, decrease 4 at index 1 by 1.
Operation 4: { 3, 3, 8, 3, 13 }, increase 12 at index 4 by 1.
The modulo of each number is equal to 3 and minimum steps required were 4.
Input: arr[] = {2, 35, 48, 23, 52}, k =3
Output: 2
Explanation:
Minimum number of steps required to make modulo of each number equal is 2.
Approach: The idea is to use Hashing that keeps the count of each modulo that has been obtained.
- Now iterate for each value of i, in range 0 <= i < k, and find the number of operation required to make the modulo of all numbers equal.
- If it is less than the value obtained than the currently stored value then update it.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Find_min(set< int >& diff_mod,
map< int , int > count_mod, int k)
{
int min_oprn = INT_MAX;
int oprn = 0;
for ( int x = 0; x < k; x++) {
oprn = 0;
for ( auto w : diff_mod) {
if (w != x) {
if (w == 0) {
oprn += min(x, k - x)
* count_mod[w];
}
else {
oprn += min(
abs (x - w),
k + x - w)
* count_mod[w];
}
}
}
if (oprn < min_oprn)
min_oprn = oprn;
}
return min_oprn;
}
int Cal_min( int arr[], int n, int k)
{
set< int > diff_mod;
map< int , int > count_mod;
for ( int i = 0; i < n; i++) {
diff_mod.insert(arr[i] % k);
count_mod[arr[i] % k]++;
}
return Find_min(diff_mod, count_mod, k);
}
int main()
{
int arr[] = { 2, 35, 48, 23, 52 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 3;
cout << Cal_min(arr, n, k);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int Find_min(HashSet<Integer> diff_mod,
HashMap<Integer,
Integer> count_mod,
int k)
{
int min_oprn = Integer.MAX_VALUE;
int oprn = 0 ;
for ( int x = 0 ; x < k; x++)
{
oprn = 0 ;
for ( int w : diff_mod)
{
if (w != x)
{
if (w == 0 )
{
oprn += Math.min(x, k - x) *
count_mod.get(w);
}
else
{
oprn += Math.min(Math.abs(x - w),
k + x - w) *
count_mod.get(w);
}
}
}
if (oprn < min_oprn)
min_oprn = oprn;
}
return min_oprn;
}
static int Cal_min( int arr[], int n, int k)
{
HashSet<Integer> diff_mod = new HashSet<>();
HashMap<Integer,
Integer> count_mod = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
diff_mod.add(arr[i] % k);
count_mod.put(arr[i] % k,
count_mod.getOrDefault(arr[i] % k, 0 ) + 1 );
}
return Find_min(diff_mod, count_mod, k);
}
public static void main(String[] args)
{
int arr[] = { 2 , 35 , 48 , 23 , 52 };
int n = arr.length;
int k = 3 ;
System.out.print(Cal_min(arr, n, k));
}
}
|
Python3
import sys
from collections import defaultdict
def Find_min(diff_mod,
count_mod, k):
min_oprn = sys.maxsize
oprn = 0
for x in range (k):
oprn = 0
for w in diff_mod:
if (w ! = x):
if (w = = 0 ):
oprn + = ( min (x, k - x) *
count_mod[w])
else :
oprn + = ( min ( abs (x - w),
k + x - w) *
count_mod[w])
if (oprn < min_oprn):
min_oprn = oprn
return min_oprn
def Cal_min(arr, n, k):
diff_mod = set ([])
count_mod = defaultdict ( int )
for i in range (n):
diff_mod.add(arr[i] % k)
count_mod[arr[i] % k] + = 1
return Find_min(diff_mod, count_mod, k)
if __name__ = = "__main__" :
arr = [ 2 , 35 , 48 , 23 , 52 ]
n = len (arr)
k = 3
print ( Cal_min(arr, n, k))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int Find_min(HashSet< int > diff_mod,
Dictionary< int ,
int > count_mod,
int k)
{
int min_oprn = int .MaxValue;
int oprn = 0;
for ( int x = 0; x < k; x++)
{
oprn = 0;
foreach ( int w in diff_mod)
{
if (w != x)
{
if (w == 0)
{
oprn += Math.Min(x, k - x) *
count_mod[w];
}
else
{
oprn += Math.Min(Math.Abs(x - w),
k + x - w) *
count_mod[w];
}
}
}
if (oprn < min_oprn)
min_oprn = oprn;
}
return min_oprn;
}
static int Cal_min( int []arr, int n, int k)
{
HashSet< int > diff_mod = new HashSet< int >();
Dictionary< int ,
int > count_mod = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
{
diff_mod.Add(arr[i] % k);
if (count_mod.ContainsKey((arr[i] % k)))
count_mod[arr[i] % k] = count_mod[(arr[i] % k)]+1;
else
count_mod.Add(arr[i] % k, 1);
}
return Find_min(diff_mod, count_mod, k);
}
public static void Main(String[] args)
{
int []arr = { 2, 35, 48, 23, 52 };
int n = arr.Length;
int k = 3;
Console.Write(Cal_min(arr, n, k));
}
}
|
Javascript
<script>
function Find_min(diff_mod, count_mod, k)
{
let min_oprn = Number.MAX_VALUE;
let oprn = 0;
for (let x = 0; x < k; x++)
{
oprn = 0;
for (let w of diff_mod.values())
{
if (w != x)
{
if (w == 0)
{
oprn += Math.min(x, k - x) *
count_mod.get(w);
}
else
{
oprn += Math.min(Math.abs(x - w),
k + x - w) *
count_mod.get(w);
}
}
}
if (oprn < min_oprn)
min_oprn = oprn;
}
return min_oprn;
}
function Cal_min(arr, n, k)
{
let diff_mod = new Set();
let count_mod = new Map();
for (let i = 0; i < n; i++)
{
diff_mod.add(arr[i] % k);
if (count_mod.has((arr[i] % k)))
count_mod.set(arr[i] % k,
count_mod.get(arr[i] % k) + 1);
else
count_mod.set(arr[i] % k, 1);
}
return Find_min(diff_mod, count_mod, k);
}
let arr = [ 2, 35, 48, 23, 52 ];
let n = arr.length;
let k = 3;
document.write(Cal_min(arr, n, k));
</script>
|
Time Complexity: O(N*K), where N is the length of the given array and K is the given value.
Auxiliary Space: O(N+K), where N is the length of the given array and K is the given value.
Share your thoughts in the comments
Please Login to comment...