📌  相关文章
📜  在数组中找到对数(x,y),使得x ^ y> y ^ x |套装2

📅  最后修改于: 2021-04-23 18:22:51             🧑  作者: Mango

给定正整数的两个数组X []Y [] ,找到对的数量,使得x ^ y> y ^ x ,其中x是X []中的元素,而y是Y []中的元素。
例子:

对于朴素的方法[O(M * N)]和[O(N logN + M logN)]方法,请参阅本文的Set 1
高效的方法:可以进一步优化上述两种方法的O(N)时间复杂度
此方法使用后缀和的概念来找到解决方案。我们可以观察到,如果y> x,则x ^ y> y ^ x。但是,需要考虑以下基本情况和例外:

  • 如果x = 0,则可能的y的计数为0。
  • 如果x = 1,则可能的y的计数是0的频率,Y []是必需的答案。
  • 如果x = 2,2 3 <3 2和2 4 = 4 2 。因此,对于x = 2,我们不能有y = {2,3,4}的有效对。因此,Y []中的频率0、1和所有数字gt 4的总和为我们提供了所需有效对的数量
  • 如果x = 3,则Y []中除3之外的所有频率的总和为我们提供了可能的对的所需计数。

请按照以下步骤解决问题:

  • 存储Y数组中每个元素的频率。
  • 存储包含频率的数组的后缀和。
  • 对于不属于任何基本情况的X []中的每个元素x,y的可能数目将是后缀[x + 1] + Y []中的0计数+ Y []中的1计数。对于基本情况,如上所述,相应地计算对。
  • 打印对总数。

下面是上述方法的实现:

C++
// C++ program to finds the number of
// pairs (x, y) from X[] and Y[]
// such that x^y > y^x
 
#include 
using namespace std;
 
// Function to return the count of pairs
int countPairs(int X[], int Y[], int m,
               int n)
{
    vector suffix(1005);
    long long total_pairs = 0;
    for (int i = 0; i < n; i++)
        suffix[Y[i]]++;
 
    // Compute suffix sums till i = 3
    for (int i = 1e3; i >= 3; i--)
        suffix[i] += suffix[i + 1];
 
    for (int i = 0; i < m; i++) {
 
        // Base Case: x = 0
        if (X[i] == 0)
 
            // No valid pairs
            continue;
 
        // Base Case: x = 1
        else if (X[i] == 1) {
 
            // Store the count of 0's
            total_pairs += suffix[0];
            continue;
        }
 
        // Base Case: x = 2
        else if (X[i] == 2)
 
            // Store suffix sum upto 5
            total_pairs += suffix[5];
 
        // Base Case: x = 3
        else if (X[i] == 3)
 
            // Store count of 2 and
            // suffix sum upto 4
            total_pairs += suffix[2]
                           + suffix[4];
 
        // For all other values of x
        else
            total_pairs += suffix[X[i] + 1];
 
        // For all x >=2, every y = 0
        // and every y = 1 makes a valid pair
        total_pairs += suffix[0] + suffix[1];
    }
 
    // Return the count of pairs
    return total_pairs;
}
 
// Driver Program
int main()
{
    int X[] = { 10, 19, 18 };
    int Y[] = { 11, 15, 9 };
 
    int m = sizeof(X) / sizeof(X[0]);
    int n = sizeof(Y) / sizeof(Y[0]);
 
    cout << countPairs(X, Y, m, n);
 
    return 0;
}


