Skip to content
Related Articles

Related Articles

Make all array elements divisible by a number K
  • Difficulty Level : Expert
  • Last Updated : 13 Apr, 2020
GeeksforGeeks - Summer Carnival Banner

Given an array arr[] and a number K, the task is to make all elements of array divisible by K. To make elements divisible by K, one can perform following operation:

  • Choose any index C in the array.
  • You are allowed to subtract any value from any of the numbers up to the index C and you are allowed to add any value to any of the elements present after index C.
  • The only condition required for the above operation is total sum of values subtracted up to index C can be added to the elements after index C.

Print the value of index C and the difference of subtracted numbers to the numbers added after index C.

Examples:

Input: arr[] = {1, 14, 4, 41, 1}, K = 7
Output: C = 3, difference = 5
Explanation:
Please Refere below for explanation.

Input: arr[] = {1, 10, 19}, K=9
Output: C = 2, difference = 2



Explanation:

Approach:

  • Create two auxiliary arrays arr1[] and arr2[].
  • The first array arr1[] stores the value of element that can be subtracted from every element to make it divisible by K in arr[].
  • The second array arr2[] stores the value of element that can be added to make the element divisible by K.
  • Then Iterate over the possible values for the C and find the index in which the sum of subtracted values from arr1[] up to that index is greater than or equal to the sum of the added values to the index after C in arr2[].

Below is the implementation of the above approach:

C++




// C++ implementation to make
// the array elements divisible by K
#include <bits/stdc++.h>
using namespace std;
  
// Function to make array divisible
pair<int,int> makeDivisble(int arr[], int n, int k)
{
      
    vector<int>b1; 
    vector<int>b2;
    int c, suml, sumr, index, rem;
      
    // For each element of array
    // how much number to be subtracted
    // to make it divisible by k 
    for (int i = 0; i < n; i++)
        b1.push_back(arr[i] % k);
      
    // For each element of array
    // how much number to be added
    // to make it divisible by K 
    for (int j = 0; j < n; j++)
        if ((arr[j] % k) != 0)
            b2.push_back(k - (arr[j] % k));
        else
            b2.push_back(0);
              
    c = 0;
    float mini = INT_MAX;
    suml = 0;
    sumr = 0;
    index = -1;
      
    // Calculate minimum difference 
    for (int c = 0; c < n; c++)
    {
        suml = accumulate(b1.begin(),b1.begin() + c + 1, 0);
        sumr = accumulate(b2.begin() + c + 1 , b2.end(), 0);
        if (suml >= sumr)
        {
            rem = suml - sumr;
            if (rem < mini)
            {
                mini = rem;
                index = c;
            }
        }
    }
      
    return make_pair(index, mini);
  
}
  
// Driver Code
int main() {
    int arr[] = {1, 14, 4, 41, 1};
    int k = 7;
    int n=sizeof(arr)/sizeof(arr[0]);
      
    pair<int ,int>ans;
    ans = makeDivisble(arr, n, k);
    cout << ans.first << " " << ans.second;
      
    return 0;
}
  
// This code is contributed by Atul_kumar_Shrivastava

Python




# Python implementation to make
# the array elements divisible by K
  
# Function to make array divisible
def makeDivisble(arr, k):
    n = len(arr)
    b1 =[]
    b2 =[]
      
    # For each element of array
    # how much number to be subtracted
    # to make it divisible by k 
    for i in range (n):
        b1.append(arr[i]% k)
      
    # For each element of array
    # how much number to be added
    # to make it divisible by K 
    for j in range(n):
        if ((arr[j]% k)!= 0):
            b2.append(k-(arr[j]% k))
        else:
            b2.append(0
    c = 0
    mini = float('inf')
    suml = 0
    sumr = 0
    index = -1
      
    # Calculate minimum difference 
    for c in range(0, n+1, 1):
        suml = sum(b1[ : c + 1])
        sumr = sum(b2)
        if suml>= sumr:
            rem = suml-sumr
            if rem<mini:
                mini = rem
                index = c
    return index, mini
  
# Driver Code
if __name__ == "__main__":
    arr = [1, 14, 4, 41, 1]
    k = 7
    index, diff = makeDivisble(arr, k)
    print(index, diff)
Output:
3 5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :