Repository: TheAlgorithms/Java (java)

Issue:
[BUG] Incorrect column pointer initialization in RowColumnWiseSorted2dArrayBinarySearch
### Description

-  I've identified a bug in the `RowColumnWiseSorted2dArrayBinarySearch.java` implementation.
- The column pointer (colPointer) is initialized using `matrix.length - 1`. But, **matrix.length returns the number of rows, not the number of columns**.
- This causes the algorithm's starting point to be incorrect for any matrix that is not square.
- The problematic line is:
```java
// in src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java

int colPointer = matrix.length - 1; // The pointer at end column
```

### Steps to reproduce

- The bug can be reproduced with any non-square matrix where the number of rows and columns are different.
- For example, with a 2x4 matrix:
```java
   1 int[][] matrix = {
   2     {10, 20, 30, 40},
   3     {15, 25, 35, 45}
   4 };
```
- With the current code, matrix.length is 2, so colPointer is initialized to 1.
- The search incorrectly starts at matrix[0][1] (value 20) instead of the top-right corner matrix[0][3] (value 40).

### Excepted behavior

- The colPointer should be initialized to the index of the last column, allowing the search to correctly start at the top-right corner of the matrix. The correct initialization should be:
`int colPointer = matrix[0].length - 1;`

### Screenshots

_No response_

### Additional context

I would like to submit a Pull Request to fix this initialization issue.

Pull request:
Fix(searches): Correct column pointer initialization in RowColumnWiseSorted2dArrayBinarySearch
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

Closes #6332 

This pull request fixes a bug in the `RowColumnWiseSorted2dArrayBinarySearch.java` file where the column pointer was incorrectly initialized.

**The Problem:**
The column pointer (`colPointer`) was being initialized with `matrix.length - 1`. However, `matrix.length` returns the number of rows, not columns. This causes the algorithm to start at an incorrect position and fail for any matrix that is not square.

**The Solution:**
I have corrected the initialization to `int colPointer = matrix[0].length - 1;`. This correctly uses the length of the first row to determine the number of columns, ensuring the pointer is set to the last column's index as intended by the algorithm's logic.

