Skip to content

Completed Binary-Search-1#2484

Open
samikshm wants to merge 1 commit into
super30admin:masterfrom
samikshm:master
Open

Completed Binary-Search-1#2484
samikshm wants to merge 1 commit into
super30admin:masterfrom
samikshm:master

Conversation

@samikshm
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)

It looks like you haven't written any code for this problem. The problem requires implementing a binary search algorithm to find a target in a rotated sorted array with O(log n) runtime complexity.

Here are some steps to get started:

  1. Understand the problem: The array is sorted in ascending order but may be rotated at an unknown pivot. You need to find the index of the target efficiently.
  2. Plan your approach: The reference solution uses a modified binary search. It checks which half of the array is sorted (left or right) and then determines if the target lies within that sorted half. If it does, it narrows the search to that half; otherwise, it searches the other half.
  3. Write code: Start by initializing low and high pointers. Then, in a while loop, calculate mid. Compare the mid element with the target. If they match, return mid. Then, check if the left half (from low to mid) is sorted. If it is, check if the target is within this range. Similarly, if the right half is sorted, check if the target is there. Adjust low and high accordingly.
  4. Test your solution: Use the provided examples to test your code. For instance, with nums = [4,5,6,7,0,1,2] and target=0, your code should return 4.

Since you haven't submitted any code, I encourage you to try implementing the solution. If you're stuck, refer to the reference solution for guidance, but make sure you understand each step. Good luck!

VERDICT: NEEDS_IMPROVEMENT


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

Your solution is excellent! You've correctly implemented the algorithm for searching in an unknown-sized sorted array. Here are some strengths and minor suggestions:

Strengths:

  • You correctly identified the need to first expand the search range exponentially (by doubling high) until the target is within the range. This ensures that the search range grows quickly without knowing the array size.
  • You then use binary search within the determined range, which is efficient and meets the O(log n) requirement.
  • The code is well-structured and includes helpful comments.

Minor Suggestions:

  • While your code is correct, consider adding a check for the case when the target is less than the first element or greater than the maximum element in the array. However, the current implementation handles this implicitly because if the target is too small, the first while loop won't run (since reader.get(high) might be greater than target immediately), and binary search will correctly return -1. Similarly, if the target is too large, the first while loop might run indefinitely? Actually, no: because the problem states that reader.get(i) returns 2^31 - 1 for out-of-bound indices. So when high becomes large enough to be out of bounds, reader.get(high) will return 2^31 - 1, which is greater than any target (since target is at most 10^4). So the while loop condition reader.get(high) < target will break when high is out of bounds. This is correct.
  • However, note that if the array is very large, but the target is not present and is larger than all elements (but less than 2^31-1), the first while loop might continue until high becomes so large that it causes an integer overflow? Actually, since high is an integer, doubling it repeatedly might eventually exceed Integer.MAX_VALUE (which is 2^31-1). This could cause high to become negative (due to integer overflow) and then the behavior might be unpredictable. But in practice, the array length is at most 10^4, so high will never become that large. For example, starting from 1, after 20 doublings, high would be 2^20 which is about 1e6, which is still within the integer range. So it's safe. But for a general case where the array might be larger (though the problem constraints say length <= 10^4), it's not an issue.
  • You might want to add a comment explaining why the expansion step is safe: because the array length is bounded (though unknown to the algorithm), but actually the problem says the array length is at most 10^4, so high will be at most around 10^4 * 2? Actually, the expansion stops when reader.get(high) >= target, which will happen when high is at most the index of the target (if present) or the end of the array (if target is not present). Since the array length is at most 10^4, high will never exceed 10^4 by too much (maybe 2*10^4). So no risk of integer overflow.

Overall, your solution is correct and efficient. Well done!

VERDICT: PASS


Search a 2D Matrix (search_2D_matrix.java)

Your solution is excellent! You have correctly implemented the binary search algorithm by treating the 2D matrix as a 1D array. The use of integer division and modulus to compute the row and column indices from the mid index is correct and efficient. The code is well-structured and easy to read. There are no obvious optimizations needed beyond what you have done. Keep up the good work!

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