📜  查找给定数组中偶数对的计数

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

给定数组arr [] ,任务是查找数组中的偶数对数。
例子:

天真的方法:

  1. 运行两个嵌套循环以获取数组中所有可能的数字对。
  2. 对于每对,检查a [i]是否为偶数,而a [j]是否为奇数。
  3. 如果是这样,则将所需的计数增加一。
  4. 检查所有对后,最后打印计数。

下面是上述方法的实现:

C++
// C++ program to count the pairs in array
// of the form (even, odd)
 
#include 
using namespace std;
 
// Function to count the pairs in array
// of the form (even, odd)
int findCount(int arr[], int n)
{
    // variable to store count of such pairs
    int res = 0;
 
    // Iterate through all pairs
    for (int i = 0; i < n - 1; i++)
        for (int j = i + 1; j < n; j++)
 
            // Increment count
            // if condition is satisfied
            if ((arr[i] % 2 == 0)
                && (arr[j] % 2 == 1)) {
                res++;
            }
 
    return res;
}
 
// Driver code
int main()
{
    int a[] = { 5, 4, 1, 2, 3 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << findCount(a, n);
    return 0;
}


Java
// Java program to count the pairs in array
// of the form (even, odd)
import java.util.*;
 
class GFG{
 
// Function to count the pairs in array
// of the form (even, odd)
static int findCount(int arr[], int n)
{
    // Variable to store count of such pairs
    int res = 0;
 
    // Iterate through all pairs
    for (int i = 0; i < n - 1; i++)
        for (int j = i + 1; j < n; j++)
 
            // Increment count
            // if condition is satisfied
            if ((arr[i] % 2 == 0) &&
                (arr[j] % 2 == 1))
            {
                res++;
            }
    return res;
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 5, 4, 1, 2, 3 };
    int n = a.length;
    System.out.print(findCount(a, n));
}
}
 
// This code is contributed by Rohit_ranjan


Python3
# Python3 program to count the pairs
# in array of the form (even, odd)
 
# Function to count the pairs in 
# array of the form (even, odd)
def findCount(arr, n):
 
    # Variable to store count
    # of such pairs
    res = 0
 
    # Iterate through all pairs
    for i in range(0, n - 1):
        for j in range(i + 1, n):
 
            # Increment count
            # if condition is satisfied
            if ((arr[i] % 2 == 0) and
                (arr[j] % 2 == 1)):
                res = res + 1
 
    return res
 
# Driver code
a = [ 5, 4, 1, 2, 3 ]
n = len(a)
 
print(findCount(a, n))
 
# This code is contributed by PratikBasu


C#
// C# program to count the pairs in array
// of the form (even, odd)
using System;
 
class GFG{
 
// Function to count the pairs in array
// of the form (even, odd)
static int findCount(int []arr, int n)
{
 
    // Variable to store count of such pairs
    int res = 0;
 
    // Iterate through all pairs
    for(int i = 0; i < n - 1; i++)
       for(int j = i + 1; j < n; j++)
        
          // Increment count
          // if condition is satisfied
          if ((arr[i] % 2 == 0) &&
              (arr[j] % 2 == 1))
          {
              res++;
          }
           
    return res;
}
 
// Driver code
public static void Main(String[] args)
{
    int []a = { 5, 4, 1, 2, 3 };
    int n = a.Length;
     
    Console.Write(findCount(a, n));
}
}
 
// This code is contributed by Rohit_ranjan


Javascript


C++
// C++ program to count the pairs in array
// of the form (even, odd)
 
#include 
using namespace std;
 
// Function to count the pairs in array
// of the form (even, odd)
int findCount(int arr[], int n)
{
    int count = 0, ans = 0;
    for (int i = 0; i < n; i++) {
 
        // check if number is even or not
        if (arr[i] % 2 == 0)
            count++;
        else
            ans = ans + count;
    }
    return ans;
}
 
