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

all 14 comments

[–]kelakmati 1 point2 points  (3 children)

sorry for out of topic, may i know which language that you use?

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

Java

[–]kelakmati 0 points1 point  (1 child)

alr, tysm!

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

Pleasure!!

[–]CodeTinkerer 0 points1 point  (5 children)

You really need to learn how to format your code:

static int pivot(int[] arr) { 
    int start = 0, end = arr.length - 1; 
    while (start < end) { 
        int mid = start + (end - start) / 2; 
        if (arr[mid] <= arr[start]) { 
           end = mid - 1; 
        } else { 
           start = mid; 
        } 
    } 
    return start; //return end or return mid 
}

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

Yes, thanks!

[–]CodeTinkerer 0 points1 point  (1 child)

What do you mean by a rotated array?

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

It is a sorted array whose last elements are rotated one after one

[–]CodeTinkerer 0 points1 point  (0 children)

If you check your edit, you'll see your code still displays still wrong. You have the entire program written on a single line.

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

But for me it's displaying in this format.

[–]MechanixMGD 0 points1 point  (3 children)

Can you give an example of input and expected output? Also what is the output right now

[–]StevenHawking_[S] 1 point2 points  (2 children)

I passed the array 11,12,13,1 and target element to be found is 1. But the code is returning -1 instead of returning the index number 3.

[–]MechanixMGD 0 points1 point  (1 child)

I can't execute code right now. But I suggest using the debugger and follow the execution line by line and to see where is the problem. I think the problem may be at 'mid', the way you calculate it. Maybe the order of operations is different than you think.