计数绝对差可被 K 整除的数组中的对 |使用地图
给定一个数组,由N个元素组成的arr[]和一个整数K ,任务是找到对(i, j)的数量,使得(arr[i] – arr[j])的绝对值是克。
例子:
Input: N = 4, K = 2, arr[] = {1, 2, 3, 4}
Output: 2
Explanation: Total 2 pairs exists in the array with absolute difference divisible by 2. The pairs are: (1, 3), (2, 4).
Input: N = 3, K = 3, arr[] = {3, 3, 3}
Output: 3
Explanation: Total 3 pairs exists in this array with absolute difference divisible by 3. The pairs are: (3, 3), (3, 3), (3, 3).
天真的方法:最简单的方法是遍历数组中的每个可能对,如果数字的绝对差是K的倍数,则将计数增加1 。在处理完所有对后打印计数值。
时间复杂度: O(N 2 )
辅助空间: O(1)
频率阵列方法:本文的第 1 组讨论了使用频率阵列解决此问题的方法。在这种方法中,我们讨论了使用地图解决它的方法。
高效方法:为了优化上述方法,想法是观察以下事实:对于两个数字a[i]和a[j] ,如果a[i] % k = a[j] % k ,则abs(a[ i] – a[j])是K的倍数。请按照以下步骤解决问题:
- 将变量ans初始化为0以存储答案。
- 声明一个unordered_map
count_map[] ,它用K存储数组元素的余数。 - 使用变量index遍历范围[1, N]并为每个索引将count_map中的值arr[index]%k增加1 。
- 遍历 count_map 中的所有键值对。对于每个键值对:
- 值count_map[rem]是与K的余数等于“ rem ”的元素的数量。
- 对于要形成的有效对,请从count_map[rem]数字中选择任意两个数字。
- 从“ N ”个数中选择两个数的方法数是Nc2 = N * (N – 1) / 2 。
- 添加所有键值对的答案并打印ans 。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count number of pairs
// (i, j) such that abs(arr[i] - arr[j])
// is divisible by k.
void countOfPairs(int* arr, int N, int K)
{
// Frequency Map to keep count of
// remainders of array elements with K.
unordered_map count_map;
for (int index = 0; index < N; ++index) {
count_map[arr[index] % K]++;
}
// To store the final answer.
int ans = 0;
for (auto it : count_map) {
// Number of ways of selecting any two
// numbers from all numbers having the
// same remainder is Nc2 = N
// * (N - 1) / 2
ans += (it.second * (it.second - 1)) / 2;
}
// Output the answer.
cout << ans << endl;
}
// Driver Code
int main()
{
int K = 2;
// Input array
int arr[] = { 1, 2, 3, 4 };
// Size of array
int N = sizeof arr / sizeof arr[0];
countOfPairs(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to count number of pairs
// (i, j) such that Math.abs(arr[i] - arr[j])
// is divisible by k.
static void countOfPairs(int[] arr, int N, int K) {
// Frequency Map to keep count of
// remainders of array elements with K.
HashMap count_map =
new HashMap();
for (int index = 0; index < N; ++index) {
if (count_map.containsKey(arr[index] % K)) {
count_map.put(arr[index] % K,
count_map.get(arr[index] % K) + 1);
} else {
count_map.put(arr[index] % K, 1);
}
}
// To store the final answer.
int ans = 0;
for (Map.Entry it : count_map.entrySet()) {
// Number of ways of selecting any two
// numbers from all numbers having the
// same remainder is Nc2 = N
// * (N - 1) / 2
ans += (it.getValue() * (it.getValue() - 1)) / 2;
}
// Output the answer.
System.out.print(ans + "\n");
}
// Driver Code
public static void main(String[] args) {
int K = 2;
// Input array
int arr[] = { 1, 2, 3, 4 };
// Size of array
int N = arr.length;
countOfPairs(arr, N, K);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python Program to implement
# the above approach
# Function to count number of pairs
# (i, j) such that abs(arr[i] - arr[j])
# is divisible by k.
def countOfPairs(arr, N, K):
# Frequency Map to keep count of
# remainders of array elements with K.
count_map = {}
for index in range(N):
if (not arr[index] % K in count_map):
count_map[arr[index] % K] = 1
else:
count_map[arr[index] % K] += 1
# To store the final answer.
ans = 0
for val in count_map.values():
# Number of ways of selecting any two
# numbers from all numbers having the
# same remainder is Nc2 = N
# * (N - 1) / 2
ans += (val * (val - 1)) // 2
# Output the answer.
print(ans)
# Driver Code
K = 2
# Input array
arr = [1, 2, 3, 4]
# Size of array
N = len(arr)
countOfPairs(arr, N, K)
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG {
// Function to count number of pairs
// (i, j) such that Math.Abs(arr[i] - arr[j])
// is divisible by k.
static void countOfPairs(int[] arr, int N, int K) {
// Frequency Map to keep count of
// remainders of array elements with K.
Dictionary count_map =
new Dictionary();
for (int index = 0; index < N; ++index) {
if (count_map.ContainsKey(arr[index] % K)) {
count_map[arr[index] % K] =
count_map[arr[index] % K] + 1;
} else {
count_map.Add(arr[index] % K, 1);
}
}
// To store the readonly answer.
int ans = 0;
foreach (KeyValuePair it in count_map) {
// Number of ways of selecting any two
// numbers from all numbers having the
// same remainder is Nc2 = N
// * (N - 1) / 2
ans += (it.Value * (it.Value - 1)) / 2;
}
// Output the answer.
Console.Write(ans + "\n");
}
// Driver Code
public static void Main(String[] args) {
int K = 2;
// Input array
int []arr = { 1, 2, 3, 4 };
// Size of array
int N = arr.Length;
countOfPairs(arr, N, K);
}
}
// This code is contributed by shikhasingrajput
Javascript
2
时间复杂度: O(NlogN)
辅助空间: O(N)