Question: https://leetcode.com/problems/product-of-array-except-self/

Somehow we love to try the things that are restricted.

In this problem too there were 2 restrictions.

  1. time complexity cannot be more than O(n)
  2. You cannot use divisibility operation.

I tried both. 😅 It’s not my fault they are the most basic solution one can think of.

But, what else can be done?

I sneak peeked into the topics to be able to think of that.

It had 2 things Array and Prefix Sum.

Now Prefix Sum is something new to me.

Prefix Sum means something like this:

arr = [10, 20, 10, 4]

pref_arr = [10, 30, 40, 44]

This seems quite similar to what I would need

nums = [1, 2, 3, 4]

op = [24, 12, 8, 6]

After like an hour of trial and error. I finally went on to see the solution.

The solution was almost like what I was trying to do.

My Logic was correct but I couldn’t covert it to code.

By that time I had spent almost 2 hours. After a small break when I sat back I was able to solve this problem.