You've successfully subscribed to Smartcodehub ™ Blog
Great! Next, complete checkout for full access to Smartcodehub ™ Blog
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
3 number sum in array problem

3 number sum in array problem

Today in this blog we are going to see how we can solve 3 sum array problem using data structure.so before getting started let me make you understand what is 3 sum array problem.

Abhishek Dana
Abhishek Dana

Hello there today in this blog we are going to see how we can solve 3 sum array problem using data structure.so before getting started let me make you understand what is 3 sum array problem.

So basically the would be an array give and the sum of three number would be given and we have to find the numbers that when sum up together there value should be equal to targeted sum.

So simple example of the 3 sum algorithm is :

array = [1, 2, 4, 5, 7, 8, 9, 6, 5];
target = 24;

Now we have to find the three number that when sum up together and we get the targeted value so here the values are 7 8 9.

So what is the logic behind this here we can call this as three window sliding problem also.

So to solve the problem here is a small demonstration by Neeraj sir  given to me when i was solving this problem was

const arr = [1, 2, 4, 5, 7, 8, 9, 6, 5];
const target = 24;

let first = 0;
let second = 1;
let third = 2;

// order of o(n) space o(1)
while (first < arr.length - 1) {
  if (arr[first] + arr[second] + arr[third] === target) break;

  if (third - second === 1 && second - first === 1) third++;
  else if (third - second > 1 && second - first === 1) second++;
  else if (third - second === 1 && second - first > 1) first++;
}
console.log("final answer", arr[first], arr[second], arr[third]);

So here we have got the array and the targeted value so to find the solution

we have firstly initialize three pointer named first , second , third. when the pointer are assing a value so first pointer points to starting index of array, second pointer points to second value for array and third pointer points to third value of array.

After pointer initialization, we have run a while loop which run till the first pointer points to the last value of array.

Inside while loop we have check the condition if we found that targeted sum then the loop should break.

After that we have again given a check of if condition when first we check if third pointer - second pointer === 1 which means is space between this pointer is 1 and second pointer - first pointer === 1 then we need to increment third pointer.

In second if condition we have check if the space between third and second is greater then 1 but second and first is equal to 1 then we need to increment second pointer.

Similarly in third condition we have check if space between third and second pointer is equal to 1 and second and first pointer is greater then 1 then we need to increment first pointer.

By this simple logic we get the solution for our problem.

Thank you.