Maximize a digit array using adjacent swaps of same-parity digits

Quick Overview

Given an array of digits where neighboring digits of the same parity may be swapped any number of times, return the arrangement that represents the largest number. It tests reasoning about which positions an element can reach under restricted swaps and turning that invariant into a simple per-segment ordering.

Maximize a digit array using adjacent swaps of same-parity digits

Company: NVIDIA

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of digits `nums`, neighboring elements of the same parity (both even or both odd) can be exchanged any number of times. The array represents a number in which the first element is the most significant digit. Find the maximum possible value the array can represent, and return the array that represents the maximum number achievable through valid swaps. ### Function Signature ```python def max_value_with_parity_swaps(nums: list[int]) -> list[int]: ``` ### Rules - A swap exchanges `nums[i]` and `nums[i + 1]`, and it is allowed only when both values are even or both are odd. - The value is read with `nums[0]` as the most significant digit. Leading zeros are allowed and stay in the returned array. - All arrangements have the same length, so the arrangement with the largest value is unique. ### Constraints - `1 <= len(nums) <= 10^5` - `0 <= nums[i] <= 9` ### Examples ```text Input: nums = [3, 5, 1, 4, 2, 8] Output: [5, 3, 1, 8, 4, 2] ``` The first three elements [3, 5, 1] are all odd, so they can be rearranged to [5, 3, 1]. The last three elements [4, 2, 8] are all even, so they can be rearranged to [8, 4, 2]. ```text Input: nums = [1, 2, 3, 4, 5] Output: [1, 2, 3, 4, 5] ``` The parity alternates (odd, even, odd, even, odd), so no two neighbors share a parity and no swap is possible. ```text Input: nums = [4, 1, 6, 8, 0] Output: [4, 1, 8, 6, 0] ``` The even digits 6, 8 and 0 sit next to each other and can be reordered to 8, 6, 0. The 8 cannot move to the front, because the odd 1 sits between it and the 4.

Overview: Given an array of digits where neighboring digits of the same parity may be swapped any number of times, return the arrangement that represents the largest number. It tests reasoning about which positions an element can reach under restricted swaps and turning that invariant into a simple per-segment ordering.

NVIDIA logo
NVIDIA
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of digits nums, neighboring elements of the same parity (both even or both odd) can be exchanged any number of times.

The array represents a number in which the first element is the most significant digit. Find the maximum possible value the array can represent, and return the array that represents the maximum number achievable through valid swaps.

Function Signature

def max_value_with_parity_swaps(nums: list[int]) -> list[int]:

Rules

  • A swap exchanges nums[i] and nums[i + 1] , and it is allowed only when both values are even or both are odd.
  • The value is read with nums[0] as the most significant digit. Leading zeros are allowed and stay in the returned array.
  • All arrangements have the same length, so the arrangement with the largest value is unique.

Constraints

  • 1 <= len(nums) <= 10^5
  • 0 <= nums[i] <= 9

Examples

Input: nums = [3, 5, 1, 4, 2, 8]
Output: [5, 3, 1, 8, 4, 2]

The first three elements [3, 5, 1] are all odd, so they can be rearranged to [5, 3, 1]. The last three elements [4, 2, 8] are all even, so they can be rearranged to [8, 4, 2].

Input: nums = [1, 2, 3, 4, 5]
Output: [1, 2, 3, 4, 5]

The parity alternates (odd, even, odd, even, odd), so no two neighbors share a parity and no swap is possible.

Input: nums = [4, 1, 6, 8, 0]
Output: [4, 1, 8, 6, 0]

The even digits 6, 8 and 0 sit next to each other and can be reordered to 8, 6, 0. The 8 cannot move to the front, because the odd 1 sits between it and the 4.