Related Articles
Count indices where the maximum in the prefix array is less than that in the suffix array
• Last Updated : 21 Apr, 2021

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++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to print the count of indices``// in which the maximum in prefix arrays``// is less than that in the suffix array``void` `count(``int` `a[], ``int` `n)``{``    ``// If size of array is 1``    ``if` `(n == 1) {``        ``cout << 0;``        ``return``;``    ``}` `    ``// pre[]: Prefix array``    ``// suf[]: Suffix array``    ``int` `pre[n - 1], suf[n - 1];``    ``int` `max = a;` `    ``// Stores the required count``    ``int` `ans = 0, i;` `    ``pre = a;` `    ``// Find the maximum in prefix array``    ``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];` `    ``// Find the maximum in suffix array``    ``for` `(i = n - 2; i >= 1; i--) {` `        ``if` `(a[i] > max)``            ``max = a[i];` `        ``suf[i - 1] = max;``    ``}` `    ``// Traverse the array``    ``for` `(i = 0; i < n - 1; i++) {` `        ``// If maximum in prefix array``        ``// is less than maximum in``        ``// the suffix array``        ``if` `(pre[i] < suf[i])``            ``ans++;``    ``}` `    ``// Print the answer``    ``cout << ans;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 2, 3, 4, 8, 1, 4 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);` `    ``// Function Call``    ``count(arr, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``public` `class` `gfg``{``  ``// Function to print the count of indices``  ``// in which the maximum in prefix arrays``  ``// is less than that in the suffix array``  ``static` `void` `count(``int` `a[], ``int` `n)``  ``{` `    ``// If size of array is 1``    ``if` `(n == ``1``)``    ``{``      ``System.out.print(``0``);``      ``return``;``    ``}` `    ``// pre[]: Prefix array``    ``// suf[]: Suffix array``    ``int``[] pre = ``new` `int``[n - ``1``];``    ``int``[] suf = ``new` `int``[n - ``1``];``    ``int` `max = a[``0``];` `    ``// Stores the required count``    ``int` `ans = ``0``, i;         ``    ``pre[``0``] = a[``0``];` `    ``// Find the maximum in prefix array``    ``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``];` `    ``// Find the maximum in suffix array``    ``for``(i = n - ``2``; i >= ``1``; i--)``    ``{``      ``if` `(a[i] > max)``        ``max = a[i];                 ``      ``suf[i - ``1``] = max;``    ``}` `    ``// Traverse the array``    ``for``(i = ``0``; i < n - ``1``; i++)``    ``{` `      ``// If maximum in prefix array``      ``// is less than maximum in``      ``// the suffix array``      ``if` `(pre[i] < suf[i])``        ``ans++;``    ``}` `    ``// Print the answer``    ``System.out.print(ans);``  ``}` `  ``// Driver code``  ``public` `static` `void` `main(String[] args)``  ``{``    ``int` `arr[] = { ``2``, ``3``, ``4``, ``8``, ``1``, ``4` `};``    ``int` `N = arr.length;` `    ``// Function Call``    ``count(arr, N);``  ``}``}` `// This code is contributed by divyesh072019.`

## Python3

 `# Python program for the above approach` `# Function to print the count of indices``# in which the maximum in prefix arrays``# is less than that in the suffix array``def` `count(a, n) :` `    ``# If size of array is 1``    ``if` `(n ``=``=` `1``) :``        ``print``(``0``)``        ``return` `    ``# pre[]: Prefix array``    ``# suf[]: Suffix array``    ``pre ``=` `[``0``] ``*` `(n ``-` `1``)``    ``suf ``=` `[``0``] ``*` `(n ``-` `1``)``    ``max` `=` `a[``0``]` `    ``# Stores the required count``    ``ans ``=` `0` `    ``pre[``0``] ``=` `a[``0``]` `    ``# Find the maximum in prefix array``    ``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``]` `    ``# Find the maximum in suffix array``    ``for` `i ``in` `range``(n``-``2``, ``0``, ``-``1``):` `        ``if` `(a[i] > ``max``):``            ``max` `=` `a[i]` `        ``suf[i ``-` `1``] ``=` `max` `    ``# Traverse the array``    ``for` `i ``in` `range``(n ``-` `1``):` `        ``# If maximum in prefix array``        ``# is less than maximum in``        ``# the suffix array``        ``if` `(pre[i] < suf[i]) :``            ``ans ``+``=` `1` `    ``# Print the answer``    ``print``(ans)``    `  `# Driver Code` `arr ``=` `[ ``2``, ``3``, ``4``, ``8``, ``1``, ``4` `]``N ``=` `len``(arr)` `# Function Call``count(arr, N)` `# This code is contributed by code_hunt.`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{``    ` `// Function to print the count of indices``// in which the maximum in prefix arrays``// is less than that in the suffix array``static` `void` `count(``int``[] a, ``int` `n)``{``    ` `    ``// If size of array is 1``    ``if` `(n == 1)``    ``{``        ``Console.Write(0);``        ``return``;``    ``}``  ` `    ``// pre[]: Prefix array``    ``// suf[]: Suffix array``    ``int``[] pre = ``new` `int``[n - 1];``    ``int``[] suf = ``new` `int``[n - 1];``    ``int` `max = a;``  ` `    ``// Stores the required count``    ``int` `ans = 0, i;``    ` `    ``pre = a;``    ` `    ``// Find the maximum in prefix array``    ``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];``    ` `    ``// Find the maximum in suffix array``    ``for``(i = n - 2; i >= 1; i--)``    ``{``        ``if` `(a[i] > max)``            ``max = a[i];``            ` `        ``suf[i - 1] = max;``    ``}``  ` `    ``// Traverse the array``    ``for``(i = 0; i < n - 1; i++)``    ``{``        ` `        ``// If maximum in prefix array``        ``// is less than maximum in``        ``// the suffix array``        ``if` `(pre[i] < suf[i])``            ``ans++;``    ``}``    ` `    ``// Print the answer``    ``Console.Write(ans);``}` `// Driver code``static` `void` `Main()``{``    ``int``[] arr = { 2, 3, 4, 8, 1, 4 };``    ``int` `N = arr.Length;``    ` `    ``// Function Call``    ``count(arr, N);``}``}` `// This code is contributed by divyeshrabadiya07`

## Javascript

 ``
Output:
`3`

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