< Summary

Information
Class: LeetCode.Algorithms.NumberOfSubArraysWithOddSum.NumberOfSubArraysWithOddSumPrefixSum
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\NumberOfSubArraysWithOddSum\NumberOfSubArraysWithOddSumPrefixSum.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 54
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
NumOfSubarrays(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\NumberOfSubArraysWithOddSum\NumberOfSubArraysWithOddSumPrefixSum.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 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.NumberOfSubArraysWithOddSum;
 13
 14/// <inheritdoc />
 15public class NumberOfSubArraysWithOddSumPrefixSum : INumberOfSubArraysWithOddSum
 16{
 17    private const int Mod = (int)(1e9 + 7);
 18
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(1)
 22    /// </summary>
 23    /// <param name="arr"></param>
 24    /// <returns></returns>
 25    public int NumOfSubarrays(int[] arr)
 326    {
 327        var count = 0;
 328        var oddCount = 0;
 329        var evenCount = 1;
 330        var prefixSum = 0;
 31
 3532        foreach (var num in arr)
 1333        {
 1334            prefixSum += num;
 35
 1336            if (prefixSum % 2 == 0)
 737            {
 738                count += oddCount;
 39
 740                evenCount++;
 741            }
 42            else
 643            {
 644                count += evenCount;
 45
 646                oddCount++;
 647            }
 48
 1349            count %= Mod;
 1350        }
 51
 352        return count;
 353    }
 54}

Methods/Properties

NumOfSubarrays(System.Int32[])