A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

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:**

_ arr[] = {5, 7, 2, 8, 7, 4, 5, 9}, K = 5_Input:

_ 2_Output:

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.

_ arr[] = {12, 16, 12, 13, 10}, K = 13_Input:

_ 3_Output:

Explanation:

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

`// C++ Program to implement`

`// the above approach`

`#include <bits/stdc++.h>`

`**using**`

`**namespace**`

`std;`

`// Function ot find the length`

`// of the smallest subarray`

`**int**`

`smallSubarray(``**int**`

`arr[],`

`**int**`

`n,`

`**int**`

`total_sum)`

`{`

`// Stores (prefix Sum, index)`

`// as (key, value) mappings`

`unordered_map<``**int**``,`

`**int**``> m;`

`**int**`

`length = INT_MAX;`

`**int**`

`prefixSum = 0;`

`// Iterate till N`

`**for**`

`(``**int**`

`i = 0; i < n; i++) {`

`// Update the prefixSum`

`prefixSum += arr[i];`

`// Update the length`

`**if**`

`(prefixSum == total_sum) {`

`length = min(length, i + 1);`

`}`

`// Put the latest index to`

`// find the minimum length`

`m[prefixSum] = i;`

`**if**`

`(m.count(prefixSum - total_sum)) {`

`// Update the length`

`length`

`= min(length,`

`i - m[prefixSum - total_sum]);`

`}`

`}`

`// Return the answer`

`**return**`

`length;`

`}`

`// Function to find the length of`

`// the largest subarray`

`**int**`

`smallestSubarrayremoved(``**int**`

`arr[],`

`**int**`

`n,`

`**int**`

`k)`

`{`

`// Stores the sum of all array`

`// elements after modification`

`**int**`

`total_sum = 0;`

`**for**`

`(``**int**`

`i = 0; i < n; i++) {`

`// Change greater than k to 1`

`**if**`

`(arr[i] > k) {`

`arr[i] = 1;`

`}`

`// Change smaller than k to -1`

`**else**`

`**if**`

`(arr[i] < k) {`

`arr[i] = -1;`

`}`

`// Change equal to k to 0`

`**else**`

`{`

`arr[i] = 0;`

`}`

`// Update total_sum`

`total_sum += arr[i];`

`}`

`// No deletion required, return 0`

`**if**`

`(total_sum == 0) {`

`**return**`

`0;`

`}`

`**else**`

`{`

`// Delete smallest subarray`

`// that has sum = total_sum`

`**return**`

`smallSubarray(arr, n,`

`total_sum);`

`}`

`}`

`// Driver Code`

`**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;`

`}`

**Output:**

`3`

** Time Complexity:**_ O(N)_

** Auxiliary Space:**_ O(N)_

Attention reader! Donâ€™t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

arrays hash mathematical cpp-unordered_map prefix-sum subarray

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.