Given an array arr[] of size N, the task is to find the number of prefix arrays whose maximum is less than the maximum element in the remaining suffix array.
Examples:
Input: arr[] = {2, 3, 4, 8, 1, 4}
Output: 3
Explanation:
Prefix array = {2}, {2, 3}, {2, 3, 4}, {2, 3, 4, 8}, {2, 3, 4, 8, 1}
Respective Suffix = {3, 4, 8, 1, 4}, {4, 8, 1, 4}, {8, 1, 4}, {1, 4}, {4}
Only the first 3 the prefix arrays have maximum valued element less than the maximum value element in the respective suffix array.
Input: arr[] = {4, 4, 4, 4, 5}
Output: 4
Naive Approach: The simplest approach is to find all possible prefixes and suffixes of the given array and count the number of indices where maximum element in the prefix array is less than the maximum element in the suffix array.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to store the maximum value for every prefix and suffix of the array and then calculate the number of prefix arrays with a maximum value less than its corresponding suffix array. Follow the steps below to solve the problem:
- Initialize a variable, say ans and two arrays prefix[] and suffix[] of size N.
- Traverse the array arr[] over the range [0, N – 1] and for each ith index in prefix[], store the maximum element as prefix[i] = max(prefix[i – 1], arr[i]).
- Traverse the given array in the reverse order over the range [N – 1, 0] and for each ith index in suffix[], store the maximum element as suffix[i] = max(suffix[i + 1], arr[i]).
- Now, traverse the array arr[] over the range [0, N – 2] and if prefix[i] is less than suffix[i], then increment ans by 1.
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void count( int a[], int n)
{
if (n == 1) {
cout << 0;
return ;
}
int pre[n - 1], suf[n - 1];
int max = a[0];
int ans = 0, i;
pre[0] = a[0];
for (i = 1; i < n - 1; i++) {
if (a[i] > max)
max = a[i];
pre[i] = max;
}
max = a[n - 1];
suf[n - 2] = a[n - 1];
for (i = n - 2; i >= 1; i--) {
if (a[i] > max)
max = a[i];
suf[i - 1] = max;
}
for (i = 0; i < n - 1; i++) {
if (pre[i] < suf[i])
ans++;
}
cout << ans;
}
int main()
{
int arr[] = { 2, 3, 4, 8, 1, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
count(arr, N);
return 0;
}
|
Java
public class gfg
{
static void count( int a[], int n)
{
if (n == 1 )
{
System.out.print( 0 );
return ;
}
int [] pre = new int [n - 1 ];
int [] suf = new int [n - 1 ];
int max = a[ 0 ];
int ans = 0 , i;
pre[ 0 ] = a[ 0 ];
for (i = 1 ; i < n - 1 ; i++)
{
if (a[i] > max)
max = a[i];
pre[i] = max;
}
max = a[n - 1 ];
suf[n - 2 ] = a[n - 1 ];
for (i = n - 2 ; i >= 1 ; i--)
{
if (a[i] > max)
max = a[i];
suf[i - 1 ] = max;
}
for (i = 0 ; i < n - 1 ; i++)
{
if (pre[i] < suf[i])
ans++;
}
System.out.print(ans);
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 4 , 8 , 1 , 4 };
int N = arr.length;
count(arr, N);
}
}
|
Python3
def count(a, n) :
if (n = = 1 ) :
print ( 0 )
return
pre = [ 0 ] * (n - 1 )
suf = [ 0 ] * (n - 1 )
max = a[ 0 ]
ans = 0
pre[ 0 ] = a[ 0 ]
for i in range (n - 1 ):
if (a[i] > max ):
max = a[i]
pre[i] = max
max = a[n - 1 ]
suf[n - 2 ] = a[n - 1 ]
for i in range (n - 2 , 0 , - 1 ):
if (a[i] > max ):
max = a[i]
suf[i - 1 ] = max
for i in range (n - 1 ):
if (pre[i] < suf[i]) :
ans + = 1
print (ans)
arr = [ 2 , 3 , 4 , 8 , 1 , 4 ]
N = len (arr)
count(arr, N)
|
C#
using System;
class GFG{
static void count( int [] a, int n)
{
if (n == 1)
{
Console.Write(0);
return ;
}
int [] pre = new int [n - 1];
int [] suf = new int [n - 1];
int max = a[0];
int ans = 0, i;
pre[0] = a[0];
for (i = 1; i < n - 1; i++)
{
if (a[i] > max)
max = a[i];
pre[i] = max;
}
max = a[n - 1];
suf[n - 2] = a[n - 1];
for (i = n - 2; i >= 1; i--)
{
if (a[i] > max)
max = a[i];
suf[i - 1] = max;
}
for (i = 0; i < n - 1; i++)
{
if (pre[i] < suf[i])
ans++;
}
Console.Write(ans);
}
static void Main()
{
int [] arr = { 2, 3, 4, 8, 1, 4 };
int N = arr.Length;
count(arr, N);
}
}
|
Javascript
<script>
function count(a, n)
{
if (n == 1)
{
document.write(0);
return ;
}
let pre = new Array(n - 1);
let suf = new Array(n - 1);
let max = a[0];
let ans = 0, i;
pre[0] = a[0];
for (i = 1; i < n - 1; i++)
{
if (a[i] > max)
max = a[i];
pre[i] = max;
}
max = a[n - 1];
suf[n - 2] = a[n - 1];
for (i = n - 2; i >= 1; i--)
{
if (a[i] > max)
max = a[i];
suf[i - 1] = max;
}
for (i = 0; i < n - 1; i++)
{
if (pre[i] < suf[i])
ans++;
}
document.write(ans);
}
let arr = [ 2, 3, 4, 8, 1, 4 ];
let N = arr.length;
count(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)