The Wayback Machine - https://web.archive.org/web/20211222143155/https://www.geeksforgeeks.org/maximum-count-of-pairs-such-that-element-at-each-index-i-is-included-in-i-pairs/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Maximum count of pairs such that element at each index i is included in i pairs

  • Last Updated : 22 Dec, 2021

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:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr[] = {2, 2, 3, 4} 
Output: 
5
1 3
2 4
2 4
3 4
3 4
Explaination: 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:

Java




// Java implementation of above appraoch
import java.io.*;
import java.util.*;
class GFG {
    public static void maxPairs(int arr[])
    {
        // Stores the final list
        // of pairs required
        List<Integer> matchList = new ArrayList<>();
  
        // Max Priority Queue to
        // store indiced in order
        // of their array value
        PriorityQueue<Integer> pq = new PriorityQueue<>(
            (x, y) -> arr[y] - arr[x]);
  
        // Loop to iterate arr[]
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 0)
                pq.add(i);
        }
  
        // Loop to iterate pq
        // till it has more
        // than 2 elements
        while (pq.size() >= 2) {
  
            // Stores the maximum
            int top = pq.poll();
  
            // Stores the second
            // maximum
            int cur = pq.poll();
  
            // Insert pair into the
            // final list
            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);
        }
  
        // Print Answer
        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));
        }
    }
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 3, 4 };
        maxPairs(arr);
    }
}
Output
5
4 3
4 3
2 4
3 4
2 1

Time Complexity: O(M*log M), where M denotes the sum of all array elements
Auxiliary Space: O(N)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!