Data Structures & AlgorithmsHardLeetCode-style · Codeforces Div2 C

You may swap the digits at exactly one pair of positions in the number 98368 (at most once). What is the largest number you can obtain?

  1. A. 86389
  2. B. 98368
  3. C. 98863
  4. D. 99763

Correct answer: C. 98863

Scan from left to right; at the first position whose digit can be increased by a later larger digit, swap it with the LAST occurrence of that digit. Swapping the first and last digits, or stopping at the first larger digit found, can miss the best swap.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic