Difficulty: Easy | Acceptance: 76.30% | Paid: No Topics: Array, Math
You are given two integers n and m. Return an array arr of length n such that:
arr[i] is in the range [1, m]. The parity of arr[i] is the same as the parity of i (0-indexed). In other words, if i is even, arr[i] must be even, and if i is odd, arr[i] must be odd. If there are multiple answers, return any of them. If no answer exists, return an empty array.
- Examples
- Constraints
- Iterative Greedy Construction
- Mathematical Formula Initialization
- Functional Stream Construction
Examples
Example 1
Input:
nums1 = [2,3]
Output:
true
Explanation: Choose nums2[0] = nums1[0] - nums1[1] = 2 - 3 = -1.
Choose nums2[1] = nums1[1] = 3.
nums2 = [-1, 3], and both elements are odd. Thus, the answer is true.
Example 2
Input:
nums1 = [4,6]
Output:
true
Explanation:
Choose nums2[0] = nums1[0] = 4.
Choose nums2[1] = nums1[1] = 6.
nums2 = [4, 6], and all elements are even. Thus, the answer is true.
Constraints
- 1 <= n == nums1.length <= 100
- 1 <= nums1[i] <= 100
- nums1 consists of distinct integers.
Iterative Greedy Construction
Intuition To satisfy the parity condition, we simply need at least one even number and one odd number within the allowed range [1, m]. The smallest even number is 2 and the smallest odd number is 1. If m is at least 2, we have both numbers available. Since the problem allows repetition, we can greedily fill the array by iterating through indices and placing 2 for even indices and 1 for odd indices.
Steps
- Check if m is less than 2. If so, return an empty array because we cannot satisfy the parity for index 0 (which requires an even number).
- Initialize an empty list/array to store the result.
- Loop from i = 0 to n - 1.
- If i is even, append 2 to the result.
- If i is odd, append 1 to the result.
- Return the result array.
from typing import List
class Solution:
def constructArray(self, n: int, m: int) -> List[int]:
if m < 2:
return []
arr = []
for i in range(n):
if i % 2 == 0:
arr.append(2)
else:
arr.append(1)
return arrComplexity
- Time: O(n) - We iterate through the array once to fill values.
- Space: O(n) - We store the result array. (Auxiliary space is O(1) if we ignore the output).
- Notes: This is the most straightforward approach, easy to understand and implement.
Mathematical Formula Initialization
Intuition
Instead of using conditional logic inside the loop, we can derive a direct mathematical formula to determine the value at each index. We observe that for even indices we want 2, and for odd indices we want 1. This can be expressed as 2 - (i % 2). If i is even, i % 2 is 0, so result is 2. If i is odd, i % 2 is 1, so result is 1.
Steps
- Check if m is less than 2. If so, return an empty array.
- Initialize an array of size n.
- Iterate from i = 0 to n - 1.
- Set
arr[i] = 2 - (i % 2). - Return the array.
from typing import List
class Solution:
def constructArray(self, n: int, m: int) -> List[int]:
if m < 2:
return []
return [2 - (i % 2) for i in range(n)]Complexity
- Time: O(n) - We iterate through the array once.
- Space: O(n) - We store the result array.
- Notes: This approach avoids explicit if-else statements inside the loop, which can be slightly more performant and cleaner.
Functional Stream Construction
Intuition We can treat the array generation as a stream of values. We generate a sequence of n elements where each element depends on its index. This approach leverages functional programming constructs like generators, streams, or iterators to build the array declaratively.
Steps
- Check if m is less than 2. If so, return an empty array.
- Create a sequence/stream of integers from 0 to n-1.
- Map each integer i to its corresponding value (2 if even, 1 if odd).
- Collect the results into an array/list and return it.
from typing import List
class Solution:
def constructArray(self, n: int, m: int) -> List[int]:
if m < 2:
return []
# Using list comprehension (functional style)
return [2 if i % 2 == 0 else 1 for i in range(n)]Complexity
- Time: O(n) - We generate n elements.
- Space: O(n) - We store the result array.
- Notes: This approach is concise and expressive, particularly in languages like Python, JavaScript, and Java with Streams.