The Indian Engineer

Problem 3152 Special Array II

Posted on 6 mins

Array Binary-Search Prefix-Sum

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:

  1. The subarray is [4,3,1]. 3 and 1 are both odd. So the answer to this query is false.
  2. 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 is true.

Constraints:

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 Calculation | O(n + q)        | O(n)             |

Explanation

Intial Thoughts

To determine if a subarray is special, we need to check if every pair of adjacent elements has different parity. This can be approached by creating a prefix array that keeps track of the number of pairs with the same parity. The key is to identify a pattern or a way to efficiently compare the parity of adjacent elements. We can start by examining the given solution and understanding how it utilizes the prefix array to simplify the comparison process. The initial steps should involve analyzing the array and queries to identify any patterns or relationships that can aid in solving the problem. We should also consider how the prefix array is constructed and how it is used to determine if a subarray is special.

Intuitive Analysis

The given solution uses a prefix array to efficiently check if a subarray is special. Intuitively, this approach makes sense because it allows us to avoid comparing every pair of adjacent elements in the subarray. Instead, we can use the prefix array to quickly determine if there are any pairs with the same parity. To further develop our intuition, we can think of the prefix array as a ‘counter’ that keeps track of the number of pairs with the same parity. By comparing the values in the prefix array at different indices, we can quickly determine if a subarray is special. We should also consider how the use of the modulo operator (%) to check parity affects the solution and how it contributes to the efficiency of the approach. Additionally, we can explore how the solution would change if we were to use a different data structure or approach.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Proof of correctness

$$\text{Let } n \text{ be the length of the array } nums.$$$$\text{We use a prefix sum array } prefix \text{ where } prefix[i] \text{ stores the count of pairs with the same parity up to index } i.$$$$\forall i \in [1, n], \text{ if } nums[i] \equiv nums[i-1] \pmod{2}, \text{ then } prefix[i] = prefix[i-1] + 1.$$$$\text{Otherwise, } prefix[i] = prefix[i-1].$$$$\text{For each query } [start, end], \text{ the subarray } nums[start..end] \text{ is special if and only if } prefix[end] - prefix[start] = 0.$$

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.