< Summary

Information
Class: LeetCode.Algorithms.MakeSumDivisibleByP.MakeSumDivisibleByPPrefixSum
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MakeSumDivisibleByP\MakeSumDivisibleByPPrefixSum.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 56
Line coverage: 100%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinSubarray(...)87.5%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MakeSumDivisibleByP\MakeSumDivisibleByPPrefixSum.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.MakeSumDivisibleByP;
 13
 14/// <inheritdoc />
 15public class MakeSumDivisibleByPPrefixSum : IMakeSumDivisibleByP
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <param name="p"></param>
 23    /// <returns></returns>
 24    public int MinSubarray(int[] nums, int p)
 325    {
 1426        var sum = nums.Aggregate<int, long>(0, (current, num) => current + num);
 27
 328        var target = (int)(sum % p);
 29
 330        if (target == 0)
 131        {
 132            return 0;
 33        }
 34
 235        var prefixModDictionary = new Dictionary<int, int> { [0] = -1 };
 36
 237        var result = nums.Length;
 238        var currentMod = 0;
 39
 2040        for (var i = 0; i < nums.Length; i++)
 841        {
 842            currentMod = (currentMod + nums[i]) % p;
 43
 844            var complement = (currentMod - target + p) % p;
 45
 846            if (prefixModDictionary.TryGetValue(complement, out var value))
 447            {
 448                result = Math.Min(result, i - value);
 449            }
 50
 851            prefixModDictionary[currentMod] = i;
 852        }
 53
 254        return result == nums.Length ? -1 : result;
 355    }
 56}