| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 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.ClosestPrimeNumbersInRange; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class ClosestPrimeNumbersInRangeSieveOfEratosthenes : IClosestPrimeNumbersInRange |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(R * log(log R)) |
| | 19 | | /// Space complexity - O(R) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="left"></param> |
| | 22 | | /// <param name="right"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int[] ClosestPrimes(int left, int right) |
| 2 | 25 | | { |
| 2 | 26 | | var isPrime = SieveOfEratosthenes(right); |
| 2 | 27 | | var primes = new List<int>(); |
| | 28 | |
|
| 30 | 29 | | for (var num = left; num <= right; num++) |
| 13 | 30 | | { |
| 13 | 31 | | if (isPrime[num]) |
| 5 | 32 | | { |
| 5 | 33 | | primes.Add(num); |
| 5 | 34 | | } |
| 13 | 35 | | } |
| | 36 | |
|
| 2 | 37 | | if (primes.Count < 2) |
| 1 | 38 | | { |
| 1 | 39 | | return [-1, -1]; |
| | 40 | | } |
| | 41 | |
|
| 1 | 42 | | var minDiff = int.MaxValue; |
| 1 | 43 | | var minNum1 = -1; |
| 1 | 44 | | var minNum2 = -1; |
| | 45 | |
|
| 2 | 46 | | for (var i = 1; i < primes.Count; i++) |
| 1 | 47 | | { |
| 1 | 48 | | var diff = primes[i] - primes[i - 1]; |
| | 49 | |
|
| 1 | 50 | | if (diff >= minDiff) |
| 0 | 51 | | { |
| 0 | 52 | | continue; |
| | 53 | | } |
| | 54 | |
|
| 1 | 55 | | minDiff = diff; |
| 1 | 56 | | minNum1 = primes[i - 1]; |
| 1 | 57 | | minNum2 = primes[i]; |
| | 58 | |
|
| 1 | 59 | | if (minNum2 - minNum1 == 2) |
| 1 | 60 | | { |
| 1 | 61 | | break; |
| | 62 | | } |
| 0 | 63 | | } |
| | 64 | |
|
| 1 | 65 | | return [minNum1, minNum2]; |
| 2 | 66 | | } |
| | 67 | |
|
| | 68 | | private static bool[] SieveOfEratosthenes(int n) |
| 2 | 69 | | { |
| 2 | 70 | | var isPrime = new bool[n + 1]; |
| | 71 | |
|
| 2 | 72 | | Array.Fill(isPrime, true); |
| | 73 | |
|
| 2 | 74 | | isPrime[0] = isPrime[1] = false; |
| | 75 | |
|
| 12 | 76 | | for (var i = 2; i * i <= n; i++) |
| 4 | 77 | | { |
| 4 | 78 | | if (!isPrime[i]) |
| 1 | 79 | | { |
| 1 | 80 | | continue; |
| | 81 | | } |
| | 82 | |
|
| 34 | 83 | | for (var j = i * i; j <= n; j += i) |
| 14 | 84 | | { |
| 14 | 85 | | isPrime[j] = false; |
| 14 | 86 | | } |
| 3 | 87 | | } |
| | 88 | |
|
| 2 | 89 | | return isPrime; |
| 2 | 90 | | } |
| | 91 | | } |