Comleted binary-search- 2#2336
Conversation
Find the First and Last Position of an Element in given Sorted Array (find_peak_element.py)Your solution is for a different problem (Find Peak Element) than the one assigned (Find First and Last Position of an Element in Sorted Array). Please make sure you are solving the correct problem. For the given problem, you need to find the first and last occurrence of a target in a sorted array. You should use binary search to find the first occurrence and then another binary search to find the last occurrence. Here are some tips:
Your current code does not address the problem and has boundary issues even for the peak finding problem. For example, when mid is 0, accessing nums[mid-1] will cause an index error. Similarly, when mid is len(nums)-1, accessing nums[mid+1] will cause an index error. You should handle these edge cases. For the peak finding problem, you can check:
But since this is not the assigned problem, you should focus on the correct problem. VERDICT: NEEDS_IMPROVEMENT Find the Minimum Element in a Rotated Array(sorted) (findmin_in_rotated_sortedarray.py)Your solution shows a good understanding of using binary search for this problem, and you've correctly identified that the time complexity should be O(log n). However, there are a few areas where the solution can be improved:
Alternatively, here is a common approach that is similar to your attempt but without This approach compares VERDICT: NEEDS_IMPROVEMENT Find the Peak Element (firstlast_ele_in_sortedarray.py)It seems there has been a mix-up in the problem you are solving. The problem you are addressing is "Find First and Last Position of Element in Sorted Array", but the problem you were asked to solve is "Find the Peak Element". For the "Find the Peak Element" problem, you need to find an element that is greater than its neighbors. The array is not necessarily sorted, and there is no target value to search for. Instead, you should use a binary search that compares the middle element with its neighbors to determine if it is a peak. If not, you move towards the side where the neighbor is larger, as that side is guaranteed to have a peak. Here is a brief explanation of the correct approach for "Find the Peak Element":
Your current solution is correct for the "Find First and Last Position" problem, but please ensure you are solving the correct problem. For the peak element problem, you need to implement a different binary search logic. VERDICT: NEEDS_IMPROVEMENT |
No description provided.