using
System;
public
class
GFG{
class
Pair
{
public
int
f, s, t;
public
Pair(
int
f,
int
s,
int
t)
{
this
.f = f;
this
.s = s;
this
.t = t;
}
};
static
Pair getSubArray(
int
[]arr,
int
n,
int
K)
{
int
currSum = 0;
int
prevDif = 0;
int
currDif = 0;
Pair result =
new
Pair(
-1, -1, Math.Abs(K - Math.Abs(currSum)));
Pair resultTmp = result;
int
i = 0;
int
j = 0;
while
(i <= j && j < n)
{
currSum += arr[j];
prevDif = currDif;
currDif = K - Math.Abs(currSum);
if
(currDif <= 0)
{
if
(Math.Abs(currDif) < Math.Abs(prevDif))
{
resultTmp =
new
Pair(i, j, currDif);
}
else
{
resultTmp =
new
Pair(i, j - 1, prevDif);
currSum -= (arr[i] + arr[j]);
i += 1;
}
}
else
{
resultTmp =
new
Pair(i, j, currDif);
j += 1;
}
if
(Math.Abs(resultTmp.t) < Math.Abs(result.t))
{
result = resultTmp;
}
}
return
result;
}
public
static
void
Main(String[] args)
{
int
[]arr = { 15, -3, 5, 2, 7, 6, 34, -6 };
int
n = arr.Length;
int
K = 50;
Pair tmp = getSubArray(arr, n, K);
int
i = tmp.f;
int
j = tmp.s;
int
minDev = tmp.t;
if
(i == -1)
{
Console.Write(
"The empty array shows minimum Deviation"
+
"\n"
);
return
;
}
for
(
int
k = i + 1; k < j + 1; k++)
{
Console.Write(arr[k]+
" "
);
}
}
}