📌  相关文章
📜  具有相同奇偶校验的所有可能的数组元素对计数

📅  最后修改于: 2021-05-04 22:17:27             🧑  作者: Mango

给定整数数组A [] ,任务是找到对总数,以使每对包含偶数或奇数元素。仅当i!= j时,才能形成有冠词对( A [i]A [j] )。

例子:

天真的方法:
最简单的方法是生成所有可能的对。对于每对,检查两个元素是否都是奇数或均是偶数。如果是这样,请增加一个计数器。最终的数量将是必需的答案。

时间复杂度: O(N 2 )

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to return the answer
int countPairs(int A[], int n)
{
    int count = 0, i, j;
     
    // Generate all possible pairs
    for(i = 0; i < n; i++)
    {
        for(j = i + 1; j < n; j++)
        {
             
            // Increment the count if
            // both even or both odd
            if ((A[i] % 2 == 0 &&
                 A[j] % 2 == 0) ||
                (A[i] % 2 != 0 &&
                 A[j] % 2 != 0))
                count++;
        }
    }
    return count;
}
 
// Driver Code
int main()
{
    int A[] = { 1, 2, 3, 1, 3 };
    int n = sizeof(A) / sizeof(int);
     
    cout << countPairs(A, n);
}
 
// This code is contributed by jrishabh99


Java
// Java program for above approach
 
import java.util.*;
 
class GFG {
 
    static int countPairs(
        int[] A, int n)
    {
        int count = 0, i, j;
 
        // Generate all possible pairs
        for (i = 0; i < n; i++) {
            for (j = i + 1; j < n; j++) {
 
                // Increment the count if
                // both even or both odd
                if ((A[i] % 2 == 0
                     && A[j] % 2 == 0)
                    || (A[i] % 2 != 0
                        && A[j] % 2 != 0))
                    count++;
            }
        }
        return count;
    }
 
    // Driver Code
    public static void main(
        String[] args)
    {
        int[] A = { 1, 2, 3, 1, 3 };
        int n = A.length;
        System.out.println(
            countPairs(A, n));
    }
}


Python3
# Python3 program for
# the above approach
 
# Function to return the answer
def countPairs(A, n):
 
    count = 0
     
    # Generate all possible pairs
    for i in range (n):
        for j in range (i + 1, n):
       
            # Increment the count if
            # both even or both odd
            if ((A[i] % 2 == 0 and
                 A[j] % 2 == 0) or
                (A[i] % 2 != 0 and
                 A[j] % 2 != 0)):
                count += 1
     
    return count
 
# Driver Code
if __name__ == "__main__":
   
    A = [1, 2, 3, 1, 3]
    n = len(A)
    print(countPairs(A, n))
     
# This code is contributed by Chitranayal


