import
java.util.*;
import
java.util.ArrayList;
import
java.util.Collections;
public
class
GFG {
static
List<Integer> closestInterval(List<
int
[]> arr,
int
N)
{
List<
int
[]> V =
new
ArrayList<>();
for
(
int
i =
0
; i < N; i++) {
V.add(
new
int
[] { arr.get(i)[
0
], i });
}
Collections.sort(
V, (a, b) -> Integer.compare(a[
0
], b[
0
]));
List<Integer> res
=
new
ArrayList<>(Collections.nCopies(N, -
1
));
for
(
int
i =
0
; i < N; i++) {
int
[] curr = arr.get(i);
int
[] key =
new
int
[] { curr[
1
],
0
};
int
idx = Collections.binarySearch(
V, key,
(a, b) -> Integer.compare(a[
0
], b[
0
]));
if
(idx >=
0
) {
res.set(i, V.get(idx)[
1
]);
}
}
return
res;
}
public
static
void
main(String[] args)
{
List<
int
[]> arr = Arrays.asList(
new
int
[] {
1
,
4
},
new
int
[] {
3
,
4
},
new
int
[] {
2
,
3
});
for
(
int
x : closestInterval(arr, arr.size())) {
System.out.print(x +
" "
);
}
}
}