<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>

<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>

<p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-30 &lt;= nums[i] &lt;= 30</code></li>
	<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

static const bool Booster = []() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    return true;
}();

inline bool isdigit(char c) { return c >= '0' && c <= '9'; }

std::array<int, 100000> nums;
void parse_input_and_solve(std::ofstream& out, const std::string& s) {
    const int N = s.size();
    int left = 0;
    int idx = 0;
    while (left < N) {
        if (!isdigit(s[left]) &&
            !(s[left] == '-' && left + 1 < N && isdigit(s[left + 1]))) {
            ++left;
            continue;
        }
        int right = left;
        int value = 0;
        bool negative = false;

        if (s[right] == '-') {
            negative = true;
            ++right;
        }
        while (right < N && isdigit(s[right])) {
            value = value * 10 + (s[right] - '0');
            ++right;
        }

        if (negative)
            value = -value;
        left = right;
        nums[idx++] = value;
    }

    int arr[idx];
    for (int i = 0; i < idx; i++)
        arr[i] = nums[i];
    arr[0] = 1;
    int res = 1;
    for (int i = 0; i < idx - 1; i++) {
        res *= nums[i];
        arr[i + 1] = res;
    }
    res = 1;
    for (int i = idx - 1; i >= 1; i--) {
        res *= nums[i];
        arr[i - 1] *= res;
    }

    out << "[";
    for (int i = 0; i < idx - 1; i++) {
        out << arr[i] << ",";
    }
    out << arr[idx - 1] << "]\n";
}

bool Solve = []() {
    std::ofstream out("user.out");
    for (std::string s; std::getline(std::cin, s);) {
        parse_input_and_solve(out, s);
    }
    out.flush();
    exit(0);
    return true;
}();

class Solution {

public:

    std::vector<int> productExceptSelf(std::vector<int>& nums) {
        
        int mulNums = 1;
        static constexpr auto npos = static_cast<size_t>(-1); 
        auto posZero = npos; 
        auto size = nums.size();

        for (size_t i = 0; i < size; ++i) {
            int num = nums[i];

            if (num) {
                mulNums *= num;
            }
            else {
                if (posZero != npos) {
                    std::fill(nums.begin(), nums.end(), 0);
                    return std::move(nums);
                }
                posZero = i;
            }
        }

        if (posZero != npos) {
            std::fill(nums.begin(), nums.end(), 0);
            nums[posZero] = mulNums;
            return std::move(nums);
        }

        for (size_t i = 0; i < size; ++i)
            nums[i] = mulNums / nums[i];

        return std::move(nums);
    }
};

9500