📌  相关文章
📜  连接时可被 K 整除的数组元素对的计数

📅  最后修改于: 2021-10-27 08:21:30             🧑  作者: Mango

给定一个数组arr[]和一个整数K ,任务是计算一对索引(i, j)使得i !=j并且a[i]a[j] 的串联可以被K整除。

例子:

天真的方法:
解决问题的最简单方法如下:

  • 使用带有变量ij 的嵌套循环遍历数组。
  • 连接arr[i]arr[j] ,对于每个i != j ,通过等式:
  • 检查连接数被K整除的可能性。
  • 对于所有这样的(arr[i], arr[j]) 对,增加count
  • 打印count的最终值。

时间复杂度: O(N 2 *len(maxm),其中 maxm 表示数组中的最大元素,len(maxm) 表示 maxm 的位数。

辅助空间: O(1)
有效的方法:

要优化上述方法,请按照以下步骤操作:

  • 要应用上述公式,请为从110 的每个长度维护一个 Map 。
  • 将 { len[a[i]] , a[i] % k } 存储在地图中。
  • 要计算对,对于[1, 10] 中的每个j ,通过存储在Map 中(k – ((arr[i] * 10^j) % k))的频率增加计数{j, k – ((arr[i] * 10^j) % k)}映射。
  • 如果对(arr[i], arr[i])进行计数,则将计数1
  • 完成数组遍历后,打印最终计数

下面是上述方法的实现:

C++
// C++ Program to count pairs
// of array elements which are
// divisible by K when concatenated
#include 
using namespace std;
 
map rem[11];
 
// Function to calculate and return the
// count of pairs
int countPairs(vector a, int n, int k)
{
 
    vector len(n);
 
    // Compute power of 10 modulo k
    vector 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 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


输出
4

时间复杂度: O(N * len(maxm))
辅助空间: O(N)