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

0% completed

Vote For New Content
Why do we need to use this specific sequence while finding the diff: if ((arr[st...

Learner

Feb 14, 2022

Why do we need to use this specific sequence while finding the diff: if ((arr[start] - key) < (key - arr[end])) instead of the absolute value?

Can you please provide an example why absolute won't work?

1

0

Comments
Comments
Design Gurus
Design Gurus4 years ago

Absolute difference would work too; it depends how you write it, taking care of all negative/positive numbers.

Mohammed Dh Abbas
Mohammed Dh Abbasa year ago

because of the

while start <= end:

start will be larger than end and end will be less than start after the while finishes. you are looping as long as the start is <= end

meaning the

arr[start] is larger than the key since you did not find anythi...

On this page