< Summary

Information
Class: LeetCode.Algorithms.ProductOfArrayExceptSelf.ProductOfArrayExceptSelfPrefixSum
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ProductOfArrayExceptSelf\ProductOfArrayExceptSelfPrefixSum.cs
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 52
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ProductExceptSelf(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ProductOfArrayExceptSelf\ProductOfArrayExceptSelfPrefixSum.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.ProductOfArrayExceptSelf;
 13
 14/// <inheritdoc />
 15public sealed class ProductOfArrayExceptSelfPrefixSum : IProductOfArrayExceptSelf
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <returns></returns>
 23    public int[] ProductExceptSelf(int[] nums)
 224    {
 225        var n = nums.Length;
 26
 227        Span<int> prefix = stackalloc int[n + 1];
 28
 229        prefix[0] = 1;
 30
 2231        for (var i = 1; i <= n; i++)
 932        {
 933            prefix[i] = prefix[i - 1] * nums[i - 1];
 934        }
 35
 236        Span<int> suffix = stackalloc int[n + 1];
 37
 238        suffix[n] = 1;
 39
 2240        for (var i = n - 1; i >= 0; i--)
 941        {
 942            suffix[i] = suffix[i + 1] * nums[i];
 943        }
 44
 2245        for (var i = 0; i < n; i++)
 946        {
 947            nums[i] = prefix[i] * suffix[i + 1];
 948        }
 49
 250        return nums;
 251    }
 52}