educative.io

Break statement after sign change

It is pointless to keep on calling the searchPair function upon the sign change, the array is sorted. Let’s say the first positive number is 5, i will not find Y and Z that yield me a -5 after that because my array is sorted.


Course: Grokking the Coding Interview: Patterns for Coding Questions - Learn Interactively
Lesson: Triplet Sum to Zero (medium) - Grokking the Coding Interview: Patterns for Coding Questions

Hi, @Sathvik_Reddi Thank you for contacting the Educative team with your query!
The logic used in the code box in the lesson has a specific approach that is just for implementation purposes. There can be multiple solutions to a problem. According to the Logic, the mathematical form is used X+Y+Z=0 which changes the equation to Y+Z=-X upon adding “-X” on both sides. If X=3, then the Y+Z=(-X), which says Y+Z=-3.

If you come up with another approach we appreciate it.

Please feel free to reply here if there is still some confusion.

Hope you have an amazing learning experience on Educative!