Algorithm


Problem Name: 922. Sort Array By Parity II

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

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int[] sortArrayByParityII(int[] A) {
    int start = 1;
    for (int i = 0; i  <  A.length; i += 2) {
      if (A[i] % 2 == 1) {
        while (A[start] % 2 != 0) {
          start += 2;
        }
        int temp = A[i];
        A[i] = A[start];
        A[start] = temp;
      }
    }
    return A;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [4,2,5,7]

Output

x
+
cmd
[4,5,2,7]

#2 Code Example with Javascript Programming

Code - Javascript Programming


const sortArrayByParityII = function(A) {
  const res = []
  const odd = []
  const even = []
  for(let i = 0, len = A.length; i  <  len; i++) {
    if(A[i] % 2 === 0) even.push(A[i])
    else odd.push(A[i])
  }
  for(let i = 0, len = odd.length; i < len; i++) {
    res.push(even[i], odd[i]>
  }
  return res
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [4,2,5,7]

Output

x
+
cmd
[4,5,2,7]

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def sortArrayByParityII(self, A):
        even, odd = [a for a in A if not a % 2], [a for a in A if a % 2]
        return [even.pop() if not i % 2 else odd.pop() for i in range(len(A))]
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [2,3]

Output

x
+
cmd
[2,3]

#4 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _0922_SortArrayByParityII
    {
        public int[] SortArrayByParityII(int[] A)
        {
            var oddIndex = 1;
            for (int evenIndex = 0; evenIndex  <  A.Length; evenIndex += 2)
            {
                if (A[evenIndex] % 2 == 1)
                {
                    while (A[oddIndex] % 2 == 1)
                        oddIndex += 2;

                    var temp = A[evenIndex];
                    A[evenIndex] = A[oddIndex];
                    A[oddIndex] = temp;
                }
            }

            return A;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [2,3]

Output

x
+
cmd
[2,3]
Advertisements

Demonstration


Previous
#921 Leetcode Minimum Add to Make Parentheses Valid Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#923 Leetcode 3Sum With Multiplicity Solution in C, C++, Java, JavaScript, Python, C# Leetcode