Count of Pairs in given Array having both even or both odd or sum as K
Given an array arr[] of distinct integers of size N and an integer K, The task is to find the total no of possible pairs in the array such that, either both are even or both are odd or the sum of the pair is K
Note: No element is part of more than one pair.
Examples:
Input: N = 6, K = 7, arr[] = {1, 2, 3, 4, 5, 6}
Output: 3
Explanation: Possible pairs are: (5, 2), (1, 3), (4, 6)Input: N = 4, K = 22, arr[] = {1, 3, 12, 9}
Output: 1
Explanation: Only one pair is possible.
The pair can be either (1, 3) or (1, 9) or (3, 9).
Approach: The task can be solved using observations, one can observe that maximum pairing is possible if grouping of both odds together & both evens together are done initially and then, consider the involvement of K.
Follow the below steps to solve the problem:
- Let ‘odds‘ and ‘evens‘ store the count of the number of odds & even elements respectively in the array.
- If both ‘odds’ and ‘evens’ are odd then after pairing odds & evens together, 1 odd & 1 even element will be left. To make maximum possible pairs, see whether there exists a pair with different parities & sum equal to K, If such a pair exists, increment the count of possible pairs.
- Otherwise, the total possible pairs would be the sum of odds/2 & evens/2
Below is the implementation of the above approach:
C++
// C++ code to implement above approach #include <bits/stdc++.h> using namespace std; // Function to find the total possible pairs void go( int ar[], int N, int K) { // Stores the count of odd & even // elements in the array int odds = 0, evens = 0; for ( int i = 0; i < N; i++) { if (ar[i] & 1) odds++; else evens++; } // Total pairs int ans = odds / 2 + evens / 2; // If number of both odds & even // elements are odd if ((odds & 1) && (evens & 1)) { unordered_map< int , int > occ; for ( int i = 0; i < N; i++) occ[ar[i]]++; // Check if there exists a pair // with different parity & // sum equals to K for ( int i = 0; i < N; i++) { if (occ.find(K - ar[i]) != occ.end()) { if (((ar[i] & 1) && !((K - ar[i]) & 1)) || (!(ar[i] & 1) && ((K - ar[i]) & 1))) { ans++; break ; } } } } cout << ans << endl; } // Driver Code int main() { int N = 6, K = 7; int arr[N] = { 1, 2, 3, 4, 5, 6 }; go(arr, N, K); return 0; } |
Java
// Java code to implement above approach import java.util.HashMap; class GFG { // Function to find the total possible pairs static void go( int [] ar, int N, int K) { // Stores the count of odd & even // elements in the array int odds = 0 , evens = 0 ; for ( int i = 0 ; i < N; i++) { if ((ar[i] & 1 ) > 0 ) odds++; else evens++; } // Total pairs int ans = odds / 2 + evens / 2 ; // If number of both odds & even // elements are odd if ((odds & 1 ) > 0 && (evens & 1 ) > 0 ) { HashMap<Integer, Integer> occ = new HashMap<Integer, Integer>(); for ( int i = 0 ; i < N; i++) { if (occ.containsKey(ar[i])) occ.put(ar[i], occ.get(ar[i]) + 1 ); else occ.put(ar[i], 1 ); } // Check if there exists a pair // with different parity & // sum equals to K for ( int i = 0 ; i < N; i++) { if (occ.containsKey(K - ar[i])) { if (((ar[i] & 1 ) > 0 && ((K - ar[i]) & 1 ) < 1 ) || ((ar[i] & 1 ) < 1 && ((K - ar[i]) & 1 ) > 0 )) { ans++; break ; } } } } System.out.println(ans); } // Driver Code public static void main(String args[]) { int N = 6 , K = 7 ; int [] arr = { 1 , 2 , 3 , 4 , 5 , 6 }; go(arr, N, K); } } // This code is contributed by gfgking |
Python3
# Python code for the above approach # Function to find the total possible pairs def go(ar, N, K): # Stores the count of odd & even # elements in the array odds = 0 evens = 0 for i in range (N): if (ar[i] & 1 ): odds + = 1 else : evens + = 1 # Total pairs ans = odds / / 2 + evens / / 2 ; # If number of both odds & even # elements are odd if ((odds & 1 ) and (evens & 1 )): occ = {}; for i in range (N): if (arr[i] in occ): occ[ar[i]] + = 1 else : occ[arr[i]] = 1 # Check if there exists a pair # with different parity & # sum equals to K for i in range (N): if ((K - ar[i]) in occ): if (((ar[i] & 1 ) and not ((K - ar[i]) & 1 )) or ( not (ar[i] & 1 ) and ((K - ar[i]) & 1 ))): ans + = 1 break ; print ( int (ans)) # Driver Code N = 6 K = 7 ; arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]; go(arr, N, K); # This code is contributed by Saurabh Jaiswal |
C#
// C# code to implement above approach using System; using System.Collections.Generic; class GFG { // Function to find the total possible pairs static void go( int [] ar, int N, int K) { // Stores the count of odd & even // elements in the array int odds = 0, evens = 0; for ( int i = 0; i < N; i++) { if ((ar[i] & 1) > 0) odds++; else evens++; } // Total pairs int ans = odds / 2 + evens / 2; // If number of both odds & even // elements are odd if ((odds & 1) > 0 && (evens & 1) > 0) { Dictionary< int , int > occ = new Dictionary< int , int >(); for ( int i = 0; i < N; i++) { if (occ.ContainsKey(ar[i])) occ[ar[i]]++; else occ[ar[i]] = 1; } // Check if there exists a pair // with different parity & // sum equals to K for ( int i = 0; i < N; i++) { if (occ.ContainsKey(K - ar[i])) { if (((ar[i] & 1) > 0 && ((K - ar[i]) & 1) < 1) || ((ar[i] & 1) < 1 && ((K - ar[i]) & 1) > 0)) { ans++; break ; } } } } Console.WriteLine(ans); } // Driver Code public static void Main() { int N = 6, K = 7; int [] arr = { 1, 2, 3, 4, 5, 6 }; go(arr, N, K); } } // This code is contributed by ukasp. |
Javascript
<script> // JavaScript code for the above approach // Function to find the total possible pairs function go(ar, N, K) { // Stores the count of odd & even // elements in the array let odds = 0, evens = 0; for (let i = 0; i < N; i++) { if (ar[i] & 1) odds++; else evens++; } // Total pairs let ans = odds / 2 + evens / 2; // If number of both odds & even // elements are odd if ((odds & 1) && (evens & 1)) { let occ = new Map(); for (let i = 0; i < N; i++) { if (occ.has(arr[i])) occ.set(ar[i], occ.get(arr[i]) + 1); else occ.set(arr[i], 1) } // Check if there exists a pair // with different parity & // sum equals to K for (let i = 0; i < N; i++) { if (occ.has(K - ar[i]) ) { if (((ar[i] & 1) && !((K - ar[i]) & 1)) || (!(ar[i] & 1) && ((K - ar[i]) & 1))) { ans++; break ; } } } } document.write(ans + "<br" ) } // Driver Code let N = 6, K = 7; let arr = [1, 2, 3, 4, 5, 6]; go(arr, N, K); // This code is contributed by Potta Lokesh </script> |
3
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...