Given n queries of the form range [L, R]. The task is to find the maximum difference between two prime numbers in the range for each query. If there are no prime in the range then print 0. All ranges are below 100005.
Input : Q = 3 query1 = [2, 5] query2 = [2, 2] query3 = [24, 28] Output : 3 0 0 In first query, 2 and 5 are prime number in the range with maximum difference which is 3. In second and third queries, there is only 1 prime number in range, so output is 0.
The idea is to compute Prime numbers using Sieve of Eratosthenes along with some precomputing.
Below are the step to solve the question:
Step 1: Find the prime numbers using Sieve of Eratosthenes algorithm.
Step 2: Make an array, let say prefix, where prefix[i] represents largest prime number smaller or equal to i.
Step 3: Make an array, let say suffix, where suffix[i] represents smallest prime number greater or equal to i.
Step 4: Now for each query having [L, R], do the following:
if (prefix[R] R) return 0; else return prefix[R] - suffix[L];
Below is the implementation of this approach:
# Python 3 program to find maximum
# differences between two prime numbers
# in given ranges
from math import sqrt
MAX = 100005
# Precompute Sieve, Prefix array, Suffix array
def precompute(prefix, suffix):
prime = [True for i in range(MAX)]
# Sieve of Eratosthenes
k = int(sqrt(MAX))
for i in range(2, k, 1):
for j in range(i + i, MAX, i):
prime[j] = False
prefix = 1
suffix[MAX – 1] = int(1e9 + 7)
# Precomputing Prefix array.
for i in range(2, MAX, 1):
prefix[i] = i
prefix[i] = prefix[i – 1]
# Precompute Suffix array.
i = MAX – 2
while(i > 1):
suffix[i] = i
suffix[i] = suffix[i + 1]
i -= 1
# Function to solve each query
def query(prefix, suffix, L, R):
if (prefix[R] < L or suffix[L] > R):
return prefix[R] – suffix[L]
# Driver Code
if __name__ == ‘__main__’:
q = 3
L = [2, 2, 24]
R = [5, 2, 28]
prefix = [ 0 for i in range(MAX)]
suffix = [ 0 for i in range(MAX)]
for i in range(0, q, 1):
# This code is contributed by
3 0 0
- Queries for the difference between the count of composite and prime numbers in a given range
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Pair of prime numbers with a given sum and minimum absolute difference
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Minimum and Maximum prime numbers in an array
- Maximum no. of contiguous Prime Numbers in an array
- Minimum and Maximum Prime Numbers of a Singly Linked List
- Program to find the maximum difference between the index of any two different numbers
- P - smooth numbers in given ranges
- Choose points from two ranges such that no point lies in both the ranges
- Represent a number as a sum of maximum possible number of Prime Numbers
- Prime Factorization using Sieve O(log n) for multiple queries
- Queries on the sum of prime factor counts in a range
- Queries to count the number of unordered co-prime pairs from 1 to N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.