C#
// C# program for above approach
using System;
class GFG{
 
static int countPairs(int[] A, int n)
{
    int count = 0, i, j;
 
    // Generate all possible pairs
    for (i = 0; i < n; i++)
    {
        for (j = i + 1; j < n; j++)
        {
 
            // Increment the count if
            // both even or both odd
            if ((A[i] % 2 == 0 && A[j] % 2 == 0) ||
                (A[i] % 2 != 0 && A[j] % 2 != 0))
                count++;
        }
    }
    return count;
}
 
// Driver Code
public static void Main(String[] args)
{
    int[] A = { 1, 2, 3, 1, 3 };
    int n = A.Length;
    Console.Write(countPairs(A, n));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
int countPairs(int A[], int n)
{
     
    // Store count of
    // even and odd elements
    int even = 0, odd = 0;
     
    for(int i = 0; i < n; i++)
    {
        if (A[i] % 2 == 0)
            even++;
        else
            odd++;
    }
 
    return (even * (even - 1)) / 2 +
             (odd * (odd - 1)) / 2;
}
 
// Driver code
int main()
{
    int A[] = { 1, 2, 3, 1, 3 };
    int n = sizeof(A) / sizeof(int);
     
    cout << countPairs(A, n);
}
 
// This code is contributed by jrishabh99


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG {
 
    static int countPairs(
        int[] A, int n)
    {
        // Store count of
        // even and odd elements
        int even = 0, odd = 0;
 
        for (int i = 0; i < n; i++) {
 
            if (A[i] % 2 == 0)
                even++;
            else
                odd++;
        }
 
        return (even * (even - 1)) / 2
            + (odd * (odd - 1)) / 2;
    }
 
    // Driver Program
    public static void main(
        String[] args)
    {
 
        int[] A = { 1, 2, 3, 1, 3 };
        int n = A.length;
        System.out.println(
            countPairs(A, n));
    }
}


Python3
# Python3 program for the above approach
 
# Function to return count of pairs
def countPairs(A, n):
     
    # Store count of
    # even and odd elements
    even, odd = 0, 0
     
    for i in range(0, n):
        if A[i] % 2 == 0:
            even = even + 1
        else:
            odd = odd + 1
 
    return ((even * (even - 1)) // 2 +
              (odd * (odd - 1)) // 2)
 
# Driver code
A = [ 1, 2, 3, 1, 3 ]
n = len(A)
 
print(countPairs(A, n))
 
# This code is contributed by jrishabh99


C#
// C# program for the above approach
using System;
 
class GFG{
 
static int countPairs(int[] A, int n)
{
     
    // Store count of
    // even and odd elements
    int even = 0, odd = 0;
 
    for(int i = 0; i < n; i++)
    {
    if (A[i] % 2 == 0)
        even++;
    else
        odd++;
    }
 
    return (even * (even - 1)) / 2 +
            (odd * (odd - 1)) / 2;
}
 
// Driver code
public static void Main()
{
    int[] A = { 1, 2, 3, 1, 3 };
    int n = A.Length;
     
    Console.Write(countPairs(A, n));
}
}
 
// This code is contributed by nidhi_biet


Javascript


输出:
6

高效方法:
遍历数组并计数并将偶数和奇数存储在数组中,并根据各自的计数计算可能的对,并显示其总和。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
int countPairs(int A[], int n)
{
     
    // Store count of
    // even and odd elements
    int even = 0, odd = 0;
     
    for(int i = 0; i < n; i++)
    {
        if (A[i] % 2 == 0)
            even++;
        else
            odd++;
    }
 
    return (even * (even - 1)) / 2 +
             (odd * (odd - 1)) / 2;
}
 
// Driver code
int main()
{
    int A[] = { 1, 2, 3, 1, 3 };
    int n = sizeof(A) / sizeof(int);
     
    cout << countPairs(A, n);
}
 
// This code is contributed by jrishabh99

Java

// Java program for the above approach
 
import java.util.*;
 
class GFG {
 
    static int countPairs(
        int[] A, int n)
    {
        // Store count of
        // even and odd elements
        int even = 0, odd = 0;
 
        for (int i = 0; i < n; i++) {
 
            if (A[i] % 2 == 0)
                even++;
            else
                odd++;
        }
 
        return (even * (even - 1)) / 2
            + (odd * (odd - 1)) / 2;
    }
 
    // Driver Program
    public static void main(
        String[] args)
    {
 
        int[] A = { 1, 2, 3, 1, 3 };
        int n = A.length;
        System.out.println(
            countPairs(A, n));
    }
}

Python3

# Python3 program for the above approach
 
# Function to return count of pairs
def countPairs(A, n):
     
    # Store count of
    # even and odd elements
    even, odd = 0, 0
     
    for i in range(0, n):
        if A[i] % 2 == 0:
            even = even + 1
        else:
            odd = odd + 1
 
    return ((even * (even - 1)) // 2 +
              (odd * (odd - 1)) // 2)
 
# Driver code
A = [ 1, 2, 3, 1, 3 ]
n = len(A)
 
print(countPairs(A, n))
 
# This code is contributed by jrishabh99

C#

// C# program for the above approach
using System;
 
class GFG{
 
static int countPairs(int[] A, int n)
{
     
    // Store count of
    // even and odd elements
    int even = 0, odd = 0;
 
    for(int i = 0; i < n; i++)
    {
    if (A[i] % 2 == 0)
        even++;
    else
        odd++;
    }
 
    return (even * (even - 1)) / 2 +
            (odd * (odd - 1)) / 2;
}
 
// Driver code
public static void Main()
{
    int[] A = { 1, 2, 3, 1, 3 };
    int n = A.Length;
     
    Console.Write(countPairs(A, n));
}
}
 
// This code is contributed by nidhi_biet

Java脚本


输出:
6

时间复杂度: O(N)