Special Array I

Updated on 25 June, 2025
Special Array I header image

Problem Statement

In this problem, we define a "special" array based on the parity (evenness or oddness) of its adjacent elements. Specifically, an array is termed as special if each adjacent pair of elements consists of one even and one odd number. The task is to assess whether a given array of integers, nums, fits this definition of special. The function should return true if the array is special, and false otherwise.

Examples

Example 1

Input:

nums = [1]

Output:

true

Explanation:

There is only one element. So the answer is `true`.

Example 2

Input:

nums = [2,1,4]

Output:

true

Explanation:

There is only two pairs: `(2,1)` and `(1,4)`, and both of them contain numbers with different parity. So the answer is `true`.

Example 3

Input:

nums = [4,3,1,6]

Output:

false

Explanation:

`nums[1]` and `nums[2]` are both odd. So the answer is `false`.

Constraints

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Approach and Intuition

To determine if the provided array nums is special, we need to evaluate the parity of each pair of adjacent elements. Here is a step-by-step plan to achieve this:

  1. If the length of the array is 1, we immediately return true since there are no adjacent pairs to compare, as seen in example 1.
  2. Traverse the array element by element starting from the first till the second last element.
  3. For each element nums[i], check the parity with nums[i + 1]:
    • If both elements have the same parity (both even or both odd), then the array can immediately be determined as not special, and we return false.
  4. If no such pair (with the same parity) is found throughout the array, this implies every adjacent pair has different parity, and hence, the array is special, and we return true.

Each of these steps ensures that we efficiently determine the special nature of the array based solely on the requirement that each pair of adjacent numbers must consist of one odd and one even number. The given constraints make this approach feasible for every possible input.

Solutions

  • C++
  • Java
  • Python
cpp
class Solution {
public:
    bool checkSpecialArray(vector<int>& values) {
        for (int i = 0; i < values.size() - 1; i++) {
            if (((values[i] & 1) ^ (values[i + 1] & 1)) == 0) {
                return false;
            }
        }
        return true;
    }
};

The provided solution in C++ addresses the problem of checking whether an array is "special". A special array is defined in such a way that no two consecutive elements in the array have the same parity (i.e., both are odd or both are even).

  • The function checkSpecialArray takes a vector of integers values as its parameter.
  • Utilize a loop to iterate through elements of the vector up until the second to last element.
  • Within the loop, determine the parity of the current element and the next element using bitwise AND operation with 1 (i.e., values[i] & 1 for the current element and values[i + 1] & 1 for the next element).
  • Check if both elements have the same parity with the XOR operation. If they do (^ result is 0), the function immediately returns false meaning the array is not special.
  • If the loop completes without finding any consecutive elements of the same parity, the function returns true, confirming the array as special.

This solution efficiently checks the condition for each pair of consecutive elements, ensuring the special property of the array with a minimal set of operations.

java
class Solution {

    public boolean checkSpecialArray(int[] elements) {
        for (int i = 0; i < elements.length - 1; i++) {
            if (((elements[i] & 1) ^ (elements[i + 1] & 1)) == 0) {
                return false;
            }
        }
        return true;
    }
}

The Java solution presented checks if an array qualifies as "special" based on specific conditions. The array is considered "special" if no two consecutive elements share the same evenness (i.e., both are even or both are odd).

Review the approach with these steps:

  1. Establish a method checkSpecialArray that accepts an integer array as a parameter.
  2. Loop through the entire length of the array minus one using a for loop.
  3. Within the loop, apply a bitwise AND operation (&) with each element and 1 to determine if the element is even or odd.
  4. Use the XOR operation (^) to compare the odd/even status of consecutive elements.
  5. If the result is 0, it implies both elements are similarly even or odd, thus returning false.
  6. If the loop completes without finding consecutive similar evenness elements, return true.

By understanding this method, ensure arrays are effectively checked for the special condition as described.

python
class Solution:
    def checkArrayParity(self, arr):
        for i in range(len(arr) - 1):
            if ((arr[i] & 1) == (arr[i + 1] & 1)):
                return False
        return True

The solution provided addresses the challenge of determining if a given array has alternating parity. In simpler terms, it checks if odd and even numbers in the array alternate without two consecutive numbers sharing the same parity (either both odd or both even).

This Python function, checkArrayParity, iterates through the array arr up to the second last element. During each iteration:

  • It uses a bitwise AND operation (&) with 1 to determine the parity of the current element (arr[i]) and the next element (arr[i + 1]). If both elements have the same result (both are 0 for even or both are 1 for odd), it returns False.
  • If the loop completes without finding two consecutive elements of the same parity, it returns True, indicating that the parities alternate properly throughout the array.

Keep the following in mind when incorporating this function:

  • Ensure the input array arr is not empty and has more than one element for the function to work correctly.
  • This function is especially useful in scenarios where data needs validation for alternating patterns, which can be common in signal processing or data stream management tasks.

Comments

No comments yet.