text: Count maximum possible pairs from an array having sum K | Function to count the maximum number of pairs from given array with sum K ; Sort array in increasing order ; Stores the final result ; Initialize the left and right pointers ; Traverse array until start < end ; Decrement right by 1 ; Increment left by 1 ; Increment result and left poleter by 1 and decrement right pointer by 1 ; Print the result ; Driver Code ; Function Call
code: function maxPairs ( nums , k ) { nums . sort ( ) ; let result = 0 ; let start = 0 , end = nums . length - 1 ; while ( start < end ) { if ( nums [ start ] + nums [ end ] > k ) end -- ; else if ( nums [ start ] + nums [ end ] < k ) start ++ ; else { start ++ ; end -- ; result ++ ; } } document . write ( result ) ; } let arr = [ 1 , 2 , 3 , 4 ] ; let K = 5 ; maxPairs ( arr , K ) ;