Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

922. Sort Array By Parity II

Easy70.9% Acceptance
ArrayTwo PointersSorting
Asked by:
M
Microsoft
ProblemSolutions (8)VideosCompanies (1)Notes

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:

Input: nums = [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.

Example 2:

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

Constraints:

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

Follow Up: Could you solve it in-place?

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

In #922 Sort Array By Parity II, the goal is to rearrange the array so that numbers at even indices are even and numbers at odd indices are odd. Since the problem guarantees an equal number of even and odd elements, we can use a two-pointer strategy to place values in their correct positions.

The efficient idea is to maintain two pointers: one that scans even indices (0, 2, 4, ...) and another that scans odd indices (1, 3, 5, ...). When a misplaced element is found at an even index (an odd number), the odd pointer moves forward until it finds an even number placed incorrectly. These two elements can then be swapped to restore the parity rule.

This method avoids unnecessary sorting and processes the array in a single pass. The approach leverages index parity checks using index % 2 and value parity using num % 2. As a result, the algorithm runs efficiently with linear time and minimal extra memory.

Complexity

ApproachTime ComplexitySpace Complexity
Two Pointers (Even/Odd Index Scan)O(n)O(1)
Sorting + RearrangementO(n log n)O(1) or O(n)

Video Solution Available

NeetCodeIO

View all video solutions

Solutions (8)

Two Pointer Approach

This approach utilizes two pointers to iteratively place even and odd numbers at the correct indexes.

One pointer is used to find even numbered indexes and the other one for odd numbered indexes. Traverse through the array, and whenever you find an element not in its correct place, swap it with the correct one using the two pointers.

Time Complexity: O(n) since we may visit each element a couple of times.
Space Complexity: O(1) as the swap is done in-place.

CC++JavaPythonC#JavaScript
1#include <stdbool.h>
2void sortArrayByParityII(int* nums, int numsSize) {
3    int even = 0, odd = 1;
4    

Explanation

This C implementation uses two pointers; one for indexing even positions and another for odd positions.

Both pointers step through the array stepping by two positions each time, and they swap the odd-even positioned elements whenever they're misplaced.

Separate Arrays for Even and Odd Numbers

This approach involves segregating even and odd numbers into separate lists and then efficiently assembling them at respected places in the resultant array.

Despite potentially not being in-place, this technique simplifies understanding the array content and meets the problem requirements.

Time Complexity: O(n)
Space Complexity: O(n) due to the auxiliary lists for even and odd numbers.

PythonJavaScript
1class Solution:
2    def sortArrayByParityII(self

Video Solutions

Watch expert explanations and walkthroughs

Sort Array by Parity - Leetcode 905 - Python

NeetCodeIO
7:259,990 views

Asked By Companies

1 companies
M
Microsoft

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Container With Most WaterMedium
3SumMedium
3Sum ClosestMedium
4SumMedium
More similar problems

Related Topics

ArrayTwo PointersSorting

Problem Stats

Acceptance Rate70.9%
DifficultyEasy
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Sort Array By Parity II asked in FAANG interviews?

Yes, variations of parity or array rearrangement problems often appear in technical interviews, including FAANG-style interviews. They test understanding of array manipulation, pointer techniques, and efficient in-place operations.

What data structure is best for Sort Array By Parity II?

The problem can be solved directly using the input array with index pointers, so no additional data structures are required. Simple pointer manipulation and parity checks are enough to rearrange the elements efficiently.

What is the optimal approach for Sort Array By Parity II?

The optimal approach uses a two-pointer technique that scans even and odd indices separately. When a mismatch is found, the elements are swapped to restore the correct parity placement. This allows the array to be fixed in a single linear pass.

Why does the two-pointer method work well for this problem?

Because the problem guarantees equal counts of even and odd numbers, every misplaced element must have a corresponding element to swap with. Two pointers help locate these mismatches quickly without scanning the array multiple times.

while
(
even
<
numsSize
&&
odd
<
numsSize
)
{
5
while
(
even
<
numsSize
&&
nums
[
even
]
%
2
==
0
)
even
+=
2
;
6
while
(
odd
<
numsSize
&&
nums
[
odd
]
%
2
==
1
)
odd
+=
2
;
7
if
(
even
<
numsSize
&&
odd
<
numsSize
)
{
8
int
temp
=
nums
[
even
]
;
9
nums
[
even
]
=
nums
[
odd
]
;
10
nums
[
odd
]
=
temp
;
11
}
12
}
13
}
,
nums
)
:
3
evens
=
[
x
for
x
in
nums
if
x
%
2
==
0
]
4
odds
=
[
x
for
x
in
nums
if
x
%
2
==
1
]
5
result
=
[
0
]
*
len
(
nums
)
6
result
[
:
:
2
]
=
evens
7
result
[
1
:
:
2
]
=
odds
8
return
result

Explanation

This Python method separates evens and odds into different sublists. Then it assembles those lists alternatively filling into the result array.

This approach straightforwardly constructs a properly sequenced result list.