Skip to content

Practice Sort Array By Parity II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#922
LeetCode Problem

Sort Array By Parity II

Problem Statement

Given an array of integers nums, half of the integers in nums are odd, and the other half are even. Sort the array so that whenever nums[i] is odd, i is odd, and whenever nums[i] is even, i is even. Return any answer array that satisfies this condition. Example 1: Example 2: Constraints: Follow Up: Could you solve it in-place?

Source: Sort Array By Parity II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [4,2,5,7]
Output
[4,5,2,7]

# Example 2

Input
nums = [2,3]
Output
[2,3]

Constraints

2 <= nums.length <= 2 * 10^4
nums.length is even.
Half of the integers in nums are even.
0 <= nums[i] <= 1000

Related LeetCode Problems

Practice more problems similar to Sort Array By Parity II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.