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

📅  最后修改于: 2021-05-14 07:32:12             🧑  作者: Mango

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

  • | x | > = min(| x – y |,| x + y |)
  • | 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 |计算对{x,y}的对数<= 2 | y |和| y | <= 2 | x | 。为了解决这个问题,我们取所有A [i]的绝对值并对数组进行排序,该数组是l

下面是上述方法的实现:

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)