Open In App

Count subarrays which contains both the maximum and minimum array element

Last Updated : 15 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[] consisting of N distinct integers, the task is to find the number of subarrays which contains both the maximum and the minimum element from the given array.

Examples:

Input: arr[] = {1, 2, 3, 4}
Output:
Explanation:  
Only a single subarray {1, 2, 3, 4} consists of both the maximum (= 4) and the minimum (= 1) array elements.

Input: arr[] = {4, 1, 2, 3}
Output: 3
Explanation:  
Subarrays {4, 1}  , {4, 1, 2}, {4, 1, 2, 3}  consists of both the maximum(= 4) and the minimum(= 1) array elements .

Naive Approach: The simplest approach is to first, traverse the array and find the maximum and minimum of the array and then generate all possible subarrays of the given array. For each subarray, check if it contains both the maximum and the minimum array element. For all such subarrays, increase the count by 1. Finally, print the count of such subarrays.

Code-

C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;

// Function to count subarray
// containing  both maximum and
// minimum array elements
int countSubArray(int arr[], int n)
{
    int maxi=INT_MIN;
    int mini=INT_MAX;
    
    for(int i=0;i<n;i++){
        maxi=max(maxi,arr[i]);
        mini=min(mini,arr[i]);
    }
   
    int count=0;
    
    for(int i=0;i<n;i++){
        int temp_max=arr[i];
        int temp_min=arr[i];
        for(int j=i;j<n;j++){
            if(arr[j]>temp_max){temp_max=arr[j];}
            if(arr[j]<temp_min){temp_min=arr[j];}
            
            //Checking that our subarray will contain 
            //maximum and minimum element of array
            if((mini==temp_min) && (maxi==temp_max) ){
                count++;
            }
        }
    }
    
    return count;
}

// Driver Code
int main()
{
    int arr[] = { 4,1,2,3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    // Function call
    cout << countSubArray(arr, n);

    return 0;
}
Java Python3 C# JavaScript

Output-

3


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

Efficient Approach:  Follow the steps below to optimize the above approach:

  • Find the index of the maximum and minimum elements. Let i and j be the respective indices such that i < j.
  • All the subarray which starts from indices up to i and ends at indices after j will contain the maximum as well as the minimum array element.
  • Therefore, the possible indices for the starting index of the subarray are [0, i] (total = i + 1 ).
  • Therefore, the possible indices for the ending index of the subarray are [j, N - 1]  (total = N - j).
  • Therefore, the count of subarrays is given by (i + 1) * ( N - j).

Below is the implementation of the above approach:

C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;

// Function to count subarray
// containing  both maximum and
// minimum array elements
int countSubArray(int arr[], int n)
{
    // If the length of the
    // array is less than 2
    if (n < 2)
        return n;

    // Find the index of maximum element
    int i
        = max_element(arr, arr + n) - arr;

    // Find the index of minimum element
    int j
        = min_element(arr, arr + n) - arr;

    // If i > j, then swap
    // the value of i and j
    if (i > j)
        swap(i, j);

    // Return the answer
    return (i + 1) * (n - j);
}

// Driver Code
int main()
{
    int arr[] = { 4, 1, 2, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    // Function call
    cout << countSubArray(arr, n);

    return 0;
}
Java Python3 C# JavaScript

Output
3

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


Article Tags :
Practice Tags :

Similar Reads