The Wayback Machine - https://web.archive.org/web/20210719143731/https://www.geeksforgeeks.org/count-of-subarrays-starting-or-ending-at-an-index-i-such-that-arri-is-maximum-in-subarray/
Skip to content
Related Articles

Related Articles

Improve Article

Count of subarrays starting or ending at an index i such that arr[i] is maximum in subarray

  • Last Updated : 19 Jul, 2021

Given an array arr[] consisting of N integers, the task is to find the number of subarrays starting or ending at an index i such that arr[i] is the maximum element of the subarray.

Examples:

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

  1. The subarray starting or ending at index 0 and with maximum arr[0](=3) is {3}. Therefore, the count is 1.
  2. The subarrays starting or ending at index 1 and with maximum arr[1](=4) are {3, 4}, {4}, and {4, 1}. Therefore, the count is 3.
  3. The subarray starting or ending at index 2 and with maximum arr[2](=1) is {1}. Therefore, the count is 1.
  4. The subarrays starting or ending at index 3 and with maximum arr[3](=6) are {3, 4, 1, 6}, {4, 1, 6}, {1, 6}, {6}, and {6, 2}. Therefore, the count is 5.
  5. The subarray starting or ending at index 4 and with maximum arr[4](=2) is {2}. Therefore, the count is 1.

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

Naive Approach: The simplest approach to solve the given problem is for every ith index, iterate backward and forward until the maximum of the subarray remains equal to arr[i] and then print the total count of subarrays obtained.



Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized by storing the index of the next greater element and previous greater element for every index i and find the count of subarrays accordingly for each index. Follow the steps below to solve the given problem:

Below is the implementation of the above approach:

Java




// Java program for the above approach
import java.util.*;
  
public class GFG {
  
    // Function to find previous greater
    // element
    private static int[] getPGE(int[] arr)
    {
        // Stores the previous greater
        // element for each index
        int[] pge = new int[arr.length];
        Stack<Integer> stack = new Stack<>();
  
        // Traverse the array
        for (int i = 0; i < arr.length; i++) {
  
            // Iterate until stack is empty
            // and top element is less than
            // the current element arr[i]
            while (!stack.isEmpty()
                   && arr[stack.peek()] <= arr[i]) {
                stack.pop();
            }
  
            // Update the previous greater
            // element for arr[i]
            pge[i] = stack.isEmpty() ? -1 : stack.peek();
  
            // Push the current index to
            // the stacl
            stack.push(i);
        }
  
        // Return the PGE[] array
        return pge;
    }
  
    // Function to find the Next Greater Element
    private static int[] getNGE(int[] arr)
    {
        // Stores the next greater element
        // for each index
        int[] nge = new int[arr.length];
        Stack<Integer> stack = new Stack<>();
  
        // Traverse the array from the back
        for (int i = arr.length - 1; i >= 0; i--) {
  
            // Iterate until stack is empty
            // and top element is less than
            // the current element arr[i]
            while (!stack.isEmpty()
                   && arr[stack.peek()] <= arr[i]) {
                stack.pop();
            }
  
            // Update the next greater
            // element for arr[i]
            nge[i] = stack.isEmpty() ? arr.length
                                     : stack.peek();
  
            // Push the current index
            stack.push(i);
        }
  
        // Return the NGE[] array
        return nge;
    }
  
    // Function to find the count of
    // subarrays starting or ending at
    // index i having arr[i] as maximum
    private static void countSubarrays(int[] arr)
    {
  
        // Function call to find the
        // previous greater element
        // for each array elements
        int[] pge = getPGE(arr);
  
        // Function call to find the
        // previous greater element
        // for each elements
        int[] nge = getNGE(arr);
  
        // Traverse the array arr[]
        for (int i = 0; i < arr.length; i++) {
  
            // Print count of subarrays
            // satisfying the conditions
            System.out.print(
                nge[i] - pge[i] - 1 + " ");
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = new int[] { 3, 4, 1, 6, 2 };
        countSubarrays(arr);
    }
}
Output:
1 3 1 5 1

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :