Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal | Function ot find the length of the smallest subarray ; Stores ( prefix Sum , index ) as ( key , value ) mappings ; Iterate till N ; Update the prefixSum ; Update the length ; Put the latest index to find the minimum length ; Update the length ; Return the answer ; Function to find the length of the largest subarray ; Stores the sum of all array elements after modification ; Change greater than k to 1 ; Change smaller than k to - 1 ; Change equal to k to 0 ; Update total_sum ; No deletion required , return 0 ; Delete smallest subarray that has sum = total_sum ; Driver Code ; Function call
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 ) ) ;