-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathCountNegativeIntegersInRowColumnSortedMatrix.java
38 lines (31 loc) · 1.29 KB
/
CountNegativeIntegersInRowColumnSortedMatrix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.geeksforgeeks.array;
public class CountNegativeIntegersInRowColumnSortedMatrix {
public static void main(String[] args) {
int mat[][] = { {-3, -2, -1, 1},
{-2, 2, 3, 4},
{4, 5, 7, 8}};
System.out.println(getNegativeNumbersCount(mat));
}
public static int getNegativeNumbersCount(int[][] matrix) {
int count = findIndexOfFirstPositiveNumberInTheRow(matrix[0], 0, matrix[0].length);
int indexOfFirstPositiveNumber = count;
for (int i = 1; i < matrix.length; i++) {
indexOfFirstPositiveNumber = findIndexOfFirstPositiveNumberInTheRow(matrix[i], 0, indexOfFirstPositiveNumber - 1);
count += indexOfFirstPositiveNumber;
}
return count;
}
public static int findIndexOfFirstPositiveNumberInTheRow(int[] row, int low, int high) {
if (low <= high) {
int mid = (low + high) / 2;
if (mid == 0 || (row[mid] > 0 && row[mid - 1] < 0)) {
return mid;
} else if (row[mid] < 0) {
return findIndexOfFirstPositiveNumberInTheRow(row, mid + 1, high);
} else {
return findIndexOfFirstPositiveNumberInTheRow(row, low, mid - 1);
}
}
return -1;
}
}