Maximum count of pairs such that element at each index i is included in i pairs
Given an array arr[] and an integer N, the task is to find the maximum number of pairs that can be formed such that ith index is included in almost arr[i] pairs.
Examples:
Input: arr[] = {2, 2, 3, 4}
Output:
5
1 3
2 4
2 4
3 4
3 4
Explanation: For the given array, a maximum of 5 pairs can be created where 1st index is included in 1 pair, 2nd index in 2 pairs, 3rd index in 3 pairs and 4th index in 4 pairs as shown above.
Input: arr[] = {8, 2, 0, 1, 1}
Output:
4
1 2
1 5
1 4
1 2
Approach: The given problem can be solved using a greedy approach. It can be observed that the most optimal choice at every step is to choose the elements with the maximum value and create their respective pair. Using this observation, follow the below steps to solve this problem:
- Create a Max Priority Queue which stores the indices of the given array in decreasing order of their respective array value.
- Create a loop to iterate the priority queue until there are more than two elements in it and follow the below steps:
- Select the top two indices at the priority queue, append their pair into an answer array.
- Reinsert them into the priority queue after decrementing their respective array values if their values are greater than 0.
- Print all the pairs in the answer array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxPairs( int arr[], int n)
{
vector< int > matchList;
priority_queue< int > pq;
for ( int i = 0; i < n; i++)
{
if (arr[i] > 0)
pq.push(i);
}
while (pq.size() >= 2)
{
int top = pq.top();
pq.pop();
int cur = pq.top();
pq.pop();
matchList.push_back(top + 1);
matchList.push_back(cur + 1);
arr[top]--;
arr[cur]--;
if (arr[top] > 0)
pq.push(top);
if (arr[cur] > 0)
pq.push(cur);
}
cout << (matchList.size() / 2) << "\n" ;
for ( int i = 0; i < matchList.size(); i += 2)
{
cout << matchList[i] << " " << matchList[i+1] << "\n" ;
}
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int n = sizeof (arr)/ sizeof (arr[0]);
maxPairs(arr,n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void maxPairs( int arr[])
{
List<Integer> matchList = new ArrayList<>();
PriorityQueue<Integer> pq = new PriorityQueue<>(
(x, y) -> arr[y] - arr[x]);
for ( int i = 0 ; i < arr.length; i++) {
if (arr[i] > 0 )
pq.add(i);
}
while (pq.size() >= 2 ) {
int top = pq.poll();
int cur = pq.poll();
matchList.add(top + 1 );
matchList.add(cur + 1 );
arr[top]--;
arr[cur]--;
if (arr[top] > 0 )
pq.add(top);
if (arr[cur] > 0 )
pq.add(cur);
}
System.out.println(matchList.size() / 2 );
for ( int i = 0 ; i < matchList.size(); i += 2 ) {
System.out.println(matchList.get(i) + " "
+ matchList.get(i + 1 ));
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
maxPairs(arr);
}
}
|
Python3
import bisect
def maxPairs(arr,n):
matchList = []
pq = []
for i in range (n):
if (arr[i]> 0 ):
bisect.insort(pq,i)
while ( len (pq)> = 2 ):
top = pq[ - 1 ]
pq.pop( - 1 )
cur = pq[ - 1 ]
pq.pop( - 1 )
matchList.append(top + 1 )
matchList.append(cur + 1 )
arr[top] = arr[top] - 1
arr[cur] = arr[cur] - 1
if (arr[top]> 0 ):
bisect.insort(pq,top)
if (arr[cur]> 0 ):
bisect.insort(pq,cur)
print ( len (matchList) / / 2 )
for i in range ( 0 , len (matchList), 2 ):
print (matchList[i],end = " " )
print (matchList[i + 1 ])
arr = [ 1 , 2 , 3 , 4 ]
n = len (arr)
maxPairs(arr,n)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class HelloWorld {
class GFG : IComparer< int >
{
public int Compare( int x, int y)
{
return y.CompareTo(x);
}
}
public static void maxPairs( int [] arr, int n)
{
List< int > matchList = new List< int >();;
List< int > pq = new List< int >();
for ( int i = 0; i < n; i++)
{
if (arr[i] > 0)
pq.Add(i);
}
GFG gg = new GFG();
pq.Sort(gg);
while (pq.Count >= 2)
{
int top = pq.First();
pq.RemoveAt(0);
int cur = pq.First();
pq.RemoveAt(0);
matchList.Add(top + 1);
matchList.Add(cur + 1);
arr[top]--;
arr[cur]--;
if (arr[top] > 0){
pq.Add(top);
pq.Sort(gg);
}
if (arr[cur] > 0){
pq.Add(cur);
pq.Sort(gg);
}
}
Console.WriteLine(matchList.Count / 2);
for ( int i = 0; i < matchList.Count; i += 2)
{
Console.WriteLine(matchList[i] + " " + matchList[i+1]);
}
}
static void Main() {
int [] arr = { 1, 2, 3, 4 };
int n = arr.Length;
maxPairs(arr,n);
}
}
|
Javascript
function maxPairs(arr, n)
{
let matchList = [];
let pq = [];
for (let i = 0; i < n; i++) {
if (arr[i] > 0) {
pq.push(i);
pq.sort();
}
}
while (pq.length >= 2) {
let top = pq.pop();
let cur = pq.pop();
matchList.push(top + 1);
matchList.push(cur + 1);
arr[top] = arr[top] - 1;
arr[cur] = arr[cur] - 1;
if (arr[top] > 0) {
pq.push(top);
pq.sort();
}
if (arr[cur] > 0) {
pq.push(cur);
pq.sort();
}
}
console.log(matchList.length / 2);
for (let i = 0; i < matchList.length; i += 2) {
console.log(matchList[i], matchList[i + 1]);
}
}
let arr = [1, 2, 3, 4];
let n = arr.length;
maxPairs(arr, n);
|
Output
5
4 3
4 3
4 3
4 2
2 1
Time Complexity: O(M*log M), where M denotes the sum of all array elements
Auxiliary Space: O(N)
Last Updated :
21 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...