Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
Given an integer K and an array arr[] consisting of N integers, the task is to find the length of the subarray of smallest possible length to be removed such that the count of array elements smaller than and greater than K in the remaining array are equal.
Examples:
Input: arr[] = {5, 7, 2, 8, 7, 4, 5, 9}, K = 5
Output: 2
Explanation:
Smallest subarray required to be removed is {8, 7}, to make the largest resultant array {5, 7, 2, 4, 5, 9} satisfy the given condition.
Input: arr[] = {12, 16, 12, 13, 10}, K = 13
Output: 3
Explanation:
smallest subarray required to be removed is {12, 13, 10} to make the largest resultant array {12, 16} satisfy the given condition.
Naive Approach: The simplest approach to solve the problem is to generate all possible subarrays, and traverse the remaining array, to keep count of array elements that are strictly greater than and smaller than integer K. Then, select the smallest subarray whose deletion gives an array having equal number of smaller and greater elements.
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Efficient Approach: The idea is to use Hashing with some modification to the array to solve it in O(N) time. The given array can have 3 types of elements:
- element = K: change element to 0 (because we need elements, that are strictly greater than K or smaller than K)
- element > K: change element to 1
- element < K: change element to -1
Now, calculate the sum of all array elements and store it in a variable, say total_sum. Now, the total_sum can have three possible ranges of values:
- If total_sum = 0: all 1s are cancelled by -1s. So, equal number of greater and smaller elements than K are already present. No deletion operation is required. Hence, print 0 as the required answer.
- If total_sum > 0: some 1s are left uncancelled by -1s. i.e. array has more number of greater elements than K and less number of smaller elements than K. So, find the smallest subarray of sum = total_sum as it is the smallest subarray to be deleted.
- If total_sum < 0: some -1s are left uncancelled by 1s. i.e. array has more number of smaller elements than k and less number of greater elements than K. So, find the smallest subarray of sum = total_sum as it is the smallest subarray to be deleted.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int smallSubarray( int arr[], int n,
int total_sum)
{
unordered_map< int , int > m;
int length = INT_MAX;
int prefixSum = 0;
for ( int i = 0; i < n; i++) {
prefixSum += arr[i];
if (prefixSum == total_sum) {
length = min(length, i + 1);
}
m[prefixSum] = i;
if (m.count(prefixSum - total_sum)) {
length
= min(length,
i - m[prefixSum - total_sum]);
}
}
return length;
}
int smallestSubarrayremoved( int arr[], int n,
int k)
{
int total_sum = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] > k) {
arr[i] = 1;
}
else if (arr[i] < k) {
arr[i] = -1;
}
else {
arr[i] = 0;
}
total_sum += arr[i];
}
if (total_sum == 0) {
return 0;
}
else {
return smallSubarray(arr, n,
total_sum);
}
}
int main()
{
int arr[] = { 12, 16, 12, 13, 10 };
int K = 13;
int n = sizeof (arr) / sizeof ( int );
cout << smallestSubarrayremoved(
arr, n, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int smallSubarray( int arr[], int n,
int total_sum)
{
Map<Integer,
Integer> m = new HashMap<Integer,
Integer>();
int length = Integer.MAX_VALUE;
int prefixSum = 0 ;
for ( int i = 0 ; i < n; i++)
{
prefixSum += arr[i];
if (prefixSum == total_sum)
{
length = Math.min(length, i + 1 );
}
m.put(prefixSum, i);
if (m.containsKey(prefixSum - total_sum))
{
length = Math.min(length,
i - m.get(prefixSum -
total_sum));
}
}
return length;
}
static int smallestSubarrayremoved( int arr[], int n,
int k)
{
int total_sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] > k)
{
arr[i] = 1 ;
}
else if (arr[i] < k)
{
arr[i] = - 1 ;
}
else
{
arr[i] = 0 ;
}
total_sum += arr[i];
}
if (total_sum == 0 )
{
return 0 ;
}
else
{
return smallSubarray(arr, n, total_sum);
}
}
public static void main(String[] args)
{
int arr[] = { 12 , 16 , 12 , 13 , 10 };
int K = 13 ;
int n = arr.length;
System.out.println(
smallestSubarrayremoved(arr, n, K));
}
}
|
Python3
import sys
def smallSubarray(arr, n, total_sum):
m = {}
length = sys.maxsize
prefixSum = 0
for i in range (n):
prefixSum + = arr[i]
if (prefixSum = = total_sum):
length = min (length, i + 1 )
m[prefixSum] = i
if ((prefixSum - total_sum) in m.keys()):
length = min (length,
i - m[prefixSum - total_sum])
return length
def smallestSubarrayremoved(arr, n, k):
total_sum = 0
for i in range (n):
if (arr[i] > k):
arr[i] = 1
elif (arr[i] < k):
arr[i] = - 1
else :
arr[i] = 0
total_sum + = arr[i]
if (total_sum = = 0 ):
return 0
else :
return smallSubarray(arr, n,
total_sum)
arr = [ 12 , 16 , 12 , 13 , 10 ]
K = 13
n = len (arr)
print (smallestSubarrayremoved(arr, n, K))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int smallSubarray( int []arr, int n,
int total_sum)
{
Dictionary< int ,
int > m = new Dictionary< int ,
int >();
int length = int .MaxValue;
int prefixSum = 0;
for ( int i = 0; i < n; i++)
{
prefixSum += arr[i];
if (prefixSum == total_sum)
{
length = Math.Min(length, i + 1);
}
if (m.ContainsKey(prefixSum))
m[prefixSum] = i;
else
m.Add(prefixSum, i);
if (m.ContainsKey(prefixSum - total_sum))
{
length = Math.Min(length,
i - m[prefixSum -
total_sum]);
}
}
return length;
}
static int smallestSubarrayremoved( int []arr,
int n, int k)
{
int total_sum = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] > k)
{
arr[i] = 1;
}
else if (arr[i] < k)
{
arr[i] = -1;
}
else
{
arr[i] = 0;
}
total_sum += arr[i];
}
if (total_sum == 0)
{
return 0;
}
else
{
return smallSubarray(arr, n, total_sum);
}
}
public static void Main(String[] args)
{
int []arr = { 12, 16, 12, 13, 10 };
int K = 13;
int n = arr.Length;
Console.WriteLine(
smallestSubarrayremoved(arr, n, K));
}
}
|
Javascript
<script>
function smallSubarray(arr,n,total_sum)
{
let m = new Map();
let length = Number.MAX_VALUE;
let prefixSum = 0;
for (let i = 0; i < n; i++)
{
prefixSum += arr[i];
if (prefixSum == total_sum)
{
length = Math.min(length, i + 1);
}
m.set(prefixSum, i);
if (m.has(prefixSum - total_sum))
{
length = Math.min(length,
i - m.get(prefixSum -
total_sum));
}
}
return length;
}
function smallestSubarrayremoved(arr,n,k)
{
let total_sum = 0;
for (let i = 0; i < n; i++)
{
if (arr[i] > k)
{
arr[i] = 1;
}
else if (arr[i] < k)
{
arr[i] = -1;
}
else
{
arr[i] = 0;
}
total_sum += arr[i];
}
if (total_sum == 0)
{
return 0;
}
else
{
return smallSubarray(arr, n, total_sum);
}
}
let arr=[12, 16, 12, 13, 10];
let K = 13;
let n = arr.length;
document.write(smallestSubarrayremoved(arr, n, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
17 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...