u/DravitGupta

Are the testcases for this problem not that tight or am I missing something? (Problem 33. Search in a rotated sorted array)

Are the testcases for this problem not that tight or am I missing something? (Problem 33. Search in a rotated sorted array)

A pretty standard question - Search in rotated sorted array

My idea was to find the index of the smallest element in the array, that index will divide the whole array in 2 arrays, which are both sorted. Then I can check the existence of target in both of those sorted arrays. This works when the array is rotated at least once and isn't in its default sorted position.

If the array is in its default sorted position, directly doing binary search to check the existence of target works.

I made one submission, it got accepted. But after a while I read my program again and thought that it should have failed instead. Like, for nums = [11, 33] it doesn't matter what the target is, my incorrect program will throw an Out of Bounds Exception, but it somehow gets accepted.

Link to my incorrect submission - Incorrect submission (I've also attached a photo of it below, if the link doesn't work)

This should have failed according to me

I know that replacing 31st line with return low is the correct thing to do, and I did rectify it.

But my main question is that am I thinking correctly? The program provided above isn't the correct solution right?

reddit.com
u/DravitGupta — 14 days ago

Did Leetcode miss some testcases or am I tripping? (Problem 33. Search in a rotated sorted array)

A pretty standard question - Search in rotated sorted array

My idea was to find the index of the smallest element in the array, that index will divide the whole array in 2 arrays, which are both sorted. Then I can check the existence of target in both of those sorted arrays. This works when the array is rotated at least once and isn't in its default sorted position.

If the array is in its default sorted position, directly doing binary search to check the existence of target works.

I made one submission, it got accepted. But after a while I read my program again and thought that it should have failed instead. Like, for nums = [11, 33] it doesn't matter what the target is, my incorrect program will throw an Out of Bounds Exception, but it somehow gets accepted.

Link to my incorrect submission - Incorrect submission (I've also attached a photo of it below, if the link doesn't work)

This should have failed according to me

I know that replacing 31st line with return low is the correct thing to do, and I did rectify it.

But my main question is that am I thinking correctly? The program provided above isn't the correct solution right?

reddit.com
u/DravitGupta — 14 days ago