Skip to content

Commit 921efb7

Browse files
committed
Update search rotated sorted array
1 parent 380a8c0 commit 921efb7

File tree

7 files changed

+92
-10
lines changed

7 files changed

+92
-10
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ List of Programs related to data structures and algorithms
6666
11. Rotate array: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/11.rotateArray/rotateArray.js)
6767
[Playground](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/11.rotateArray/rotateArray.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/11.rotateArray/rotateArray.md)
6868

69-
12. Search in rotated sorted array: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/searchRotatedSortedArray.js)
70-
69+
12. Search in rotated sorted array: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/12.searchRotatedSortedArray/searchRotatedSortedArray.js)
70+
[Playground](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/12.searchRotatedSortedArray/searchRotatedSortedArray.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/11.searchRotatedSortedArray/searchRotatedSortedArray.md)
71+
7172
13. Container with most water: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/containerWithMostWater.js)
7273

7374
14. First missing positive number: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/firstMissingPositive/firstMissingPositive.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/firstMissingPositive/firstMissingPositive.md)

src/java1/algorithms/array/rotateArray/RotateArray.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This problem is solved with the help of two pointers, which is used to reverse s
3131

3232
**Time and Space complexity:**
3333

34-
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements. This is because we are performing three reversal operations on subarrays and each reversal operation takes O(n). So the overall time complexity is O(n).
34+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements. This is because we are performing three reversal operations on subarrays and each reversal operation takes `O(n)`. So the overall time complexity is `O(n)`.
3535

36-
Here, we don't use any additional datastructure due to in-place rotation. Hence, the space complexity will be O(1).
36+
Here, we don't use any additional datastructure due to in-place rotation. Hence, the space complexity will be `O(1)`.
3737

