Given an array arr[] of length N, the task is to count the number of pairs (X, Y) such that XY is even and count the number of pairs such that XY is odd.
Examples:
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.
Naive approach: Calculate the powers for every single pair possible and find whether the calculated value is even or odd.
Efficient approach: Count the even and odd elements in the array and then use the concept pow (even, any element except itself) is even and pow (odd, any element except itself) is odd.
So, the number of pairs (X, Y) are,
- pow(X, Y) is even = (count of even number * (n – 1))
- pow(X, Y) is odd = (count of odd number * (n – 1))
Below is the implementation of the above approach:
CPP
// C++ implementation of the approach #include <iostream> 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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Even numbers at even index and odd numbers at odd index
- Count of odd and even sum pairs in an array
- Find the count of even odd pairs in a given Array
- Count number of ordered pairs with Even and Odd Sums
- Count number of ordered pairs with Even and Odd Product
- Find the sum of power of bit count raised to the power B
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Generate an array of given size with equal count and sum of odd and even numbers
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Count pairs in array such that one element is power of another
- Count pairs in Array whose product is a Kth power of any positive integer
- Smallest number possible by swapping adjacent even odd pairs
- Generate an alternate odd-even sequence having sum of all consecutive pairs as a perfect square
- Check if count of even divisors of N is equal to count of odd divisors
- Count number of even and odd elements in an array
- Count of even and odd set bit with array element after XOR with K
- Count number of even and odd length elements in an Array
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

