forked from prateekshyap/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmallestIntersectingElement.java
75 lines (64 loc) · 2.26 KB
/
SmallestIntersectingElement.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*https://binarysearch.com/problems/Smallest-Intersecting-Element*/
import java.util.*;
class Solution {
public int solve(int[][] matrix) {
Map<Integer, Integer> map = new HashMap<>();
for (int[] row : matrix) {
int prev = Integer.MAX_VALUE;
for (int v : row) {
if (v == prev) //to avoid duplicates in a single row
continue;
map.put(v, map.containsKey(v) ? (Integer)map.get(v)+1 : 1);
prev = v;
}
}
int res = Integer.MAX_VALUE;
Iterator it = map.entrySet().iterator();
while (it.hasNext())
{
Map.Entry elem = (Map.Entry)it.next();
if ((Integer)elem.getValue() == matrix.length) //if present in all row
res = Math.min(res,(Integer)elem.getKey()); // update result
}
return res == Integer.MAX_VALUE ? -1 : res;
}
}
class Solution {
public int solve(int[][] matrix) {
int rows = matrix.length;
if (rows == 0) return -1;
int cols = matrix[0].length;
// Going through each elements of first row
for (int i = 0; i < cols; i++) {
int target = matrix[0][i];
// Count is to maintain the number of rows in which target is found
int ct = 1;
// Finding whether the element is present in all other rows
// Using Binary Search, since rows are sorted
for (int j = 1; j < rows; j++) {
boolean found = findElement(target, matrix[j]);
if (found) ct++;
}
// If the element is present in all rows, that is your answer
// Since it will be the smallest
if (ct == rows) return target;
}
return -1;
}
// Basic Binary Search to find element in a particular row
boolean findElement(int val, int[] arr) {
int len = arr.length;
int start = 0;
int end = len - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (arr[mid] == val)
return true;
if (arr[mid] > val)
end = mid - 1;
else
start = mid + 1;
}
return false;
}
}