bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 922. Sort Array By Parity II

922. Sort Array By Parity II

Leetcode Solutions

In-Place Two Pointers Approach

  1. Initialize two pointers i and j to 0 and 1, respectively.
  2. While i is less than the length of the array: a. If nums[i] is even, increment i by 2. b. Otherwise, while j is less than the length of the array and nums[j] is odd, increment j by 2. c. If j is less than the length of the array, swap nums[i] and nums[j]. d. Increment i by 2.
  3. Return the modified array nums.
UML Thumbnail

Two Pass with Auxiliary Array Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...