Count Element In Row And Column Wise Sorted Matrix
package org.wesome.dsalgo;
public class SortedMatrixCount {
public static int sortedMatrixCount(int[][] matrix, int dimension, int find) {
int count = 0;
int row = 0, col = dimension - 1;
while (row < dimension && col >= 0) {
if (matrix[row][col] == find) {
System.out.println(find + " is present at [" + row + "][" + col + "]");
count++;
}
if (matrix[row][col] > find) {
System.out.println(find + " is less then last element " + matrix[row][col] + " at [" + row + "][" + col + "], hence decrementing colum");
col--;
} else {
System.out.println(find + " is greater then last element " + matrix[row][col] + " at [" + row + "][" + col + "], hence incrementing row");
row++;
}
}
System.out.println(find + " is present " + count + " times");
return count;
}
}