Posts

Showing posts from March, 2025

🔍 Finding the Closest Prime Numbers in a Range using the Sieve of Eratosthenes (leetcode - 2523)

🔍 Finding the Closest Prime Numbers in a Range using the Sieve of Eratosthenes Prime numbers play a crucial role in number theory and various computational applications. In this blog, we will explore an optimized approach to finding the two closest prime numbers within a given range using the Sieve of Eratosthenes . 🚀 📌 Problem Statement Given two integers left and right , find the two prime numbers within the range [left, right] that have the smallest difference. If there are fewer than two prime numbers, return [-1, -1] . 🛠️ Approach To efficiently solve this problem, we use the Sieve of Eratosthenes algorithm to precompute prime numbers up to right . Then, we extract the prime numbers from the given range and find the closest pair. ✅ Steps to Solve Initialize a boolean list primes where primes[i] = True indicates i is prime. Mark 0 and 1 as Fa...