// Driver code
int main()
{
    int a[] = { 5, 4, 1, 2, 3 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << findCount(a, n);
    return 0;
}


Java
// Java program to count the pairs
// in array of the form (even, odd)
class GFG{
 
// Function to count the pairs in 
// array of the form (even, odd)
static int findCount(int arr[], int n)
{
    int count = 0, ans = 0;
    for(int i = 0; i < n; i++)
    {
         
       // Check if number is even
       // or not
       if (arr[i] % 2 == 0)
       {
           count++;
       }
       else
       {
           ans = ans + count;
       }
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 5, 4, 1, 2, 3 };
    int n = a.length;
     
    System.out.print(findCount(a, n));
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program to count the pairs
# in array of the form (even, odd)
 
# Function to count the pairs in
# array of the form (even, odd)
def findCount(arr, n):
 
    count = 0
    ans = 0
     
    for i in range(0, n):
 
        # Check if number is even or not
        if (arr[i] % 2 == 0):
            count = count + 1
        else:
            ans = ans + count
             
    return ans
 
# Driver code
a = [ 5, 4, 1, 2, 3 ]
n = len(a)
 
print(findCount(a, n))
 
# This code is contributed by PratikBasu


C#
// C# program to count the pairs in
// array of the form (even, odd)
using System;
 
class GFG{
 
// Function to count the pairs in
// array of the form (even, odd)
static int findCount(int []arr, int n)
{
    int count = 0, ans = 0;
    for(int i = 0; i < n; i++)
    {
 
       // Check if number is even or not
       if (arr[i] % 2 == 0)
       {
           count++;  
       }
       else
       {
           ans = ans + count;
       }
    }
    return ans;
}
 
// Driver code
public static void Main()
{
    int []a = { 5, 4, 1, 2, 3 };
    int n = a.Length;
     
    Console.WriteLine(findCount(a, n));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
3

时间复杂度: O(n 2 )

辅助空间: O(1)

高效方法:

  1. 对于从索引0开始的每个元素,我们将检查其是否为偶数。
  2. 如果是的话,我们将计数增加1。
  3. 否则,我们将按计数增加最终答案。

下面是上述方法的实现:

C++

// C++ program to count the pairs in array
// of the form (even, odd)
 
#include 
using namespace std;
 
// Function to count the pairs in array
// of the form (even, odd)
int findCount(int arr[], int n)
{
    int count = 0, ans = 0;
    for (int i = 0; i < n; i++) {
 
        // check if number is even or not
        if (arr[i] % 2 == 0)
            count++;
        else
            ans = ans + count;
    }
    return ans;
}
 
// Driver code
int main()
{
    int a[] = { 5, 4, 1, 2, 3 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << findCount(a, n);
    return 0;
}

Java

// Java program to count the pairs
// in array of the form (even, odd)
class GFG{
 
// Function to count the pairs in 
// array of the form (even, odd)
static int findCount(int arr[], int n)
{
    int count = 0, ans = 0;
    for(int i = 0; i < n; i++)
    {
         
       // Check if number is even
       // or not
       if (arr[i] % 2 == 0)
       {
           count++;
       }
       else
       {
           ans = ans + count;
       }
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 5, 4, 1, 2, 3 };
    int n = a.length;
     
    System.out.print(findCount(a, n));
}
}
 
// This code is contributed by amal kumar choubey

Python3

# Python3 program to count the pairs
# in array of the form (even, odd)
 
# Function to count the pairs in
# array of the form (even, odd)
def findCount(arr, n):
 
    count = 0
    ans = 0
     
    for i in range(0, n):
 
        # Check if number is even or not
        if (arr[i] % 2 == 0):
            count = count + 1
        else:
            ans = ans + count
             
    return ans
 
# Driver code
a = [ 5, 4, 1, 2, 3 ]
n = len(a)
 
print(findCount(a, n))
 
# This code is contributed by PratikBasu

C#

// C# program to count the pairs in
// array of the form (even, odd)
using System;
 
class GFG{
 
// Function to count the pairs in
// array of the form (even, odd)
static int findCount(int []arr, int n)
{
    int count = 0, ans = 0;
    for(int i = 0; i < n; i++)
    {
 
       // Check if number is even or not
       if (arr[i] % 2 == 0)
       {
           count++;  
       }
       else
       {
           ans = ans + count;
       }
    }
    return ans;
}
 
// Driver code
public static void Main()
{
    int []a = { 5, 4, 1, 2, 3 };
    int n = a.Length;
     
    Console.WriteLine(findCount(a, n));
}
}
 
// This code is contributed by Code_Mech

Java脚本


输出:
3

时间复杂度: O(n)

辅助空间: O(1)