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 <bits/stdc++.h>
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[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
cout << ans;
}
// Driver Code
int main()
{
int arr[] = { 2, 3, 4, 8, 1, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
// 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[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
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
<script>
// Javascript 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
function count(a, n)
{
// If size of array is 1
if (n == 1)
{
document.write(0);
return;
}
// pre[]: Prefix array
// suf[]: Suffix array
let pre = new Array(n - 1);
let suf = new Array(n - 1);
let max = a[0];
// Stores the required count
let 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
document.write(ans);
}
let arr = [ 2, 3, 4, 8, 1, 4 ];
let N = arr.length;
// Function Call
count(arr, N);
// This code is contributed by suresh07.
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Rearrange the given array to minimize the indices with prefix sum at least arr[i] Given an array arr[] consisting of N positive integers, rearrange the array to minimize the number of indices i such that the prefix sum from index 1 to index i-1 is greater than or equal to the current element arr[i] i.e. arr[1]+arr[2]+...+arr[i-1] >= arr[i]. Examples: Input: arr[] = [4, 2, 1]Ou
6 min read
Count of indices in Array having all prefix elements less than all in suffix Given an array arr[], the task is to calculate the total number of indices where all elements in the left part is less than all elements in the right part of the array. Examples: Input: arr[] = {1, 5, 4, 2, 3, 8, 7, 9}Output: 3Explanation: Lets consider left part = [1], right part = [5, 4, 2, 3, 8,
12 min read
Count of indices up to which prefix and suffix sum is equal for given Array Given an array arr[] of integers, the task is to find the number of indices up to which prefix sum and suffix sum are equal. Example: Input: arr = [9, 0, 0, -1, 11, -1]Output: 2Explanation: The indices up to which prefix and suffix sum are equal are given below:At index 1 prefix and suffix sum are 9
15+ min read
Find arrangement of Array such that prefix OR is maximum for each index Given an array arr[] consisting of N non-negative integers. The task is to find a rearrangement of the array such that the prefix OR for each index is maximum among all possible arrangements. Examples: Input: N = 4, arr[] = [1, 2, 4, 8] Output: [8, 4, 2, 1]Explanation: Prefix of the rearranged array
9 min read
Count pair of indices in Array having equal Prefix-MEX and Suffix-MEX Given an array arr[] of N elements, the task is to find the number of pairs of indices such that the Prefix-MEX of the one index is equal to the Suffix-MEX of the other index. Order of indices in pair matters. MEX of an array refers to the smallest missing non-negative integer of the array. For the
10 min read