generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem41.cs
More file actions
40 lines (34 loc) · 1.23 KB
/
Problem41.cs
File metadata and controls
40 lines (34 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/product-of-array-except-self/">Product of Array Except Self</see>.
/// </summary>
public static class Problem41
{
/// <summary>
/// Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
/// You must write an algorithm that runs in O(n) time and without using the division operation.
/// Time complexity: O(n).
/// Space complexity: O(n).
/// </summary>
/// <param name="nums">Array to traverse.</param>
/// <returns>Product of array except self.</returns>
public static int[] ProductExceptSelf(int[] nums)
{
var left = new int[nums.Length];
var right = new int[nums.Length];
left[0] = nums[0];
right[^1] = nums[^1];
for (int i = 1, j = nums.Length - 2; i < nums.Length && j >= 0; i++, j--)
{
left[i] = left[i - 1] * nums[i];
right[j] = right[j + 1] * nums[j];
}
nums[0] = right[1];
nums[^1] = left[^2];
for (var i = 1; i < nums.Length - 1; i++)
{
nums[i] = left[i - 1] * right[i + 1];
}
return nums;
}
}