Loading problem…
You're building a recommendation algorithm for an e-commerce platform. For each product, you need to calculate a recommendation score based on the product of all other products' ratings, excluding the current product. This helps generate "customers who bought this also bought" suggestions.
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 operator.
productExceptSelf([1,2,3,4]); // [24,12,8,6]
// For index 0: 2*3*4 = 24
// For index 1: 1*3*4 = 12
// For index 2: 1*2*4 = 8
// For index 3: 1*2*3 = 6
productExceptSelf([-1,1,0,-3,3]); // [0,0,9,0,0]This problem models real recommendation systems: