# Check if all array elements can be made divisible by K by replacing array elements with sum of pairs

• Difficulty Level : Hard
• Last Updated : 21 Apr, 2021

Given an array arr[] of size N and a positive integer K, the task is to make every element of the array divisible by K by repeatedly selecting a pair of indices (i, j) and perform arr[i] = arr[i] + arr[j].

Examples:

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.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr[] = {3, 6, 3}, K = 6
Output: True
Explanation:
Update arr = arr + arr.
Update arr = arr + arr

Input: arr[] = {6, 6, 8, 7, 3}, K = 6
Output: False

Approach: Follow the steps below to solve the problem:

• Traverse the array arr[]:
• Check if arr[i] is divisible by K or not:
• Initialize a variable, say temp, to store the value of arr[i] after each iteration.
• Repeatedly Multiply arr[i] by 2 until arr[i] < K * temp.
• Check if arr[i] % K != 0. If found to be true, print False and return.
• After completing the above steps, print True as all the elements are now divisible by K.

Below is the implementation of the above approach:

## C++

 `// C++ Program for the above approach` `#include ``using` `namespace` `std;` `// Utility function to check if all array``// elements can be made divisible by K or not``bool` `makeArrayDivisibleByKUtil(``    ``int` `arr[], ``int` `N, ``int` `K)``{``    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// Check whether arr[i] is``        ``// divisible by K or not``        ``if` `(arr[i] % K != 0) {` `            ``// Stores value of arr[i]``            ``int` `temp = arr[i];` `            ``while` `(arr[i] < K * temp) {` `                ``// Multiply by 2 until it``                ``// is less than K * temp``                ``arr[i] = arr[i] * 2;``            ``}` `            ``if` `(arr[i] % K != 0) {``                ``return` `false``;``            ``}``        ``}``    ``}` `    ``// Return true as all array``    ``// elements are divisible by K``    ``return` `true``;``}` `// Function to check whether all array``// elements can be made divisible by K or not``void` `makeArrayDivisibleByK(``    ``int` `arr[], ``int` `N, ``int` `K)``{``    ``if` `(makeArrayDivisibleByKUtil(arr, N, K)) {``        ``cout << ``"True"` `<< endl;``    ``}``    ``else` `{``        ``cout << ``"False"` `<< endl;``    ``}``}` `// Driver Code``int` `main()``{``    ``// Given array``    ``int` `arr[] = { 3, 6, 3 };` `    ``// Size of the array``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);` `    ``// Given value of K``    ``int` `K = 6;` `    ``makeArrayDivisibleByK(arr, N, K);` `    ``return` `0;``}`

