-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Day-18 Q2 : reformat and rectify mistakes
- Loading branch information
1 parent
5b36ab4
commit 6036556
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
Day-18/q2: Search in Rotated Sorted Array/namita0210--java.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
|
||
} |