Maximize subarrays count containing the maximum and minimum Array element after deleting at most one element
Last Updated :
17 Jun, 2022
Given an array arr[] of size N. The task is to maximize the count of subarrays that contain both the minimum and maximum elements of the array by deleting at most one element from the array.
Examples:
Input: arr[] = {7, 2, 5, 4, 3, 1}
Output: 4
Explanation:
Delete 1 from the array then resultant array will be {7, 2, 5, 4, 3}. So the number of subarrays which contain maximum element 7 and minimum element 2 will be 4 {[7, 2], [7, 2, 5], [7, 2, 5, 4], [7, 2, 5, 4, 3]}
Input: arr[] = {9, 9, 8, 9, 8, 9, 9, 8, 9, 8}
Output: 43
Naive Approach: The simplest approach is to delete every element and then count the number of subarrays having the minimum and maximum element of the resultant array.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: This approach is based on the observation that deletion of elements other than the maximum or minimum element never maximizes the overall result. Below are the steps:
- Initialize the overall result with INT_MIN.
- Create a function say proc which returns the number of subarrays containing the smallest and the largest element of the array.
- To calculate the number of subarrays find the starting and ending index of the subarray using two pointers approach:
- Initialize the smallest and the largest element say low and high with the last element of the array.
- Initialize two pointers p1 and p2 with the last index of array which stores the location of low and high.
- Now, iterate over the array and check if the current element is less than low, then update p1.
- If the current element is more than high, then update p2.
- At each step, update the maximum number of subarrays.
- Now, calculate the number of subarrays in the following three cases:
- Without removing any element
- After removing the largest element
- After removing the smallest element.
- Take the maximum of all three cases.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long proc(vector< int > &v)
{
long long int n = v.size();
int low = v[n - 1], high = v[n - 1];
long long int p1 = n, p2 = n;
long long ans = 0;
for ( int i = n - 1; i >= 0; i--) {
int x = v[i];
if (x < low) {
low = x;
ans = 0;
}
else if (x > high) {
high = x;
ans = 0;
}
if (x == low)
p1 = i;
if (x == high)
p2 = i;
ans += n - max(p1, p2);
}
return ans;
}
long long subarray(vector< int >& v)
{
long long int n=v.size();
if (n<=1)
return n;
long long ans=proc(v);
int low=v[0],pos_low=0,high=v[0],pos_high=0;
for ( int i = 1; i < n; i++) {
int x = v[i];
if (x < low) {
low = x;
pos_low = i;
}
else if (x > high) {
high = x;
pos_high = i;
}
}
vector< int >u;
u=v;
u.erase(u.begin()+pos_low);
ans=max(ans,proc(u));
vector< int >w;
w=v;
w.erase(w.begin()+pos_high);
return max(ans,proc(w));
}
int main()
{
vector< int >v;
v.push_back(7);
v.push_back(2);
v.push_back(5);
v.push_back(4);
v.push_back(3);
v.push_back(1);
cout<<subarray(v)<<endl;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
static long subarray(List<Integer> v)
{
int n = v.size();
if (n <= 1 )
return n;
long ans = proc(v);
int low = v.get( 0 ), pos_low = 0 ;
int high = v.get( 0 ), pos_high = 0 ;
for ( int i = 1 ; i < n; i++) {
int x = v.get(i);
if (x < low) {
low = x;
pos_low = i;
}
else if (x > high) {
high = x;
pos_high = i;
}
}
List<Integer> u
= new ArrayList<>(
Collections.nCopies(n, 0 ));
Collections.copy(u, v);
u.remove(pos_low);
ans = Math.max(ans, proc(u));
List<Integer> w
= new ArrayList<>(
Collections.nCopies(n, 0 ));
Collections.copy(w, v);
w.remove(pos_high);
return Math.max(ans, proc(w));
}
static long proc(List<Integer> v)
{
int n = v.size();
int low = v.get(n - 1 ), high = v.get(n - 1 );
int p1 = n, p2 = n;
long ans = 0 ;
for ( int i = n - 1 ; i >= 0 ; i--) {
int x = v.get(i);
if (x < low) {
low = x;
ans = 0 ;
}
else if (x > high) {
high = x;
ans = 0 ;
}
if (x == low)
p1 = i;
if (x == high)
p2 = i;
ans += n - Math.max(p1, p2);
}
return ans;
}
public static void main(String[] args)
{
List<Integer> arr = Arrays.asList( 7 , 2 , 5 , 4 , 3 , 1 );
System.out.println(subarray(arr));
}
}
|
Python3
def proc(v):
n = len (v);
low = v[n - 1 ]
high = v[n - 1 ]
p1 = n
p2 = n;
ans = 0 ;
for i in range (n - 1 , - 1 , - 1 ):
x = v[i];
if (x < low):
low = x;
ans = 0 ;
elif (x > high):
high = x;
ans = 0 ;
if (x = = low): p1 = i;
if (x = = high): p2 = i;
ans + = n - max (p1, p2);
return ans;
def subarray(v):
n = len (v);
if (n < = 1 ):
return n;
ans = proc(v);
low = v[ 0 ]
pos_low = 0
high = v[ 0 ]
pos_high = 0
for i in range ( 1 , n):
x = v[i];
if (x < low):
low = x;
pos_low = i;
elif (x > high):
high = x;
pos_high = i;
u = v[:];
del u[pos_low];
ans = max (ans, proc(u));
w = v[:];
del w[pos_high];
return max (ans, proc(w));
v = [];
v.append( 7 );
v.append( 2 );
v.append( 5 );
v.append( 4 );
v.append( 3 );
v.append( 1 );
print (subarray(v));
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
static long subarray(List< int > v)
{
int n = v.Count;
if (n <= 1){
return n;
}
long ans = proc(v);
int low = v[0], pos_low = 0;
int high = v[0], pos_high = 0;
for ( int i = 1 ; i < n ; i++) {
int x = v[i];
if (x < low) {
low = x;
pos_low = i;
}
else if (x > high) {
high = x;
pos_high = i;
}
}
List< int > u = new List< int >();
for ( int i = 0 ; i < n ; i++){
u.Add(0);
}
u = new List< int >(v);
u.Remove(v[pos_low]);
ans = Math.Max(ans, proc(u));
List< int > w = new List< int >();
for ( int i = 0 ; i < n ; i++){
w.Add(0);
}
w = new List< int >(v);
w.Remove(v[pos_high]);
return Math.Max(ans, proc(w));
}
static long proc(List< int > v)
{
int n = v.Count;
int low = v[n-1], high = v[n-1];
int p1 = n, p2 = n;
long ans = 0;
for ( int i = n - 1; i >= 0; i--) {
int x = v[i];
if (x < low) {
low = x;
ans = 0;
}
else if (x > high) {
high = x;
ans = 0;
}
if (x == low)
p1 = i;
if (x == high)
p2 = i;
ans += n - Math.Max(p1, p2);
}
return ans;
}
public static void Main( string [] args){
List< int > arr = new List< int >{
7, 2, 5, 4, 3, 1
};
Console.WriteLine(subarray(arr));
}
}
|
Javascript
<script>
function proc(v)
{
let n = v.length;
let low = v[n - 1],
high = v[n - 1];
let p1 = n,
p2 = n;
let ans = 0;
for (let i = n - 1; i >= 0; i--) {
let x = v[i];
if (x < low) {
low = x;
ans = 0;
}
else if (x > high) {
high = x;
ans = 0;
}
if (x == low) p1 = i;
if (x == high) p2 = i;
ans += n - Math.max(p1, p2);
}
return ans;
}
function subarray(v) {
let n = v.length;
if (n <= 1) {
return n;
}
let ans = proc(v);
let low = v[0],
pos_low = 0,
high = v[0],
pos_high = 0;
for (let i = 1; i < n; i++) {
let x = v[i];
if (x < low) {
low = x;
pos_low = i;
} else if (x > high) {
high = x;
pos_high = i;
}
}
let u = [...v];
u.splice(pos_low, 1);
ans = Math.max(ans, proc(u));
let w = [...v];
w.splice(pos_high, 1);
return Math.max(ans, proc(w));
}
let v = [];
v.push(7);
v.push(2);
v.push(5);
v.push(4);
v.push(3);
v.push(1);
document.write(subarray(v));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...