📌  相关文章
📜  从绝对差不小于对中最小元素的数组中计数对

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

从绝对差不小于对中最小元素的数组中计数对

给定一个由N个正整数组成的数组arr[] ,任务是找到对(arr[i], arr[j])的数量,使得两个元素之间的绝对差至少等于一对。

例子:

朴素方法:解决给定问题的简单方法是从数组中生成所有可能的对,并对满足给定条件的那些对进行计数。检查所有对后,打印获得的总数。

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

有效的方法:上述方法也可以通过对给定数组进行排序然后迭代两个嵌套循环来优化,这样第一个循环迭代到N ,第二个循环从arr[i] 开始迭代 – (i%arr[i]) j 的增量为j += arr[i]直到N并计算满足给定条件的那些对。请按照以下步骤解决问题:

  • 初始化变量,比如count0 ,存储对的结果计数。
  • 使用变量i迭代范围[0, N]并执行以下步骤:
    • 使用变量j迭代范围[arr[i] – (i%arr[i]), N] ,其中 j 的增量为j += arr[i]并且如果i小于j并且abs(arr[ i] – arr[j])至少是arr[i]arr[j]的最小值,然后将计数增加1
  • 执行上述步骤后,打印count的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the number of pairs
// (i, j) such that abs(a[i]-a[j]) is
// at least the minimum of (a[i], a[j])
int getPairsCount(int arr[], int n)
{
    // Stores the resultant count of pairs
    int count = 0;
 
    // Iterate over the range [0, n]
    for (int i = 0; i < n; i++) {
 
        // Iterate from arr[i] - (i%arr[i])
        // till n with an increment
        // of arr[i]
        for (int j = arr[i] - (i % arr[i]);
             j < n; j += arr[i]) {
 
            // Count the possible pairs
            if (i < j
                && abs(arr[i] - arr[j])
                       >= min(arr[i], arr[j])) {
                count++;
            }
        }
    }
 
    // Return the total count
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 2, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << getPairsCount(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to find the number of pairs
    // (i, j) such that abs(a[i]-a[j]) is
    // at least the minimum of (a[i], a[j])
    static int getPairsCount(int arr[], int n)
    {
       
        // Stores the resultant count of pairs
        int count = 0;
 
        // Iterate over the range [0, n]
        for (int i = 0; i < n; i++) {
 
            // Iterate from arr[i] - (i%arr[i])
            // till n with an increment
            // of arr[i]
            for (int j = arr[i] - (i % arr[i]); j < n;
                 j += arr[i]) {
 
                // Count the possible pairs
                if (i < j
                    && Math.abs(arr[i] - arr[j])
                           >= Math.min(arr[i], arr[j])) {
                    count++;
                }
            }
        }
 
        // Return the total count
        return count;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 2, 3 };
        int N = arr.length;
 
        System.out.println(getPairsCount(arr, N));
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python 3 program for the above approach
 
# Function to find the number of pairs
# (i, j) such that abs(a[i]-a[j]) is
# at least the minimum of (a[i], a[j])
def getPairsCount(arr, n):
   
    # Stores the resultant count of pairs
    count = 0
 
    # Iterate over the range [0, n]
    for i in range(n):
       
        # Iterate from arr[i] - (i%arr[i])
        # till n with an increment
        # of arr[i]
        for j in range(arr[i] - (i % arr[i]),n,arr[i]):
           
            # Count the possible pairs
            if (i < j and abs(arr[i] - arr[j]) >= min(arr[i], arr[j])):
                count += 1
 
    # Return the total count
    return count
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 2, 3]
    N = len(arr)
    print(getPairsCount(arr, N))
     
    # This code is contributed by ipg2016107.


C#
// C# program for above approach
using System;
 
class GFG{
 
    // Function to find the number of pairs
    // (i, j) such that abs(a[i]-a[j]) is
    // at least the minimum of (a[i], a[j])
    static int getPairsCount(int[] arr, int n)
    {
       
        // Stores the resultant count of pairs
        int count = 0;
 
        // Iterate over the range [0, n]
        for (int i = 0; i < n; i++) {
 
            // Iterate from arr[i] - (i%arr[i])
            // till n with an increment
            // of arr[i]
            for (int j = arr[i] - (i % arr[i]); j < n;
                 j += arr[i]) {
 
                // Count the possible pairs
                if (i < j
                    && Math.Abs(arr[i] - arr[j])
                           >= Math.Min(arr[i], arr[j])) {
                    count++;
                }
            }
        }
 
        // Return the total count
        return count;
    }
 
// Driver Code
public static void Main(String[] args)
{
    int[] arr = { 1, 2, 2, 3 };
    int N = arr.Length;
 
    Console.Write(getPairsCount(arr, N));
     
}
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
3

时间复杂度: O(N*log N)
辅助空间: O(1)