Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)

Let (a, b) represent the ordered pair of the second maximum and the maximum element of an array respectively. We need to find all such unique pairs overall contiguous sub-arrays of a given array.

Examples:

Input: Arr = [ 1, 2, 3, 4, 5 ]
Output: (1, 2) (2, 3) (3, 4) (4, 5)

Input: Arr = [ 1, 1, 2 ]
Output: (1, 1) (1, 2)

Input: Arr = [ 1, 2, 6, 4, 5 ]
Output: (1, 2) (2, 6) (4, 5) (4, 6) (5, 6)



Brute Force Approach:

  • A simple way to solve this problem would be to scan each sub-array and find the maximum and second maximum element in that sub-array
  • This can be done in O(N^2) time
  • Then we can insert each pair in a set to ensure duplicates are removed, and then print them
  • Each insertion operation costs O(log(N)), pushing the final complexity to O(N^2log(N))

CPP14

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the set of pairs
set<pair<int, int>> 
    pairs(vector<int>& arr)
{
    set<pair<int, int>> pairs;
  
    //find all subarrays
    for (int i = 0; i < arr.size()-1; ++i) 
    {
        int maximum = max(arr[i],arr[i+1]),secondmax = min(arr[i],arr[i+1]);
          
        for(int j = i + 1; j < arr.size(); ++j)
        {
            //update max and second max
            if(arr[j] > maximum)
            {
               secondmax = maximum;
               maximum = arr[j]; 
            }
            if(arr[j] < maximum && arr[j] > secondmax)
            {
               secondmax = arr[j];
            }
              
            //insert a pair in set
            pairs.insert(make_pair(secondmax,maximum));
        }
    }
    return pairs;
}
  
int main()
{
    vector<int> vec = { 1, 2, 6, 4, 5 };
  
    set<pair<int, int> > st = pairs(vec);
    cout << "Total Number of valid pairs is :"
                << (int)st.size() << "\n";
    for (auto& x : st) {
        cout << "(" << x.first << ", "
                    << x.second << ") ";
    }
    return 0;
}

chevron_right



Output:

Total Number of valid pairs is :5
(1, 2) (2, 6) (4, 5) (4, 6) (5, 6)

Complexity Analysis:

  • Time Complexity: O(N^2 log(N)).
    Insertion in set takes log N time. There can be at most N^2 sub-arrays. So the time Complexity is O(N^2 log N).
  • Auxiliary Space: O(n^2).
    As extra space is required to store the elements in a set.

Efficient Approach:

  • It could bring down the complexity of finding pairs to O(N) by observing that an element X can form pairs with elements only till the closest element to the right which is greater than X.
  • To see why this holds, consider X = 4 in the next example.
     Arr = {1, 4, 5, 3, 2, 1}
    • It could see that 5 > 4 is the nearest element to the right which is greater than 4. (4, 5) forms a pair considering the sub-array [4, 5].
    • Other sub-arrays, that start with 4 must include 5. Considering one of them, if another element Y >=5 exists in the sub-array, then (5, Y) will be the pair for that sub-array.
    • Else either (4, 5) will be formed or (Z, 5) will be formed, where Z is the max element to the right of 5 in the sub-array.
    • In any cases, 4 cannot form a pair with any element to the right of 5.
  • Using this observation, we can implement the logic using stack which brings down the pair generation complexity to O(N).
  • Each pair can be inserted into a set for eliminating duplicates, giving a final time complexity of O(Nlog(N))

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the set of pairs
set<pair<int, int>> 
     pairs(vector<int>& arr)
{
    stack<int> st;
    set<pair<int, int>> pairs;
  
    // Push first element into stack
    st.push(arr[0]);
  
    // For each element 'X' in arr,
    // pop the stack while top Element
    // is smaller than 'X' and form a pair.
    // If the stack is not empty after
    // the previous operation, create
    // a pair. Push X into the stack.
  
    for (int i = 1; i < arr.size(); ++i) {
        while (!st.empty() &&
                arr[i] > st.top()) {
            pairs.insert(make_pair(st.top(),
                                    arr[i]));
            st.pop();
        }
        if (!st.empty()) {
            pairs.insert(make_pair(min(st.top(),
                                       arr[i]),
                                   max(st.top(),
                                      arr[i])));
        }
        st.push(arr[i]);
    }
    return pairs;
}
  
int main()
{
    vector<int> vec = { 1, 2, 6, 4, 5 };
  
    set<pair<int, int> > st = pairs(vec);
    cout << "Total Number of valid pairs is :" 
                   << (int)st.size() << "\n";
    for (auto& x : st) {
        cout << "(" << x.first << ", "
                       << x.second << ") ";
    }
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementationof the above approach
  
# Function to return the set of pairs 
def pairs(arr) :
  
    st = []; 
    pairs = []; 
  
    # Push first element into stack 
    st.append(arr[0]); 
  
    # For each element 'X' in arr, 
    # pop the stack while top Element 
    # is smaller than 'X' and form a pair. 
    # If the stack is not empty after 
    # the previous operation, create 
    # a pair. Push X into the stack. 
    for i in range(1, len(arr) ) :
        while len(st) != 0 and arr[i] > st[-1] :
            pairs.append((st[-1], arr[i])); 
            st.pop(); 
      
        if len(st) != 0 :
            pairs.append((min(st[-1], arr[i]), 
                        max(st[-1], arr[i]))); 
          
        st.append(arr[i]); 
      
    return pairs; 
  
# Driver code
if __name__ == "__main__"
  
    vec = [ 1, 2, 6, 4, 5 ];
    st = pairs(vec);
    print("Total Number of valid pairs is :",len(st)); 
      
    for x in st :
        print("(" ,x[0], ", ",x[1], ")",end=" "); 
  
# This code is contributed by AnkitRai01

chevron_right


Output:

Total Number of valid pairs is :5
(1, 2) (2, 6) (4, 5) (4, 6) (5, 6)

Complexity Analysis:

  • Time Complexity: O(N log(N)).
    Each pair can be inserted into a set for eliminating duplicates, giving a final time complexity of O(N log N)
  • Auxiliary Space: O(N).
    As extra space is required to store the elements in a set.

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, andrew1234

Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.