# SUBARRAY - Editorial

Author: Devendra Agarwal
Tester: Anudeep Nekkanti
Editorialist: Amit Pandey

Medium.

### PREREQUISITES:

Dynamic programming and Data Structure(stack).

### PROBLEM:

You are given a character parenthesis ( having [,],{,},<,>,(,) ) array and an integer array.
You need to find the maximum sum sub-array in the integer array such that the corresponding sub-array in the character array has balanced parenthesis.

### QUICK EXPLANATION:

The given problem can be solved using a dynamic programming approach quite similar to maximum subarray problem.
We need to take care of balanced parenthesis, which can be done using a classical approach.

### EXPLANATION:

First Problem:
How to solve maximum sum sub array problem using Kadane ALgorithm, which is a classical dynamic programming problem.

def max_subarray(A):
max_ending_here = max_so_far = 0
for x in A:
max_ending_here = max(0, max_ending_here + x)
max_so_far = max(max_so_far, max_ending_here)
return max_so_far


Second problem:
Given a character parenthesis array, check if it is balanced or not.

1. Declare a character stack S.
2. Now traverse the expression string expression.
• If the current character is a starting bracket then push it to stack.
• If the current character is a closing bracket, then pop from stack and if the popped character is the matching starting bracket then fine else parenthesis are not balanced.
1. After complete traversal, if there is some starting bracket left in stack then “not balanced”.

Original Problem:
Now back to original problem. Traverse the character array and if there is a closing brace at position i, determine the largest index(j < i) such that [j,i] is balanced. We can this in one pass of the character array using a stack. The approach is similar to the second problem given above.

for(int i=0;i<=N+1;i++)
hsh[i] = 0;     // This array will store j for each i.
stack st;
st.push(0);
for (int i = 1; i <= n; i++) {
if (!st.empty()) {
// check if top of stack is opposite of current parenthesis.
if (closingBracket(s[i]) && s[st.top()] == opposite(s[i])) {
hsh[i] = st.top(); // We found j for the i.
st.pop();
} else {
st.push(i);
}
} else {
st.push(i);
}
}


When we are at index i, we may consider what we have solved problem upto index i-1, and we have created an array DP[ \hspace{1 mm}]. Where, DP[j] stored the maximum sum ending at index j.

Dynamic Programming recurrence:

Let us call a sub-array valid, if its corresponding character array is balanced. Let dp[i] denotes maximum sum valid sub-array
ending at position i.
So recurrence for DP will be as follows.

DP[i] = max (DP[i], dp[hsh[i] - 1] + sum(hsh[i], i))

hsh[i] \text{ is the largest j (j < i) such that segment} [j,i] \text{ is the balanced.}

The given recurrence is using the simple fact if expressions E_{1} and E_{2} are balanced, expression E_{1}E_{2} is balanced.

For finding out overall maximum sum sub-array we can iterate over each i and take maximum of DP[i].

### Solutions:

Setter’s solution can be found here.

Tester’s solution can be found here.

5 Likes

can anybody tell why this solution got tle
solution

@Anurag92, You are declaring sum[100100] in each of the test case. While there can be 10^5 test cases. You should either keep it global or declare array of size N.

why largest index j in the hsh[] array?

thnx . should have declared outside

Sorry for the confusion, please check now.

shouldn’t it be for all j < i such that [j, i] is properly parenthesized?

Actually i’m wondering why not for all j < i such that [j, i] is properly parenthesized?

correct me if i’m mistaken anywhere

according to the solution, you are trying to keep [j,i] segment length as minimum as possible

will there be a unique j for each i? Actually i tried making a balanced parenthesis but observed that j is always unique. if i tried making j lesser ( close to index 0 ), the segment [j,i] was getting imbalanced. I’ll figure out my confusion. Thanks

For each i, there will be only one j such that [j,i] will be balanced. I couldn’t come up with a counter example, so I assume its true.

Yes, I am talkign about largest j, such that segment [j,i] is balanced.

Just try to realize it using the fact which is written in penultimate line.

can anybody tell why this solution got wa link text

Counterexample: ()(), For last parenthesis, there are two j’s.

1 Like

I applied the same logic, but got tle. Can anybody suggest what’s wrong in my solution

Yes and segment [j,i] is balanced as well.

j=i-1 or (flagvalue,say:-1) I don’t think j can take any other value according to you.Am I correct?
If we take j as the smallest j such that [j,i] is valid, then sum of elements from j,i should be the answer and that can be calculated in O(1) by keeping prefix sums array.Is this solution Correct?

//