Skip to content
Related Articles

Related Articles

Count subarrays where second highest lie before highest
  • Difficulty Level : Hard
  • Last Updated : 22 Dec, 2020

Given an array of N distinct element of at least size 2. A pair (a, b) in an array is defined as ‘a’ is the index of second highest element and ‘b’ is the index of highest element in the array. The task is to count all the distinct pair where a < b in all the subarrays.

Examples :  

Input : arr[] = { 1, 3, 2, 4 }
Output : 3

Explanation : 
The subarray { 1 }, { 3 }, { 2 }, { 4 } does not contain any such pair 
The subarray { 1, 3 }, { 2, 4 } contain (1, 2) as pair 
The subarray { 3, 2 } does not contain any such pair 
The subarray { 3, 2, 4 } contain (1, 3) as a pair 
The subarray { 1, 3, 2 } does not contain any such pair 
The subarray { 1, 3, 2, 4 } contain (2, 4) as a pair 
So, there are 3 distinct pairs, which are (1, 2), (1, 3) and (2, 4).
 

Method 1: (Brute Force) – Simple approach can be, 
1. Find all the subarrays. 
2. For each subarray, find second largest and largest element. 
3. Check if second largest element lie before largest element. 
4. If so, check if such index pair is already counted or not. If not store the pair and increment the count by 1, else ignore. 
Time Complexity: O(n2).

Method 2: (Using stack) – 
For given array A, suppose for an element at index curr (A[curr]), first element greater than it and after it is A[next] and first element greater than it and before it A[prev]. Observe that for all subarray starting from any index in range [prev + 1, curr] and ending at index next, A[curr] is the second largest and A[next] is the largest, which generate (curr – prev + 1) pairs in total with difference of (next – curr + 1) in maximum and second maximum.



If we get next and prev greater element in an array, and keep track of maximum number of pairs possible for any difference (of largest and second largest). We will need to add all these numbers.

Now only job left is to get greater element (after and before) any element. For this, refer Next Greater Element.
Traverse from the starting element in an array, keeping track of all numbers in the stack in decreasing order. After arriving at any number, pop all elements from stack which are less than current element to get location of number bigger than it and push current element on it. This generates required value for all numbers in the array. 

C++




// C++ program to count number of distinct instance
// where second highest number lie
// before highest number in all subarrays.
#include <bits/stdc++.h>
#define MAXN 100005
using namespace std;
 
// Finding the next greater element of the array.
void makeNext(int arr[], int n, int nextBig[])
{
    stack<pair<int, int> > s;
 
    for (int i = n - 1; i >= 0; i--) {
 
        nextBig[i] = i;
        while (!s.empty() && s.top().first < arr[i])
            s.pop();
 
        if (!s.empty())
            nextBig[i] = s.top().second;
 
        s.push(pair<int, int>(arr[i], i));
    }
}
 
// Finding the previous greater element of the array.
void makePrev(int arr[], int n, int prevBig[])
{
    stack<pair<int, int> > s;
    for (int i = 0; i < n; i++) {
 
        prevBig[i] = -1;
        while (!s.empty() && s.top().first < arr[i])
            s.pop();
 
        if (!s.empty())
            prevBig[i] = s.top().second;
 
        s.push(pair<int, int>(arr[i], i));
    }
}
 
// Wrapper Function
int wrapper(int arr[], int n)
{
    int nextBig[MAXN];
    int prevBig[MAXN];
    int maxi[MAXN];
    int ans = 0;
 
    // Finding previous largest element
    makePrev(arr, n, prevBig);
 
    // Finding next largest element
    makeNext(arr, n, nextBig);
 
    for (int i = 0; i < n; i++)
        if (nextBig[i] != i)
            maxi[nextBig[i] - i] = max(maxi[nextBig[i] - i],
                                       i - prevBig[i]);
 
    for (int i = 0; i < n; i++)
        ans += maxi[i];
 
    return ans;
}
 
