Count of Pairs in given Array having both even or both odd or sum as K
Last Updated :
25 Jan, 2022
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++
#include <bits/stdc++.h>
using namespace std;
void go( int ar[], int N, int K)
{
int odds = 0, evens = 0;
for ( int i = 0; i < N; i++) {
if (ar[i] & 1)
odds++;
else
evens++;
}
int ans = odds / 2 + evens / 2;
if ((odds & 1) && (evens & 1)) {
unordered_map< int , int > occ;
for ( int i = 0; i < N; i++)
occ[ar[i]]++;
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;
}
int main()
{
int N = 6, K = 7;
int arr[N] = { 1, 2, 3, 4, 5, 6 };
go(arr, N, K);
return 0;
}
|
Java
import java.util.HashMap;
class GFG {
static void go( int [] ar, int N, int K) {
int odds = 0 , evens = 0 ;
for ( int i = 0 ; i < N; i++) {
if ((ar[i] & 1 ) > 0 )
odds++;
else
evens++;
}
int ans = odds / 2 + evens / 2 ;
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 );
}
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);
}
public static void main(String args[]) {
int N = 6 , K = 7 ;
int [] arr = { 1 , 2 , 3 , 4 , 5 , 6 };
go(arr, N, K);
}
}
|
Python3
def go(ar, N, K):
odds = 0
evens = 0
for i in range (N):
if (ar[i] & 1 ):
odds + = 1
else :
evens + = 1
ans = odds / / 2 + evens / / 2 ;
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
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))
N = 6
K = 7 ;
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ];
go(arr, N, K);
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void go( int [] ar, int N, int K)
{
int odds = 0, evens = 0;
for ( int i = 0; i < N; i++) {
if ((ar[i] & 1) > 0)
odds++;
else
evens++;
}
int ans = odds / 2 + evens / 2;
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;
}
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);
}
public static void Main()
{
int N = 6, K = 7;
int [] arr = { 1, 2, 3, 4, 5, 6 };
go(arr, N, K);
}
}
|
Javascript
<script>
function go(ar, N, K)
{
let odds = 0, evens = 0;
for (let i = 0; i < N; i++) {
if (ar[i] & 1)
odds++;
else
evens++;
}
let ans = odds / 2 + evens / 2;
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)
}
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" )
}
let N = 6, K = 7;
let arr = [1, 2, 3, 4, 5, 6];
go(arr, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...