📜  数组中唯一对 (i, j) 的计数,使得 A[i] 和 A[j] 的反向之和等于 A[i] 和 A[j] 的反向之和

📅  最后修改于: 2021-09-06 06:25:59             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,任务是找到唯一对(i, j)的计数,使得arr[i]反向 (arr[j])的总和与总和相同reverse(arr[i])arr[j]

例子:

朴素方法:最简单的方法是生成给定数组的所有可能对,如果任何一对元素满足给定条件,则计算这些对。完成上述步骤m后,打印count的值作为结果。

时间复杂度: O(N 2 *log M),其中 M 是 A[] 中的最大元素
辅助空间: O(1)

高效的方法:上述方法可以通过使用哈希技术并将等式重写为:

现在,我们的想法是为每个元素arr[i]计算(A[i] – reverse(A[i]))的频率,然后计算满足给定条件的有效对的可能数量。请按照以下步骤解决问题:

  • 维护一个 Hashmap,比如u_map来存储A[i] – reverse(A[i])对于任何索引i的频率计数。
  • 初始化一个变量来存储满足给定条件的对的数量。
  • 使用变量i遍历给定数组A[]并执行以下操作:
    • A[i] – reverse(A[i])的频率存储在val 中
    • val递增
    • 更新u_mapval的频率。
  • 完成上述步骤后,打印pairs的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the
// reverse of the number n
int reverse(int n)
{
    int temp = n, rev = 0, r;
 
    // Iterate until temp is 0
    while (temp) {
 
        r = temp % 10;
        rev = rev * 10 + r;
        temp /= 10;
    }
 
    // Return the reversed number
    return rev;
}
 
// Function to count number of unique
// pairs (i, j) from the array A[]
// which satisfies the given condition
void countPairs(int A[], int N)
{
    // Store the frequency of keys
    // as A[i] - reverse(A[i])
    unordered_map u_map;
 
    // Stores count of desired pairs
    int pairs = 0;
 
    // Iterate the array A[]
    for (int i = 0; i < N; i++) {
 
        int val = A[i] - reverse(A[i]);
 
        // Add frequency of val
        // to the required answer
        pairs += u_map[val];
 
        // Increment frequency of val
        u_map[val]++;
    }
 
    // Print the number of pairs
    cout << pairs;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 15, 11, 7 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    countPairs(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to find the
// reverse of the number n
static int reverse(int n)
{
    int temp = n, rev = 0, r;
 
    // Iterate until temp is 0
    while (temp > 0)
    {
        r = temp % 10;
        rev = rev * 10 + r;
        temp /= 10;
    }
 
    // Return the reversed number
    return rev;
}
 
// Function to count number of unique
// pairs (i, j) from the array A[]
// which satisfies the given condition
static void countPairs(int A[], int N)
{
     
    // Store the frequency of keys
    // as A[i] - reverse(A[i])
    HashMap map = new HashMap<>();
 
    // Stores count of desired pairs
    int pairs = 0;
 
    // Iterate the array A[]
    for(int i = 0; i < N; i++)
    {
        int val = A[i] - reverse(A[i]);
 
        // Add frequency of val
        // to the required answer
        pairs += map.getOrDefault(val, 0);
 
        // Increment frequency of val
        map.put(val, map.getOrDefault(val, 0) + 1);
    }
 
    // Print the number of pairs
    System.out.println(pairs);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 2, 15, 11, 7 };
    int N = arr.length;
 
    // Function Call
    countPairs(arr, N);
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
from collections import defaultdict
 
# Function to find the
# reverse of the number n
def reverse(n):
    temp = n
    rev = 0
 
    # Iterate until temp is 0
    while (temp):
        r = temp % 10
        rev = rev * 10 + r
        temp //= 10
 
    # Return the reversed number
    return rev
 
# Function to count number of unique
# pairs (i, j) from the array A[]
# which satisfies the given condition
def countPairs(A, N):
 
    # Store the frequency of keys
    # as A[i] - reverse(A[i])
    u_map = defaultdict(int)
 
    # Stores count of desired pairs
    pairs = 0
 
    # Iterate the array A[]
    for i in range(N):
        val = A[i] - reverse(A[i])
 
        # Add frequency of val
        # to the required answer
        pairs += u_map[val]
 
        # Increment frequency of val
        u_map[val] += 1
 
    # Print the number of pairs
    print(pairs)
 
 
# Driver Code
if __name__ == "__main__":
 
    arr = [2, 15, 11, 7]
    N = len(arr)
 
    # Function Call
    countPairs(arr, N)
 
    # This code is contributed by chitranayal.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to find the
// reverse of the number n
static int reverse(int n)
{
    int temp = n, rev = 0, r;
 
    // Iterate until temp is 0
    while (temp > 0)
    {
        r = temp % 10;
        rev = rev * 10 + r;
        temp /= 10;
    }
 
    // Return the reversed number
    return rev;
}
 
// Function to count number of unique
// pairs (i, j) from the array A[]
// which satisfies the given condition
static void countPairs(int []A, int N)
{
     
    // Store the frequency of keys
    // as A[i] - reverse(A[i])
    Dictionary u_map = new Dictionary();
                                            
    // Stores count of desired pairs
    int pairs = 0;
 
    // Iterate the array A[]
    for(int i = 0; i < N; i++)
    {
        int val = A[i] - reverse(A[i]);
 
        // Add frequency of val
        // to the required answer
        if (u_map.ContainsKey(val))
            pairs += u_map[val];
 
        // Increment frequency of val
        if (u_map.ContainsKey(val))
            u_map[val]++;
        else
            u_map.Add(val, 1);
    }
 
    // Print the number of pairs
    Console.Write(pairs);
}
 
// Driver Code
public static void Main()
{
    int []arr = { 2, 15, 11, 7 };
    int N = arr.Length;
 
    // Function Call
    countPairs(arr, N);
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出:
3

时间复杂度: O(N*log M),其中M数组中最大的元素
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live