#C6602. Find Minimum Index in Rotated Sorted Array

    ID: 50381 Type: Default 1000ms 256MiB

Find Minimum Index in Rotated Sorted Array

Find Minimum Index in Rotated Sorted Array

You are given a rotated sorted array. A rotated sorted array is an array that has been sorted in ascending order and then rotated at some pivot unknown to you beforehand. For example, the array [1, 2, 3, 4, 5] might become [3, 4, 5, 1, 2] after rotation.

Your task is to determine the index of the minimum element in the array. It is guaranteed that the array contains no duplicate elements.

You are expected to solve this problem in \(O(\log n)\) time complexity using a binary search approach. Specifically, if \(A\) is the original sorted array and it is rotated, then the minimal element is the point where the order is disrupted. Formally, if the array is given as \(a_0, a_1, \dots, a_{n-1}\), then find the index \(i\) such that \(a_i \leq a_j \) for all \(0 \leq j < n\).

inputFormat

The first line of the input contains an integer \(T\) representing the number of test cases. Each test case is described as follows:

  • The first line contains an integer \(N\) representing the number of elements in the array.
  • The second line contains \(N\) space-separated integers representing the rotated sorted array.

It is guaranteed that each array is rotated at least once (or may be in sorted order if no rotation) and contains no duplicate elements.

outputFormat

For each test case, print a single line containing the index of the minimum element in the array.

The answer for each test case should be output on a separate line.

## sample
3
5
4 5 1 2 3
7
3 4 5 6 7 1 2
3
2 3 1
2

5 2

</p>