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?
- A. 86389
- B. 98368
- C. 98863
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Children need greed factors [1, 2, 3, 5, 7]. Each child can receive at most one cookie, and a child is satisfied if the cookie size is at…
- In one move you may increase ALL but one element of the array [1, 2, 4, 7] by 1. What is the minimum number of moves to make all elements…
- Each move changes an element of [1, 2, 9, 10, 4, 4, 3] by exactly 1 (up or down). What is the minimum number of moves needed to make all…
- Ropes of lengths [4, 3, 2, 6] are joined two at a time, and joining costs the sum of their lengths. You may choose the order of joins…
- Vertical lines have heights [1, 8, 6, 2, 5, 4, 8, 3, 7, 9, 1, 6] at positions 0 to 11. Choose two lines; the water they hold is…
- What is the maximum possible sum of entries of a non-empty contiguous rectangular submatrix of [[1, -2, 3, 0], [-1, 4, -2, 2], [2, -3, 5…