• + 0 comments

    Simple solution

    vector<int> kFactorization(int n, vector<int> A) {
        // sort A
        sort(A.begin(), A.end());
        // initialize answer vector
        vector<int> ans;
        // loop
        int i = A.size() - 1;
        while (n != 1 && i >= 0){
            while (n % A[i] == 0){
                ans.push_back(n);
                n = n / A[i];
            }
            i--;
        }
        // if no way to reach N print -1
        if (n != 1){
            ans.clear();
            ans.push_back(-1);
            return ans;
        }
        // else return answer
        ans.push_back(1);
        reverse(ans.begin(), ans.end());
        return ans;
    }