Skip to content

Commit

Permalink
Good question to practice
Browse files Browse the repository at this point in the history
  • Loading branch information
oyerounak committed Oct 8, 2023
1 parent 37d069b commit fb0ec05
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class Solution {
public int[] searchRange(int[] nums, int target) {

int n=nums.length;
int arr[] =new int[2];
int arr1[] =new int[]{-1,-1};
arr[0] = -1; arr[1] = -1;
for (int i = 0; i < n; i++) {
if (target != nums[i])
continue;
if (arr[0] == -1)
arr[0] = i;
arr[1] = i;
}
if (arr[0] != -1) {
return arr;

}
else
return arr1;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
### `searchRange` Function Explanation

1. Get the length of the input array `nums`.
2. Initialize an array to store the search range.
3. Initialize an array with [-1, -1] (default return value).
4. Initialize the start index of the search range as -1.
5. Initialize the end index of the search range as -1.
6. Start iterating through the input array.
7. If the current element is not the target, continue to the next element.
8. If the current element is not the target, continue to the next iteration.
9. If the start index of the search range is -1, set it to the current index.
10. Update the end index of the search range to the current index when a match is found.
11. If the start index is not -1, return the search range.
12. Return the search range containing the start and end indices of the target.
13. If no match was found, return the default [-1, -1] array.
14. Return the default search range [-1, -1] to indicate no match.

0 comments on commit fb0ec05

Please sign in to comment.