| | | 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.LargestPositiveIntegerThatExistsWithItsNegative; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class LargestPositiveIntegerThatExistsWithItsNegativeSortingTwoPointers : |
| | | 16 | | ILargestPositiveIntegerThatExistsWithItsNegative |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Time complexity - O(n log n) |
| | | 20 | | /// Space complexity - O(log n) |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="nums"></param> |
| | | 23 | | /// <returns></returns> |
| | | 24 | | public int FindMaxK(int[] nums) |
| | 4 | 25 | | { |
| | 4 | 26 | | Array.Sort(nums); |
| | | 27 | | |
| | 4 | 28 | | var left = 0; |
| | 4 | 29 | | var right = nums.Length - 1; |
| | | 30 | | |
| | 10 | 31 | | while (left < right) |
| | 8 | 32 | | { |
| | 8 | 33 | | var absLeft = Math.Abs(nums[left]); |
| | | 34 | | |
| | 8 | 35 | | if (absLeft == nums[right]) |
| | 2 | 36 | | { |
| | 2 | 37 | | return nums[right]; |
| | | 38 | | } |
| | | 39 | | |
| | 6 | 40 | | if (absLeft > nums[right]) |
| | 2 | 41 | | { |
| | 2 | 42 | | left++; |
| | 2 | 43 | | } |
| | | 44 | | else |
| | 4 | 45 | | { |
| | 4 | 46 | | right--; |
| | 4 | 47 | | } |
| | 6 | 48 | | } |
| | | 49 | | |
| | 2 | 50 | | return -1; |
| | 4 | 51 | | } |
| | | 52 | | } |