// Driven Program
int main()
{
    int arr[] = { 1, 3, 2, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << wrapper(arr, n) << endl;
    return 0;
}


Java




// Java program to count number of distinct instance
// where second highest number lie
// before highest number in all subarrays.
import java.util.*;
 
class GFG
{
     
static int MAXN = 100005;
 
static class pair
{
    int first, second;
    public pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
}
// Finding the next greater element of the array.
static void makeNext(int arr[], int n, int nextBig[])
{
    Stack<pair> s = new Stack<>();
 
    for (int i = n - 1; i >= 0; i--)
    {
 
        nextBig[i] = i;
        while (!s.empty() && s.peek().first < arr[i])
            s.pop();
 
        if (!s.empty())
            nextBig[i] = s.peek().second;
 
        s.push(new pair(arr[i], i));
    }
}
 
// Finding the previous greater element of the array.
static void makePrev(int arr[], int n, int prevBig[])
{
    Stack<pair> s = new Stack<>();
    for (int i = 0; i < n; i++)
    {
 
        prevBig[i] = -1;
        while (!s.empty() && s.peek().first < arr[i])
            s.pop();
 
        if (!s.empty())
            prevBig[i] = s.peek().second;
 
        s.push(new pair(arr[i], i));
    }
}
 
// Wrapper Function
static int wrapper(int arr[], int n)
{
    int []nextBig = new int[MAXN];
    int []prevBig = new int[MAXN];
    int []maxi = new int[MAXN];
    int ans = 0;
 
    // Finding previous largest element
    makePrev(arr, n, prevBig);
 
    // Finding next largest element
    makeNext(arr, n, nextBig);
 
    for (int i = 0; i < n; i++)
        if (nextBig[i] != i)
            maxi[nextBig[i] - i] = Math.max(maxi[nextBig[i] - i],
                                    i - prevBig[i]);
 
    for (int i = 0; i < n; i++)
        ans += maxi[i];
 
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
 
    int arr[] = { 1, 3, 2, 4 };
    int n = arr.length;
 
    System.out.println(wrapper(arr, n));
    }
}
 
// This code is contributed by Princi Singh


Python3




# Python3 program to count number of distinct
# instance where second highest number lie
# before highest number in all subarrays.
from typing import List
 
MAXN = 100005
 
# Finding the next greater element
# of the array.
def makeNext(arr: List[int], n: int,
         nextBig: List[int]) -> None:
 
    # Stack
    s = []
 
    for i in range(n - 1, -1, -1):
        nextBig[i] = i
         
        while len(s) and s[-1][0] < arr[i]:
            s.pop()
 
        if len(s):
            nextBig[i] = s[-1][1]
 
        s.append((arr[i], i))
 
# Finding the previous greater
# element of the array.
def makePrev(arr: List[int], n: int,
         prevBig: List[int]) -> None:
 
    # Stack
    s = []
    for i in range(n):
        prevBig[i] = -1
         
        while (len(s) and s[-1][0] < arr[i]):
            s.pop()
 
        if (len(s)):
            prevBig[i] = s[-1][1]
 
        s.append((arr[i], i))
 
# Wrapper Function
def wrapper(arr: List[int], n: int) -> int:
 
    nextBig = [0] * MAXN
    prevBig = [0] * MAXN
    maxi = [0] * MAXN
    ans = 0
 
    # Finding previous largest element
    makePrev(arr, n, prevBig)
 
    # Finding next largest element
    makeNext(arr, n, nextBig)
 
    for i in range(n):
        if (nextBig[i] != i):
            maxi[nextBig[i] - i] = max(
                maxi[nextBig[i] - i],
                    i - prevBig[i])
 
    for i in range(n):
        ans += maxi[i]
 
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    arr = [ 1, 3, 2, 4 ]
    n = len(arr)
     
    print(wrapper(arr, n))
 
# This code is contributed by sanjeev2552


C#




// C# program to count number of distinct instance
// where second highest number lie
// before highest number in all subarrays.
using System;
using System.Collections.Generic;
     
class GFG
{
     
static int MAXN = 100005;
public class pair
{
    public int first, second;
    public pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
}
 
// Finding the next greater element of the array.
static void makeNext(int []arr, int n, int []nextBig)
{
    Stack<pair> s = new Stack<pair>();
 
    for (int i = n - 1; i >= 0; i--)
    {
 
        nextBig[i] = i;
        while (s.Count!=0 && s.Peek().first < arr[i])
            s.Pop();
 
        if (s.Count!=0)
            nextBig[i] = s.Peek().second;
 
        s.Push(new pair(arr[i], i));
    }
}
 
// Finding the previous greater element of the array.
static void makePrev(int []arr, int n, int[] prevBig)
{
    Stack<pair> s = new Stack<pair>();
    for (int i = 0; i < n; i++)
    {
 
        prevBig[i] = -1;
        while (s.Count!=0 && s.Peek().first < arr[i])
            s.Pop();
 
        if (s.Count!=0)
            prevBig[i] = s.Peek().second;
 
        s.Push(new pair(arr[i], i));
    }
}
 
// Wrapper Function
static int wrapper(int []arr, int n)
{
    int []nextBig = new int[MAXN];
    int []prevBig = new int[MAXN];
    int []maxi = new int[MAXN];
    int ans = 0;
 
    // Finding previous largest element
    makePrev(arr, n, prevBig);
 
    // Finding next largest element
    makeNext(arr, n, nextBig);
 
    for (int i = 0; i < n; i++)
        if (nextBig[i] != i)
            maxi[nextBig[i] - i] = Math.Max(maxi[nextBig[i] - i],
                                    i - prevBig[i]);
 
    for (int i = 0; i < n; i++)
        ans += maxi[i];
 
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
 
    int[] arr = { 1, 3, 2, 4 };
    int n = arr.Length;
 
    Console.WriteLine(wrapper(arr, n));
}
}
 
// This code is contributed by 29AjayKumar


Output : 

3

Time Complexity: O(n)

This article is contributed by anuj0503. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geekforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

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
Recommended Articles
Page :