Count of pairs of Array elements which are divisible by K when concatenated
Last Updated :
01 Sep, 2021
Given an array arr[] and an integer K, the task is to count the pair of indices (i, j) such that i !=j and concatenation of a[i] and a[j] is divisible by K.
Example:
Input: arr[] = [4, 5, 2], K = 2
Output: 4
Explanation:
All possible concatenations are {45, 42, 54, 52, 24, 25}.
Out of these, the numbers divisible by 2 are {42, 52, 24, 54}.
Therefore, the count is 4.
Input: arr[] =[45, 1, 10, 12, 11, 7], K = 11
Output: 7
Naive Approach:
The simplest approach to solve the problem is as follows:
- Iterate over the array using nested loops with variables i and j.
- Concatenate arr[i] and arr[j], for every i != j, by the equation:
Concatenation of arr[i] and arr[j] = (arr[i] * 10lenj) + arr[j], where lenj is the number of digits in arr[j]
- Check the divisibility of the concatenated number by K.
Concatenated Number is divisible by k if and only if the sum of (arr[j] % k) and ((arr[i] * 10lenj) % k) is 0 mod k.
- For all such pairs of (arr[i], arr[j]), increase count.
- Print the final value of count.
Time complexity: O(N2*len(maxm), where maxm denotes the maximum element in the array and len(maxm) denotes the count of digits of maxm.
Auxiliary Space: O(1)
Efficient Approach:
To optimize the above approach follow the steps below:
- To apply the above formula, maintain a Map for each length from 1 to 10.
- Store {len[a[i]], a[i] % k } in the map.
- To count the pairs, for each j in [1, 10], increase the count by the frequency of (k - ((arr[i] * 10^j) % k)) stored in the Map as {j, k - ((arr[i] * 10^j) % k)} mapping.
- If the pair (arr[i], arr[i]) is counted, decrease the count by 1.
- After complete traversal of the array, print the final count.
Below is the implementation of the above approach:
C++
// C++ Program to count pairs
// of array elements which are
// divisible by K when concatenated
#include <bits/stdc++.h>
using namespace std;
map<int, int> rem[11];
// Function to calculate and return the
// count of pairs
int countPairs(vector<int> a, int n, int k)
{
vector<int> len(n);
// Compute power of 10 modulo k
vector<int> p(11);
p[0] = 1;
for (int i = 1; i <= 10; i++) {
p[i] = (p[i - 1] * 10) % k;
}
for (int i = 0; i < n; i++) {
int x = a[i];
// Calculate length of a[i]
while (x > 0) {
len[i]++;
x /= 10;
}
// Increase count of remainder
rem[len[i]][a[i] % k]++;
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j <= 10; j++) {
// Calculate (a[i]* 10^lenj) % k
int r = (a[i] * p[j]) % k;
// Calculate (k - (a[i]* 10^lenj)% k) % k
int xr = (k - r) % k;
// Increase answer by count
ans += rem[j][xr];
// If a pair (a[i], a[i]) is counted
if (len[i] == j
&& (r + a[i] % k) % k == 0)
ans--;
}
}
// Return the count of pairs
return ans;
}
// Driver Code
int main()
{
vector<int> a = { 4, 5, 2 };
int n = a.size(), k = 2;
cout << countPairs(a, n, k);
}
Java
// Java program to count pairs
// of array elements which are
// divisible by K when concatenated
import java.util.*;
import java.lang.*;
class GFG{
static int[][] rem = new int[11][11];
// Function to calculate and return the
// count of pairs
static int countPairs(int[] a, int n, int k)
{
int[] len = new int[n];
// Compute power of 10 modulo k
int[] p = new int[11];
p[0] = 1;
for(int i = 1; i <= 10; i++)
{
p[i] = (p[i - 1] * 10) % k;
}
for(int i = 0; i < n; i++)
{
int x = a[i];
// Calculate length of a[i]
while (x > 0)
{
len[i]++;
x /= 10;
}
// Increase count of remainder
rem[len[i]][a[i] % k]++;
}
int ans = 0;
for(int i = 0; i < n; i++)
{
for(int j = 1; j <= 10; j++)
{
// Calculate (a[i]* 10^lenj) % k
int r = (a[i] * p[j]) % k;
// Calculate (k - (a[i]* 10^lenj)% k) % k
int xr = (k - r) % k;
// Increase answer by count
ans += rem[j][xr];
// If a pair (a[i], a[i]) is counted
if (len[i] == j &&
(r + a[i] % k) % k == 0)
ans--;
}
}
// Return the count of pairs
return ans;
}
// Driver code
public static void main (String[] args)
{
int[] a = { 4, 5, 2 };
int n = a.length, k = 2;
System.out.println(countPairs(a, n, k));
}
}
// This code is contributed by offbeat
Python3
# Python3 program to count pairs
# of array elements which are
# divisible by K when concatenated
rem = [ [ 0 for x in range(11) ]
for y in range(11) ]
# Function to calculate and return the
# count of pairs
def countPairs(a, n, k):
l = [0] * n
# Compute power of 10 modulo k
p = [0] * (11)
p[0] = 1
for i in range(1, 11):
p[i] = (p[i - 1] * 10) % k
for i in range(n):
x = a[i]
# Calculate length of a[i]
while (x > 0):
l[i] += 1
x //= 10
# Increase count of remainder
rem[l[i]][a[i] % k] += 1
ans = 0
for i in range(n):
for j in range(1, 11):
# Calculate (a[i]* 10^lenj) % k
r = (a[i] * p[j]) % k
# Calculate (k - (a[i]* 10^lenj)% k) % k
xr = (k - r) % k
# Increase answer by count
ans += rem[j][xr]
# If a pair (a[i], a[i]) is counted
if (l[i] == j and
(r + a[i] % k) % k == 0):
ans -= 1
# Return the count of pairs
return ans
# Driver Code
a = [ 4, 5, 2 ]
n = len(a)
k = 2
print(countPairs(a, n, k))
# This code is contributed by chitranayal
C#
// C# program to count pairs
// of array elements which are
// divisible by K when concatenated
using System;
class GFG{
static int [,]rem = new int[11, 11];
// Function to calculate and
// return the count of pairs
static int countPairs(int[] a,
int n, int k)
{
int[] len = new int[n];
// Compute power of 10 modulo k
int[] p = new int[11];
p[0] = 1;
for(int i = 1; i <= 10; i++)
{
p[i] = (p[i - 1] * 10) % k;
}
for(int i = 0; i < n; i++)
{
int x = a[i];
// Calculate length of a[i]
while (x > 0)
{
len[i]++;
x /= 10;
}
// Increase count of remainder
rem[len[i], a[i] % k]++;
}
int ans = 0;
for(int i = 0; i < n; i++)
{
for(int j = 1; j <= 10; j++)
{
// Calculate (a[i]* 10^lenj) % k
int r = (a[i] * p[j]) % k;
// Calculate (k - (a[i]* 10^lenj)% k) % k
int xr = (k - r) % k;
// Increase answer by count
ans += rem[j, xr];
// If a pair (a[i], a[i]) is counted
if (len[i] == j &&
(r + a[i] % k) % k == 0)
ans--;
}
}
// Return the count of pairs
return ans;
}
// Driver code
public static void Main(string[] args)
{
int[] a = {4, 5, 2};
int n = a.Length, k = 2;
Console.Write(countPairs(a, n, k));
}
}
// This code is contributed by rutvik_56
JavaScript
<script>
// JavaScript program to count pairs
// of array elements which are
// divisible by K when concatenated
let rem = new Array(11);
// Loop to create 2D array using 1D array
for (var i = 0; i < rem.length; i++) {
rem[i] = new Array(2);
}
for (var i = 0; i < rem.length; i++) {
for (var j = 0; j < rem.length; j++) {
rem[i][j] = 0;
}
}
// Function to calculate and return the
// count of pairs
function countPairs(a, n, k)
{
let len = Array.from({length: n}, (_, i) => 0);
// Compute power of 10 modulo k
let p = Array.from({length: 11}, (_, i) => 0);
p[0] = 1;
for(let i = 1; i <= 10; i++)
{
p[i] = (p[i - 1] * 10) % k;
}
for(let i = 0; i < n; i++)
{
let x = a[i];
// Calculate length of a[i]
while (x > 0)
{
len[i]++;
x = Math.floor(x / 10);
}
// Increase count of remainder
rem[len[i]][a[i] % k]++;
}
let ans = 0;
for(let i = 0; i < n; i++)
{
for(let j = 1; j <= 10; j++)
{
// Calculate (a[i]* 10^lenj) % k
let r = (a[i] * p[j]) % k;
// Calculate (k - (a[i]* 10^lenj)% k) % k
let xr = (k - r) % k;
// Increase answer by count
ans += rem[j][xr];
// If a pair (a[i], a[i]) is counted
if (len[i] == j &&
(r + a[i] % k) % k == 0)
ans--;
}
}
// Return the count of pairs
return ans;
}
// Driver Code
let a = [ 4, 5, 2 ];
let n = a.length, k = 2;
document.write(countPairs(a, n, k));
</script>
Time Complexity: O(N * len(maxm))
Auxiliary Space: O(N)
Similar Reads
Count the number of elements in an array which are divisible by k Given an array of integers. The task is to calculate the count of a number of elements which are divisible by a given number k. Examples: Input: arr[] = { 2, 6, 7, 12, 14, 18 }, k = 3 Output: 3 Numbers which are divisible by k are { 6, 12, 18 } Input: arr[] = { 2, 6, 7, 12, 14, 18 }, k = 2 Output: 5
6 min read
Count maximum concatenation of pairs from given array that are divisible by 3 Given an array arr[] of size N, the task is to count the pairs whose concatenation of elements is divisible by 3 and each array element present in at most one pair. Examples: Input: arr[] = { 5, 3, 2, 8, 7 } Output: 1 Explanation: Possible pairs whose concatenation is divisible by 3 are { 27, 72, 78
13 min read
Count number of pairs not divisible by any element in the array Given an array arr[] of size N, the task is to count the number of pairs of integers (i, j) for which there does not exist an integer k such that arr[i] is divisible by arr[k] and arr[j] is divisible by arr[k], such that k can be any index between [0, N - 1]. Examples: Input: N = 4, arr[] = {2, 4, 5
5 min read
Count pairs made up of an element divisible by the other from an array consisting of powers of 2 Given an array arr[] consisting of N powers of 2, the task is to count the number of pairs (arr[i], arr[j]) such that i < j and arr[j] is divisible by arr[i]. Examples: Input: arr[] = {4, 16, 8, 64}Output: 5Explanation:The pairs satisfying the given condition are {4, 16}, {4, 8}, {4, 64}, {16, 64
9 min read
Count of pairs of Array elements with average at least K Given an array A[] of size N consisting of N integers, the task is to count the number of pairs such that their average is greater or equal to K. Example: Input: N = 4, K = 3, A = {5, 1, 3, 4}Output: 4Explanation: (5, 1), (5, 3), (5, 4) and (3, 4) are the required pairs with average greater or equal
11 min read