Maximize an integer by swapping digits of the same parity

Quick Overview

Given a positive integer, swap pairs of digits that are both odd or both even as many times as you like and return the largest value you can reach. Tests reasoning about which arrangements are reachable, sorting within groups, and a greedy argument over digit positions.

Maximize an integer by swapping digits of the same parity

Company: Zscaler

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a positive integer `num`. You may swap any two digits of `num` that have the same parity, meaning both digits are odd or both are even. You may do this any number of times, including zero. Return the largest possible value of `num` after any number of swaps. ### Function Signature ```python def largest_integer(num: int) -> int: ``` ### Rules - A swap exchanges the digits at any two positions of the decimal form of `num`, adjacent or not. The two digits must both be odd or both be even. The digit `0` is even. - Return the resulting value as an integer. ### Constraints - `1 <= num <= 10^9` ### Examples ```text Input: num = 1234 Output: 3412 ``` Swapping 1 with 3 (both odd) gives 3214, and swapping 2 with 4 (both even) then gives 3412. The first and third positions must hold odd digits and the second and fourth must hold even digits, so no larger value is reachable. ```text Input: num = 65875 Output: 87655 ``` Swapping the 5 in the second position with the 7 (both odd) gives 67855, and swapping 6 with 8 (both even) then gives 87655. ```text Input: num = 206 Output: 620 ``` All three digits are even, so they can be placed in any order, and 620 is the largest arrangement.

Overview: Given a positive integer, swap pairs of digits that are both odd or both even as many times as you like and return the largest value you can reach. Tests reasoning about which arrangements are reachable, sorting within groups, and a greedy argument over digit positions.

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

You are given a positive integer num. You may swap any two digits of num that have the same parity, meaning both digits are odd or both are even. You may do this any number of times, including zero.

Return the largest possible value of num after any number of swaps.

Function Signature

def largest_integer(num: int) -> int:

Rules

  • A swap exchanges the digits at any two positions of the decimal form of num , adjacent or not. The two digits must both be odd or both be even. The digit 0 is even.
  • Return the resulting value as an integer.

Constraints

  • 1 <= num <= 10^9

Examples

Input: num = 1234
Output: 3412

Swapping 1 with 3 (both odd) gives 3214, and swapping 2 with 4 (both even) then gives 3412. The first and third positions must hold odd digits and the second and fourth must hold even digits, so no larger value is reachable.

Input: num = 65875
Output: 87655

Swapping the 5 in the second position with the 7 (both odd) gives 67855, and swapping 6 with 8 (both even) then gives 87655.

Input: num = 206
Output: 620

All three digits are even, so they can be placed in any order, and 620 is the largest arrangement.