Given an array of integers, find three integers in the array that sum to a specific target number.
left
and right
pointers.sort
the array first.for loop
till the length of the array.left = i+1
and right = length - 1
left + right + arr[i]
.is equal to the target
, push the triplets in a global array and continue the for loop and adjust left and right pointers accordingly.currentSum
is LESS THAN
the targetSum
- This means we can increment the left
pointer and we might get a greater number, resulting in a greater currentSum
which MIGHT be our target.currentSum
is GREATER THAN
the targetSum
- This means we can decrement the right
pointer and we might get a lesser number, resulting in a lesser currentSum
which MIGHT be our target.triplets
global array at the end.O(nlogn)
- sorting the arrayO(N^2)