Skip to content

Commit c076a89

Browse files
committed
Update min rotated sorted array logic
1 parent e089e45 commit c076a89

File tree

6 files changed

+112
-22
lines changed

6 files changed

+112
-22
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ List of Programs related to data structures and algorithms
5757
8. Maximum product subarray: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/8.maxProductSubarray/maxProductSubarray.js)
5858
[Playground](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/8.maxProductSubarray/maxProductSubarray.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/8.maxProductSubarray/maxProductSubarray.md)
5959

60-
9. Find minimum in rotated sorted array: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/minRotatedSortedArray.js)
60+
9. Find minimum in rotated sorted array: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/9.minRotatedSortedarray/minRotatedSortedarray.js)
61+
[Playground](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/9.minRotatedSortedarray/minRotatedSortedarray.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/9.minRotatedSortedarray/minRotatedSortedarray.md)
6162

6263
10. Maximum Circular subarray: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/maxCircularSubArray.js)
6364

src/java1/algorithms/array/MinimumRotatedSortedArray.java renamed to src/java1/algorithms/array/minimumRotatedSortedarray/MinimumRotatedSortedarray.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1+
package minimumRotatedSortedarray;
12
//TC: O(log n), SC: O(1)
23

3-
public class MinimumRotatedSortedArray {
4+
public class MinimumRotatedSortedarray {
45
private static int minRotatedSortedArray(int[] nums) {
6+
int result = nums[0];
7+
58
if (nums.length == 1)
6-
return nums[0];
9+
return result;
710
if (nums.length == 2)
811
return Math.min(nums[0], nums[1]);
912

1013
int left = 0, right = nums.length - 1;
11-
if (nums[left] < nums[right]) {
12-
return nums[left];
13-
}
1414

1515
while (left <= right) {
16+
if (nums[left] <= nums[right]) {
17+
result = nums[left];
18+
break;
19+
}
20+
1621
int mid = left + (right - left) / 2;
17-
if (nums[mid] > nums[mid + 1])
18-
return nums[mid + 1];
19-
if (nums[mid - 1] > nums[mid])
20-
return nums[mid];
2122

22-
if (nums[left] < nums[mid]) {
23+
if (nums[mid] > nums[right]) {
2324
left = mid + 1;
2425
} else {
25-
right = mid - 1;
26+
right = mid;
2627
}
2728
}
28-
return 0;
29+
return result;
2930
}
3031

3132
public static void main(String[] args) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
**Description:**
2+
Given a sorted array `nums` of length `n` with all unique elements, in which the array is rotated between 1 and n times. Return the minimum element of this array.
3+
4+
**Note:** You need to write an algorithm that runs in O(log n) time.
5+
6+
### Examples
7+
Example 1:
8+
Input: nums = [3, 4, 5, 6, 7, 1, 2]
9+
Output: 1
10+
11+
Example 2:
12+
Input: nums = [ 0, 1, 2, 4, 5, 6, 7, 8]
13+
Output: 0
14+
15+
**Algorithmic Steps:**
16+
17+
This problem is solved with the help of binary search technique. Since it is a rotated sorted array, there will be two subararys of elements in an ascending order. Once you find the correct subarray, the left most element is the minimum value. The algorithmic approach can be summarized as follows:
18+
19+
1. Initialize the minimum value(`result`) to first element of an array.
20+
21+
2. Add preliminary checks by returning the first element if the length of array is one, or minimum of first two values if the length of an array is two.
22+
23+
3. Initialize left and right pointers(`left` and `right`) to first index and last index of an array.
24+
25+
4. Iterate over an input array using while loop with the condition of left pointer is less than or equal to right pointer.
26+
27+
5. If the left element is less than or equal to right element, update the result value and break the while loop to return the minimum value.
28+
29+
6. Calculate the middle index of an array to separate the sorted subarrays.
30+
31+
7. If the middle value is greater than right most element, the minimum value exists with in the right side subarray. So the left pointer will be updated to next element of middle element.
32+
33+
8. If the middle value is less than or equal to left most element, the minimum value exists with in the left side subarray. So the right pointer will be updated to the middle element.
34+
35+
9. Repeat steps 4-7 until you find the left most value which is minimum in the entire input array.
36+
37+
10. Return the result element after the end of an iteration.
38+
39+
**Time and Space complexity:**
40+
41+
This algorithm has a time complexity of `O(log n)`, where `n` is the number of elements. This is because we divide the array into two subarray each time and find the minimum element in the correct subarray using binary search alogorithm.
42+
43+
Here, we don't use any additional datastructure other than the result variable. Hence, the space complexity will be O(1).

src/javascript/algorithms/array/8.maxProductSubarray/maxProductSubarray.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This problem is solved with the help of Kadane's algorithm(dynamic programming t
2020

2121
2. Initialize two variables named `currentMax` and `currentMin` to `1`. They are used to keep track of the running maximum and minimum products.
2222

23-
3. Iterate over the entire input array using for-each loop
23+
3. Iterate over the entire input array using for-of loop
2424

2525
4. Find the temporary maximum and minimum products for each current element.
2626

src/javascript/algorithms/array/minRotatedSortedArray.js renamed to src/javascript/algorithms/array/9.minRotatedSortedarray/minRotatedSortedarray.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
function minRotatedSortedArray(nums) {
2+
let result = nums[0];
23
if(nums.length === 1) return nums[0];
34
if(nums.length === 2) return Math.min(nums[0], nums[1]);
45

56
let left = 0, right = nums.length - 1;
6-
if(nums[left] < nums[right]) return nums[left];
77

8-
while(left <= right) {
9-
let mid = left + (right - left)/2;
8+
while(left < right) {
9+
if(nums[left] < nums[right]) {
10+
result = nums[left];
11+
break;
12+
}
1013

11-
if(nums[mid] > nums[mid+1]) return nums[mid+1];
12-
if(nums[mid-1] > nums[mid]) return nums[mid];
14+
let mid = left + (right - left)/2;
1315

14-
if(nums[left] < nums[mid]) {
16+
if(nums[mid] > nums[right]) {
1517
left = mid + 1;
1618
} else {
17-
right = mid - 1;
19+
right = mid;
1820
}
1921
}
20-
return 0;
22+
return result;
2123
}
2224

2325
let sortedArray = [3, 4, 5, 6, 7, 1, 2];
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
**Description:**
2+
Given a sorted array `nums` of length `n` with all unique elements, in which the array is rotated between 1 and n times. Return the minimum element of this array.
3+
4+
**Note:** You need to write an algorithm that runs in O(log n) time.
5+
6+
### Examples
7+
Example 1:
8+
Input: nums = [3, 4, 5, 6, 7, 1, 2]
9+
Output: 1
10+
11+
Example 2:
12+
Input: nums = [ 0, 1, 2, 4, 5, 6, 7, 8]
13+
Output: 0
14+
15+
**Algorithmic Steps:**
16+
17+
This problem is solved with the help of binary search technique. Since it is a rotated sorted array, there will be two subararys of elements in an ascending order. Once you find the correct subarray, the left most element is the minimum value. The algorithmic approach can be summarized as follows:
18+
19+
1. Initialize the minimum value(`result`) to first element of an array.
20+
21+
2. Add preliminary checks by returning the first element if the length of array is one, or minimum of first two values if the length of an array is two.
22+
23+
3. Initialize left and right pointers(`left` and `right`) to first index and last index of an array.
24+
25+
4. Iterate over an input array using while loop with the condition of left pointer is less than or equal to right pointer.
26+
27+
5. If the left element is less than or equal to right element, update the result value and break the while loop to return the minimum value.
28+
29+
6. Calculate the middle index of an array to separate the sorted subarrays.
30+
31+
7. If the middle value is greater than right most element, the minimum value exists with in the right side subarray. So the left pointer will be updated to next element of middle element.
32+
33+
8. If the middle value is less than or equal to left most element, the minimum value exists with in the left side subarray. So the right pointer will be updated to the middle element.
34+
35+
9. Repeat steps 4-7 until you find the left most value which is minimum in the entire input array.
36+
37+
10. Return the result element after the end of an iteration.
38+
39+
**Time and Space complexity:**
40+
41+
This algorithm has a time complexity of `O(log n)`, where `n` is the number of elements. This is because we divide the array into two subarray each time and find the minimum element in the correct subarray using binary search alogorithm.
42+
43+
Here, we don't use any additional datastructure other than the result variable. Hence, the space complexity will be O(1).

0 commit comments

Comments
 (0)