Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
Let
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
time - Then we can insert each pair in a set to ensure duplicates are removed, and then print them
- Each insertion operation costs
, pushing the final complexity to 
Efficient Approach:
- It could bring down the complexity of finding pairs to
by observing that an element
can form pairs with elements only till the closest element to the right which is greater than
. - To see why this holds, consider
=
in the next example.
Arr = {1, 4, 5, 3, 2, 1}- It could see that
is the nearest element to the right which is greater than
.
forms a pair considering the sub-array
. - Other sub-arrays, that start with
must include
. Considering one of them, if another element
exists in the sub-array, then
will be the pair for that sub-array. - Else either
will be formed or
will be formed, where
is the max element to the right of
in the sub-array. - In any cases,
cannot form a pair with any element to the right of
.
- It could see that
- Using this observation, we can implement the logic using stack which brings down the pair generation complexity to
. - Each pair can be inserted into a set for eliminating duplicates, giving a final time complexity of

Below is the implementation of the above approach
// 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; } |
Total Number of valid pairs is :5 (1, 2) (2, 6) (4, 5) (4, 6) (5, 6)
Recommended Posts:
- Sum of minimum and maximum elements of all subarrays of size k.
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time
- Find the maximum cost of an array of pairs choosing at most K pairs
- Sliding Window Maximum (Maximum of all subarrays of size k)
- Find the maximum possible value of a[i] % a[j] over all pairs of i and j
- Find maximum xor of k elements in an array
- Find the maximum elements in the first and the second halves of the Array
- Find the maximum number of elements divisible by 3
- Find a number that divides maximum array elements
- Find all numbers that divide maximum array elements
- Maximum of all Subarrays of size k using set in C++ STL
- Find integers that divides maximum number of elements of the array
- Recursive Programs to find Minimum and Maximum elements of array
- Find maximum array sum after making all elements same with repeated subtraction
- Find maximum difference between nearest left and right smaller elements
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.


