If sum is greater than K
if ( leftSum + rightSum > K ) break ; } } if ( leftTaken + rightTaken <= N ) { for ( let i = 0 ; i < leftTaken ; i ++ ) document . write ( A [ i ] + " " ) ; for ( let i = 0 ; i < rightTaken ; i ++ ) document . write ( A [ N - i - 1 ] + " " ) ; }