using
System;
using
System.Collections.Generic;
public
class
GFG {
static
bool
check(
int
req, List<
int
> posA, List<
int
> posB, List<
int
> negA,
List<
int
> negB,
int
K) {
int
cnt = 0;
int
first = 0;
int
second = negB.Count - 1;
while
(first < negA.Count) {
while
(second >= 0 && negA[first]
* negB[second] <= req)
second--;
cnt += negB.Count - second - 1;
first++;
}
first = 0;
second = posB.Count - 1;
while
(first < posA.Count) {
while
(second >= 0 && posA[first] * posB[second] > req)
second--;
cnt += second + 1;
first++;
}
first = posA.Count - 1;
second = negB.Count - 1;
while
(second >= 0) {
while
(first >= 0 && posA[first] * negB[second] <= req)
first--;
cnt += posA.Count - first - 1;
second--;
}
first = negA.Count - 1;
second = posB.Count - 1;
for
(; first >= 0; first--) {
while
(second >= 0 && negA[first]* posB[second]<= req)
second--;
cnt += posB.Count - second - 1;
}
return
(cnt >= K);
}
static
int
kthSmallestProduct(
int
[] A,
int
[] B,
int
K) {
List<
int
> posA =
new
List<
int
>();
List<
int
> negA =
new
List<
int
>();
List<
int
> posB =
new
List<
int
>();
List<
int
> negB =
new
List<
int
>();
foreach
(
int
it
in
A) {
if
(it >= 0)
posA.Add(it);
else
negA.Add(it);
}
foreach
(
int
it
in
B)
if
(it >= 0)
posB.Add(it);
else
negB.Add(it);
int
l =
int
.MinValue, r =
int
.MaxValue;
int
ans = 0;
while
(l <= r) {
int
mid = (l + r) / 2;
if
(check(mid, posA, posB, negA, negB, K)) {
ans = mid;
r = mid - 1;
}
else
{
l = mid + 1;
}
}
return
ans;
}
public
static
void
Main(String[] args) {
int
[] A = { -4, -2, 0, 3 };
int
[] B = { 1, 10 };
int
K = 7;
Console.Write(kthSmallestProduct(A, B, K));
}
}