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:
3 0 0
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Choose points from two ranges such that no point lies in both the ranges
- Queries to minimize sum added to given ranges in an array to make their Bitwise AND non-zero
- Queries for the difference between the count of composite and prime numbers in a given range
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- P - smooth numbers in given ranges
- Sum of prime numbers in range [L, R] from given Array for Q queries
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Minimum change in given value so that it lies in all given Ranges
- Number of intersections between two ranges
- Count all prime numbers in a given range whose sum of digits is also prime
- Count numbers in a given range whose count of prime factors is a Prime Number
- Find if it is possible to get a ratio from given ranges of costs and quantities
- Count distinct median possible for an Array using given ranges of elements
- Sum of all possible triplet products from given ranges
- Queries for the smallest and the largest prime number of given digit
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Prime numbers after prime P with sum S
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.