| | | 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 | | |
| | | 12 | | namespace LeetCode.Algorithms.MinimumAbsoluteDifference; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class MinimumAbsoluteDifferenceSorting : IMinimumAbsoluteDifference |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n log n) |
| | | 19 | | /// Space complexity - O(log n) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="arr"></param> |
| | | 22 | | /// <returns></returns> |
| | | 23 | | public IList<IList<int>> MinimumAbsDifference(int[] arr) |
| | 3 | 24 | | { |
| | 3 | 25 | | Array.Sort(arr); |
| | | 26 | | |
| | 3 | 27 | | var result = new List<IList<int>>(arr.Length); |
| | | 28 | | |
| | 3 | 29 | | var minDifference = int.MaxValue; |
| | | 30 | | |
| | 34 | 31 | | for (var i = 0; i < arr.Length - 1; i++) |
| | 14 | 32 | | { |
| | 14 | 33 | | var difference = arr[i + 1] - arr[i]; |
| | | 34 | | |
| | 14 | 35 | | if (difference < minDifference) |
| | 3 | 36 | | { |
| | 3 | 37 | | minDifference = difference; |
| | | 38 | | |
| | 3 | 39 | | result.Clear(); |
| | 3 | 40 | | } |
| | | 41 | | |
| | 14 | 42 | | if (difference == minDifference) |
| | 7 | 43 | | { |
| | 7 | 44 | | result.Add(new[] |
| | 7 | 45 | | { |
| | 7 | 46 | | arr[i], |
| | 7 | 47 | | arr[i + 1] |
| | 7 | 48 | | }); |
| | 7 | 49 | | } |
| | 14 | 50 | | } |
| | | 51 | | |
| | 3 | 52 | | return result; |
| | 3 | 53 | | } |
| | | 54 | | } |