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 element 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.
Input: K = 3, a = [1, 2, 2, 18]
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]
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:
Time Complexity: O(N)
- Minimum operations required to make all the elements distinct in an array
- Minimum no. of operations required to make all Array Elements Zero
- Minimum operations required to make all the array elements equal
- Find the minimum number of operations required to make all array elements equal
- Find the number of operations required to make all array elements Equal
- Minimum number of operations required to delete all elements of the array
- Minimum increment or decrement operations required to make the array sorted
- Minimum pair merge operations required to make Array non-increasing
- Minimum pair sum operations to make array each element divisible by 4
- Minimum changes required to make all Array elements Prime
- Minimum number of increment-other operations to make all array elements equal.
- Minimum gcd operations to make all array elements one
- Minimum number of operations on an array to make all elements 0
- Make all the array elements odd with minimum operations of given type
- Minimum operations to make all elements equal using the second array
- Minimum Cost to make all array elements equal using given operations
- Minimum delete operations to make all elements of array same
- Minimum steps to make all the elements of the array divisible by 4
- Minimum operations of the given type required to make a complete graph
- Minimum operations required to make two numbers equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.