An array is considered to follow an alternating parity pattern if it does not contain two consecutive integers of the same parity. In other words, consecutive integers in the array should never be two even numbers or two odd numbers.
Given an array of non-negative integers numbers, determine whether there are elements which break the alternating parity pattern.
Return the index of the first element where the alternating parity pattern breaks, or -1 if there are no such elements.
Example
- For
numbers = [1, 2, 5, 3, 6], the output should besolution(numbers) = 3. - Since both
numbers[2] = 5andnumbers[3] = 3are odd, the first index where the alternating parity pattern breaks isnumbers[3]. - For
numbers = [1, 4, 7, 2, 5, 6], the output should besolution(numbers) = -1. - All elements in even-numbered positions are odd, and all elements in odd-numbered positions are even, so there are no indices where the pattern breaks.
This problem asks you to detect whether an array follows an alternating parity pattern, meaning adjacent elements must alternate between even and odd. A simple linear scan is enough: compare each pair of neighbors and return the first index where their parity matches; if no violation appears, return -1. The input size is small enough that an O(n) solution is more than sufficient.