Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

238. Product of Array Except Self #3

Merged
merged 1 commit into from
Jul 8, 2024
Merged

238. Product of Array Except Self #3

merged 1 commit into from
Jul 8, 2024

Conversation

stada526
Copy link
Owner

@stada526 stada526 commented Jul 8, 2024

Problem Link

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

Solution

One immediate approach is to calculate the product of all elements in nums, and then divide this product by each element at index i to obtain the result.
For example, if nums is [2,3,4], the total product is 2 * 3 * 4 = 24. Dividing this product by each element gives [24/2, 24/3, 24/4] = [12, 8, 6].
This solution operates in O(n) time complexity, but division is not allowed in this problem.

Therefore, another approach can be considered.
In this approach, for each index i in nums, we calculate the product of all elements before i (from 0 to i-1) and the product of all elements after i (from i+1 to the end).
Finally, we multiply these two products together to get the desired result, which is the product of all elements expect for nums[i].

@stada526 stada526 merged commit 1055c56 into main Jul 8, 2024
1 check passed
@stada526 stada526 deleted the f/238 branch July 8, 2024 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant