📜  满足给定方程的 Array 的无序对 (x, y) 的计数

📅  最后修改于: 2021-09-16 11:01:57             🧑  作者: Mango

给定一个由N 个正负整数组成的数组 arr[] ,我们的任务是找到满足给定方程的无序对 (x, y) 的数量:

  • | × | > = min(| x – y|, | x + y|)
  • |是 | < = max(| x – y|, | x + y|)

例子:

天真的方法:
为了解决上述问题,朴素的方法是遍历所有可能的对,并计算满足这些条件的无序对的数量。但这种方法成本高,需要更多时间,可以进一步优化。

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

有效的方法:
为了优化上述方法,主要思想是对数组进行排序,然后应用二分搜索来找出数组中每个索引的正确边界。我们必须观察到,如果我们将x 改为 – x ,那么 |x| 的值和 |y|保持不变,而 |x – y|和 |x + y|将交换值。这意味着当且仅当 {-x, y} 有效时,一对 {x, y} 有效。同样,我们可以切换 y 的符号。这意味着我们可以用它们的绝对值替换 x 和 y ,并且当且仅当新的一对有效时,原始对有效。

所以问题简化为计算对 {x, y} 和 |x| 的数量<= 2|y|和 |y| <= 2|x| .为了解决这个问题,我们取所有 A[i] 的绝对值并对数组进行排序,该数组是 (l, r) 对数的数组,其中 l < r 且 a[r] < = 2 * a[l]。对于每个固定的 l,我们计算满足此条件的最大 r,并将其与答案相加。

下面是上述方法的实现:

C++
// C++ Program to find the number of
// unordered pairs (x, y) which satisfy
// the given equation for the array
 
#include 
using namespace std;
 
// Return the number of unordered
// pairs satisfying the conditions
int numPairs(int a[], int n)
 
{
    int ans, i, index;
 
    // ans stores the number
    // of unordered pairs
    ans = 0;
 
    // Making each value of
    // array to positive
    for (i = 0; i < n; i++)
        a[i] = abs(a[i]);
 
    // Sort the array
    sort(a, a + n);
 
    // For each index calculating
    // the right boundary for
    // the unordered pairs
    for (i = 0; i < n; i++) {
 
        index = upper_bound(
                    a,
                    a + n,
                    2 * a[i])
                - a;
        ans += index - i - 1;
    }
 
    // Return the final result
    return ans;
}
 
// Driver code
int main()
{
    int a[] = { 3, 6 };
    int n = sizeof(a)
            / sizeof(a[0]);
 
    cout << numPairs(a, n)
         << endl;
 
    return 0;
}


Java
// Java Program to find the number of
// unordered pairs (x, y) which satisfy
// the given equation for the array
import java.util.Arrays;
class GFG{
     
// Return the number of unordered
// pairs satisfying the conditions
static int numPairs(int a[], int n)
{
    int ans, i, index;
 
    // ans stores the number
    // of unordered pairs
    ans = 0;
 
    // Making each value of
    // array to positive
    for (i = 0; i < n; i++)
        a[i] = Math.abs(a[i]);
 
    // Sort the array
    Arrays.sort(a);
 
    // For each index calculating
    // the right boundary for
    // the unordered pairs
    for (i = 0; i < n; i++)
    {
        index = 2;
        ans += index - i - 1;
    }
 
    // Return the final result
    return ans;
}
 
// Driver code
public static void main(String []args)
{
    int a[] = new int[]{ 3, 6 };
    int n = a.length;
 
    System.out.println(numPairs(a, n));
}
}
 
// This code is contributed by rock_cool


Python3
# Python3 program to find the number of
# unordered pairs (x, y) which satisfy
# the given equation for the array
  
# Return the number of unordered
# pairs satisfying the conditions
def numPairs(a, n):
  
    # ans stores the number
    # of unordered pairs
    ans = 0
  
    # Making each value of array
    # to positive
    for i in range(n):
        a[i] = abs(a[i])
  
    # Sort the array
    a.sort()
  
    # For each index calculating
    # the right boundary for
    # the unordered pairs
    for i in range(n):
        index = 0
         
        for j in range(i + 1, n):
            if (2 * a[i] >= a[j - 1] and
                2 * a[i] < a[j]):
                index = j
                 
        if index == 0:
            index = n
             
        ans += index - i - 1
  
    # Return the final result
    return ans
  
# Driver code
a = [ 3, 6 ]
n = len(a)
 
print(numPairs(a, n))
 
# This code is contributed by avanitrachhadiya2155


C#
// C# Program to find the number of
// unordered pairs (x, y) which satisfy
// the given equation for the array
using System;
class GFG{
     
// Return the number of unordered
// pairs satisfying the conditions
static int numPairs(int []a, int n)
{
    int ans, i, index;
 
    // ans stores the number
    // of unordered pairs
    ans = 0;
 
    // Making each value of
    // array to positive
    for (i = 0; i < n; i++)
        a[i] = Math.Abs(a[i]);
 
    // Sort the array
    Array.Sort(a);
 
    // For each index calculating
    // the right boundary for
    // the unordered pairs
    for (i = 0; i < n; i++)
    {
        index = 2;
        ans += index - i - 1;
    }
 
    // Return the final result
    return ans;
}
 
// Driver code
public static void Main()
{
    int []a = new int[]{ 3, 6 };
    int n = a.Length;
 
    Console.Write(numPairs(a, n));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
1

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

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