< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FindMedianSortedArrays(...)100%2020100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MedianOfTwoSortedArrays\MedianOfTwoSortedArraysIterative.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.MedianOfTwoSortedArrays;
 13
 14/// <inheritdoc />
 15public class MedianOfTwoSortedArraysIterative : IMedianOfTwoSortedArrays
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n + m)
 19    ///     Space complexity - O(n + m)
 20    /// </summary>
 21    /// <param name="nums1"></param>
 22    /// <param name="nums2"></param>
 23    /// <returns></returns>
 24    public double FindMedianSortedArrays(int[] nums1, int[] nums2)
 1625    {
 1626        var concat = new int[nums1.Length + nums2.Length];
 27
 1628        var nums1Index = 0;
 1629        var nums2Index = 0;
 30
 10331        while (nums1Index < nums1.Length || nums2Index < nums2.Length)
 8732        {
 8733            int? num1 = null;
 34
 8735            if (nums1Index < nums1.Length)
 6036            {
 6037                num1 = nums1[nums1Index];
 6038            }
 39
 8740            int? num2 = null;
 41
 8742            if (nums2Index < nums2.Length)
 8043            {
 8044                num2 = nums2[nums2Index];
 8045            }
 46
 8747            if (num1.HasValue && num2.HasValue)
 5348            {
 5349                if (num1 < num2)
 3350                {
 3351                    concat[nums1Index + nums2Index] = num1.Value;
 52
 3353                    nums1Index++;
 3354                }
 55                else
 2056                {
 2057                    concat[nums1Index + nums2Index] = num2.Value;
 58
 2059                    nums2Index++;
 2060                }
 5361            }
 3462            else if (num1.HasValue)
 763            {
 764                concat[nums1Index + nums2Index] = num1.Value;
 65
 766                nums1Index++;
 767            }
 2768            else if (num2.HasValue)
 2769            {
 2770                concat[nums1Index + nums2Index] = num2.Value;
 71
 2772                nums2Index++;
 2773            }
 8774        }
 75
 1676        var index = concat.Length / 2;
 77
 1678        if (concat.Length % 2 == 0)
 779        {
 780            return (concat[index - 1] + concat[index]) / 2.0;
 81        }
 82
 983        return concat[index];
 1684    }
 85}