using
System;
using
System.Collections.Generic;
class
GFG
{
static
List<
int
> res(
int
[] arr,
int
[] q,
int
n,
int
m)
{
List<
int
> result =
new
List<
int
>();
List<
int
> v1 =
new
List<
int
>();
List<
int
> v2 =
new
List<
int
>();
int
last_m1 = -1, last_1 = -1;
for
(
int
i = 0; i < n; i++) {
v2.Add(last_1);
if
(arr[i] == 1)
last_1 = i;
}
for
(
int
i = n - 1; i >= 0; i--) {
v1.Add(last_m1);
if
(arr[i] == -1)
last_m1 = i;
}
v1.Reverse();
for
(
int
i = 0; i < m; i++) {
if
(v1[q[i]] != -1 && v2[q[i]] != -1)
result.Add(
Math.Min(Math.Abs(v1[q[i]] - q[i]),
Math.Abs(v2[q[i]] - q[i])));
else
if
(v1[q[i]] != -1)
result.Add(Math.Abs(v1[q[i]] - q[i]));
else
if
(v2[q[i]] != -1)
result.Add(Math.Abs(v2[q[i]] - q[i]));
else
result.Add(-1);
}
return
result;
}
public
static
void
Main()
{
int
[] arr
= { -1, 0, 0, 1, -1, 1, 1, 0, 0, 1, -1, 0 };
int
n = arr.Length;
int
[] q = { 1, 5, 10 };
int
m = q.Length;
List<
int
> x = res(arr, q, n, m);
foreach
(
int
y
in
x) Console.Write(y +
" "
);
}
}