Skip to content

Commit

Permalink
Day-18 Q2 : reformat and rectify mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
namita0210 committed Jan 23, 2024
1 parent 5b36ab4 commit 6036556
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Day-18/q2: Search in Rotated Sorted Array/namita0210--java.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public class namita0210_java {

public static int search(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;

while (left <= right) {
int mid = left + (right - left) / 2;

if (nums[mid] == target) {
return mid;
}

if (nums[left] <= nums[mid]) {
// Left half is sorted
if (nums[left] <= target && target < nums[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
// Right half is sorted
if (nums[mid] < target && target <= nums[right]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}

return -1; // Target not found
}


}

0 comments on commit 6036556

Please sign in to comment.