Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
// the second part of the following 'if' is to handle the smallest sum when we ...

Learner

Jan 7, 2022

// the second part of the following 'if' is to handle the smallest sum when we // have more than one solution

Isn't the second part unnecessary? Smallest is smallest, nothing changes even if they are the same. Can you please explain with an example if I have misunderstood?

3

0

Comments
Comments
Design Gurus
Design Gurus4 years ago

This problem is asking to find a triplet with two conditions. 1) Closest to the target 2) If there are more than one such triplet, find the triplet with the smallest sum.

For example: Array [0, 0, 1, 1, 2, 6], Target = 5

We have two solutions here: (1, 1, 2) & (0, 0, ...

L
Learner 4 years ago

Awesome thanks for the explanation!

T
Thilak G4 years ago

Why cant the closest difference be in the -ve ? The below condition - can somebody explain it's purpose please - Math.abs(target_diff) === Math.abs(smallest_difference) && target_diff > smallest_difference)

A
Adam Seery3 years ago

i think adding this as a 4th example would be really helpful. thanks for the explanation

Design Gurus
Design Gurus3 years ago

Adam Seery , sure.

E
Ethan Shook3 years ago

Thanks for the explanation, I didn't want to move on before understanding the intention of that line. A larger sum of the three pointers (index, left, and right) would result in a smaller or further negative number when subtracted from the target if I'm getting it corre...

On this page

Problem Statement

Try it yourself