`{"id":"46d5a222-a322-4947-8fc0-ddb0e4f82199","name":"Find Minimum In Rotated Sorted Array","description":"1. Suppose an array of length n sorted in ascending order is rotated between 1 and n times.\r\n2. Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].\r\n3. Given the sorted rotated array nums of unique elements, return the minimum element of this array.\r\n4. You must write an algorithm that runs in O(log n) time.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"n == nums.length\r\n1 &lt;= n &lt;= 5000\r\n-5000 &lt;= nums[i] &lt;= 5000\r\nAll the integers of nums are unique.\r\nnums is sorted and rotated between 1 and n times.","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint solution(vector<int> & arr){\n //Write your code here\n}\n\nint main(){\n int n;\n cin>>n;\n vector<int> arr(n,0);\n for(int i=0; i<n; i++){\n cin>>arr[i];\n }\n int ans = solution(arr);\n cout<<ans<<endl;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int findMinimum(int[]arr) {\r\n //write your code here\r\n return -1;\r\n }\r\n\r\n public static void main(String[]args) {\r\n //input work\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n\r\n int[]arr = new int[n];\r\n for(int i = 0 ; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n int ans = findMinimum(arr);\r\n System.out.println(ans);\r\n }\r\n}"},"python":{"code":"def main():\r\n pass\r\n\r\nif_name_=='__main__':\r\n main()"}},"points":10,"difficulty":"easy","sampleInput":"6\r\n5 6 1 2 3 4","sampleOutput":"1","questionVideo":"https://www.youtube.com/embed/Kcj2NGnuSNg","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"cb36811c-9cd7-4d80-aa52-ae9b8409862a","name":"Searching And Sorting For Intermediate","slug":"searching-and-sorting-for-intermediate-10001","type":0},{"id":"03328050-a985-4d06-9d2a-544a07ebca9c","name":"Find Minimum In Rotated Sorted Array","slug":"find-minimum-in-rotated-sorted-array","type":1}],"next":{"id":"ea0f600f-c04a-43ed-97c8-457ed22deb3e","name":"Find minimum in rotated sorted array","type":3,"slug":"find-minimum-in-rotated-sorted-array"},"prev":{"id":"fe8a4dec-8ed7-4a87-841a-315c054482d5","name":"Search In Rotated Sorted Array MCQ","type":0,"slug":"search-in-rotated-sorted-array-mcq"}}}`

# Find Minimum In Rotated Sorted Array

1. Suppose an array of length n sorted in ascending order is rotated between 1 and n times. 2. Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. 3. Given the sorted rotated array nums of unique elements, return the minimum element of this array. 4. You must write an algorithm that runs in O(log n) time.

`{"id":"46d5a222-a322-4947-8fc0-ddb0e4f82199","name":"Find Minimum In Rotated Sorted Array","description":"1. Suppose an array of length n sorted in ascending order is rotated between 1 and n times.\r\n2. Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].\r\n3. Given the sorted rotated array nums of unique elements, return the minimum element of this array.\r\n4. You must write an algorithm that runs in O(log n) time.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"n == nums.length\r\n1 &lt;= n &lt;= 5000\r\n-5000 &lt;= nums[i] &lt;= 5000\r\nAll the integers of nums are unique.\r\nnums is sorted and rotated between 1 and n times.","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint solution(vector<int> & arr){\n //Write your code here\n}\n\nint main(){\n int n;\n cin>>n;\n vector<int> arr(n,0);\n for(int i=0; i<n; i++){\n cin>>arr[i];\n }\n int ans = solution(arr);\n cout<<ans<<endl;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int findMinimum(int[]arr) {\r\n //write your code here\r\n return -1;\r\n }\r\n\r\n public static void main(String[]args) {\r\n //input work\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n\r\n int[]arr = new int[n];\r\n for(int i = 0 ; i < n; i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n int ans = findMinimum(arr);\r\n System.out.println(ans);\r\n }\r\n}"},"python":{"code":"def main():\r\n pass\r\n\r\nif_name_=='__main__':\r\n main()"}},"points":10,"difficulty":"easy","sampleInput":"6\r\n5 6 1 2 3 4","sampleOutput":"1","questionVideo":"https://www.youtube.com/embed/Kcj2NGnuSNg","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"cb36811c-9cd7-4d80-aa52-ae9b8409862a","name":"Searching And Sorting For Intermediate","slug":"searching-and-sorting-for-intermediate-10001","type":0},{"id":"03328050-a985-4d06-9d2a-544a07ebca9c","name":"Find Minimum In Rotated Sorted Array","slug":"find-minimum-in-rotated-sorted-array","type":1}],"next":{"id":"ea0f600f-c04a-43ed-97c8-457ed22deb3e","name":"Find minimum in rotated sorted array","type":3,"slug":"find-minimum-in-rotated-sorted-array"},"prev":{"id":"fe8a4dec-8ed7-4a87-841a-315c054482d5","name":"Search In Rotated Sorted Array MCQ","type":0,"slug":"search-in-rotated-sorted-array-mcq"}}}`

Editor

# Find Minimum In Rotated Sorted Array

easy

1. Suppose an array of length n sorted in ascending order is rotated between 1 and n times. 2. Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. 3. Given the sorted rotated array nums of unique elements, return the minimum element of this array. 4. You must write an algorithm that runs in O(log n) time.

## Constraints

n == nums.length 1 <= n <= 5000 -5000 <= nums[i] <= 5000 All the integers of nums are unique. nums is sorted and rotated between 1 and n times.

## Format

### Input

Input is managed for you

### Output

Output is managed for you

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}6 5 6 1 2 3 4```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}1`

Question Video

Discussions

Show Discussion

Related Resources