class
GFG
{
static
class
pair<E,R>
{
E first;
R second;
public
pair(E first, R second)
{
this
.first = first;
this
.second = second;
}
}
static
void
maximumAbsSum(pair<String,Integer> []arr,
int
N)
{
int
start =
0
, end =
0
, res =
0
,
negIndex =
0
, posIndex =
0
,
negPrefix =
0
, posPrefix =
0
;
for
(
int
i =
0
; i < N; i++)
{
if
(arr[i].second <
0
)
{
if
(res < Math.abs(arr[i].second
+ negPrefix)) {
res = Math.abs(arr[i].second
+ negPrefix);
start = negIndex;
end = i;
}
}
else
{
if
(res < Math.abs(arr[i].second
+ posPrefix)) {
res = Math.abs(arr[i].second
+ posPrefix);
start = posIndex;
end = i;
}
}
if
(negPrefix + arr[i].second >
0
) {
negPrefix =
0
;
negIndex = i +
1
;
}
else
{
negPrefix += arr[i].second;
}
if
(posPrefix + arr[i].second >=
0
) {
posPrefix += arr[i].second;
}
else
{
posPrefix =
0
;
posIndex = i +
1
;
}
}
for
(
int
i = start; i <= end; i++) {
System.out.print(arr[i].first+
" "
);
}
}
@SuppressWarnings
(
"unchecked"
)
public
static
void
main(String[] args)
{
@SuppressWarnings
(
"rawtypes"
)
pair arr[] = {
new
pair(
"geeks"
,
4
),
new
pair(
"for"
, -
3
),
new
pair(
"geeks"
,
2
),
new
pair(
"tutorial"
,
3
),
new
pair(
"program"
, -
4
) };
int
N = arr.length;
maximumAbsSum(arr, N);
}
}