< Summary

Information
Class: LeetCode.Algorithms.MedianOfTwoSortedArrays.MedianOfTwoSortedArraysBinarySearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MedianOfTwoSortedArrays\MedianOfTwoSortedArraysBinarySearch.cs
Line coverage
92%
Covered lines: 48
Uncovered lines: 4
Coverable lines: 52
Total lines: 96
Line coverage: 92.3%
Branch coverage
90%
Covered branches: 20
Total branches: 22
Branch coverage: 90.9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FindMedianSortedArrays(...)90.9%222292.3%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MedianOfTwoSortedArrays\MedianOfTwoSortedArraysBinarySearch.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 MedianOfTwoSortedArraysBinarySearch : IMedianOfTwoSortedArrays
 16{
 17    /// <summary>
 18    ///     Time complexity - O(log min(n,m))
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="nums1"></param>
 22    /// <param name="nums2"></param>
 23    /// <returns></returns>
 24    public double FindMedianSortedArrays(int[] nums1, int[] nums2)
 1625    {
 2226        while (true)
 2227        {
 2228            if (nums1.Length > nums2.Length)
 629            {
 630                (nums1, nums2) = (nums2, nums1);
 31
 632                continue;
 33            }
 34
 1635            var low = 0;
 1636            var high = nums1.Length;
 1637            var halfLen = (nums1.Length + nums2.Length + 1) / 2;
 38
 2639            while (low <= high)
 2640            {
 2641                var i = (low + high) / 2;
 2642                var j = halfLen - i;
 43
 2644                if (i < nums1.Length && nums2[j - 1] > nums1[i])
 945                {
 946                    low = i + 1;
 947                }
 1748                else if (i > 0 && nums1[i - 1] > nums2[j])
 149                {
 150                    high = i - 1;
 151                }
 52                else
 1653                {
 54                    int maxLeft;
 55
 1656                    if (i == 0)
 357                    {
 358                        maxLeft = nums2[j - 1];
 359                    }
 1360                    else if (j == 0)
 161                    {
 162                        maxLeft = nums1[i - 1];
 163                    }
 64                    else
 1265                    {
 1266                        maxLeft = Math.Max(nums1[i - 1], nums2[j - 1]);
 1267                    }
 68
 1669                    if ((nums1.Length + nums2.Length) % 2 == 1)
 970                    {
 971                        return maxLeft;
 72                    }
 73
 74                    int minRight;
 75
 776                    if (i == nums1.Length)
 377                    {
 378                        minRight = nums2[j];
 379                    }
 480                    else if (j == nums2.Length)
 081                    {
 082                        minRight = nums1[i];
 083                    }
 84                    else
 485                    {
 486                        minRight = Math.Min(nums1[i], nums2[j]);
 487                    }
 88
 789                    return (maxLeft + minRight) / 2.0;
 90                }
 1091            }
 92
 093            return 0;
 94        }
 1695    }
 96}