给定长度为N的数组arr [] ,任务是计算对(X,Y)的对数,以使X Y为偶数,并计算对的数以使X Y为奇数。
例子:
Input: arr[] = {2, 3, 4, 5}
Output:
6
6
Explanation: (2, 3), (2, 4), (2, 5), (4, 2), (4, 3) and (4, 5) are the pairs with even values
and (3, 2), (3, 4), (3, 5), (5, 2), (5, 3) and (5, 4) are the pairs with odd values.
Input: arr[] = {10, 11, 20, 60, 70}
Output:
16
4
Explanation: (10, 11), (10, 20), (10, 60), (10, 70), (20, 10), (20, 11), (20, 60), (20, 70), (60, 10), (60, 11), (60, 20), (60, 70), (70, 10), (70, 11), (70, 20), (70, 60) are the pairs with even values and (11, 10), (11, 20), (11, 60), (11, 70) are the pairs with odd values.
天真的方法:计算每个可能的对的功效,并找到计算出的值是偶数还是奇数。
高效的方法:计算数组中的偶数和奇数元素,然后使用pow(偶数,除自身以外的任何元素)为偶数,pow(奇数,除自身以外的任何元素)为奇数的概念。
因此,对数(X,Y)为,
- pow(X,Y)是偶数=(偶数的数量*(n – 1))
- pow(X,Y)是奇数=(奇数个数*(n – 1))
下面是上述方法的实现:
CPP
// C++ implementation of the approach
#include
using namespace std;
// Function to find and print the
// required count of pairs
void countPairs(int arr[], int n)
{
// Find the count of even and
// odd elements in the array
int even = 0, odd = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0)
even++;
else
odd++;
}
// Print the required count of pairs
cout << (even) * (n - 1) << endl;
cout << (odd) * (n - 1) << endl;
}
// Driver code
int main()
{
int arr[] = { 2, 3, 4, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
countPairs(arr, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to find and print the
// required count of pairs
static void countPairs(int arr[], int n)
{
// Find the count of even and
// odd elements in the array
int even = 0, odd = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
even++;
else
odd++;
}
// Print the required count of pairs
System.out.println((even) * (n - 1));
System.out.println((odd) * (n - 1));
}
// Driver code
public static void main(String args[])
{
int arr[] = { 2, 3, 4, 5 };
int n = arr.length;
countPairs(arr, n);
}
}
// This code is contributd by ANKITUMAR34
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to find and print the
// required count of pairs
static void countPairs(int []arr, int n)
{
// Find the count of even and
// odd elements in the array
int even = 0, odd = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
even++;
else
odd++;
}
// Print the required count of pairs
Console.WriteLine((even) * (n - 1));
Console.WriteLine((odd) * (n - 1));
}
// Driver code
public static void Main()
{
int []arr = { 2, 3, 4, 5 };
int n = arr.Length;
countPairs(arr, n);
}
}
// This code is contributd by AnkitRai01
Python3
# Python3 implementation of the approach
# Function to find and print the
# required count of pairs
def countPairs(arr, n):
# Find the count of even and
# odd elements in the array
odd = 0
even = 0
for i in range(n):
if (arr[i] % 2 == 0):
even += 1
else:
odd += 1
# Count the number of odd pairs
odd_pairs = odd*(n-1)
# Count the number of even pairs
even_pairs = even*(n-1)
print(odd_pairs)
print(even_pairs)
# Driver code
if __name__ == '__main__':
arr = [2, 3, 4, 5]
n = len(arr)
countPairs(arr, n)
6
6