| | | 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.ArithmeticSubarrays; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class ArithmeticSubarraysSort : IArithmeticSubarrays |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(m * n log n) |
| | | 19 | | /// Space complexity - O(n) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="nums"></param> |
| | | 22 | | /// <param name="l"></param> |
| | | 23 | | /// <param name="r"></param> |
| | | 24 | | /// <returns></returns> |
| | | 25 | | public IList<bool> CheckArithmeticSubarrays(int[] nums, int[] l, int[] r) |
| | 6 | 26 | | { |
| | 6 | 27 | | var result = new bool[l.Length]; |
| | | 28 | | |
| | 48 | 29 | | for (var i = 0; i < l.Length; i++) |
| | 18 | 30 | | { |
| | 18 | 31 | | var subArrayLength = r[i] - l[i] + 1; |
| | | 32 | | |
| | 18 | 33 | | var subArray = new int[subArrayLength]; |
| | | 34 | | |
| | 156 | 35 | | for (var j = 0; j < subArrayLength; j++) |
| | 60 | 36 | | { |
| | 60 | 37 | | subArray[j] = nums[l[i] + j]; |
| | 60 | 38 | | } |
| | | 39 | | |
| | 18 | 40 | | Array.Sort(subArray); |
| | | 41 | | |
| | 18 | 42 | | var diff = subArray[1] - subArray[0]; |
| | | 43 | | |
| | 18 | 44 | | var currentResult = true; |
| | | 45 | | |
| | 84 | 46 | | for (var j = 2; j < subArray.Length; j++) |
| | 24 | 47 | | { |
| | 24 | 48 | | currentResult &= subArray[j] - subArray[j - 1] == diff; |
| | 24 | 49 | | } |
| | | 50 | | |
| | 18 | 51 | | result[i] = currentResult; |
| | 18 | 52 | | } |
| | | 53 | | |
| | 6 | 54 | | return result; |
| | 6 | 55 | | } |
| | | 56 | | } |