📌  相关文章
📜  计算绝对差可被 K 整除的数组中的对

📅  最后修改于: 2021-10-27 07:14:45             🧑  作者: Mango

给定一个数组arr[]和一个正整数K 。任务是计算数组中绝对差可被 K 整除的对的总数。

例子:

朴素方法:思想是对数组中的每一对进行一一检查,并计算绝对差可被 K 整除的对总数。

C++
#include 
using namespace std;
 
// function to count pairs in an array
// whose absolute difference is
// divisible by k
void countPairs(int arr[], int n, int k)
{
 
    // initialize count as zero.
    int i, j, cnt = 0;
 
    // loop to count the valid pair
    for (i = 0; i < n - 1; i++) {
        for (j = i + 1; j < n; j++) {
            if ((arr[i] - arr[j] + k) % k == 0)
                cnt += 1;
        }
    }
    cout << cnt << endl;
}
 
// Driver code
int main()
{
    // input array
    int arr[] = {3, 3, 3};
    int k = 3;
 
    // calculate the size of array
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // function to count the valid pair
    countPairs(arr, n, k);
    return 0;
}


Java
import java.util.*;
 
class GFG
{
 
// function to count pairs in an array
// whose absolute difference is
// divisible by k
static void countPairs(int arr[], int n, int k)
{
 
    // initialize count as zero.
    int i, j, cnt = 0;
 
    // loop to count the valid pair
    for (i = 0; i < n - 1; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if ((arr[i] - arr[j] + k) % k == 0)
                cnt += 1;
        }
    }
    System.out.print(cnt +"\n");
}
 
