brief summary of question is
- find a triplet in given array whose sum is 0 and
- such that i!=j, j!=k and k!=i
i able to find the triple and save it in the output array but challenge i am facing is in array like [0,0,0,0], same triplet is saving in output array more than once. although their index is different but all numbers are same i.e 0.
Here is my code. i am using multiple pointer algorithm.
function checkTriplet(nums, value) {
if (nums && nums.length > 0) {
nums = nums.sort((a, b) => a - b)
let i = 0, j = i + 1, k = nums.length - 1, output = [];
while (i < j && j < k && i < k) {
let sum = nums[i] + nums[j] + nums[k];
if (sum === value) {
output.push([nums[i], nums[j], nums[k]]);
console.log(output);
k--;
}
else {
if (sum < value) {
i++;
j++
}
else {
k--;
}
}
}
return output;
}
return false;
}