Problem 3152 Special Array II
Table of Contents
Problem Statement
Link - Problem 3152
Question
An array is considered special if every pair of its adjacent elements contains two numbers with different parity.
You are given an array of integer nums and a 2D integer matrix queries, where for queries[i] = [fromi, toi] your task is to check that subarray nums[fromi..toi] is special or not.
Return an array of booleans answer such that answer[i] is true if nums[fromi..toi] is special.
Example 1:
Input: nums = [3,4,1,2,6], queries = [[0,4]]
Output: [false]
Explanation:
The subarray is [3,4,1,2,6]. 2 and 6 are both even.
Example 2:
Input: nums = [4,3,1,6], queries = [[0,2],[2,3]]
Output: [false,true]
Explanation:
- The subarray is
[4,3,1]. 3 and 1 are both odd. So the answer to this query isfalse. - The subarray is
[1,6]. There is only one pair:(1,6)and it contains numbers with different parity. So the answer to this query istrue.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1051 <= queries.length <= 105queries[i].length == 20 <= queries[i][0] <= queries[i][1] <= nums.length - 1
Solution
class Solution {
public:
vector<bool> isArraySpecial(vector<int>& nums, vector<vector<int>>& queries) {
vector<bool> ans(queries.size(), false);
vector<int> prefix(nums.size(), 0);
prefix[0] = 0;
for (int i = 1; i < nums.size(); i++) {
if (nums[i] % 2 == nums[i - 1] % 2) {
prefix[i] = prefix[i - 1] + 1;
}
else {
prefix[i] = prefix[i - 1];
}
}
for (int i = 0; i < queries.size(); i++) {
vector<int> query = queries[i];
int start = query[0];
int end = query[1];
ans[i] = prefix[end] - prefix[start] == 0;
}
return ans;
}
};
Complexity Analysis
| Algorithm | Time Complexity | Space Complexity |
| --------------------------------- | --------------- | ---------------- |
| Prefix Sum Array with Linear Scan | O(n + m) | O(n + m) |
Explanation
Intial Thoughts
To approach this problem, we need to think about how to efficiently check if every pair of adjacent elements in a given subarray has different parity. One possible approach could be to iterate over each subarray and check the parity of each pair of adjacent elements. Another approach could be to use some kind of prefix sum or cumulative sum to keep track of the number of pairs with the same parity. We also need to think about how to efficiently handle multiple queries on the same array.
Intuitive Analysis
Intuitively, we can solve this problem by creating a prefix array that keeps track of the number of pairs of adjacent elements with the same parity. We can then use this prefix array to quickly answer each query by checking the difference in the prefix array values between the start and end of the subarray. If this difference is zero, then the subarray is special. We can also think of this problem in terms of finding the ‘bad’ pairs of adjacent elements and counting them. The subarray is special if there are no such ‘bad’ pairs. We can also consider using a similar approach to the one used in the solution, where we create a prefix array and then use it to answer each query.
1. Intuition
- The problem requires checking if every pair of adjacent elements in a subarray has different parity.
- To solve this efficiently, we can use a prefix array to keep track of the number of pairs with the same parity.
- The prefix array will help us to calculate the number of pairs with the same parity in any subarray in constant time.
- We need to iterate over the array and update the prefix array based on the parity of the current and previous elements.
- If the parity of the current and previous elements is the same, we increment the prefix array value.
- The key insight is that a subarray is special if the difference between the prefix array values at the end and start of the subarray is 0.
- This approach allows us to solve the problem in linear time complexity.
2. Implementation
- We initialize a
prefixarray with the same length as the inputnumsarray and setprefix[0]to 0. - We iterate over the
numsarray and update theprefixarray using the conditionif (nums[i] % 2 == nums[i - 1] % 2)to check if the current and previous elements have the same parity. - If the condition is true, we increment the
prefixarray value at the current indexiby settingprefix[i] = prefix[i - 1] + 1. - Otherwise, we set
prefix[i] = prefix[i - 1]to keep the same value as the previous index. - We then iterate over the
queriesarray and for each query, we calculate the difference between theprefixarray values at the end and start of the subarray usingprefix[end] - prefix[start]. - If the difference is 0, we set the corresponding value in the
ansarray totrue, indicating that the subarray is special. - Finally, we return the
ansarray containing the results for all queries.
Complexity Analysis
Time Complexity:
- The algorithm has two main loops: the first loop iterates over the
numsarray to calculate theprefixarray, resulting in a time complexity of O(n), where n is the size of thenumsarray. - The second loop iterates over the
queriesarray, and for each query, it performs a constant amount of work, resulting in a time complexity of O(m), where m is the size of thequeriesarray. Since these loops are executed sequentially, the overall time complexity is O(n + m). - The mathematical reasoning behind this time complexity is based on the fact that the algorithm performs a linear scan over the input arrays, and the number of operations grows linearly with the size of the input. This justifies the Big O classification of O(n + m), as it represents the worst-case, average-case, and best-case time complexities.
Space Complexity:
- The algorithm uses two additional arrays:
prefixandans, which have sizes of n and m, respectively, where n is the size of thenumsarray and m is the size of thequeriesarray. - The
prefixarray is used to store the prefix sum, and its size is directly proportional to the size of thenumsarray. Theansarray is used to store the results of the queries, and its size is directly proportional to the size of thequeriesarray. - The space complexity is therefore O(n + m), as the algorithm uses a total of n + m extra space to store the
prefixandansarrays, in addition to the input arraysnumsandqueries.
Footnote
This question is rated as Medium difficulty.
Hints
Try to split the array into some non-intersected continuous special subarrays.
For each query check that the first and the last elements of that query are in the same subarray or not.