Advertisement

Special Array II - LeetCode 3152 Solution

Special Array II - Complete Solution Guide

Special Array II is LeetCode problem 3152, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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] = [from i , to i ] your task is to check that subarray nums[from i ..to i ] is special or not. Return an array of booleans answer such that answer[i] is true if nums[from i ..to i ] is special. Example 1: Input: nums = [3,4,1,2,6], queries = [[0,4]] Output: [false] Explanation: The subarray

Detailed Explanation

The problem asks you to determine, for a given array of integers `nums` and a list of queries `queries`, whether the subarray specified by each query is "special". A subarray is considered "special" if every pair of adjacent elements within that subarray has different parity (i.e., one is even and the other is odd). Each query `queries[i]` contains a start and end index [from_i, to_i] indicating the subarray `nums[from_i...to_i]` to be tested. The output is an array of booleans, where each element indicates whether the corresponding queried subarray is special (true) or not (false). The array indices are inclusive.

Solution Approach

The provided solution uses a prefix sum technique to efficiently determine if a subarray is special. It first calculates a prefix sum array `prefix_violations`, where each element `prefix_violations[i]` stores the number of times adjacent elements in `nums[0...i]` have the same parity. Then, for each query `[from_i, to_i]`, it subtracts `prefix_violations[from_i]` from `prefix_violations[to_i]` to obtain the number of parity violations within the subarray `nums[from_i+1...to_i]`. If this count is zero and the `from_i` and `to_i` indices are different, or the `from_i` and `to_i` are same the subarray is considered special, if `from_i` and `to_i` are different, and the violation count is zero, then the subarray `nums[from_i...to_i]` does not have any adjacent element with same parity and is therefore special.

Step-by-Step Algorithm

  1. Step 1: Create a `prefix_violations` array of the same length as `nums`, initialized with zeros.
  2. Step 2: Iterate through `nums` from index 1 to `n-1`. For each index `i`, calculate the number of violations up to that point. A violation occurs if `nums[i-1]` and `nums[i]` have the same parity. Update `prefix_violations[i]` as `prefix_violations[i-1] + (1 if nums[i-1]%2 == nums[i]%2 else 0)`.
  3. Step 3: Create an `answer` array to store the boolean results for each query.
  4. Step 4: Iterate through the `queries` array. For each query `[from_i, to_i]`, calculate the number of violations within the subarray `nums[from_i...to_i]`. If `from_i` and `to_i` are equal then the subarray only contains one element and it is special, so add 'true' to answer. Otherwise set `violations_count` to `prefix_violations[to_i] - prefix_violations[from_i]`
  5. Step 5: If `violations_count` is 0, it means there are no adjacent element with same parity and add `true` to the `answer` array. Otherwise, add `false` to the `answer` array.
  6. Step 6: Return the `answer` array.

Key Insights

  • Insight 1: The core challenge is efficiently checking the parity of adjacent elements within the given subarray for each query.
  • Insight 2: Prefix sums can be used to precompute the number of parity violations (adjacent elements with the same parity) up to each index, allowing for O(1) query processing after the O(n) precomputation.
  • Insight 3: An edge case to consider is when the 'from' and 'to' indices are equal, meaning the subarray contains only one element, and hence it is always considered special.

Complexity Analysis

Time Complexity: O(n + q)

Space Complexity: O(n)

Topics

This problem involves: Array, Binary Search, Prefix Sum.

Companies

Asked at: National Payments Corporation of India.