Patch:
diff --git a/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java b/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java
index 3b4b0b08..b40c7554 100644
--- a/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java
+++ b/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java
@@ -29,7 +29,7 @@ public class RowColumnWiseSorted2dArrayBinarySearch implements MatrixSearchAlgor
 
     public static <T extends Comparable<T>> int[] search(T[][] matrix, T target) {
         int rowPointer = 0; // The pointer at 0th row
-        int colPointer = matrix.length - 1; // The pointer at end column
+        int colPointer = matrix[0].length - 1; // The pointer at end column
 
         while (rowPointer < matrix.length && colPointer >= 0) {
             int comp = target.compareTo(matrix[rowPointer][colPointer]);


Test patch:
diff --git a/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java
index 39ac5bf0..8d1423cb 100644
--- a/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java
+++ b/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java
@@ -110,4 +110,131 @@ public class RowColumnWiseSorted2dArrayBinarySearchTest {
         assertEquals(expected[0], ans[0]);
         assertEquals(expected[1], ans[1]);
     }
+
+    /**
+     * Tests for a WIDE rectangular matrix (3 rows, 4 columns)
+     */
+    private static final Integer[][] WIDE_RECTANGULAR_MATRIX = {
+        {10, 20, 30, 40},
+        {15, 25, 35, 45},
+        {18, 28, 38, 48},
+    };
+
+    @Test
+    public void rowColumnSorted2dArrayBinarySearchTestWideMatrixMiddle() {
+        Integer target = 25; // A value in the middle
+        int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
+        int[] expected = {1, 1};
+        assertEquals(expected[0], ans[0]);
+        assertEquals(expected[1], ans[1]);
+    }
+
+    @Test
+    public void rowColumnSorted2dArrayBinarySearchTestWideMatrixTopRightCorner() {
+        Integer target = 40; // The top-right corner element
+        int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
+        int[] expected = {0, 3};
+        assertEquals(expected[0], ans[0]);
+        assertEquals(expected[1], ans[1]);
+    }
+
+    @Test
+    public void rowColumnSorted2dArrayBinarySearchTestWideMatrixBottomLeftCorner() {
+        Integer target = 18; // The bottom-left corner element
+        int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
+        int[] expected = {2, 0};
+        assertEquals(expected[0], ans[0]);
+        assertEquals(expected[1], ans[1]);
+    }
+
+    @Test
+    public void rowColumnSorted2dArrayBinarySearchTestWideMatrixTopLeftCorner() {
+        Integer target = 10; // The top-left corner element
+        int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
+        int[] expected = {0, 0};
+        assertEquals(expected[0], ans[0]);
+        assertEquals(expected[1], ans[1]);
+    }
+
+    @Test
+    public void rowColumnSorted2dArrayBinarySearchTestWideMatrixBottomRightCorner() {
+        Integer target = 48; // The bottom-right corner element
+        int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
+        int[] expected = {2, 3};
+        assertEquals(expected[0], ans[0]);
+        assertEquals(expected[1], ans[1]);
+    }
+
+    @Test
+    public void rowColumnSorted2dArrayBinarySearchTestWideMatrixNotFound() {
+        Integer target = 99; // A value that does not exist
+        int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, target);
+        int[] expected = {-1, -1};
+        assertEquals(expected[0], ans[0]);
+        assertEquals(expected[1], ans[1]);
+    }
+
+    /**
+     * Tests for a TALL rectangular matrix (4 rows, 3 columns)
+     */
+    private static final Integer[][] TALL_RECTANGULAR_MATRIX = {
+        {10, 20, 30},
+        {15, 25, 35},
+        {18, 28, 38},
+        {21, 31, 41},
+    };
+
+    @Test
+    public void rowColumnSorted2dArrayBinarySearchTestTallMatrixMiddle() {
+        Integer target = 28; // A value in the middle
+        int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
+        int[] expected = {2, 1};
+        assertEquals(expected[0], ans[0]);
+        assertEquals(expected[1], ans[1]);
+    }
+
+    @Test
+    public void rowColumnSorted2dArrayBinarySearchTestTallMatrixTopRightCorner() {
+        Integer target = 30; // The top-right corner element
+        int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
+        int[] expected = {0, 2};
+        assertEquals(expected[0], ans[0]);
+        assertEquals(expected[1], ans[1]);
+    }
+
+    @Test
+    public void rowColumnSorted2dArrayBinarySearchTestTallMatrixBottomLeftCorner() {
+        Integer target = 21; // The bottom-left corner element
+        int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
+        int[] expected = {3, 0};
+        assertEquals(expected[0], ans[0]);
+        assertEquals(expected[1], ans[1]);
+    }
+
+    @Test
+    public void rowColumnSorted2dArrayBinarySearchTestTallMatrixTopLeftCorner() {
+        Integer target = 10; // The top-left corner element
+        int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
+        int[] expected = {0, 0};
+        assertEquals(expected[0], ans[0]);
+        assertEquals(expected[1], ans[1]);
+    }
+
+    @Test
+    public void rowColumnSorted2dArrayBinarySearchTestTallMatrixBottomRightCorner() {
+        Integer target = 41; // The bottom-right corner element
+        int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
+        int[] expected = {3, 2};
+        assertEquals(expected[0], ans[0]);
+        assertEquals(expected[1], ans[1]);
+    }
+
+    @Test
+    public void rowColumnSorted2dArrayBinarySearchTestTallMatrixNotFound() {
+        Integer target = 5; // A value that does not exist
+        int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(TALL_RECTANGULAR_MATRIX, target);
+        int[] expected = {-1, -1};
+        assertEquals(expected[0], ans[0]);
+        assertEquals(expected[1], ans[1]);
+    }
 }