| | | 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.ReplaceNonCoprimeNumbersInArray; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class ReplaceNonCoprimeNumbersInArrayGreedy : IReplaceNonCoprimeNumbersInArray |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n log M), where n is the length of nums and M is max(nums[i]) |
| | | 19 | | /// Space complexity - O(1) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="nums"></param> |
| | | 22 | | /// <returns></returns> |
| | | 23 | | public IList<int> ReplaceNonCoprimes(int[] nums) |
| | 2 | 24 | | { |
| | 2 | 25 | | var top = 0; |
| | | 26 | | |
| | 28 | 27 | | for (var i = 1; i < nums.Length; i++) |
| | 12 | 28 | | { |
| | 12 | 29 | | var x = nums[i]; |
| | | 30 | | |
| | 19 | 31 | | while (top >= 0) |
| | 15 | 32 | | { |
| | 15 | 33 | | var y = nums[top]; |
| | | 34 | | |
| | 15 | 35 | | var greatestCommonDivisor = GetGreatestCommonDivisor(x, y); |
| | | 36 | | |
| | 15 | 37 | | if (!IsNonComprime(greatestCommonDivisor)) |
| | 8 | 38 | | { |
| | 8 | 39 | | break; |
| | | 40 | | } |
| | | 41 | | |
| | 7 | 42 | | x = GetLeastCommonMultiple(x, y, greatestCommonDivisor); |
| | | 43 | | |
| | 7 | 44 | | top--; |
| | 7 | 45 | | } |
| | | 46 | | |
| | 12 | 47 | | top++; |
| | | 48 | | |
| | 12 | 49 | | nums[top] = x; |
| | 12 | 50 | | } |
| | | 51 | | |
| | 2 | 52 | | var resultLength = top + 1; |
| | | 53 | | |
| | 2 | 54 | | var result = new List<int>(resultLength); |
| | | 55 | | |
| | 18 | 56 | | for (var i = 0; i < resultLength; i++) |
| | 7 | 57 | | { |
| | 7 | 58 | | result.Add(nums[i]); |
| | 7 | 59 | | } |
| | | 60 | | |
| | 2 | 61 | | return result; |
| | 2 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Time complexity - O(1) |
| | | 66 | | /// Space complexity - O(1) |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="greatestCommonDivisor"></param> |
| | | 69 | | /// <returns></returns> |
| | | 70 | | private static bool IsNonComprime(int greatestCommonDivisor) |
| | 15 | 71 | | { |
| | 15 | 72 | | return greatestCommonDivisor > 1; |
| | 15 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Time complexity - O(log(min(x, y))) |
| | | 77 | | /// Space complexity - O(1) |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="x"></param> |
| | | 80 | | /// <param name="y"></param> |
| | | 81 | | /// <returns></returns> |
| | | 82 | | private static int GetGreatestCommonDivisor(int x, int y) |
| | 15 | 83 | | { |
| | 44 | 84 | | while (y != 0) |
| | 29 | 85 | | { |
| | 29 | 86 | | var temp = y; |
| | | 87 | | |
| | 29 | 88 | | y = x % y; |
| | | 89 | | |
| | 29 | 90 | | x = temp; |
| | 29 | 91 | | } |
| | | 92 | | |
| | 15 | 93 | | return int.Abs(x); |
| | 15 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Time complexity - O(1) |
| | | 98 | | /// Space complexity - O(1) |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="x"></param> |
| | | 101 | | /// <param name="y"></param> |
| | | 102 | | /// <param name="greatestCommonDivisor"></param> |
| | | 103 | | /// <returns></returns> |
| | | 104 | | private static int GetLeastCommonMultiple(int x, int y, int greatestCommonDivisor) |
| | 7 | 105 | | { |
| | 7 | 106 | | return int.Abs(x / greatestCommonDivisor * y); |
| | 7 | 107 | | } |
| | | 108 | | } |