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 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

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


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.