diff --git "a/Beginner Level \360\237\223\201/Search in rotated sorted array/Question.md" "b/Beginner Level \360\237\223\201/Search in rotated sorted array/Question.md" new file mode 100644 index 00000000..a817e914 --- /dev/null +++ "b/Beginner Level \360\237\223\201/Search in rotated sorted array/Question.md" @@ -0,0 +1,9 @@ +There is an integer array nums sorted in ascending order (with distinct values). + +Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2]. + +Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums. + +You must write an algorithm with O(log n) runtime complexity. + +leetcode: https://leetcode.com/problems/search-in-rotated-sorted-array/description/ \ No newline at end of file diff --git "a/Beginner Level \360\237\223\201/Search in rotated sorted array/Solution.java" "b/Beginner Level \360\237\223\201/Search in rotated sorted array/Solution.java" new file mode 100644 index 00000000..d00db504 --- /dev/null +++ "b/Beginner Level \360\237\223\201/Search in rotated sorted array/Solution.java" @@ -0,0 +1,25 @@ +class Solution { + public int search(int[] arr, int target) { + int start=0; + int end=arr.length-1; + while(start<=end){ + int mid=start+(end-start)/2; + if(target==arr[mid]){ + return mid; + } + if(arr[start]<=arr[mid]){ + if(target>=arr[start] && targetarr[mid] && target<=arr[end]){ + start=mid+1; + }else{ + end=mid-1; + } + } + }return -1; + } +} \ No newline at end of file diff --git "a/Beginner Level \360\237\223\201/data.json" "b/Beginner Level \360\237\223\201/data.json" index d7efd767..02fee6be 100644 --- "a/Beginner Level \360\237\223\201/data.json" +++ "b/Beginner Level \360\237\223\201/data.json" @@ -8,6 +8,12 @@ // Add your details below as above mention + + { + "name": "Shivam Shukla", + "githubUsername": "shivam8112005" + }, + { "name": "Sharan Swaroop", "githubUsername": "S-Swaroop"