Maximum count of pairs such that element at position i is included in a[i] pairs


In this article, we will find the number of the pair of indices, such that an index i can be included in at most a[i] number of pairs.

In the approach discussed in this article, we will use a priority queue data structure which will contain the elements of the array. The priority queue data structure will be a maximum heap which will allow us to get the current maximum elements of the array on log(N) time. It will also allow us to modify the elements and insert them back in, in the same amount of time. We will take the maximum 2 elements from the priority queue, make a pair out of the indices of these two elements, decrease these two elements and insert them back in. If we do this till the priority queue has only a single element left, we will get the required result.

Problem Statement

Given an array of size N which contains integer elements, we need to find the maximum count of pairs that we can make such that the element at index i can only be included in at most a[i] number of pairs.

Sample Examples

Input

arr = {1,2,3,4}

Output

5

Explanation

The following pairs of indices can be made −

{1,2}, {2,4}, {3,4}, {3,4}, {3,4}

So, the output is 5.

Input

arr = {0,0,1,1,1}

Output

1

Explanation

Only a single pair can be made by taking any two of the indexes 3, 4 and 5.

Hence, the output is 1.

Input

arr = {1,3,2,0,6}

Output

6

Explanation

We can make the pairs using the indices as follow −

{1,5}, {2,5}, {2,5}, {2,5}, {3,5}, {3,5}

So, the output is 6.

Approach 1

In this approach we will use a method where we utilize a priority queue data structure. This data structure will store the array elements and will be implemented as a maximum heap. This choice of structure enables us to efficiently retrieve the current maximum elements from the array in logarithmic time (O(log N)). Additionally, it facilitates seamless element modification and reinsertion in the same timeframe. The process involves extracting the top two maximum elements from the priority queue, generating an index pair for these elements, reducing their values, adding one to the result and then placing them back into the queue. By iteratively performing these steps until the priority queue contains only one element, we can achieve the desired outcome.

Algorithm

  • Step 1 − Initialize a priority queue data structure

  • Step 2 − Traverse all the array elements, if the element is greater than 0, push the element in the priority queue.

  • Step 3 − Initialize a while loop which will run till the priority queue has at least two elements.

  • Step 3.1 − Also, initialize a result variable with 0.

  • Step 4 − In the while loop, pop out the top two elements from the priority queue, the current maximum elements of the array.

  • Step 4.1 − Make a pair with the indices of these two elements and decrease the value of each of them by 1.

  • Step 4.2 − If the values of the one or both of the elements after the decreasing operation is still greater than zero, push them back into the priority queue.

  • Step 4.3 − Increase the value of the result by 1.

  • Step 5 − After the while loop ends, return the result.

Example

#include <bits/stdc++.h> 
using namespace std; 
int Max_pair_count(vector<int> &arr){ 
   priority_queue<int> elements;
   int res = 0;
   for(int i=0;i<arr.size();i++){
      if(arr[i]>0)
         elements.push(arr[i]);
   }
   while(elements.size()>1){
      int x = elements.top();
      elements.pop();
      int y = elements.top();
      elements.pop();
      x--;
      y--;
      res++;
      if(x>0)elements.push(x);
      if(y>0)elements.push(y);
   }
   return res;
} 
int main(){ 
   vector<int> arr = {1,3,2,0,6};
   cout<<Max_pair_count(arr)<<endl; 
   return 0; 
}

Output

6

Time complexity − O(M*log(M)), where M is summation of all the elements in the array.

Space complexity − O(N), where N is the number of elements in the array

Updated on: 01-Nov-2023

26 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements