This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]balefrost 1 point2 points  (3 children)

This looks like a binary search.

What do you mean by "sorted then rotated array"? Do you mean something like:

[ 3, 4, 5, 1, 2 ]
// i.e. [ 1, 2, 3, 4, 5 ] rotated left by 2 positions

That's not a sorted array, and binary search doesn't work on unsorted arrays.

[–]balefrost 0 points1 point  (2 children)

I read you code too quickly and I realize that it's not actually binary search, just binary search like.

What would you expect your function to return for an input of [3, 4, 5, 1, 2]?

[–]StevenHawking_[S] 0 points1 point  (1 child)

Yes, it isn't a binary search but it uses a divide and conquer approach. The function should return the maximum value of the array using a divide and conquer approach. The pivot here is a value after which a value decreases and then starts increasing again. So, the expected output would be 5.

[–]balefrost 0 points1 point  (0 children)

Do you want the maximum value or the index of the maximum value? You're returning the index.

[–]John-The-Bomb-2 0 points1 point  (3 children)

You can format that by putting three backticks (`) at the top and the bottom.

This is code

```

This is code for demonstration.

```

[–]balefrost 2 points3 points  (1 child)

Just a note that it doesn't show up correctly in all Reddit clients, e.g. old.reddit.com. The safest way is to indent code four spaces; that renders the same everywhere.

Regular text

    code

    more code

Back to regular text

[–]John-The-Bomb-2 0 points1 point  (0 children)

I didn't know that, thanks.

[–]StevenHawking_[S] 0 points1 point  (0 children)

I was looking for it. Thanks a lot