< Summary

Information
Class: LeetCode.Algorithms.MinimumAbsoluteDifference.MinimumAbsoluteDifferenceLookup
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumAbsoluteDifference\MinimumAbsoluteDifferenceLookup.cs
Line coverage
100%
Covered lines: 42
Uncovered lines: 0
Coverable lines: 42
Total lines: 82
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinimumAbsDifference(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumAbsoluteDifference\MinimumAbsoluteDifferenceLookup.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.MinimumAbsoluteDifference;
 13
 14/// <inheritdoc />
 15public sealed class MinimumAbsoluteDifferenceLookup : IMinimumAbsoluteDifference
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n + R), where R = maxValue - MinValue + 1
 19    ///     Space complexity - O(R), where R = maxValue - MinValue + 1
 20    /// </summary>
 21    /// <param name="arr"></param>
 22    /// <returns></returns>
 23    public IList<IList<int>> MinimumAbsDifference(int[] arr)
 324    {
 325        var minValue = int.MaxValue;
 326        var maxValue = int.MinValue;
 27
 4028        for (var i = 0; i < arr.Length; i++)
 1729        {
 1730            var num = arr[i];
 31
 1732            minValue = Math.Min(minValue, num);
 1733            maxValue = Math.Max(maxValue, num);
 1734        }
 35
 336        Span<bool> numsLookup = stackalloc bool[maxValue - minValue + 1];
 37
 4038        for (var i = 0; i < arr.Length; i++)
 1739        {
 1740            var num = arr[i];
 41
 1742            numsLookup[num - minValue] = true;
 1743        }
 44
 345        var minDifference = int.MaxValue;
 46
 347        var result = new List<IList<int>>(arr.Length);
 48
 349        var previousValue = minValue;
 50
 12251        for (var i = 1; i < numsLookup.Length; i++)
 5852        {
 5853            if (!numsLookup[i])
 4454            {
 4455                continue;
 56            }
 57
 1458            var currentValue = minValue + i;
 1459            var difference = currentValue - previousValue;
 60
 1461            if (difference < minDifference)
 362            {
 363                minDifference = difference;
 64
 365                result.Clear();
 366            }
 67
 1468            if (difference == minDifference)
 769            {
 770                result.Add(new[]
 771                {
 772                    previousValue,
 773                    currentValue
 774                });
 775            }
 76
 1477            previousValue = currentValue;
 1478        }
 79
 380        return result;
 381    }
 82}