Solution 1 - Find three elements in an array that sum to zero | Array Algorithms

preview_player
Показать описание
In this video we are going to find triplets in an array that sum to zero. we will be using a brute force method to achieve this. we will be running three loops to find the triplets
Рекомендации по теме
Комментарии
Автор

Omg ..wt a explanation I really flattened by the way u clearling the program completely.

knoorjahan
Автор

Very beautiful explaination bro.thank you so much bro.god bless you.please upload these kind of beautiful explaination and tricky questions

mohammedsuhailbasha
Автор

I think an optimization regarding the end point of each loop can be made

Imagine we only have 3 numbers, nums=[3, 5, 7]
the outer loop only needs to go to the point from where the middle loop will start
the middle loop will do the same, it will go until one to the end

summarizing outer < nums.length-2
middle < nums.length-1
inner < nums.length


Here is my approach


const nums=[3, -1, -7, -4, -5, 9, 10]



for (let i=0;i< nums.length-2;i++){

for(let j=i+1;j<nums.length-1;j++){

for(let k=j+1;k<nums.length;k++){

if(
console.log('found')
console.log(`${nums[i]} ${nums[j]} ${nums[k]}`)
}else{
continue;
}
}
}
}

ellsonmendesYT
Автор

If there is no possible scenario of array values where sum of 3 values =0, array index out of bounds exception will occur when i=array length -2.

maheshvelpuri
Автор

And what consecutive number.. please make video on that too

healelactix
Автор

Very Good explanation !!! Do we have an video for other Solutions as well ?

ashokkumaraug