Find number of subarrays ending with arr[i] where arr[i] is the minimum element of that subarray
Given an array, arr[] of size N, the task is to find the number of sub-arrays ending with arr[i] and arr[i] is the minimum element of that sub-array.Â
Note: Elements in array will be unique.
Examples:Â Â
Input: arr[] = {3, 1, 2, 4}Â
Output: 1 2 1 1Â
Explanation:Â
Subarrays ending with 3 where 3 is the minimum element = {3}Â
Subarrays ending with 1 where 1 is the minimum element = {3, 1}, {1}Â
Subarrays ending with 2 where 2 is the minimum element = {2}Â
Subarrays ending with 4 where 4 is the minimum element = {4}
Input: arr[] = {5, 4, 3, 2, 1}Â
Output: 1 2 3 4 5Â
Approach: The idea is similar to Next Greater Element , We have to find the previous smaller element by maintaining a stack.
Step-wise approach for the problem is:Â Â
- Push the first element (arr[0]) of the array with count as 1 in the stack because first element will be the sub-array itself ending with current element arr[0] and minimum of the subarray.
- Then for each element arr[i] in the array-Â
- Pop the elements from the stack until the top of the stack is greater than the current element and add the count of the popped element in the count of current element.
- Push current element and the count as a pair in the stack.
For example: For arr[] = {3, 1, 2, 4},Â
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int min_subarray( int a[], int n)
{
stack<pair< int , int > > st;
for ( int i = 0; i < n; ++i) {
int count = 1;
while (!st.empty() &&
st.top().first > a[i]) {
count += st.top().second;
st.pop();
}
st.push({ a[i], count });
cout << count << " " ;
}
}
int main()
{
int a[] = {5, 1, 3, 2, 1};
int n = sizeof (a) / sizeof (a[0]);
min_subarray(a, n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
static class Pair
{
int first;
int second;
public Pair( int x, int y)
{
this .first = x;
this .second = y;
}
}
static void min_subarray( int []a, int n)
{
Stack<Pair> st = new Stack<Pair>();
for ( int i = 0 ; i < n; ++i)
{
int count = 1 ;
while (st.empty() == false &&
st.peek().first > a[i])
{
count += st.peek().second;
st.pop();
}
st.push( new Pair (a[i], count ));
System.out.print(count + " " );
}
}
public static void main(String []args)
{
int []a = { 5 , 4 , 3 , 2 , 1 };
int n = a.length;
min_subarray(a, n);
}
}
|
C#
using System;
using System.Collections.Generic;
class GFG
{
class Pair
{
public int first;
public int second;
public Pair( int x, int y)
{
this .first = x;
this .second = y;
}
}
static void min_subarray( int []a, int n)
{
Stack<Pair> st = new Stack<Pair>();
for ( int i = 0; i < n; ++i)
{
int count = 1;
while (st.Count != 0 &&
st.Peek().first > a[i])
{
count += st.Peek().second;
st.Pop();
}
st.Push( new Pair (a[i], count ));
Console.Write(count + " " );
}
}
public static void Main(String []args)
{
int []a = {5, 4, 3, 2, 1};
int n = a.Length;
min_subarray(a, n);
}
}
|
Javascript
<script>
function min_subarray(a, n)
{
var st = [];
for ( var i = 0; i < n; ++i) {
var count = 1;
while (st.length!=0 &&
st[st.length-1][0] > a[i]) {
count += st[st.length-1][1];
st.pop();
}
st.push([a[i], count]);
document.write( count + " " );
}
}
var a = [5, 4, 3, 2, 1];
var n = a.length;
min_subarray(a, n);
</script>
|
Python3
def min_subarray(a, n) :
st = [];
for i in range (n) :
count = 1 ;
while len (st) ! = 0 and st[ - 1 ][ 0 ] > a[i] :
count + = st[ - 1 ][ 1 ];
st.pop();
st.append(( a[i], count ));
print (count,end = " " );
if __name__ = = "__main__" :
a = [ 5 , 4 , 3 , 2 , 1 ];
n = len (a);
min_subarray(a, n);
|
Time Complexity: O(n) ,where n is size of given array.
Auxiliary Space: O(n)
Last Updated :
07 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...