< Summary

Information
Class: LeetCode.Algorithms.MinimizeTheMaximumDifferenceOfPairs.MinimizeTheMaximumDifferenceOfPairsBinarySearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimizeTheMaximumDifferenceOfPairs\MinimizeTheMaximumDifferenceOfPairsBinarySearch.cs
Line coverage
94%
Covered lines: 34
Uncovered lines: 2
Coverable lines: 36
Total lines: 78
Line coverage: 94.4%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinimizeMax(...)83.33%6690%
CanFormEnoughPairs(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimizeTheMaximumDifferenceOfPairs\MinimizeTheMaximumDifferenceOfPairsBinarySearch.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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.MinimizeTheMaximumDifferenceOfPairs;
 13
 14/// <inheritdoc />
 15public sealed class MinimizeTheMaximumDifferenceOfPairsBinarySearch : IMinimizeTheMaximumDifferenceOfPairs
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n log n + n log R), where R = max(nums) - min(nums)
 19    ///     Space complexity - O(log n)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <param name="requiredPairsCount"></param>
 23    /// <returns></returns>
 24    public int MinimizeMax(int[] nums, int requiredPairsCount)
 225    {
 226        if (requiredPairsCount == 0)
 027        {
 028            return 0;
 29        }
 30
 231        Array.Sort(nums);
 32
 233        var low = 0;
 234        var high = nums[^1] - nums[0];
 35
 836        while (low < high)
 637        {
 638            var mid = low + ((high - low) / 2);
 39
 640            if (CanFormEnoughPairs(nums, mid, requiredPairsCount))
 541            {
 542                high = mid;
 543            }
 44            else
 145            {
 146                low = mid + 1;
 147            }
 648        }
 49
 250        return low;
 251    }
 52
 53    private static bool CanFormEnoughPairs(int[] nums, int x, int requiredPairsCount)
 654    {
 655        var numsLength = nums.Length;
 56
 657        var pairsCount = 0;
 58
 2859        for (var i = 0; i < numsLength - 1; i++)
 1360        {
 1361            if (nums[i + 1] - nums[i] > x)
 462            {
 463                continue;
 64            }
 65
 966            pairsCount++;
 67
 968            if (pairsCount == requiredPairsCount)
 569            {
 570                return true;
 71            }
 72
 473            i++;
 474        }
 75
 176        return false;
 677    }
 78}