📌  相关文章
📜  有序对 (i, j) 的计数,使得 arr[i] 和 arr[j] 连接到 X

📅  最后修改于: 2022-05-13 01:56:09.029000             🧑  作者: Mango

有序对 (i, j) 的计数,使得 arr[i] 和 arr[j] 连接到 X

给定一个大小为N的数组arr[]和一个整数X ,任务是找到有序对(i, j)的数量,使得i != jX是数字arr [i]arr[ j]
例子

方法:可以使用hashmap解决任务按照以下步骤解决问题

  • 将所有数字的长度存储在一个向量中。
  • 遍历给定的数字数组并检查是否可以从数字中获得X。如果是这样,请增加计数以检查当前数字可以形成多少个数字对。
  • 增加地图中当前数字的值。
  • 清除地图并从面对给定的数字数组重复相同的步骤。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the number of possible
// ordered pairs
long long countPairs(int n, int x, vector v)
{
    int power[10]
        = { 1, 10, 100,
            1000, 10000, 100000,
            1000000, 10000000,
            100000000,
            1000000000 };
 
    // Stores the count of digits of each number
    vector len;
    long long count = 0;
    for (int i = 0; i < n; i++)
        len.push_back(log10(v[i]) + 1);
 
    unordered_map mp;
    mp[v[0]]++;
 
    // Iterating from the start
    for (int i = 1; i < n; i++) {
        if (x % power[len[i]] == v[i])
            count += mp[x / power[len[i]]];
cout<<"count = "<= 0; i--) {
        if (x % power[len[i]] == v[i])
            count += mp[x / power[len[i]]];
cout<<"c = "< numbers = { 1, 212, 12, 12 };
    long long ans = countPairs(N, X, numbers);
    cout << ans << endl;
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG {
 
  // Function to find the number of possible
  // ordered pairs
  static long countPairs(int n, int x, int[] v) {
    int power[] = { 1, 10, 100, 1000, 10000, 100000,
                   1000000, 10000000, 100000000, 1000000000 };
 
    // Stores the count of digits of each number
    Vector len = new Vector();
    long count = 0;
    for (int i = 0; i < n; i++)
      len.add((int) (Math.log10(v[i]) + 1));
 
    HashMap mp = new HashMap();
    if (mp.containsKey(v[0])) {
      mp.put(v[0], mp.get(v[0]) + 1);
    } else {
      mp.put(v[0], 1);
    }
 
    // Iterating from the start
    for (int i = 1; i < n; i++) {
      if (x % power[len.get(i)] == v[i]&&mp.containsKey(x / power[len.get(i)]))
        count += mp.get(x / power[len.get(i)]);
      System.out.println("count = " + count);
      if (mp.containsKey(v[i])) {
        mp.put(v[i], mp.get(v[i]) + 1);
      } else {
        mp.put(v[i], 1);
      }
    }
 
    mp.clear();
    if (mp.containsKey(v[n - 1])) {
      mp.put(v[n - 1], mp.get(v[n - 1]) + 1);
    } else {
      mp.put(v[n - 1], 1);
    }
 
    // Iterating from the end
    for (int i = n - 2; i >= 0; i--) {
      if (x % power[len.get(i)] == v[i]&&mp.containsKey(x / power[len.get(i)]))
        count += mp.get(x / power[len.get(i)]);
      System.out.println("c = " + count);
      if (mp.containsKey(v[i])) {
        mp.put(v[i], mp.get(v[i]) + 1);
      } else {
        mp.put(v[i], 1);
      }
    }
    return count;
  }
 
  // Driver Code
  public static void main(String[] args) {
    int N = 4;
    int X = 1212;
    int[] numbers = { 1, 212, 12, 12 };
    long ans = countPairs(N, X, numbers);
    System.out.print(ans + "\n");
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python 3 program for the above approach
from collections import defaultdict
import math
 
# Function to find the number of possible
# ordered pairs
def countPairs(n,  x,  v):
 
    power = [1, 10, 100,
             1000, 10000, 100000,
             1000000, 10000000,
             100000000,
             1000000000]
 
    # Stores the count of digits of each number
    length = []
    count = 0
    for i in range(n):
        length.append(int(math.log10(v[i])) + 1)
 
    mp = defaultdict(int)
    mp[v[0]] += 1
 
    # Iterating from the start
    for i in range(1, n):
        if (x % power[length[i]] == v[i]):
            count += mp[x // power[length[i]]]
        mp[v[i]] += 1
 
    mp.clear()
    mp[v[n - 1]] += 1
 
    # Iterating from the end
    for i in range(n - 2, -1, -1):
        if (x % power[length[i]] == v[i]):
            count += mp[x // power[length[i]]]
 
        mp[v[i]] += 1
 
    return count
 
# Driver Code
if __name__ == "__main__":
 
    N = 4
    X = 1212
    numbers = [1, 212, 12, 12]
    ans = countPairs(N, X, numbers)
    print(ans)
 
    # This code is contributed by ukasp.


Javascript



输出
3

时间复杂度 O(N)
辅助空间 O(N)