Skip to content

Conversation

@sandy7907
Copy link

…t in given Sorted Array

@super30admin
Copy link
Owner

Strengths:

  • The solution correctly handles edge cases, such as an empty array or when the target is not within the range of the array.
  • The binary search functions are implemented correctly, with proper conditions to check for the first and last occurrences.
  • The code is clean and well-commented, making it easy to understand.

Areas for Improvement:

  • The initial condition to check if the target is outside the array range is efficient but might be redundant because the binary search would handle it anyway. However, it is a good optimization to avoid unnecessary searches.
  • In the getFirstOccurance function, the condition if(mid == low || nums[mid-1] != nums[mid]) is correct, but note that when mid == low, accessing nums[mid-1] would cause an index out of bounds error. However, since the condition checks mid == low first (using short-circuit OR), it avoids the error. This is acceptable, but it's important to be cautious. Alternatively, you could write it as if (mid == 0 || nums[mid-1] != target) for clarity, similar to the reference solution.
  • Similarly, in getLastOccurance, the condition if(mid == high || nums[mid+1] != nums[mid]) is correct, but using target instead of nums[mid] might be more consistent, though it is equivalent since nums[mid] == target at that point.

Overall, the solution is excellent and meets all requirements.

@sandy7907 sandy7907 closed this Feb 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants