Minimum operations required to make all Array elements divisible by K
Last Updated :
23 May, 2022
Given an array a[], integer K and an integer X (which is initially initialized to 0). Our task is to find the minimum number of moves required to update the array such that each of its elements is divisible by K by performing the following operations:
- Choose one index i from 1 to N and increase ai by X and then increase X by 1. This operation cannot be applied more than once to each element of the array
- Only increase the value of X by 1.
Examples:
Input: K = 3, a = [1, 2, 2, 18]
Output: 5
Explanation:
Initially X = 0 hence update X to 1.
For X = 1 add X to the second element of array to make the array [1, 3, 2, 18] and increase X by 1.
For X = 2 add X to the first element of array [3, 3, 2, 18] and increase X by 1.
For X = 3 just increase X by 1.
For X = 4 add X to the third element of array to make the array [3, 3, 6, 18] and increase X by 1.
At last, the array becomes [3, 3, 6, 18] where all the elements are divisible by K = 3.
Input: K = 5, a[] = [15, 25, 5, 10, 20, 1005, 70, 80, 90, 100]
Output: 0
Explanation:
Here all elements are already divisible by 5.
Approach: The main idea is to find the maximum value of X that is needed to update the elements of the array to make it divisible by K.
- To do this we need to find the maximum value of (K – (ai mod K)) to add to the array elements to make it divisible by K.
- However, there can be equal elements so keep track of the number of such elements, using map data structures.
- When found another such element in the array then update the answer with (K – (ai mod K)) + (K * number of Equal Elements) because with every move we increase X by 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void compute( int a[], int N, int K)
{
map< long , long > eqVal;
long maxX = 0;
for ( int i = 0; i < N; i++) {
long val = a[i] % K;
val = (val == 0 ? 0 : K - val);
if (val == 0)
continue ;
if (eqVal.find(val) != eqVal.end()) {
long numVal = eqVal[val];
maxX = max(maxX,
val + (K * numVal));
eqVal[val]++;
}
else {
eqVal[val]++;
maxX = max(maxX, val);
}
}
cout << (maxX == 0 ? 0 : maxX + 1)
<< endl;
}
int main()
{
int K = 3;
int a[] = { 1, 2, 2, 18 };
int N = sizeof (a) / sizeof (a[0]);
compute(a, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void compute( int a[], int N, int K)
{
Map<Long,
Long> eqVal = new HashMap<Long,
Long>();
long maxX = 0 ;
for ( int i = 0 ; i < N; i++)
{
long val = a[i] % K;
val = (val == 0 ? 0 : K - val);
if (val == 0 )
continue ;
if (eqVal.containsKey(val))
{
long numVal = eqVal.get(val);
maxX = Math.max(maxX,
val + (K * numVal));
eqVal.put(val,
eqVal.getOrDefault(val, 0l) + 1l);
}
else
{
eqVal.put(val, 1l);
maxX = Math.max(maxX, val);
}
}
System.out.println(maxX == 0 ? 0 : maxX + 1 );
}
public static void main(String[] args)
{
int K = 3 ;
int a[] = { 1 , 2 , 2 , 18 };
int N = a.length;
compute(a, N, K);
}
}
|
Python3
from collections import defaultdict
def compute(a, N, K):
eqVal = defaultdict( int )
maxX = 0
for i in range (N):
val = a[i] % K
if (val ! = 0 ):
val = K - val
if (val = = 0 ):
continue
if (val in eqVal):
numVal = eqVal[val]
maxX = max (maxX,
val + (K * numVal))
eqVal[val] + = 1
else :
eqVal[val] + = 1
maxX = max (maxX, val)
if maxX = = 0 :
print ( 0 )
else :
print (maxX + 1 )
if __name__ = = "__main__" :
K = 3
a = [ 1 , 2 , 2 , 18 ]
N = len (a)
compute(a, N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void compute( int []a, int N, int K)
{
Dictionary< long ,
long > eqVal = new Dictionary< long ,
long >();
long maxX = 0;
for ( int i = 0; i < N; i++)
{
long val = a[i] % K;
val = (val == 0 ? 0 : K - val);
if (val == 0)
continue ;
if (eqVal.ContainsKey(val))
{
long numVal = eqVal[val];
maxX = Math.Max(maxX,
val + (K * numVal));
eqVal[val] = 1 +
eqVal.GetValueOrDefault(val, 0);
}
else
{
eqVal.Add(val, 1);
maxX = Math.Max(maxX, val);
}
}
Console.Write(maxX == 0 ? 0 : maxX + 1);
}
public static void Main( string [] args)
{
int K = 3;
int []a = { 1, 2, 2, 18 };
int N = a.Length;
compute(a, N, K);
}
}
|
Javascript
<script>
function compute(a,N,K)
{
let eqVal = new Map();
let maxX = 0;
for (let i = 0; i < N; i++)
{
let val = a[i] % K;
val = (val == 0 ? 0 : K - val);
if (val == 0)
continue ;
if (eqVal.has(val))
{
let numVal = eqVal.get(val);
maxX = Math.max(maxX,
val + (K * numVal));
eqVal.set(val,eqVal.get(val) + 1);
}
else
{
eqVal.set(val, 1);
maxX = Math.max(maxX, val);
}
}
document.write(maxX == 0 ? 0 : maxX + 1);
}
let K = 3;
let a=[1, 2, 2, 18];
let N = a.length;
compute(a, N, K);
</script>
|
Time Complexity: O(N*logN), where N represents the size of the given array.
Auxiliary Space: O(N), where N represents the size of the given array.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...