Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

added search in rotated sorted array folder and updated data.json #107

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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/
25 changes: 25 additions & 0 deletions Beginner Level 📁/Search in rotated sorted array/Solution.java
Original file line number Diff line number Diff line change
@@ -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] && target<arr[mid]){
end=mid-1;
}else{
start=mid+1;
}
}else{
if(target>arr[mid] && target<=arr[end]){
start=mid+1;
}else{
end=mid-1;
}
}
}return -1;
}
}
6 changes: 6 additions & 0 deletions Beginner Level 📁/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

// Add your details below as above mention


{
"name": "Shivam Shukla",
"githubUsername": "shivam8112005"
},

{
"name": "Sharan Swaroop",
"githubUsername": "S-Swaroop"
Expand Down
Loading