Member-only story
π Meta Hacker Cup 2015 β Qualification Round: Cooking the Books
Difficulty: Easy
Topic: Greedy algorithm, digit manipulation, swapping
Problem URL: https://www.facebook.com/codingcompetitions/hacker-cup/2015/qualification-round/problems/A
Problem Summary
You are given a positive integer N. Your goal is to find the minimum and maximum values achievable by swapping at most one pair of digits in N. The constraint: no leading zeros are allowed.
For example, if N = 31524, you can swap digits to get 13524 (minimum) and 51324 (maximum). For N = 897, you get 798 (minimum) and 987 (maximum). Single-digit numbers like 5 cannot be improved, so both min and max are 5.
This is a classic greedy problem: to minimize, push the smallest non-zero digit to the front (if beneficial); to maximize, place the largest digit at the front. The catch is avoiding leading zeros and choosing the best digit to swap from the remaining positions.
The problem tests your understanding of digit manipulation, the importance of string representation for numeric operations, and the power of greedy algorithms when applied with careful constraint handling. Itβs an excellent warm-up for competitive programming because it requires you to think about edge cases (leading zeros, single digits, no beneficial swaps) while keepingβ¦










