Given an array arr[] of N integers, the task is to find the longest subarray where all numbers in that subarray are prime.
Examples:
Input: arr[] = {3, 5, 2, 66, 7, 11, 8}
Output: 3
Explanation:
Maximum contiguous prime number sequence is {2, 3, 5}Input: arr[] = {1, 2, 11, 32, 8, 9}
Output: 2
Explanation:
Maximum contiguous prime number sequence is {2, 11}
Approach:
For elements of the array in order of 106, we have discussed an approach in this article.
For elements of the array in order of 109, the idea is to use Segmented Sieve to find the Prime Numbers to value upto 109.
Steps:
- Find the Prime Numbers between the range of minimum element and maximum element of the array using the approach discussed in this article.
- After calculating the Prime Numbers between the range. The longest subarray with Prime Numbers can be calculated using Kadane’s Algorithm. Following are the steps:
- Traverse the given array arr[] with two variables named current_max and max_so_far.
- If a Prime Number is found then increment current_max and compare it with max_so_far.
- If current_max is greater than max_so_far, then update max_so_far to current_max.
- If any element is non-prime element, then reset current_max to 0.
Below is the implementation of the above approach:
C++
// C++ program to find longest subarray // of prime numbers #include <bits/stdc++.h> using namespace std; // To store the prime numbers unordered_set<int> allPrimes; // Function that find prime numbers // till limit void simpleSieve(int limit, vector<int>& prime) { bool mark[limit + 1]; memset(mark, false, sizeof(mark)); // Find primes using // Sieve of Eratosthenes for (int i = 2; i <= limit; ++i) { if (mark[i] == false) { prime.push_back(i); for (int j = i; j <= limit; j += i) { mark[j] = true; } } } } // Function that finds all prime // numbers in given range using // Segmented Sieve void primesInRange(int low, int high) { // Find the limit int limit = floor(sqrt(high)) + 1; // To store the prime numbers vector<int> prime; // Comput all primes less than // or equals to sqrt(high) // using Simple Sieve simpleSieve(limit, prime); // Count the elements in the // range [low, high] int n = high - low + 1; // Declaring boolean for the // range [low, high] bool mark[n + 1]; // Initialise bool[] to false memset(mark, false, sizeof(mark)); // Traverse the prime numbers till // limit for (int i = 0; i < prime.size(); i++) { int loLim = floor(low / prime[i]); loLim *= prime[i]; // Find the minimum number in // [low..high] that is a // multiple of prime[i] if (loLim < low) { loLim += prime[i]; } if (loLim == prime[i]) { loLim += prime[i]; } // Mark the multiples of prime[i] // in [low, high] as true for (int j = loLim; j <= high; j += prime[i]) mark[j - low] = true; } // Element which are not marked in // range are Prime for (int i = low; i <= high; i++) { if (!mark[i - low]) { allPrimes.insert(i); } } } // Function that finds longest subarray // of prime numbers int maxPrimeSubarray(int arr[], int n) { int current_max = 0; int max_so_far = 0; for (int i = 0; i < n; i++) { // If element is Non-prime then // updated current_max to 0 if (!allPrimes.count(arr[i])) current_max = 0; // If element is prime, then // update current_max and // max_so_far else { current_max++; max_so_far = max(current_max, max_so_far); } } // Return the count of longest // subarray return max_so_far; } // Driver Code int main() { int arr[] = { 1, 2, 4, 3, 29, 11, 7, 8, 9 }; int n = sizeof(arr) / sizeof(arr[0]); // Find minimum and maximum element int max_el = *max_element(arr, arr + n); int min_el = *min_element(arr, arr + n); // Find prime in the range // [min_el, max_el] primesInRange(min_el, max_el); // Function call cout << maxPrimeSubarray(arr, n); return 0; } |
Python3
# Python3 program to find longest subarray # of prime numbers import math # To store the prime numbers allPrimes = set() # Function that find prime numbers # till limit def simpleSieve(limit, prime): mark = [False] * (limit + 1) # Find primes using # Sieve of Eratosthenes for i in range(2, limit + 1): if mark[i] == False: prime.append(i) for j in range(i, limit + 1, i): mark[j] = True # Function that finds all prime # numbers in given range using # Segmented Sieve def primesInRange(low, high): # Find the limit limit = math.floor(math.sqrt(high)) + 1 # To store the prime numbers prime = [] # Compute all primes less than # or equals to sqrt(high) # using Simple Sieve simpleSieve(limit, prime) # Count the elements in the # range [low, high] n = high - low + 1 # Declaring and initializing boolean # for the range[low,high] to False mark = [False] * (n + 1) # Traverse the prime numbers till # limit for i in range(len(prime)): loLim = low // prime[i] loLim *= prime[i] # Find the minimum number in # [low..high] that is a # multiple of prime[i] if loLim < low: loLim += prime[i] if loLim == prime[i]: loLim += prime[i] # Mark the multiples of prime[i] # in [low, high] as true for j in range(loLim, high + 1, prime[i]): mark[j - low] = True # Element which are not marked in # range are Prime for i in range(low, high + 1): if not mark[i - low]: allPrimes.add(i) # Function that finds longest subarray # of prime numbers def maxPrimeSubarray(arr, n): current_max = 0 max_so_far = 0 for i in range(n): # If element is Non-prime then # updated current_max to 0 if arr[i] not in allPrimes: current_max = 0 # If element is prime, then # update current_max and # max_so_far else: current_max += 1 max_so_far = max(current_max, max_so_far) # Return the count of longest # subarray return max_so_far # Driver Code arr = [ 1, 2, 4, 3, 29, 11, 7, 8, 9 ] n = len(arr) # Find minimum and maximum element max_el = max(arr) min_el = min(arr) # Find prime in the range # [min_el, max_el] primesInRange(min_el, max_el) # Function call print(maxPrimeSubarray(arr, n)) # This code is contributed by Shivam Singh |
4
Time Complexity: O(N), where N is the length of the array.
Auxiliary Space: O(N), where N is the length of the array.
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.
Recommended Posts:
- Length of longest subarray having only K distinct Prime Numbers
- Segmented Sieve
- Segmented Sieve (Print Primes in a Range)
- Longest Prime Subarray after removing one element
- Prime Factorization using Sieve O(log n) for multiple queries
- 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
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Print the nearest prime number formed by adding prime numbers to N
- Longest subarray having sum K | Set 2
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Print prime numbers with prime sum of digits in an array
- Count all prime numbers in a given range whose sum of digits is also prime
- Check if a prime number can be expressed as sum of two Prime Numbers
- Longest subarray with odd product
- Longest Mountain Subarray
- Longest increasing subarray
- Longest subarray with all elements same
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

