Ctrl K

Swapping Digits of the Same Parity

Easy
You are given a positive integer n. You may swap two of its digits as many times as you like, but only when both digits are even or both are odd. Return the largest number you can obtain this way.
Example 1
n=2716 → 6712

The even digits 2 and 6 sit at positions 0 and 3, the odd digits 7 and 1 at positions 1 and 2. Swapping 2 and 6 gives 6712; the odd ones are already in descending order.

Example 2
n=135 → 531

All digits are odd, so they can be arranged freely: 531.

Example 3
n=48 → 84
Constraints
  • 1 ≤ n ≤ 10¹⁸
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMedals and Places EasyAverage of the Best Five EasyThe Dimmest Rows EasyTwo Picks, Minus One EasyBest Picks in Order Easy
loading Python…
Input
n=2716
Expected
6712

Run checks the first 3 tests in your browser. Submit checks all 69 tests on the server, hidden ones included.