Product of Array Except self

I am getting TIME LIMIT EXCEEDED error in this program too, pls somebody tell me why this error keeps up coming & coming one after another. I am very frustated of this problem. The question is :

Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].

The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

You must write an algorithm that runs in O(n) time and without using the division operation.

And the program is as follows :

'''
class Solution {
public int[] productExceptSelf(int[] nums) {
int[] answer = new int[nums.length];

    for (int i = 0; i < nums.length; i++) {
        int total = 1;
        int left = 0;
        int right = nums.length - 1;
        
        while (left < i) {
            total *= nums[left];
            left++;
        }
        while (right > i) {
            total *= nums[right];
            right--;
        }
        answer[i] = total;
    }
    return answer;
}

}
'''

Comments (2)