Skip to content

completed binary search - 1#2507

Open
yashhh-23 wants to merge 1 commit into
super30admin:masterfrom
yashhh-23:master
Open

completed binary search - 1#2507
yashhh-23 wants to merge 1 commit into
super30admin:masterfrom
yashhh-23:master

Conversation

@yashhh-23

Copy link
Copy Markdown

No description provided.

Copilot AI review requested due to automatic review settings June 7, 2026 08:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds Java implementations of common binary-search variants (rotated sorted array search, “infinite” sorted array search via reader, and 2D matrix search).

Changes:

  • Added rotated sorted array binary search solution.
  • Added exponential-range + binary search solution using an ArrayReader.
  • Added 2D matrix binary search by flattening indices.

Reviewed changes

Copilot reviewed 3 out of 5 changed files in this pull request and generated 6 comments.

File Description
RotatedSortedArray.java Introduces rotated-array binary search implementation and a local main driver.
InfinitSortedArray.java Introduces “infinite array” search using ArrayReader, plus a local main and reader implementation.
2DMatrixSearch.java Introduces matrix binary search treating the matrix as a 1D sorted array.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread RotatedSortedArray.java

// Your code here along with comments explaining your approach in three sentences only
// Perform binary search on a rotated sorted array.Check if the left or right half is sorted and narrow search accordingly. Repeat until the target is found or search space is exhausted.
public class Solution {
Comment thread InfinitSortedArray.java
//Double the high index until the target is within range. then Perform binary search between low and high.


public class Solution {
Comment thread 2DMatrixSearch.java
Comment on lines +11 to +13
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;
Comment thread InfinitSortedArray.java
Comment on lines +14 to +27
while (reader.get(high) < target) {
low = high;
high = high * 2;
}

while (low <= high) {
int mid = low + (high - low) / 2;
if (reader.get(mid) == target) return mid;
if (reader.get(mid) > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
Comment thread InfinitSortedArray.java

while (reader.get(high) < target) {
low = high;
high = high * 2;
Comment thread 2DMatrixSearch.java


// Your code here along with comments explaining your approach in three sentences only
//treat 2d matrix as 1d array and apply binary search to get row and column index from mid value and compare with target tp get the result.
@super30admin

Copy link
Copy Markdown
Owner

Search inside a Rotated Sorted Array (RotatedSortedArray.java)

Strengths:

  • Correctly implements the binary search algorithm for rotated sorted arrays
  • Good edge case handling (null/empty array check)
  • Clear and descriptive variable names
  • Well-commented code explaining the approach
  • Proper use of left + (right - left) / 2 to prevent integer overflow
  • Includes a test harness in main method for verification

Areas for Improvement:

  • The condition target >= nums[left] could be simplified to target >= nums[left] (already correct) or just target >= nums[left] (redundant check)
  • Consider adding comments explaining the logic of why we check target < nums[mid] vs target > nums[mid] for the sorted halves
  • The test output could be more descriptive

The solution is functionally correct and efficient. It matches the reference solution in both approach and complexity.

VERDICT: PASS


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

Strengths:

  • Correctly implements the two-phase approach (exponential search + binary search)
  • Good use of comments explaining the approach in three sentences as requested
  • Clean code structure with proper variable naming
  • Includes a working ArrayReader implementation for testing
  • Handles edge cases (null array, out-of-bounds indices)

Areas for Improvement:

  • The comment "where n is the index of the target element" for time complexity is slightly imprecise; it should simply state O(log n) where n is the array length
  • The ArrayReader returns Integer.MAX_VALUE for out-of-bounds, but the problem specifies 2^31 - 1; while functionally equivalent, matching the problem specification exactly would be more precise
  • Could add input validation in the main method

VERDICT: PASS


Search a 2D Matrix (2DMatrixSearch.java)

Strengths:

  • Correctly implements the binary search algorithm on a 2D matrix
  • Good use of the overflow-safe mid calculation: low + (high - low)/2
  • Clean, readable code with appropriate comments
  • Matches the reference solution exactly, demonstrating understanding of the approach

Areas for Improvement:

  • The comment "compare with target tp get the result" contains a typo ("tp" should be "to")
  • Could add a brief comment explaining why treating the matrix as 1D is valid (rows are sorted and first element of each row is greater than last element of previous row)

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.

3 participants