Introduction - An interview problem

#1

Hi Everyone,

I started reading this book and I don’t have a CS background so I might be wrong on this one.

I believe there is a mistake in the book (Introduction Chapter) or I don’t understand it correctly. In case it’s a mistake do let me know else please help me understand it properly.

Please see the image and I have already highlighted the problem. I believe it has to be n-1 instead n-2.

Please provide a bit detailed explanation in case n-2 is correct and not n-1.

Help me in either case.

0 Likes

#2

Hey @kkhosla,

Sorry that I cannot see the picture you uploaded, would you mind to send this to my email (tsung.hsien.lee@gmail.com) such that I can take a look of the potential mistake you mentioned?

0 Likes

#3

Hi,

I sent you the email on your email address. Kindly look and please help in either case.

0 Likes

#4

Hey @KKhosla,

I got the picture, and thanks for sending that. I think it is still n - 2 because in the case of n - 1, we are updating trying to use the last element (n - 1) to find the difference between it with elements after it, where there is no element after that, so that is the reason we use n - 2 here.

From the other side, if you take a closer look of the function, you will see that even we use n - 1 in that function, it still does not change the result as n * (n - 1) / 2, which proves the correctness of this. One easy way to verify that is using a small example (e.g., 3 elements), and try to enumerate all possible combinations to see if the total number of combinations is actually 3 * (3 - 1) / 2 = 3.

Feel free to let me know if you have any other questions.

0 Likes