src/java1/algorithms/array/SearchRotatedSortedArray.java renamed to src/java1/algorithms/array/searchRotatedSortedArray/SearchRotatedSortedArray.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package searchRotatedSortedArray;
12
// TC: O(log n), SC: O(1)
23
public class SearchRotatedSortedArray {
34
private static int searchRotatedSortedArray(int[] nums, int target) {
@@ -26,10 +27,10 @@ private static int searchRotatedSortedArray(int[] nums, int target) {
2627
}
2728

2829
public static void main(String[] args) {
29-
int[] nums = {3, 4, 5, 6, 7, 0, 1, 2};
30-
System.out.println(searchRotatedSortedArray(nums, 7));
31-
3230
int[] nums1 = {3, 4, 5, 6, 7, 0, 1, 2};
33-
System.out.println(searchRotatedSortedArray(nums1, 9));
31+
System.out.println(searchRotatedSortedArray(nums1, 7));
32+
33+
int[] nums2 = {3, 4, 5, 6, 7, 0, 1, 2};
34+
System.out.println(searchRotatedSortedArray(nums2, 9));
3435
}
3536
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
**Description:**
2+
Given a sorted array `nums` in an ascending order, which is rotated at some known pivot index. You need to search for a given `target` value, if found in the array return it's index otherwise return -1.
3+
4+
**Note:** There are no duplicate values exist. The runtime complexity should be O(log n).
5+
6+
### Examples
7+
8+
Example 1:
9+
Input: nums = [3, 4, 5, 6, 7, 0, 1, 2], target = 7
10+
Output: 4
11+
12+
Example 2:
13+
Input: nums = [3, 4, 5, 6, 7, 0, 1, 2], target = 9
14+
Output: -1
15+
16+
**Algorithmic Steps:**
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. You can decide the presence of target element by verifying either in left side subarray or right side subarray. The algorithmic approach can be summarized as follows:
18+
19+
1. Initialize left(`left`) and right pointers(`right`) to the beginning and ending index of an array.
20+
21+
2. Iterate over input array until either left pointer reaches right pointer or target element found.
22+
23+
3. Calculate the middle index of the array to divide into two equal subarrays.
24+
25+
4. If the middle element is equals to target, return the middle element index as output.
26+
27+
5. If the left element is less than or equal to middle element(i.e, `nums[left] <= nums[mid]`), the left portion of input array is sorted. In this case, if the target element exists between left and middle elements, update the right pointer to the element before the middle element(`right = mid-1`). This logic indicates that target exists with in first subarray. Otherwise update the left pointer to the element just after the middle element(`left = mid+1`) to indicate the target exists with in second subarray.
28+
29+
6. In the same way, if the right element is greater than or equal to right element(i.e, `nums[mid] <= nums[right]`), the right portion of input array is sorted. In this case, if the target element exists between right and middle elements, update the left pointer to the element after the middle element(`left = mid+1`). This logic indicates that target exists with in second subarray. Otherwise update the right pointer to the element just before the middle element(`right = mid-1`) to indicate the target exists with in first subarray.
30+
31+
7. Repeat steps 4-6 element found or until end of array reached.
32+
33+
8. Return -1, if the target doesn't exist with in input array.
34+
35+
36+
**Time and Space complexity:**
37+
38+
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 subarrays each time and find the index of target element using binary search alogorithm.
39+
40+
Here, we don't use any additional datastructure other than two left pointer variables. Hence, the space complexity will be O(1).

src/javascript/algorithms/array/11.rotateArray/rotateArray.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This problem is solved with the help of two pointers, which is used to reverse s
3131

3232
**Time and Space complexity:**
3333

34-
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements. This is because we are performing three reversal operations on subarrays and each reversal operation takes O(n). So the overall time complexity is O(n).
34+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements. This is because we are performing three reversal operations on subarrays and each reversal operation takes `O(n)`. So the overall time complexity is `O(n)`.
3535

36-
Here, we don't use any additional datastructure due to in-place rotation. Hence, the space complexity will be O(1).
36+
Here, we don't use any additional datastructure due to in-place rotation. Hence, the space complexity will be `O(1)`.
3737

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
**Description:**
2+
Given a sorted array `nums` in an ascending order, which is rotated at some known pivot index. You need to search for a given `target` value, if found in the array return it's index otherwise return -1.
3+
4+
**Note:** There are no duplicate values exist. The runtime complexity should be O(log n).
5+
6+
### Examples
7+
8+
Example 1:
9+
Input: nums = [3, 4, 5, 6, 7, 0, 1, 2], target = 7
10+
Output: 4
11+
12+
Example 2:
13+
Input: nums = [3, 4, 5, 6, 7, 0, 1, 2], target = 9
14+
Output: -1
15+
16+
**Algorithmic Steps:**
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. You can decide the presence of target element by verifying either in left side subarray or right side subarray. The algorithmic approach can be summarized as follows:
18+
19+
1. Initialize left(`left`) and right pointers(`right`) to the beginning and ending index of an array.
20+
21+
2. Iterate over input array until either left pointer reaches right pointer or target element found.
22+
23+
3. Calculate the middle index of the array to divide into two equal subarrays.
24+
25+
4. If the middle element is equals to target, return the middle element index as output.
26+
27+
5. If the left element is less than or equal to middle element(i.e, `nums[left] <= nums[mid]`), the left portion of input array is sorted. In this case, if the target element exists between left and middle elements, update the right pointer to the element before the middle element(`right = mid-1`). This logic indicates that target exists with in first subarray. Otherwise update the left pointer to the element just after the middle element(`left = mid+1`) to indicate the target exists with in second subarray.
28+
29+
6. In the same way, if the right element is greater than or equal to right element(i.e, `nums[mid] <= nums[right]`), the right portion of input array is sorted. In this case, if the target element exists between right and middle elements, update the left pointer to the element after the middle element(`left = mid+1`). This logic indicates that target exists with in second subarray. Otherwise update the right pointer to the element just before the middle element(`right = mid-1`) to indicate the target exists with in first subarray.
30+
31+
7. Repeat steps 4-6 element found or until end of array reached.
32+
33+
8. Return -1, if the target doesn't exist with in input array.
34+
35+
36+
**Time and Space complexity:**
37+
38+
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 subarrays each time and find the index of target element using binary search alogorithm.
39+
40+
Here, we don't use any additional datastructure other than two left pointer variables. Hence, the space complexity will be O(1).

0 commit comments

Comments
 (0)