Case I : Diff of arr [ i ] and arr [ i - 1 ] is less then K , add to maxSum Case II : Diff between arr [ i ] and arr [ i - 1 ] is not less then K , move to next i since with sorting we know , arr [ i ] - arr [ i - 1 ] < rr [ i ] - arr [ i - 2 ] and so on .
if ( arr [ i ] - arr [ i - 1 ] < k ) {