📌  相关文章
📜  使得x <y的数组中(x,y)对的计数

📅  最后修改于: 2021-04-29 12:39:45             🧑  作者: Mango

给定N个不同整数的数组,任务是找到对(x,y)的对数,以使x

例子:

天真的方法:找到每个可能的对,并检查它们是否满足给定条件。

C++
// C++ implementation of the approach 
#include 
using namespace std;
  
// Function to return the number of 
// pairs (x, y) such that x < y 
int getPairs(int a[],int n) 
{ 
    // To store the number of valid pairs 
    int count = 0; 
    for (int i = 0; i < n; i++) 
    { 
        for (int j = 0; j < n; j++) 
        { 
  
            // If a valid pair is found 
            if (a[i] < a[j]) 
                count++; 
        } 
    } 
  
    // Return the count of valid pairs 
    return count; 
} 
  
// Driver code 
int main() 
{ 
    int a[] = { 2, 4, 3, 1 }; 
    int n = sizeof(a) / sizeof(a[0]);
    cout << getPairs(a, n); 
    return 0;
} 
      
// This code is contributed by SHUBHAMSINGH10


Java
// Java implementation of the approach
class GFG {
  
    // Function to return the number of
    // pairs (x, y) such that x < y
    static int getPairs(int a[])
    {
        // To store the number of valid pairs
        int count = 0;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length; j++) {
  
                // If a valid pair is found
                if (a[i] < a[j])
                    count++;
            }
        }
  
        // Return the count of valid pairs
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int a[] = { 2, 4, 3, 1 };
        System.out.println(getPairs(a));
    }
}


Python 3
# Python3 implementation of the approach
  
# Function to return the number of
# pairs (x, y) such that x < y
def getPairs(a):
      
    # To store the number of valid pairs
    count = 0
    for i in range(len(a)):
        for j in range(len(a)):
  
            # If a valid pair is found
            if (a[i] < a[j]):
                count += 1
  
    # Return the count of valid pairs
    return count
  
# Driver code
if __name__ == "__main__":
  
    a = [ 2, 4, 3, 1 ]
    print(getPairs(a))
  
# This code is contributed by ita_c


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
  
    // Function to return the number of 
    // pairs (x, y) such that x < y 
    static int getPairs(int []a) 
    { 
        // To store the number of valid pairs 
        int count = 0; 
        for (int i = 0; i < a.Length; i++) 
        { 
            for (int j = 0; j < a.Length; j++) 
            { 
  
                // If a valid pair is found 
                if (a[i] < a[j]) 
                    count++; 
            } 
        } 
  
        // Return the count of valid pairs 
        return count; 
    } 
  
    // Driver code 
    public static void Main() 
    { 
        int []a = { 2, 4, 3, 1 }; 
        Console.WriteLine(getPairs(a)); 
    } 
} 
  
// This code is contributed by Ryuga


PHP


C++
// C++ implementation of the approach
#include  
using namespace std; 
  
// Function to return the number of
// pairs (x, y) such that x < y
int getPairs(int a[])
{
    // Length of the array
    int n = sizeof(a[0]);
      
    // Calculate the number valid pairs
    int count = (n * (n - 1)) / 2;
  
    // Return the count of valid pairs
    return count;
}
  
// Driver code
int main()
{
    int a[] = { 2, 4, 3, 1 };
    cout << getPairs(a);
    return 0;
}
  
// This code is contributed 
// by SHUBHAMSINGH10


Java
// Java implementation of the approach
class GFG {
  
    // Function to return the number of
    // pairs (x, y) such that x < y
    static int getPairs(int a[])
    {
        // Length of the array
        int n = a.length;
  
        // Calculate the number of valid pairs
        int count = (n * (n - 1)) / 2;
  
        // Return the count of valid pairs
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int a[] = { 2, 4, 3, 1 };
        System.out.println(getPairs(a));
    }
}


Python
# Python implementation of the approach 
  
# Function to return the number of 
# pairs (x, y) such that x < y 
def getPairs(a):
      
    # Length of the array 
    n = len(a)
      
    # Calculate the number of valid pairs 
    count = (n * (n - 1)) // 2
      
    # Return the count of valid pairs 
    return count
      
# Driver code 
a = [2, 4, 3, 1]
print(getPairs(a)) 
  
# This code is contributed by SHUBHAMSINGH10


C#
// C# implementation of the approach
using System;
class GFG 
{
  
    // Function to return the number of
    // pairs (x, y) such that x < y
    static int getPairs(int []a)
    {
        // Length of the array
        int n = a.Length;
  
        // Calculate the number of valid pairs
        int count = (n * (n - 1)) / 2;
  
        // Return the count of valid pairs
        return count;
    }
  
    // Driver code
    public static void Main()
    {
        int []a = { 2, 4, 3, 1 };
        Console.Write(getPairs(a));
    }
}
  
// This code is contributed 
// by Akanksha Rai


输出:
6

时间复杂度: O(n 2 )

高效的方法:对于元素x 。为了找到形式为(x,y1)(x,y2) ,…, (x,yn)的有效对的计数,我们需要计算大于x的元素。对于最小的元素,将比其大n – 1个元素。同样,第二个最小的元素可以形成n – 2对,依此类推。因此,有效对的期望计数将为(n – 1)+(n – 2)+…。 + 1 = n *(n – 1)/ 2其中n是数组的长度。

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include  
using namespace std; 
  
// Function to return the number of
// pairs (x, y) such that x < y
int getPairs(int a[])
{
    // Length of the array
    int n = sizeof(a[0]);
      
    // Calculate the number valid pairs
    int count = (n * (n - 1)) / 2;
  
    // Return the count of valid pairs
    return count;
}
  
// Driver code
int main()
{
    int a[] = { 2, 4, 3, 1 };
    cout << getPairs(a);
    return 0;
}
  
// This code is contributed 
// by SHUBHAMSINGH10

Java

// Java implementation of the approach
class GFG {
  
    // Function to return the number of
    // pairs (x, y) such that x < y
    static int getPairs(int a[])
    {
        // Length of the array
        int n = a.length;
  
        // Calculate the number of valid pairs
        int count = (n * (n - 1)) / 2;
  
        // Return the count of valid pairs
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int a[] = { 2, 4, 3, 1 };
        System.out.println(getPairs(a));
    }
}

Python

# Python implementation of the approach 
  
# Function to return the number of 
# pairs (x, y) such that x < y 
def getPairs(a):
      
    # Length of the array 
    n = len(a)
      
    # Calculate the number of valid pairs 
    count = (n * (n - 1)) // 2
      
    # Return the count of valid pairs 
    return count
      
# Driver code 
a = [2, 4, 3, 1]
print(getPairs(a)) 
  
# This code is contributed by SHUBHAMSINGH10

C#

// C# implementation of the approach
using System;
class GFG 
{
  
    // Function to return the number of
    // pairs (x, y) such that x < y
    static int getPairs(int []a)
    {
        // Length of the array
        int n = a.Length;
  
        // Calculate the number of valid pairs
        int count = (n * (n - 1)) / 2;
  
        // Return the count of valid pairs
        return count;
    }
  
    // Driver code
    public static void Main()
    {
        int []a = { 2, 4, 3, 1 };
        Console.Write(getPairs(a));
    }
}
  
// This code is contributed 
// by Akanksha Rai
输出:
6

时间复杂度: O(1)