Problem solution · C++

Next Special Palindrome Number

Next Special Palindrome Number: a C++ solution using binary search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Binary search
Source
Kamyu LeetCode Solutions
Length
57 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Binary search

For Next Special Palindrome Number, the implementation exploits a monotonic condition to discard half of the remaining search space after every check.

  1. Identify the ordered answer range or sorted search domain.
  2. Write a predicate whose truth changes only once.
  3. Move the appropriate boundary after each midpoint check and return the final feasible position.

Code notes

  • 57 lines of C++ from the credited upstream file next-special-palindrome-number.cpp.
  • The implementation visibly relies on sequence storage.
  • 4 loop blocks detected.

Complexity

Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeNext Special Palindrome Number · C++C++
Use this to learn the idea, then write your own version.
// Time:  precompute: O(9 * 2^9 + 16 * p + plogp), p = len(PALINDROMES)//        runtime:    O(logp)// Space: O(p) // precompute, bitmasks, sort, binary searchconst int MAX_LEN = 16; const auto& precompute = []() {    const auto& f = [](int mask) {        string result, mid;        for (int i = 0; i < 9; ++i) {            if ((mask & (1 << i)) == 0) {                continue;            }            if ((i + 1) % 2) {                if (!empty(mid)) {                    return tuple(result, mid, false);                }                mid = to_string(i + 1);            }            for (int _ = 0; _ < (i + 1) / 2; ++_) {                result.push_back('0' + (i + 1));            }        }        return tuple(result, mid, true);    };     vector<int64_t> result;    for (int mask = 1; mask < (1 << 9); ++mask) {        auto [left, mid, ok] = f(mask);        if (!ok) {            continue;        }        do {            string right = left;            reverse(begin(right), end(right));            string p = left;            p += mid;            p += right;            if (size(p) > MAX_LEN) {                break;            }            result.emplace_back(stoll(p));        } while (next_permutation(begin(left), end(left)));    }    sort(begin(result), end(result));    return result;}; const auto& PALINDROMES = precompute();class Solution {public:    long long specialPalindrome(long long n) {        return *upper_bound(begin(PALINDROMES), end(PALINDROMES), n);    }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