The Wayback Machine - https://web.archive.org/web/20210218062750/https://www.geeksforgeeks.org/check-if-all-array-elements-can-be-made-divisible-by-k-by-replacing-array-elements-with-sum-of-pairs/
Skip to content
Related Articles

Related Articles

Check if all array elements can be made divisible by K by replacing array elements with sum of pairs
  • Difficulty Level : Hard
  • Last Updated : 17 Feb, 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:

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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program for the above approach
  
#include <bits/stdc++.h>
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[0]);
  
    // Given value of K
    int K = 6;
  
    makeArrayDivisibleByK(arr, N, K);
  
    return 0;
}

chevron_right


Output:

True

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

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :