Leetcode 1010 Solution

This article provides solution to leetcode question 1010 (powerful-integers)

https://leetcode.com/problems/powerful-integers

Solution

class Solution {
public:
    vector<int> powerfulIntegers(int x, int y, int bound) {
        int i = 0;
        int j = 0;
        set<int> ans;

        int x_i = 1;
        int y_j = 1;

        while (x_i + y_j <= bound)
        {
            while (x_i + y_j <= bound)
            {
                ans.insert(x_i + y_j);

                if (y == 1)
                    break;

                y_j *= y;
            }

            if (x == 1)
                break;

            x_i *= x;
            y_j = 1;
        }

        std::vector<int> res;
        std::copy(ans.begin(), ans.end(), std::back_inserter(res));

        return res;
    }
};