Java
// Java program to finds the number of
// pairs (x, y) from X[] and Y[]
// such that x^y > y^x
class GFG{
     
// Function to return the count of pairs
static int countPairs(int X[], int Y[], int m,
                      int n)
{
    int []suffix = new int[1005];
    long total_pairs = 0;
     
    for(int i = 0; i < n; i++)
        suffix[Y[i]]++;
     
    // Compute suffix sums till i = 3
    for(int i = (int)1e3; i >= 3; i--)
        suffix[i] += suffix[i + 1];
     
    for(int i = 0; i < m; i++)
    {
         
        // Base Case: x = 0
        if (X[i] == 0)
     
            // No valid pairs
            continue;
     
        // Base Case: x = 1
        else if (X[i] == 1)
        {
     
            // Store the count of 0's
            total_pairs += suffix[0];
            continue;
        }
     
        // Base Case: x = 2
        else if (X[i] == 2)
     
            // Store suffix sum upto 5
            total_pairs += suffix[5];
     
        // Base Case: x = 3
        else if (X[i] == 3)
     
            // Store count of 2 and
            // suffix sum upto 4
            total_pairs += suffix[2] +
                           suffix[4];
     
        // For all other values of x
        else
            total_pairs += suffix[X[i] + 1];
     
        // For all x >=2, every y = 0
        // and every y = 1 makes a valid pair
        total_pairs += suffix[0] + suffix[1];
    }
     
    // Return the count of pairs
    return (int) total_pairs;
}
     
// Driver code
public static void main(String[] args)
{
    int X[] = { 10, 19, 18 };
    int Y[] = { 11, 15, 9 };
     
    int m = X.length;
    int n = Y.length;
     
    System.out.print(countPairs(X, Y, m, n));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to finds the number of
# pairs (x, y) from X[] and Y[]
# such that x^y > y^x
 
# Function to return the count of pairs
def countPairs(X, Y, m, n):
 
    suffix = [0] * 1005
    total_pairs = 0
 
    for i in range(n):
        suffix[Y[i]] += 1
 
    # Compute suffix sums till i = 3
    for i in range(int(1e3), 2, -1 ):
        suffix[i] += suffix[i + 1]
 
    for i in range(m):
 
        # Base Case: x = 0
        if(X[i] == 0):
             
            # No valid pairs
            continue
 
        # Base Case: x = 1
        elif(X[i] == 1):
 
            # Store the count of 0's
            total_pairs += suffix[0]
            continue
 
        # Base Case: x = 2
        elif(X[i] == 2):
 
            # Store suffix sum upto 5
            total_pairs += suffix[5]
 
        # Base Case: x = 3
        elif(X[i] == 3):
 
            # Store count of 2 and
            # suffix sum upto 4
            total_pairs += (suffix[2] +
                            suffix[4])
 
        # For all other values of x
        else:
            total_pairs += suffix[X[i] + 1]
 
        # For all x >=2, every y = 0
        # and every y = 1 makes a valid pair
        total_pairs += suffix[0] + suffix[1]
 
    # Return the count of pairs
    return total_pairs
 
# Driver code
if __name__ == '__main__':
 
    X = [ 10, 19, 18 ]
    Y = [ 11, 15, 9 ]
 
    m = len(X)
    n = len(Y)
 
    print(countPairs(X, Y, m, n))
 
# This code is contributed by Shivam Singh


C#
// C# program to finds the number of
// pairs (x, y) from []X and []Y
// such that x^y > y^x
using System;
class GFG{
 
    // Function to return the count of pairs
    static int countPairs(int[] X, int[] Y, int m, int n)
    {
        int[] suffix = new int[1005];
        long total_pairs = 0;
        for (int i = 0; i < n; i++)
            suffix[Y[i]]++;
 
        // Compute suffix sums till i = 3
        for (int i = (int)1e3; i >= 3; i--)
            suffix[i] += suffix[i + 1];
 
        for (int i = 0; i < m; i++)
        {
 
            // Base Case: x = 0
            if (X[i] == 0)
 
                // No valid pairs
                continue;
 
            // Base Case: x = 1
            else if (X[i] == 1)
            {
 
                // Store the count of 0's
                total_pairs += suffix[0];
                continue;
            }
 
            // Base Case: x = 2
            else if (X[i] == 2)
 
                // Store suffix sum upto 5
                total_pairs += suffix[5];
 
            // Base Case: x = 3
            else if (X[i] == 3)
 
                // Store count of 2 and
                // suffix sum upto 4
                total_pairs += suffix[2] + suffix[4];
 
            // For all other values of x
            else
                total_pairs += suffix[X[i] + 1];
 
            // For all x >=2, every y = 0
            // and every y = 1 makes a valid pair
            total_pairs += suffix[0] + suffix[1];
        }
 
        // Return the count of pairs
        return (int)total_pairs;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int[] X = {10, 19, 18};
        int[] Y = {11, 15, 9};
        int m = X.Length;
        int n = Y.Length;
        Console.Write(countPairs(X, Y, m, n));
    }
}
 
// This code is contributed by Amit Katiyar


输出:
2



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