Given a list of integers, return a list where each element at index i is the product of all elements in the input list except the element at index i. You must solve this without using division and in O(n) time complexity.
Examples:
Input: nums = [1, 2, 3, 4]
Output: [24, 12, 8, 6]
Explanation: For index 0: 2*3*4=24, index 1: 1*3*4=12, index 2: 1*2*4=8, index 3: 1*2*3=6
Input: nums = [2, 3]
Output: [3, 2]
Explanation: For index 0: 3, for index 1: 2
Input: nums = [-1, 1, 0, -3, 3]
Output: [0, 0, 9, 0, 0]
Explanation: Any product containing 0 results in 0
Input: nums = null
Output: null
Explanation: Null input returns null
Input: nums = []
Output: []
Explanation: Empty list returns empty list
Apex Code Editor
Welcome to Lightning Challenge!
Create an Account
Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.
Create accountHow It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay Mar 2026
Contest runs March 3 - 31. Complete challenges to climb the leaderboard!
Points are calculated for challenges completed during the contest period.