Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions src/main/java/com/thealgorithms/matrix/Search2DMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.thealgorithms.matrix;

/**
* Two variations of searching in a 2D matrix
*
* Variation 1:
* - Each row is sorted left to right
* - Each column is sorted top to bottom
*
* Variation 2:
* - The matrix is a flattened sorted array
* - i.e., row 0 ends, row 1 continues (matrix is sorted as a 1D array)
*
* LeetCode: #74 and #240
*/
public class Search2DMatrix {

/**
* Search in a 2D matrix where rows and columns are sorted.
*
* @param matrix The 2D matrix with sorted rows and columns.
* @param target The target integer to search for.
* @return true if target is found, otherwise false.
*/
public static boolean searchMatrixSortedRowsAndCols(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}

int rows = matrix.length;
int cols = matrix[0].length;

int row = 0;
int col = cols - 1;

while (row < rows && col >= 0) {
int val = matrix[row][col];
if (val == target) {
return true;
} else if (val > target) {
col--;
} else {
row++;
}
}

return false;
}

/**
* Search in a 2D matrix that is sorted as if flattened into a 1D array.
*
* @param matrix The 2D matrix.
* @param target The target integer to search for.
* @return true if target is found, otherwise false.
*/
public static boolean searchMatrixFlattenedSorted(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}

int rows = matrix.length;
int cols = matrix[0].length;

int left = 0;
int right = rows * cols - 1;

while (left <= right) {
int mid = left + (right - left) / 2;
int val = matrix[mid / cols][mid % cols];

if (val == target) {
return true;
} else if (val < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}

return false;
}
}
30 changes: 30 additions & 0 deletions src/test/java/com/thealgorithms/matrix/Search2DMatrixTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.thealgorithms.matrix;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class Search2DMatrixTest {

@Test
public void testSearchMatrixSortedRowsAndCols() {
int[][] matrix = {
{1, 4, 7, 11},
{2, 5, 8, 12},
{3, 6, 9, 16},
{10, 13, 14, 17}
};
assertTrue(Search2DMatrix.searchMatrixSortedRowsAndCols(matrix, 5));
assertFalse(Search2DMatrix.searchMatrixSortedRowsAndCols(matrix, 20));
}

@Test
public void testSearchMatrixFlattenedSorted() {
int[][] matrix = {
{1, 3, 5, 7},
{10, 11, 16, 20},
{23, 30, 34, 50}
};
assertTrue(Search2DMatrix.searchMatrixFlattenedSorted(matrix, 3));
assertFalse(Search2DMatrix.searchMatrixFlattenedSorted(matrix, 4));
}
}
Loading