给定一个数组arr [] ,任务是对对进行计数,以使每对(arr [i],arr [j])中至少包含一个偶数元素,其中i!= j 。
例子:
Input: arr[] = {1, 2, 3, 1, 3}
Output: 4
Explanation:
Possible pairs are: (1, 2), (2, 3), (2, 1), (2, 3).
Input: arr[] = {8, 2, 3, 1, 4, 2}
Output: 14
Explanation:
Possible pairs are: (8, 2), (8, 3), (8, 1), (8, 4), (8, 2), (2, 3), (2, 1), (2, 4), (2, 2), (3, 4), (3, 2), (1, 4), (1, 2), (4, 2).
一种简单的方法是运行两个循环。一对一地选取每个元素,并为每个元素在数组右侧找到保持条件的元素,然后递增计数。
时间复杂度:
下面是上述方法的实现:
C++
// C++ implementation to count
// pairs in an array such that
// each pair contains at
// least one even element
#include
using namespace std;
// Function to count the pairs in
// the array such as there is at
// least one even element in each pair
int CountPairs(int arr[], int n)
{
int count = 0;
// Generate all possible pairs
// and increment then count
// if the condition is satisfied
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
if (arr[i] % 2 == 0 ||
arr[j] % 2 == 0)
count++;
}
}
return count;
}
// Driver code
int main()
{
int arr[] = { 8, 2, 3, 1, 4, 2 };
int n = sizeof(arr) / sizeof(int);
// Function call
cout << (CountPairs(arr, n));
}
// This code is contributed by rock_cool
Java
// Java implementation to Count
// pairs in an array such that
// each pair contains at
// least one even element
import java.util.*;
class GFG {
// Function to count the pairs in
// the array such as there is at
// least one even element in each pair
static int CountPairs(int[] arr, int n)
{
int count = 0;
// Generate all possible pairs
// and increment then count
// if the condition is satisfied
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] % 2 == 0
|| arr[j] % 2 == 0)
count++;
}
}
return count;
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 8, 2, 3, 1, 4, 2 };
int n = arr.length;
// Function Call
System.out.println(CountPairs(arr, n));
}
}
Python3
# Python3 implementation to count
# pairs in an array such that
# each pair contains at
# least one even element
def CountPairs(arr, n):
count = 0
# Generate all possible pairs
# and increment then count
# if the condition is satisfied
for i in range(n):
for j in range(i + 1, n):
if (arr[i] % 2 == 0 or
arr[j] % 2 == 0):
count += 1
return count
# Driver code
arr = [ 8, 2, 3, 1, 4, 2 ]
n = len(arr)
# Function call
print(CountPairs(arr, n))
# This code is contributed by rutvik_56
C#
// C# implementation to count
// pairs in an array such that
// each pair contains at
// least one even element
using System;
class GFG{
// Function to count the pairs in
// the array such as there is at
// least one even element in each pair
static int CountPairs(int[] arr, int n)
{
int count = 0;
// Generate all possible pairs
// and increment then count
// if the condition is satisfied
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
if (arr[i] % 2 == 0 ||
arr[j] % 2 == 0)
count++;
}
}
return count;
}
// Driver code
public static void Main(String[] args)
{
int[] arr = { 8, 2, 3, 1, 4, 2 };
int n = arr.Length;
// Function Call
Console.WriteLine(CountPairs(arr, n));
}
}
// This code is contributed by PrinciRaj1992
Javascript
C++
// C++ implementation to Count
// pairs in an array such that
// each pair contains at
// least one even element
#include
using namespace std;
// Function to count the pairs in
// the array such as there is at
// least one even element in each pair
int CountPairs(int arr[], int n)
{
// Store count of even
// and odd elements
int even = 0, odd = 0;
for(int i = 0; i < n; i++)
{
// Check element is
// even or odd
if (arr[i] % 2 == 0)
even++;
else
odd++;
}
return (even * (even - 1)) / 2 +
(even * odd);
}
// Driver Code
int main()
{
int arr[] = { 8, 2, 3, 1, 4, 2 };
int n = sizeof(arr) / sizeof(int);
cout << CountPairs(arr, n);
}
// This code is contributed by jrishabh99
Java
// Java implementation to Count
// pairs in an array such that
// each pair contains at
// least one even element
import java.util.*;
class GFG {
// Function to count the pairs in
// the array such as there is at
// least one even element in each pair
static int CountPairs(int[] arr, int n)
{
// strore count of even
// and odd elements
int even = 0, odd = 0;
for (int i = 0; i < n; i++) {
// check element is
// even or odd
if (arr[i] % 2 == 0)
even++;
else
odd++;
}
return (even * (even - 1)) / 2
+ (even * odd);
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 8, 2, 3, 1, 4, 2 };
int n = arr.length;
System.out.println(CountPairs(arr, n));
}
}
Python3
# Python3 implementation to count
# pairs in an array such that
# each pair contains at
# least one even element
# Function to count the pairs in
# the array such as there is at
# least one even element in each pair
def CountPairs(arr, n):
# Store count of even
# and odd elements
even = 0
odd = 0
for i in range(n):
# Check element is
# even or odd
if (arr[i] % 2 == 0):
even += 1
else:
odd += 1
return ((even * (even - 1)) // 2 +
(even * odd))
# Driver Code
arr = [ 8, 2, 3, 1, 4, 2 ]
n = len(arr)
print(CountPairs(arr, n))
# This code is contributed by code_hunt
C#
// C# implementation to Count
// pairs in an array such that
// each pair contains at
// least one even element
using System;
class GFG{
// Function to count the pairs in
// the array such as there is at
// least one even element in each pair
static int CountPairs(int[] arr, int n)
{
// Store count of even
// and odd elements
int even = 0, odd = 0;
for(int i = 0; i < n; i++)
{
// Check element is
// even or odd
if (arr[i] % 2 == 0)
even++;
else
odd++;
}
return (even * (even - 1)) / 2 +
(even * odd);
}
// Driver Code
public static void Main()
{
int[] arr = { 8, 2, 3, 1, 4, 2 };
int n = arr.Length;
Console.Write(CountPairs(arr, n));
}
}
// This code is contributed by Nidhi_biet
Javascript
输出:
14
高效的方法:想法是对数组中的偶数和奇数元素进行计数,并包括仅具有一个偶数元素的对,或者将这两个对都视为偶数元素。
- 偶数正好为一个的对:偶数正好为一个的对的计数为:
- 偶数正好为两个的对:偶数正好为两个的对的计数为:
因此,具有至少一个偶数元素的对的数量为
下面是上述方法的实现:
C++
// C++ implementation to Count
// pairs in an array such that
// each pair contains at
// least one even element
#include
using namespace std;
// Function to count the pairs in
// the array such as there is at
// least one even element in each pair
int CountPairs(int arr[], int n)
{
// Store count of even
// and odd elements
int even = 0, odd = 0;
for(int i = 0; i < n; i++)
{
// Check element is
// even or odd
if (arr[i] % 2 == 0)
even++;
else
odd++;
}
return (even * (even - 1)) / 2 +
(even * odd);
}
// Driver Code
int main()
{
int arr[] = { 8, 2, 3, 1, 4, 2 };
int n = sizeof(arr) / sizeof(int);
cout << CountPairs(arr, n);
}
// This code is contributed by jrishabh99
Java
// Java implementation to Count
// pairs in an array such that
// each pair contains at
// least one even element
import java.util.*;
class GFG {
// Function to count the pairs in
// the array such as there is at
// least one even element in each pair
static int CountPairs(int[] arr, int n)
{
// strore count of even
// and odd elements
int even = 0, odd = 0;
for (int i = 0; i < n; i++) {
// check element is
// even or odd
if (arr[i] % 2 == 0)
even++;
else
odd++;
}
return (even * (even - 1)) / 2
+ (even * odd);
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 8, 2, 3, 1, 4, 2 };
int n = arr.length;
System.out.println(CountPairs(arr, n));
}
}
Python3
# Python3 implementation to count
# pairs in an array such that
# each pair contains at
# least one even element
# Function to count the pairs in
# the array such as there is at
# least one even element in each pair
def CountPairs(arr, n):
# Store count of even
# and odd elements
even = 0
odd = 0
for i in range(n):
# Check element is
# even or odd
if (arr[i] % 2 == 0):
even += 1
else:
odd += 1
return ((even * (even - 1)) // 2 +
(even * odd))
# Driver Code
arr = [ 8, 2, 3, 1, 4, 2 ]
n = len(arr)
print(CountPairs(arr, n))
# This code is contributed by code_hunt
C#
// C# implementation to Count
// pairs in an array such that
// each pair contains at
// least one even element
using System;
class GFG{
// Function to count the pairs in
// the array such as there is at
// least one even element in each pair
static int CountPairs(int[] arr, int n)
{
// Store count of even
// and odd elements
int even = 0, odd = 0;
for(int i = 0; i < n; i++)
{
// Check element is
// even or odd
if (arr[i] % 2 == 0)
even++;
else
odd++;
}
return (even * (even - 1)) / 2 +
(even * odd);
}
// Driver Code
public static void Main()
{
int[] arr = { 8, 2, 3, 1, 4, 2 };
int n = arr.Length;
Console.Write(CountPairs(arr, n));
}
}
// This code is contributed by Nidhi_biet
Java脚本
输出:
14
时间复杂度: O(N)
空间复杂度: O(1)