Prepin
Log in

Given an integer array, return a response array where the i-th element of the output is the product of all elements in the input array, excluding the i-th element itself.

Approach / Explanation

Let's analyze the question: You are given an array of integers, for instance, [2, 3, 4, 5] . The task is to generate a new array where each i-th element is the product of all elements in the original array, excluding the i-th element. To clarify this, let's consider the example with the array [2, 3, 4, 5] . The resulting array should be [60, 40, 30, 24] . Why is that? For i = 0, the first element of the output array is calculated as 3 4 5 = 60. For i = 1, the second element is 2 4 5 = 40. We continue this process similarly for each index i. It's important to remember: Array indices are counted from 0 to n-1. The multiplication should include all elements except the one at the i-th position in the original array.

Suggested Answer

Consider our input integer array: [10, 20, 30, 40] . We can approach the solution as follows: For the 0th (first) element: We calculate the product of all numbers except the one at index 0. Thus, 20 30 40 equals 24000. Next, for the 1st (second) element: We take the product of all numbers except the one at index 1. This gives us 10 30 40, which equals 12000. Then, for the 2nd (third) element: We multiply all numbers except the one at index 2, resulting in 10 20 40, which equals 8000. Finally, for the 3rd (fourth) element: We compute the product of all numbers except the one at index 3, leading to 10 20 30, which equals 6000. Therefore, the expected output is [24000, 12000, 8000, 6000] .

Alternative Answer

Now, let's work through another random array: [4, 5, 6] . For the 0th (first) element: We multiply all the numbers except for the one at index 0. Thus, 5 * 6 equals 30. For the 1st (second) element: We multiply all the numbers except for the one at index 1. Therefore, 4 * 6 results in 24. Lastly, for the 2nd (third) element: We multiply all the numbers except for the one at index 2. Hence, 4 * 5 gives us 20. The resulting output array will be: [30, 24, 20] .

Question Details

Difficulty & Category

Medium
algorithms

Companies

Microsoft

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.