import
java.util.*;
class
GFG{
static
void
max_distance(
int
a[],
int
n,
int
x,
int
A,
int
B)
{
int
i, g =
0
, h =
0
;
for
(i =
0
; i < n; i++)
{
if
(a[i] == A)
g = i;
if
(a[i] == B)
h = i;
}
if
(x ==
0
)
{
System.out.print(Math.abs(g - h));
}
else
if
((g ==
0
) && (h == (n -
1
)))
System.out.println(n -
1
);
else
if
((g == n -
1
) && (h ==
0
))
System.out.println(n -
1
);
else
{
if
(h > g)
{
while
((x >
0
) && (h < n -
1
))
{
h++;
x--;
}
if
(x >
0
)
{
while
((x >
0
) && (g >
0
))
{
g--;
x--;
}
}
System.out.println(h - g);
}
else
{
while
((x >
0
) && (g < n -
1
))
{
g++;
x--;
}
if
(x >
0
)
{
while
((x >
0
) && (h >
0
))
{
h--;
x--;
}
}
System.out.println(g - h);
}
}
}
public
static
void
main (String []args)
{
int
a[] = {
100
,
33
,
10
,
1
};
int
x =
5
, A =
100
, B =
1
;
int
n = a.length;
max_distance(a, n, x, A, B);
}
}