BackmediumArrays Razorpay

Product Except Self Solution

Problem Statement

Given an array of integers, return an array where each element at index i is the product of all the numbers in the input array except the number at index i.

Example 1
Input
[1, 2, 3, 4]
Output
[24, 12, 8, 24]

Explanation: Step-by-step: Given the input [1, 2, 3, 4], we calculate the product of all numbers except the first number 1. The product of remaining elements is 2 * 3 * 4 = 24. Similarly, we calculate the product of all numbers except the second number 2, which is 1 * 3 * 4 = 12. We repeat this process for the third and fourth numbers, giving us the output [24, 12, 8, 24].

Example 2
Input
[5, 1, 1, 1]
Output
[5, 5, 5, 5]

Explanation: Step-by-step: Given the input [5, 1, 1, 1], we calculate the product of all numbers except the first number 5. The product of remaining elements is 1 * 1 * 1 = 1. Since the product of remaining elements for each element separately is indeed 1, the output is [5, 5, 5, 5].

Constraints

  • 2 <= n <= 10^5
  • -30 <= arr[i] <= 30
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free