Maximize count of non | Function to count the maximum number of subarrays with sum K ; Stores all the distinct prefixSums obtained ; Stores the prefix sum of the current subarray ; Stores the count of subarrays with sum K ; If a subarray with sum K is already found ; Increase count ; Reset prefix sum ; Clear the set ; Insert the prefix sum ; Driver Code
function CtSubarr ( arr , N , K ) { var st = new Set ( ) ; var prefixSum = 0 ; st . add ( prefixSum ) ; var res = 0 ; for ( var i = 0 ; i < N ; i ++ ) { prefixSum += arr [ i ] ; if ( st . has ( prefixSum - K ) ) { res += 1 ; prefixSum = 0 ; st = new Set ( ) ; st . add ( 0 ) ; } st . add ( prefixSum ) ; } return res ; } var arr = [ - 2 , 6 , 6 , 3 , 5 , 4 , 1 , 2 , 8 ] ; var N = arr . length ; var K = 10 ; document . write ( CtSubarr ( arr , N , K ) ) ;