Generate Palindromes and Binary Search | C++
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n#define ll long long\nvoid dbg() { std::cout << std::endl; } template <typename T, typename... Args> void dbg(const T& first, const Args&... args) { std::cout << first << " "; dbg(args...); }\n#define pvec(v) cout<<"[";for(int x=0;x<v.size();x++){cout<<v[x];if(x<v.size()-1)cout<<" ";}cout<<"]"<<endl;\n\n\n\n\nint createPalindrome(ll input, ll b, bool isOdd) \n{ \n    ll n = input; \n    ll palin = input; \n\n    if (isOdd) \n        n /= b; \n\n    while (n > 0) \n    { \n        palin = palin * b + (n % b); \n        n /= b; \n    } \n    return palin; \n} \n\nvector<int> generatePalindromes(int n) \n{ \n    vector<int> palindromes;\n    int number; \n\n    for (int j = 0; j < 2; j++) \n    { \n        int i = 1; \n        while ((number = createPalindrome(i, 10, j % 2)) < n) \n        { \n            palindromes.push_back(number);\n            i++; \n        } \n    } \n    sort(palindromes.begin(), palindromes.end());\n    return palindromes;\n} \nconst int MX = 1e9;\nvector<int> palindromes = generatePalindromes(MX);\n\nclass Solution {\npublic:\n    long long minimumCost(vector<int>& nums) {\n        \n        ll INF = LLONG_MAX;\n        int P = palindromes.size();\n        \n        auto getOps = [&] (int pIndex) {\n            if(pIndex < 0 || pIndex >= P) return INF;\n\n            int T = palindromes[pIndex];\n            ll cost = 0;\n            for(int x : nums) {\n                ll curCost = abs(x - T);\n                cost += curCost;\n            }\n            return cost;\n        };\n\n        int l = 0, r = P - 1;\n        ll res = INF;\n\n        while(l <= r) {\n            int mid = l + (r - l) / 2;\n            \n            ll curRes = getOps(mid);\n            ll nextRes = getOps(mid + 1);\n            res = min({res, curRes, nextRes});\n            if(nextRes < curRes) {\n                l = mid + 1;\n            }\n            else r = mid - 1;\n        }\n\n        return res;\n    }\n};\n```
['C++']