Skip to main content
added 7 characters in body
Source Link
CiaPan
  • 1.9k
  • 1
  • 12
  • 17

I'm not going to review the code, but rather give an algorithmic hint.

If that was actually some programming contest, there will probably be some weird test cases in it. One of weirdest I can think of is a loooong list of equal values. It will make the code presented to scan the whole list, which takes a loooong linear time.

You could savereduce a time complexity by getting rid of linear scans and making two binary searches instead, with different conditions: one for the first element equal to the sought one, and one for the last equal.

Then you just test if the desired key was found and (if so) how far apart the two searches stopped.

I'm not going to review the code, but rather give an algorithmic hint.

If that was actually some programming contest, there will probably be some weird test cases in it. One of weirdest I can think of is a loooong list of equal values. It will make the code presented to scan the whole list, which takes a loooong linear time.

You could save a complexity by getting rid of linear scans and making two binary searches instead, with different conditions: one for the first element equal to the sought one, and one for the last equal.

Then you just test if the desired key was found and (if so) how far apart the two searches stopped.

I'm not going to review the code, but rather give an algorithmic hint.

If that was actually some programming contest, there will probably be some weird test cases in it. One of weirdest I can think of is a loooong list of equal values. It will make the code presented to scan the whole list, which takes a loooong linear time.

You could reduce a time complexity by getting rid of linear scans and making two binary searches instead, with different conditions: one for the first element equal to the sought one, and one for the last equal.

Then you just test if the desired key was found and (if so) how far apart the two searches stopped.

deleted 19 characters in body
Source Link
CiaPan
  • 1.9k
  • 1
  • 12
  • 17

I'm not going to review the code, but rather give an algorithmic hint.

If that was actually some programming contest, there will probably be some weird test cases in it. One of weirdest I can think of is a loooong list of equal values. It will make the code presented to scan the whole list, which takes a loooong linear time.

You could save a complexity by getting rid of linear scans and making two binary searches instead, with different conditions: one for the first element greater-or-equalequal to the sought one, and one for the last less-or-equalequal.

Then you just test if the desired key was found and (if so) how far apart the two searches stopped.

I'm not going to review the code, but rather give an algorithmic hint.

If that was actually some programming contest, there will probably be some weird test cases in it. One of weirdest I can think of is a loooong list of equal values. It will make the code presented to scan the whole list, which takes a loooong linear time.

You could save a complexity by getting rid of linear scans and making two binary searches instead, with different conditions: one for the first element greater-or-equal to the sought one, and one for the last less-or-equal.

Then you just test if the desired key was found and (if so) how far apart the two searches stopped.

I'm not going to review the code, but rather give an algorithmic hint.

If that was actually some programming contest, there will probably be some weird test cases in it. One of weirdest I can think of is a loooong list of equal values. It will make the code presented to scan the whole list, which takes a loooong linear time.

You could save a complexity by getting rid of linear scans and making two binary searches instead, with different conditions: one for the first element equal to the sought one, and one for the last equal.

Then you just test if the desired key was found and (if so) how far apart the two searches stopped.

Source Link
CiaPan
  • 1.9k
  • 1
  • 12
  • 17

I'm not going to review the code, but rather give an algorithmic hint.

If that was actually some programming contest, there will probably be some weird test cases in it. One of weirdest I can think of is a loooong list of equal values. It will make the code presented to scan the whole list, which takes a loooong linear time.

You could save a complexity by getting rid of linear scans and making two binary searches instead, with different conditions: one for the first element greater-or-equal to the sought one, and one for the last less-or-equal.

Then you just test if the desired key was found and (if so) how far apart the two searches stopped.