// Driver code
public static void main(String[] args)
{
    // input array
    int arr[] = {3, 3, 3};
    int k = 3;
 
    // calculate the size of array
    int n = arr.length;
 
    // function to count the valid pair
    countPairs(arr, n, k);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 Code implementation of the above approach
 
# function to count pairs in an array
# whose absolute difference is
# divisible by k
def countPairs(arr, n, k) :
 
    # initialize count as zero.
    cnt = 0;
 
    # loop to count the valid pair
    for i in range(n - 1) :
        for j in range(i + 1, n) :
            if ((arr[i] - arr[j] + k) % k == 0) :
                cnt += 1;
     
    print(cnt) ;
 
# Driver code
if __name__ == "__main__" :
 
    # input array
    arr = [3, 3, 3];
    k = 3;
 
    # calculate the size of array
    n = len(arr);
 
    # function to count the valid pair
    countPairs(arr, n, k);
     
# This code is contributed by AnkitRai01


C#
using System;
 
class GFG
{
 
// function to count pairs in an array
// whose absolute difference is
// divisible by k
static void countPairs(int []arr, int n, int k)
{
 
    // initialize count as zero.
    int i, j, cnt = 0;
 
    // loop to count the valid pair
    for (i = 0; i < n - 1; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if ((arr[i] - arr[j] + k) % k == 0)
                cnt += 1;
        }
    }
    Console.Write(cnt +"\n");
}
 
// Driver code
public static void Main(String[] args)
{
    // input array
    int []arr = {3, 3, 3};
    int k = 3;
 
    // calculate the size of array
    int n = arr.Length;
 
    // function to count the valid pair
    countPairs(arr, n, k);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// Write CPP code here
#include 
using namespace std;
 
// function to Count pairs in an array whose
// absolute difference is divisible by k
void countPair(int arr[], int n, int k)
{
 
    // initialize the count
    int cnt = 0;
 
    // making every element of arr in
    // range 0 to k - 1
    for (int i = 0; i < n; i++) {
        arr[i] = (arr[i] + k) % k;
    }
 
    // create an array hash[]
    int hash[k] = { 0 };
 
    // store to count of element of arr
    // in hash[]
    for (int i = 0; i < n; i++) {
        hash[arr[i]]++;
    }
 
    // count the pair whose absolute
    // difference is divisible by k
    for (int i = 0; i < k; i++) {
        cnt += (hash[i] * (hash[i] - 1)) / 2;
    }
 
    // print the value of count
    cout << cnt << endl;
}
 
// Driver Code
int main()
{
    // input array
    int arr[] = {1, 2, 3, 4};
    int k = 2;
 
    // calculate the size of array
    int n = sizeof(arr) / sizeof(arr[0]);
    countPair(arr, n, k);
    return 0;
}


Java
// JAVA Implementation of above approach
import java.util.*;
 
class GFG
{
 
// function to Count pairs in an array whose
// absolute difference is divisible by k
static void countPair(int arr[], int n, int k)
{
 
    // initialize the count
    int cnt = 0;
 
    // making every element of arr in
    // range 0 to k - 1
    for (int i = 0; i < n; i++)
    {
        arr[i] = (arr[i] + k) % k;
    }
 
    // create an array hash[]
    int hash[] = new int[k];
 
    // store to count of element of arr
    // in hash[]
    for (int i = 0; i < n; i++)
    {
        hash[arr[i]]++;
    }
 
    // count the pair whose absolute
    // difference is divisible by k
    for (int i = 0; i < k; i++)
    {
        cnt += (hash[i] * (hash[i] - 1)) / 2;
    }
 
    // print the value of count
    System.out.print(cnt +"\n");
}
 
// Driver Code
public static void main(String[] args)
{
    // input array
    int arr[] = {1, 2, 3, 4};
    int k = 2;
 
    // calculate the size of array
    int n = arr.length;
    countPair(arr, n, k);
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 Implementation of above approach
 
# function to Count pairs in an array whose
# absolute difference is divisible by k
def countPair(arr, n, k):
 
    # initialize the count
    cnt = 0;
 
    # making every element of arr in
    # range 0 to k - 1
    for i in range(n):
        arr[i] = (arr[i] + k) % k;
 
    # create an array hash
    hash = [0]*k;
 
    # store to count of element of arr
    # in hash
    for i in range(n):
        hash[arr[i]] += 1;
 
    # count the pair whose absolute
    # difference is divisible by k
    for i in range(k):
        cnt += (hash[i] * (hash[i] - 1)) / 2;
 
    # prthe value of count
    print(int(cnt));
 
# Driver Code
if __name__ == '__main__':
 
    # input array
    arr = [1, 2, 3, 4];
    k = 2;
 
    # calculate the size of array
    n = len(arr);
    countPair(arr, n, k);
 
# This code is contributed by 29AjayKumar


C#
// C# Implementation of above approach
using System;
 
class GFG
{
 
// function to Count pairs in an array whose
// absolute difference is divisible by k
static void countPair(int []arr, int n, int k)
{
 
    // initialize the count
    int cnt = 0;
 
    // making every element of arr in
    // range 0 to k - 1
    for (int i = 0; i < n; i++)
    {
        arr[i] = (arr[i] + k) % k;
    }
 
    // create an array hash[]
    int []hash = new int[k];
 
    // store to count of element of arr
    // in hash[]
    for (int i = 0; i < n; i++)
    {
        hash[arr[i]]++;
    }
 
    // count the pair whose absolute
    // difference is divisible by k
    for (int i = 0; i < k; i++)
    {
        cnt += (hash[i] * (hash[i] - 1)) / 2;
    }
 
    // print the value of count
    Console.Write(cnt +"\n");
}
 
// Driver Code
public static void Main(String[] args)
{
    // input array
    int []arr = {1, 2, 3, 4};
    int k = 2;
 
    // calculate the size of array
    int n = arr.Length;
    countPair(arr, n, k);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
3

时间复杂度: O( N 2 )
空间复杂度: O(1)

有效的方法:

C++

// Write CPP code here
#include 
using namespace std;
 
// function to Count pairs in an array whose
// absolute difference is divisible by k
void countPair(int arr[], int n, int k)
{
 
    // initialize the count
    int cnt = 0;
 
    // making every element of arr in
    // range 0 to k - 1
    for (int i = 0; i < n; i++) {
        arr[i] = (arr[i] + k) % k;
    }
 
    // create an array hash[]
    int hash[k] = { 0 };
 
    // store to count of element of arr
    // in hash[]
    for (int i = 0; i < n; i++) {
        hash[arr[i]]++;
    }
 
    // count the pair whose absolute
    // difference is divisible by k
    for (int i = 0; i < k; i++) {
        cnt += (hash[i] * (hash[i] - 1)) / 2;
    }
 
    // print the value of count
    cout << cnt << endl;
}
 
// Driver Code
int main()
{
    // input array
    int arr[] = {1, 2, 3, 4};
    int k = 2;
 
    // calculate the size of array
    int n = sizeof(arr) / sizeof(arr[0]);
    countPair(arr, n, k);
    return 0;
}

Java

// JAVA Implementation of above approach
import java.util.*;
 
class GFG
{
 
// function to Count pairs in an array whose
// absolute difference is divisible by k
static void countPair(int arr[], int n, int k)
{
 
    // initialize the count
    int cnt = 0;
 
    // making every element of arr in
    // range 0 to k - 1
    for (int i = 0; i < n; i++)
    {
        arr[i] = (arr[i] + k) % k;
    }
 
    // create an array hash[]
    int hash[] = new int[k];
 
    // store to count of element of arr
    // in hash[]
    for (int i = 0; i < n; i++)
    {
        hash[arr[i]]++;
    }
 
    // count the pair whose absolute
    // difference is divisible by k
    for (int i = 0; i < k; i++)
    {
        cnt += (hash[i] * (hash[i] - 1)) / 2;
    }
 
    // print the value of count
    System.out.print(cnt +"\n");
}
 
// Driver Code
public static void main(String[] args)
{
    // input array
    int arr[] = {1, 2, 3, 4};
    int k = 2;
 
    // calculate the size of array
    int n = arr.length;
    countPair(arr, n, k);
}
}
 
// This code is contributed by PrinciRaj1992

蟒蛇3

# Python3 Implementation of above approach
 
# function to Count pairs in an array whose
# absolute difference is divisible by k
def countPair(arr, n, k):
 
    # initialize the count
    cnt = 0;
 
    # making every element of arr in
    # range 0 to k - 1
    for i in range(n):
        arr[i] = (arr[i] + k) % k;
 
    # create an array hash
    hash = [0]*k;
 
    # store to count of element of arr
    # in hash
    for i in range(n):
        hash[arr[i]] += 1;
 
    # count the pair whose absolute
    # difference is divisible by k
    for i in range(k):
        cnt += (hash[i] * (hash[i] - 1)) / 2;
 
    # prthe value of count
    print(int(cnt));
 
# Driver Code
if __name__ == '__main__':
 
    # input array
    arr = [1, 2, 3, 4];
    k = 2;
 
    # calculate the size of array
    n = len(arr);
    countPair(arr, n, k);
 
# This code is contributed by 29AjayKumar

C#

// C# Implementation of above approach
using System;
 
class GFG
{
 
// function to Count pairs in an array whose
// absolute difference is divisible by k
static void countPair(int []arr, int n, int k)
{
 
    // initialize the count
    int cnt = 0;
 
    // making every element of arr in
    // range 0 to k - 1
    for (int i = 0; i < n; i++)
    {
        arr[i] = (arr[i] + k) % k;
    }
 
    // create an array hash[]
    int []hash = new int[k];
 
    // store to count of element of arr
    // in hash[]
    for (int i = 0; i < n; i++)
    {
        hash[arr[i]]++;
    }
 
    // count the pair whose absolute
    // difference is divisible by k
    for (int i = 0; i < k; i++)
    {
        cnt += (hash[i] * (hash[i] - 1)) / 2;
    }
 
    // print the value of count
    Console.Write(cnt +"\n");
}
 
// Driver Code
public static void Main(String[] args)
{
    // input array
    int []arr = {1, 2, 3, 4};
    int k = 2;
 
    // calculate the size of array
    int n = arr.Length;
    countPair(arr, n, k);
}
}
 
// This code is contributed by 29AjayKumar

Javascript


输出:
2

时间复杂度: O( n+k )
辅助空间: O( k )

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程