给定一个整数数组A[] ,任务是找到对的总数,使得每对包含偶数或奇数元素。只有当i != j 时才能形成有效对 ( A[ i ] , A[ j ] )。
例子:
Input: A[ ] = {1, 2, 3, 1, 3}
Output: 6
Explanation:
Possible odd pairs = (1, 3), (1, 1), (1, 3), (3, 1), (3, 3), (1, 3) = 6
Possible even pairs = 0
Hence, total pairs = 6 + 0 = 6
Input: A[ ] = {8, 2, 3, 1, 4, 2}
Output: 7
Explanation:
Possible odd pair = (3, 1) = 1
Possible even pairs = (8, 2), (8, 4), (8, 2), (2, 4), (2, 2), (4, 2) = 6
Hence, total pairs = 6 + 1 = 7
天真的方法:
最简单的方法是生成所有可能的对。对于每一对,检查两个元素是奇数还是偶数。如果是这样,增加一个计数器。最终计数将是所需的答案。
时间复杂度: 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
有效的方法:
遍历数组并计数并将偶数和奇数存储在数组中,并根据各自的计数计算可能的对并显示它们的总和。
Let the count of even and odd elements in the array be EC and OC respectively.
Count of even pairs = ( EC * ( EC – 1 ) ) / 2
Count of odd pairs = ( OC * ( OC – 1 ) ) / 2
Hence, total number of possible pairs = (( EC * ( EC – 1 ) ) + ( OC * ( OC – 1 ) ))/ 2
下面是上述方法的实现:
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));
}
}
蟒蛇3
# 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
时间复杂度: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live