< Summary

Information
Class: LeetCode.Algorithms.AddToArrayFormOfInteger.AddToArrayFormOfIntegerDivision
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AddToArrayFormOfInteger\AddToArrayFormOfIntegerDivision.cs
Line coverage
100%
Covered lines: 32
Uncovered lines: 0
Coverable lines: 32
Total lines: 70
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
AddToArrayForm(...)100%1010100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AddToArrayFormOfInteger\AddToArrayFormOfIntegerDivision.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.AddToArrayFormOfInteger;
 13
 14/// <inheritdoc />
 15public class AddToArrayFormOfIntegerDivision : IAddToArrayFormOfInteger
 16{
 17    /// <summary>
 18    ///     Time complexity - O(max(n, log k)), where n is the length of the array num and log k is the number of digits
 19    ///     integer k
 20    ///     Space complexity - O(max(n, log k)), where n is the length of the array num and log k is the number of digit
 21    ///     the integer k
 22    /// </summary>
 23    /// <param name="num"></param>
 24    /// <param name="k"></param>
 25    /// <returns></returns>
 26    public IList<int> AddToArrayForm(int[] num, int k)
 327    {
 328        var result = new List<int>();
 29
 330        var kString = k.ToString();
 331        var kIndex = kString.Length - 1;
 332        var numIndex = num.Length - 1;
 33
 334        var carry = 0;
 35
 1336        while (numIndex >= 0 || kIndex >= 0)
 1037        {
 1038            var val1 = 0;
 39
 1040            if (numIndex >= 0)
 1041            {
 1042                val1 = num[numIndex];
 1043                numIndex--;
 1044            }
 45
 1046            var val2 = 0;
 47
 1048            if (kIndex >= 0)
 849            {
 850                val2 = (int)char.GetNumericValue(kString[kIndex]);
 851                kIndex--;
 852            }
 53
 1054            var sum = val1 + val2 + carry;
 1055            carry = sum / 10;
 1056            sum %= 10;
 57
 1058            result.Add(sum);
 1059        }
 60
 361        if (carry > 0)
 162        {
 163            result.Add(carry);
 164        }
 65
 366        result.Reverse();
 67
 368        return result;
 369    }
 70}