< Summary

Information
Class: LeetCode.Algorithms.SpecialArray2.SpecialArray2PrefixSum
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SpecialArray2\SpecialArray2PrefixSum.cs
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 66
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsArraySpecial(...)100%1010100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SpecialArray2\SpecialArray2PrefixSum.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.SpecialArray2;
 13
 14/// <inheritdoc />
 15public class SpecialArray2PrefixSum : ISpecialArray2
 16{
 17    /// <summary>
 18    ///     Time complexity - O(m + n)
 19    ///     Space complexity - O(m)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <param name="queries"></param>
 23    /// <returns></returns>
 24    public bool[] IsArraySpecial(int[] nums, int[][] queries)
 225    {
 226        var sameParity = new int[nums.Length - 1];
 27
 1828        for (var i = 0; i < nums.Length - 1; i++)
 729        {
 730            if (nums[i] % 2 == nums[i + 1] % 2)
 231            {
 232                sameParity[i] = 1;
 233            }
 34            else
 535            {
 536                sameParity[i] = 0;
 537            }
 738        }
 39
 240        var prefixSum = new int[nums.Length];
 41
 1842        for (var i = 1; i < nums.Length; i++)
 743        {
 744            prefixSum[i] = prefixSum[i - 1];
 45
 746            if (i - 1 < sameParity.Length)
 747            {
 748                prefixSum[i] += sameParity[i - 1];
 749            }
 750        }
 51
 252        var result = new bool[queries.Length];
 53
 1054        for (var i = 0; i < queries.Length; i++)
 355        {
 356            var start = queries[i][0];
 357            var end = queries[i][1];
 58
 359            var numBadPairs = prefixSum[end] - prefixSum[start];
 60
 361            result[i] = numBadPairs == 0;
 362        }
 63
 264        return result;
 265    }
 66}