Minimum pairs required to be removed such that the array does not contain any pair with sum K | Function to find the maximum count of pairs required to be removed such that no pairs exist whose sum equal to K ; Stores maximum count of pairs required to be removed such that no pairs exist whose sum equal to K ; Base Case ; Sort the array ; Stores index of left pointer ; Stores index of right pointer ; Stores sum of left and right pointer ; If s equal to k ; Update cntPairs ; Update left ; Update right ; If s > k ; Update right ; Update left ; Return the cntPairs ; Driver Code ; Function call
function maxcntPairsSumKRemoved ( arr , k ) { var cntPairs = 0 ; if ( arr . length <= 1 ) return cntPairs ; arr . sort ( ) ; var left = 0 ; var right = arr . length - 1 ; while ( left < right ) { var s = arr [ left ] + arr [ right ] ; if ( s == k ) { cntPairs += 1 ; left += 1 ; right -= 1 ; } else if ( s > k ) right -= 1 ; else left += 1 ; } return cntPairs ; } var arr = [ 1 , 2 , 3 , 4 ] ; var K = 5 ; document . write ( maxcntPairsSumKRemoved ( arr , K ) ) ;