DEV Community

Discussion on: Java Daily Coding Problem #001

Collapse
 
iameugn profile image
imeugn

I would even check, if the outer number is greater equal than the number k. Thus we could even skip some for loops.

Collapse
 
awwsmm profile image
Andrew (he/him)

This sounds reasonable to me but note that the problem doesn't state whether the numbers in the array can be negative!

If I got this question in a coding interview, I'm not sure I would make that assumption.

Collapse
 
iameugn profile image
imeugn

I was actually thinking more in the line of:
Let n={3,5,10,15,17} and k=13. 15 and 17 should not be considered at all since they are greater.
The numbers are not supposed to be negative. The worst case remains the same.
But if n is meant to be sorted, we can then only check the left side of n up to the very closest number to k, which is still less than k. That would change the approach in general, I would think.