Prime Subtraction Operation - Sieve of Eratosthenes & Binary Search [JS]

Description 

Solution: Sieve of Eratosthenes & Binary Search

Use the Sieve of Eratosthenes to find all the prime numbers up to the maximum number.
Greedily subtract the biggest possible prime number <= nums[i] where nums[i] - prime > nums[i - 1]
Use binary search to find this prime.

Time Complexity: O(n log(n)) 96ms
Space Complexity: O(n) 48.8MB

var primeSubOperation = function(nums) {
  let primes = getPrimes(Math.max(...nums));
  for (let i = 0; i < nums.length; i++) {
    let low = 0, high = primes.length - 1;
    while (low < high) {
      let mid = Math.ceil((low + high) / 2);
      let isValid = i === 0 ? primes[mid] < nums[i] : primes[mid] < nums[i] && nums[i] - primes[mid] > nums[i - 1];
      if (isValid) {
        low = mid;
      } else {
        high = mid - 1;
      }
    }
    let isValid = i === 0 ? primes[low] < nums[i] : primes[low] < nums[i] && nums[i] - primes[low] > nums[i - 1];
    let toSubtract = isValid ? primes[low] : 0;
    nums[i] -= toSubtract;
    if (i > 0 && nums[i] <= nums[i - 1]) return false;
  }
  return true;
};

function getPrimes(n) {
  let isPrime = Array(n).fill(1); 
  for (let i = 2; i <= n; i++) {
    if (!isPrime[i]) continue;
    for (let j = i * i; j <= n; j += i) {
      isPrime[j] = 0;
    }
  }
  let primes = [];
  for (let i = 2; i <= n; i++) {
    if (isPrime[i]) primes.push(i);
  }
  return primes;
}

Comments

Popular posts from this blog

Maximum Value of an Ordered Triplet II - Two Solutions [JS]

Maximum Sum of Distinct Subarrays With Length K - Sliding Window w/ Two Pointers & Set [JS]

Sum of Prefix Scores of Strings - Trie [JS]

Maximum Count of Positive Integer and Negative Integer - Binary Search [JS]

Count Subarrays With Median K - Count Left & Right Balance [JS]