BackeasyArrays Zomato

Sort Array by Parity Solution

Problem Statement

Given a batch of part numbers, reorder them so all even-numbered parts come first, followed by odd-numbered parts.

Example 1
Input
[2, 6, 4, 8, 10, 5, 9, 7, 3, 1]
Output
[2, 6, 4, 8, 10, 5, 9, 7, 3, 1]

Explanation: Step 1: Separate even and odd numbers. Even numbers: [2, 4, 6, 8, 10]. Odd numbers: [5, 9, 7, 3, 1]. Step 2: Sort the even numbers in ascending order: [2, 4, 6, 8, 10]. Step 3: Sort the odd numbers in ascending order: [1, 3, 5, 7, 9]. Step 4: Combine the sorted even and odd numbers: [2, 6, 4, 8, 10, 5, 9, 7, 3, 1].

Example 2
Input
[4, 10, 6, 8, 2, 7, 1, 3, 9, 5]
Output
[2, 4, 6, 8, 10, 1, 3, 7, 9, 5]

Explanation: Step 1: Separate even and odd numbers. Even numbers: [4, 10, 6, 8, 2]. Odd numbers: [7, 1, 3, 9, 5]. Step 2: Sort the even numbers in ascending order: [2, 4, 6, 8, 10]. Step 3: Sort the odd numbers in ascending order: [1, 3, 5, 7, 9]. Step 4: Combine the sorted even and odd numbers: [2, 4, 6, 8, 10, 1, 3, 7, 9, 5].

Constraints

  • 1 <= n <= 5000
  • 0 <= arr[i] <= 5000
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free