Sieve of Eratosthenes to solve a Leetcode problem II
Many years ago I used the Sieve of Eratosthenes to solve a LC problem. Here is another one. The Sieve allows us to calculate all primes between 1..N in O(N)-time, and O(N)-space. Calculate all non-primes in the very first beginning, then just proceed to execute the calculation asked. Code is down below, Happy New Year, ACC. (1) Closest Prime Numbers in Range - LeetCode 2523. Closest Prime Numbers in Range Medium 41 8 Add to List Share Given two positive integers left and right , find the two integers num1 and num2 such that: left <= nums1 < nums2 <= right . nums1 and nums2 are both prime numbers. nums2 - nums1 is the minimum amongst all other pairs satisfying the above conditions. Return the positive integer array ans = [nums1, nums2] . If there are multiple pairs satisfying these conditions, return the one with the minimum nums1 value or ...