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

0% completed

Vote For New Content
Can you please help me in explaining the if condition '(Math.abs(targetDiff) == ...

Abhinav Gupta

Jan 28, 2022

Can you please help me in explaining the if condition '(Math.abs(targetDiff) == Math.abs(smallestDifference) && targetDiff > smallestDifference)'

Also, can you please help me with an example?

Thanks

0

0

Comments
Comments
A
Abhinav Gupta4 years ago

Without above if condition also, examples mentioned in this unit are giving correct output.

N
Nirbhay Lourembam2 years ago

I think the above is for checking negative values. But I also do not understand what this checks exactly.

(Math.abs(targetDiff) == Math.abs(smallestDifference)

I think this means that if the targetDiff or smallestDifference is negative then we compare the absolu...

M
Ming Chen2 years ago

This if condition is checking that if targetDiff and smallestDifference absolute values are equal, then, they would check to see if targetDiff is a larger number than smallestDifference because it would be the value with the smaller sum. (It's confusing because the nami...

On this page

Problem Statement

Solution

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity