Find out the the largest element in a circular sorted array

Image rotation

Interview Question: Find out the fastest way to locate the largest element in a circular sorted array

You have given an array and your ask is to find largest element in that circular sorted array. Also remember the array don’t have any duplicates.

Let’s talk about the solution.

Solution : Always remember in a Sorted Rotated Array, the largest element will always be on the left side of the array. Similarly, the smallest element will always be on the right side of the array. So we can use Binary Search.

public class Printlargest {

    public static void main(String[] args) {
        int[] a = { 5, 6, 1, 2, 3, 4 };
        System.out.println(findLargestElement(a));

    }

    private static int findLargestElement(int[] a) {

        int start = 0;
        int last = a.length - 1;

        while (start + 1 < last) {

            int mid = (last - start) / 2 + start;
            if (mid < start) {
                mid = start;
            }
            if (mid > start) {
                last = mid - 1;
            } else {
                mid--;
            }

        } // while

        if (a[start] > a[last]) {
            return a[start];
        } else
            return a[last];

    }

}