Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum number of pairs having a sum K possible from the given array.
Note: Every array element can be part of a single pair.
Examples:
Input: arr[] = {1, 2, 3, 4}, K = 5
Output: 2
Explanation: Pairs with sum K from the array are (1, 4), and (2, 3).
Input: arr[] = {3, 1, 3, 4, 3}, K = 6
Output: 1
Explanation: Pair with sum K from the array is (3, 3).
Two-Pointer Approach: The idea is to use the Two Pointer Technique. Follow the steps below to solve the problem:
- Initialize the variable ans as 0 to store the maximum number of pairs with the sum K.
- Sort the array arr[] in increasing order.
- Initialize two index variables L as 0 and R as (N – 1) to find the candidate elements in the sorted array.
- Iterate until L is less than R and do the following:
- Check if the sum of arr[L] and arr[R] is K or not. If found to be true, then increment ans and L by 1 and decrement R by 1.
- If the sum of arr[L] and arr[R] is less than K, then increment L by 1.
- Otherwise, decrement R by 1.
- After the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxPairs( int nums[], int n, int k)
{
sort(nums, nums + n);
int result = 0;
int start = 0, end = n - 1;
while (start < end) {
if (nums[start] + nums[end] > k)
end--;
else if (nums[start] + nums[end] < k)
start++;
else
{
start++;
end--;
result++;
}
}
cout << result << endl;;
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int n = sizeof (arr)/ sizeof (arr[0]);
int K = 5;
maxPairs(arr, n, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void maxPairs( int [] nums, int k)
{
Arrays.sort(nums);
int result = 0 ;
int start = 0 , end = nums.length - 1 ;
while (start < end) {
if (nums[start] + nums[end] > k)
end--;
else if (nums[start] + nums[end] < k)
start++;
else {
start++;
end--;
result++;
}
}
System.out.println(result);
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 };
int K = 5 ;
maxPairs(arr, K);
}
}
|
Python3
def maxPairs(nums, k):
nums = sorted (nums)
result = 0
start, end = 0 , len (nums) - 1
while (start < end):
if (nums[start] + nums[end] > k):
end - = 1
elif (nums[start] + nums[end] < k):
start + = 1
else :
start + = 1
end - = 1
result + = 1
print (result)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 ]
K = 5
maxPairs(arr, K)
|
C#
using System;
class GFG{
public static void maxPairs( int [] nums, int k)
{
Array.Sort(nums);
int result = 0;
int start = 0, end = nums.Length - 1;
while (start < end) {
if (nums[start] + nums[end] > k)
end--;
else if (nums[start] + nums[end] < k)
start++;
else
{
start++;
end--;
result++;
}
}
Console.Write(result);
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4 };
int K = 5;
maxPairs(arr, K);
}
}
|
Javascript
<script>
function maxPairs(nums, k)
{
nums.sort();
let result = 0;
let start = 0, end = nums.length - 1;
while (start < end) {
if (nums[start] + nums[end] > k)
end--;
else if (nums[start] + nums[end] < k)
start++;
else {
start++;
end--;
result++;
}
}
document.write(result);
}
let arr = [ 1, 2, 3, 4 ];
let K = 5;
maxPairs(arr, K);
</script>
|
Output:
2
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use hashing. Follow the steps below to solve the problem:
- Initialize a variable, say ans, to store the maximum number of pairs with the sum K.
- Initialize a hash table, say S, to store the frequency of elements in arr[].
- Traverse the array arr[] using a variable, say i, and perform the following steps:
- If the frequency of (K – arr[i]) is positive, then increment ans by 1 and decrement the frequency of (K – arr[i]) by 1.
- Otherwise, insert arr[i] with frequency 1 in the Hash Table.
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
void maxPairs(vector< int > nums, int k)
{
map< int , int > m;
int result = 0;
for ( auto i : nums)
{
if (m.find(i) != m.end() && m[i] > 0)
{
m[i] = m[i] - 1;
result++;
}
else
{
m[k - i] = m[k - i] + 1;
}
}
cout << result;
}
int main()
{
vector< int > arr = { 1, 2, 3, 4 };
int K = 5;
maxPairs(arr, K);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void maxPairs(
int [] nums, int k)
{
Map<Integer, Integer> map
= new HashMap<>();
int result = 0 ;
for ( int i : nums) {
if (map.containsKey(i) &&
map.get(i) > 0 )
{
map.put(i, map.get(i) - 1 );
result++;
}
else
{
map.put(k - i,
map.getOrDefault(k - i, 0 ) + 1 );
}
}
System.out.println(result);
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 };
int K = 5 ;
maxPairs(arr, K);
}
}
|
Python3
def maxPairs(nums, k) :
m = {}
result = 0
for i in nums :
if ((i in m) and m[i] > 0 ) :
m[i] = m[i] - 1
result + = 1
else :
if k - i in m :
m[k - i] + = 1
else :
m[k - i] = 1
print (result)
arr = [ 1 , 2 , 3 , 4 ]
K = 5
maxPairs(arr, K)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static void maxPairs(
int [] nums, int k)
{
Dictionary< int , int > map
= new Dictionary< int , int >();
int result = 0;
foreach ( int i in nums)
{
if (map.ContainsKey(i) &&
map[i] > 0)
{
map[i] = map[i] - 1;
result++;
}
else
{
if (!map.ContainsKey(k - i))
map.Add(k - i, 1);
else
map[i] = map[i] + 1;
}
}
Console.WriteLine(result);
}
public static void Main(String[] args)
{
int [] arr = {1, 2, 3, 4};
int K = 5;
maxPairs(arr, K);
}
}
|
Javascript
<script>
function maxPairs(nums, k)
{
var m = new Map();
var result = 0;
nums.forEach(i => {
if (m.has(i) && m.get(i) > 0)
{
m.set(i, m.get(i)-1);
result++;
}
else
{
if (m.has(k-i))
m.set(k-i, m.get(k-i)+1)
else
m.set(k-i, 1)
}
});
document.write( result);
}
var arr = [1, 2, 3, 4];
var K = 5;
maxPairs(arr, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)