## C

 `#include` `// Utility function to check if all array``// elements can be made divisible by K or not``int` `makeArrayDivisibleByKUtil(``    ``int` `arr[], ``int` `N, ``int` `K)``{``  ` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++)``    ``{` `        ``// Check whether arr[i] is``        ``// divisible by K or not``        ``if` `(arr[i] % K != 0)``        ``{` `            ``// Stores value of arr[i]``            ``int` `temp = arr[i];``            ``while` `(arr[i] < K * temp)``            ``{` `                ``// Multiply by 2 until it``                ``// is less than K * temp``                ``arr[i] = arr[i] * 2;``            ``}` `            ``if` `(arr[i] % K != 0)``            ``{``                ``return` `0;``            ``}``        ``}``    ``}` `    ``// Return true as all array``    ``// elements are divisible by K``    ``return` `1;``}` `// Function to check whether all array``// elements can be made divisible by K or not``void` `makeArrayDivisibleByK(``    ``int` `arr[], ``int` `N, ``int` `K)``{``    ``if` `(makeArrayDivisibleByKUtil(arr, N, K))``    ``{``        ``printf``(``"True\n"``);``    ``}``    ``else``    ``{``        ``printf``(``"False\n"``);``    ``}``}` `// Driver Code``int` `main()``{``  ` `    ``// Given array``    ``int` `arr[] = { 3,6,3 };` `    ``// Size of the array``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);` `    ``// Given value of K``    ``int` `K = 6;``    ``makeArrayDivisibleByK(arr, N, K);``    ``return` `0;``}` `// This code is contributed by santoshcharan.`

## Java

 `import` `java.io.*;``class` `GFG``{``    ``public` `static` `boolean` `makeArrayDivisibleBykUtil(``int` `arr[],``                                                    ``int` `n,``int` `k)``    ``{``      ` `        ``// Traverse the array``        ``for``(``int` `i = ``0``; i < n; i++)``        ``{``          ` `            ``// Check whether arr[i] is``            ``// divisible by k or not``            ``if``(arr[i] % k != ``0``)``            ``{``              ` `                ``// Stores value of arr[i]``                ``int` `temp = arr[i];``                ``while``(arr[i] < k * arr[i])``                ``{``                  ` `                    ``// Multiply by 2 until it``                    ``// is less than k*temp``                    ``arr[i] = arr[i] * ``2``;``                ``}``                ``if``(arr[i] % k != ``0``)``                ``{``                    ``return` `false``;``                ``}``            ``}``        ``}``      ` `        ``// Return true as all array``        ``// elements are divisible by k``        ``return` `true``;``    ``}``    ``public` `static` `void` `makeArrayDivisibleByk(``int` `arr[],``                                             ``int` `n,``int` `k)``    ``{``        ``if``(makeArrayDivisibleBykUtil(arr, n, k))``        ``{``            ``System.out.println(``"True"``);``        ``}``        ``else``        ``{``            ``System.out.println(``"False"``);``        ``}``    ``}``  ` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``      ` `        ``// Given array   ``        ``int``[] arr = { ``3` `, ``6` `, ``3``};``      ` `        ``// Size of the array``        ``int` `n = ``3``;``        ``int` `k = ``6``;``      ` `        ``// Calling the function``        ``makeArrayDivisibleByk(arr, n, k);``    ``}``}` `// This code is contributed by santoshcharan.`

## Python3

 `# Python 3 Program for the above approach` `# Utility function to check if all array``# elements can be made divisible by K or not``def` `makeArrayDivisibleByKUtil(arr, N, K):``  ` `  ``# Traverse the array``  ``for` `i ``in` `range``(N):``    ` `    ``# Check whether arr[i] is``    ``# divisible by K or not``    ``if` `(arr[i] ``%` `K !``=` `0``):``      ` `      ``# Stores value of arr[i]``      ``temp ``=` `arr[i]``      ``while` `(arr[i] < K ``*` `temp):``        ` `        ``# Multiply by 2 until it``        ``# is less than K * temp``        ``arr[i] ``=` `arr[i] ``*` `2``      ``if` `(arr[i] ``%` `K !``=` `0``):``        ``return` `False` `  ``# Return true as all array``  ``# elements are divisible by K``  ``return` `True` `# Function to check whether all array``# elements can be made divisible by K or not``def` `makeArrayDivisibleByK(arr, N, K):``  ``if` `(makeArrayDivisibleByKUtil(arr, N, K)):``    ``print``(``"True"``)``  ``else``:``    ``print``(``"False"``)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``  ` `  ``# Given array``  ``arr ``=`  `[``3``, ``6``, ``3``]` `  ``# Size of the array``  ``N ``=`  `len``(arr)` `  ``# Given value of K``  ``K ``=` `6``  ``makeArrayDivisibleByK(arr, N, K)``  ` `  ``# This code is contributed by bgangwar59.`

## C#

 `using` `System;` `class` `GFG``{``  ``public` `static` `bool` `makeArrayDivisibleBykUtil(``int` `[] arr,``                                               ``int` `n,``int` `k)``  ``{` `    ``// Traverse the array``    ``for``(``int` `i = 0; i < n; i++)``    ``{` `      ``// Check whether arr[i] is``      ``// divisible by k or not``      ``if``(arr[i] % k != 0)``      ``{` `        ``// Stores value of arr[i]``        ``//int temp = arr[i];``        ``while``(arr[i] < k * arr[i])``        ``{` `          ``// Multiply by 2 until it``          ``// is less than k*temp``          ``arr[i] = arr[i] * 2;``        ``}``        ``if``(arr[i] % k != 0)``        ``{``          ``return` `false``;``        ``}``      ``}``    ``}` `    ``// Return true as all array``    ``// elements are divisible by k``    ``return` `true``;``  ``}``  ``public` `static` `void` `makeArrayDivisibleByk(``int` `[]arr,``                                           ``int` `n,``int` `k)``  ``{``    ``if``(makeArrayDivisibleBykUtil(arr, n, k))``    ``{``      ``Console.WriteLine(``"True"``);``    ``}``    ``else``    ``{``      ``Console.WriteLine(``"False"``);``    ``}``  ``}` `  ``// Driver Code``  ``public` `static` `void` `Main(``string``[] args)``  ``{` `    ``// Given array   ``    ``int``[] arr = { 3 , 6 , 3};` `    ``// Size of the array``    ``int` `n = 3;``    ``int` `k = 6;` `    ``// Calling the function``    ``makeArrayDivisibleByk(arr, n, k);``  ``}``}` `// This code is contributed by chitranayal.`

## Javascript

 ``
Output:
`True`

Time Complexity: O(N * logN)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up