Skip to content

Completed Binary-Search-1#2482

Open
Hriday-A wants to merge 1 commit into
super30admin:masterfrom
Hriday-A:master
Open

Completed Binary-Search-1#2482
Hriday-A wants to merge 1 commit into
super30admin:masterfrom
Hriday-A:master

Conversation

@Hriday-A
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Search inside a Rotated Sorted Array (Search_in_Rotated_Sorted_Array.java)

The student has implemented the solution correctly with O(log n) time and O(1) space. The code is readable but could be improved with better spacing for operators. For example, adding spaces around comparison operators would make it more readable.

But overall, it's a good solution.

VERDICT: PASS


Search Inside a Sorted Array whose Length is unknown (Search_in_a_Sorted_Array_of_Unknown_Size.java)

Your solution is well-written and efficient. It correctly implements the two-step process: first, finding the range by doubling the high index until the value at high is greater than or equal to the target (or until we go out of bounds), and then performing binary search in the identified range. This ensures logarithmic time complexity.

One minor point: you should consider the case when the target is exactly at the initial high (which is 1). Your code handles this correctly because the condition reader.get(high) < target would be false, so the binary search is performed from low=0 to high=1. So it's correct.

However, to make it more robust, you might want to consider what happens if the array is very large and the target is near the end. The exponential expansion is efficient, but note that when high is doubled, it might become very large quickly. However, since the array size is at most 10^4, this is not a problem. In fact, even if the array is the maximum size (10^4), the high index will be at most 2^14 (which is 16384) before the loop stops, which is acceptable.

Another point: the initial value of low is set to 0 and high to 1. This is standard. But if the array is empty, the code might break? However, the problem states that the array length is at least 1. So it's safe.

Overall, great job! Your code is correct and efficient.

VERDICT: PASS


Search a 2D Matrix (Search_a_2D_Matrix.java)

Your solution is excellent! You have successfully implemented the binary search algorithm by leveraging the properties of the matrix. Here are some strengths:

  • You correctly calculated the row and column indices from the mid index using integer division and modulus operations.
  • You used the standard binary search pattern with low, high, and mid variables.
  • The code is concise and easy to understand.

One minor suggestion for improvement: While your code is correct, you might consider adding comments to explain the logic, especially for the part where you compute r and c from mid. This can help others (and yourself) understand the code better in the future. For example:

int r = mid / n;  // row index
int c = mid % n;  // column index

Overall, this is a well-written solution that meets all the requirements.

VERDICT: PASS

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