Product of Array Except Self
Given an integer array nums Return an array where answer[i] is the product of all elements except nums[i] Must run in O(n) time without using division Example: [1,2,3,4] => [24,12,8,6]
Each answer is the product on its left times the product on its right. Two passes build it without division.
Examples
You write
product_except_self(nums: List[int])
Example 1
- Input
- nums = [1,2,3,4]
- returns
- [24,12,8,6]
Example 2
- Input
- nums = [-1,1,0,-3,3]
- returns
- [0,0,9,0,0]
The pattern behind it
Arrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.
Each locked part opens in the editor once the part before it passes.
Prep guides that list this problem
Our company tags come from public interview write-ups. None of these is a confirmed question.