PROBLEM LINK:
Authors: Jaub Safin
Testers: Vasya Antoniuk
Translators: Vasya Antoniuk (Russian), Team VNOI (Vietnamese) and Hu Zecong (Mandarin)
Editorialist: Praveen Dhinwa
DIFFICULTY:
Simple
PREREQUISITES:
Simple observations, dp
PROBLEM:
An array is called alternating if any two consecutive numbers in it have opposite signs (i.e. one of them should be negative, whereas the other should be positive).
You are given an array A of size N consisting of non-zero elements. For each index i from 1 to N, you have to find the length of longest subarray starting at i.
QUICK EXPLANATION:
We can observe that if an array is alternating, then all of its subarrays are also alternating.
So, we can divide the array into maximal alternating subarrays.
For doing that, we will iterate over array from left to right, if the sign of current number is different than previous number, then this number can be used to extend previous alternating subarray, Otherwise we will have to start constructing a new maximal alternating subarray.
In this way, we can partition the array into various maximal alternating subarrays.
After this, finding length of longest subarray starting at index i is quite easy, as it can be done easily by finding the position of i in the corresponding maximal alternating subarray. If p be position and L be the length of the maximal subarray, then L - p + 1 will be the length of longest subarray starting at index i.
EXPLANATION:
Observation 1:
Values of the number don’t matter, only their signs do.
So, we can change the array A such that it consists of -1 and 1’s only.
Observation 2:
If an array A is alternating, then all of it’s subarrays are alternating.
Let us take an example to understand how we can apply the above observation to solve the problem.
Let A = [1, 1, -1, 1, 1, -1, -1]
So, we start from A_1 and note that A_1 is equal A_2.
So the maximal subarray starting from 1 will be [1] itself.
Now, we go to A_2, we can see that A_2, A_3, A_4 have different signs, and A_4 has same sign as A_5.
So the maximal subarray starting from index 2 will be [1, -1, 1].
So, we break the array into several parts such that each part is maximal alternating subarray. In our example, the parts will be
[1] [1, -1, 1] [1, -1], [-1]
We can formalize the above idea to write a pseudo code to break an array into various parts of maximal alternating subarrays.
vector<vector<int> > parts;
vector<int> curPart;
subpart.push_back(a[0]);
for (int i = 1; i < n; i++) {
// If signs of current and previous number are different,
// then it means that we can extend the current part.
if (a[i] * a[i - 1] == -1) {
curPart.push_back(a[i]);
} else {
// We add the curPart into parts.
parts.push_back(curPart);
}
}
// Check at the end whether the curPart has non-zero length or not.
// If it has, then add curPart into parts.
if (curPart.size() > 0) {
parts.push_back(subpart);
}
Now, let us find the length of longest alternating subarray ending at each index i for our example subarray A.
We get [1] [3, 2, 1], [2, 1], [1]
So, this means that for an maximal alternating subarray of length L, the answers (length of longest alternating subarray start from that index) will be L, L-1, \dots, 1.
We can use this idea to solve our problem.
// i denotes the current index of array at which we currently are.
int i = 1;
for (vector<int> curPart : parts) {
int L = curPart.size();
while (L > 0) {
answer[i] = L;
// increment i
i++;
// decrement L
L--;
}
}
// Note the fact that we didn't use the explicit values of curPart, only its size matter.
Dynamic programming based Solution
You can also solve the problem by a very simple dp.
Let len[i] denote the maximum length of alternating subarray starting at position i.
We can observer that if a[i] and a[i + 1] has opposite signs, then len[i] will be 1 more than len[i + 1].
Otherwise in the case when they have same sign, then len[i] will be just 1.
len[N] = 1;
for (int i = N - 1; i >= 1; i--) {
// a[i] and a[i + 1] have different signs.
// Note that the a[i] can go upto 10^9,
// So if a is stored in int data type, then the a[i] * a[i + 1] might not fit in int.
// So, we cast it to long long
if (a[i] * (long long) a[i + 1]< 0) {
len[i] = len[i + 1] + 1;
} else {
len[i] = 1;
}
}
Time Complexity:
As we have seen in both the solutions we have to iterate over the array A only once or constant number of times. So, time complexity of the algorithm will be \mathcal{O}(N) which will easily fit in time with N = 10^5 and T = 10.