Longest sub-array of Prime Numbers using Segmented Sieve
Last Updated :
12 Jul, 2025
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;
// Compute 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;
}
Java
// Java program to find longest subarray
// of prime numbers
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
// To store the prime numbers
static Set<Integer> allPrimes = new HashSet<>();
// Function that find prime numbers
// till limit
static void simpleSieve(int limit,
ArrayList<Integer> prime)
{
boolean []mark = new boolean[limit + 1];
// Find primes using
// Sieve of Eratosthenes
for(int i = 2; i <= limit; ++i)
{
if (mark[i] == false)
{
prime.add(i);
for(int j = i; j <= limit; j += i)
{
mark[j] = true;
}
}
}
}
// Function that finds all prime
// numbers in given range using
// Segmented Sieve
static void primesInRange(int low, int high)
{
// Find the limit
int limit = (int)Math.floor(Math.sqrt(high)) + 1;
// To store the prime numbers
ArrayList<Integer> prime = new ArrayList<>();
// 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]
boolean []mark = new boolean[n + 1];
// Traverse the prime numbers till
// limit
for(int i = 0; i < prime.size(); i++)
{
int loLim = (int)Math.floor((double)low /
(int)prime.get(i));
loLim *= (int)prime.get(i);
// Find the minimum number in
// [low..high] that is a
// multiple of prime[i]
if (loLim < low)
{
loLim += (int)prime.get(i);
}
if (loLim == (int)prime.get(i))
{
loLim += (int)prime.get(i);
}
// Mark the multiples of prime[i]
// in [low, high] as true
for(int j = loLim; j <= high;
j += (int)prime.get(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.add(i);
}
}
}
// Function that finds longest subarray
// of prime numbers
static 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.contains(arr[i]))
current_max = 0;
// If element is prime, then
// update current_max and
// max_so_far
else
{
current_max++;
max_so_far = Math.max(current_max,
max_so_far);
}
}
// Return the count of longest
// subarray
return max_so_far;
}
// Driver code
public static void main(String[] args)
{
int []arr = { 1, 2, 4, 3, 29,
11, 7, 8, 9 };
int n = arr.length;
// Find minimum and maximum element
int max_el = Integer.MIN_VALUE;
int min_el = Integer.MAX_VALUE;
for(int i = 0; i < n; i++)
{
if (arr[i] < min_el)
{
min_el = arr[i];
}
if (arr[i] > max_el)
{
max_el = arr[i];
}
}
// Find prime in the range
// [min_el, max_el]
primesInRange(min_el, max_el);
// Function call
System.out.print(maxPrimeSubarray(arr, n));
}
}
// This code is contributed by offbeat
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
C#
// C# program to find longest subarray
// of prime numbers
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
// To store the prime numbers
static HashSet<int> allPrimes = new HashSet<int>();
// Function that find prime numbers
// till limit
static void simpleSieve(int limit,
ref ArrayList prime)
{
bool []mark = new bool[limit + 1];
Array.Fill(mark, false);
// Find primes using
// Sieve of Eratosthenes
for(int i = 2; i <= limit; ++i)
{
if (mark[i] == false)
{
prime.Add(i);
for(int j = i; j <= limit; j += i)
{
mark[j] = true;
}
}
}
}
// Function that finds all prime
// numbers in given range using
// Segmented Sieve
static void primesInRange(int low, int high)
{
// Find the limit
int limit = (int)Math.Floor(Math.Sqrt(high)) + 1;
// To store the prime numbers
ArrayList prime = new ArrayList();
// Comput all primes less than
// or equals to sqrt(high)
// using Simple Sieve
simpleSieve(limit, ref prime);
// Count the elements in the
// range [low, high]
int n = high - low + 1;
// Declaring boolean for the
// range [low, high]
bool []mark = new bool[n + 1];
// Initialise bool[] to false
Array.Fill(mark, false);
// Traverse the prime numbers till
// limit
for(int i = 0; i < prime.Count; i++)
{
int loLim = (int)Math.Floor((double)low /
(int)prime[i]);
loLim *= (int)prime[i];
// Find the minimum number in
// [low..high] that is a
// multiple of prime[i]
if (loLim < low)
{
loLim += (int)prime[i];
}
if (loLim == (int)prime[i])
{
loLim += (int)prime[i];
}
// Mark the multiples of prime[i]
// in [low, high] as true
for(int j = loLim; j <= high;
j += (int)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.Add(i);
}
}
}
// Function that finds longest subarray
// of prime numbers
static 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.Contains(arr[i]))
current_max = 0;
// If element is prime, then
// update current_max and
// max_so_far
else
{
current_max++;
max_so_far = Math.Max(current_max,
max_so_far);
}
}
// Return the count of longest
// subarray
return max_so_far;
}
// Driver code
public static void Main(string[] args)
{
int []arr = { 1, 2, 4, 3, 29,
11, 7, 8, 9 };
int n = arr.Length;
// Find minimum and maximum element
int max_el = Int32.MinValue;
int min_el = Int32.MaxValue;
for(int i = 0; i < n; i++)
{
if (arr[i] < min_el)
{
min_el = arr[i];
}
if (arr[i] > max_el)
{
max_el = arr[i];
}
}
// Find prime in the range
// [min_el, max_el]
primesInRange(min_el, max_el);
// Function call
Console.Write(maxPrimeSubarray(arr, n));
}
}
// This code is contributed by rutvik_56
JavaScript
<script>
// Javascript program to find longest subarray
// of prime numbers
// To store the prime numbers
let allPrimes = new Set();
// Function that find prime numbers
// till limit
function simpleSieve(limit, prime)
{
let mark = Array.from({length: limit + 1}, (_, i) => 0);
// Find primes using
// Sieve of Eratosthenes
for(let i = 2; i <= limit; ++i)
{
if (mark[i] == false)
{
prime.push(i);
for(let j = i; j <= limit; j += i)
{
mark[j] = true;
}
}
}
}
// Function that finds all prime
// numbers in given range using
// Segmented Sieve
function primesInRange(low, high)
{
// Find the limit
let limit = Math.floor(Math.sqrt(high)) + 1;
// To store the prime numbers
let 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]
let n = high - low + 1;
// Declaring boolean for the
// range [low, high]
let mark = Array.from({length: n + 1}, (_, i) => 0);
// Traverse the prime numbers till
// limit
for(let i = 0; i < prime.length; i++)
{
let loLim = Math.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(let j = loLim; j <= high;
j += prime[i])
mark[j - low] = true;
}
// Element which are not marked in
// range are Prime
for(let i = low; i <= high; i++)
{
if (!mark[i - low])
{
allPrimes.add(i);
}
}
}
// Function that finds longest subarray
// of prime numbers
function maxPrimeSubarray(arr, n)
{
let current_max = 0;
let max_so_far = 0;
for(let i = 0; i < n; i++)
{
// If element is Non-prime then
// updated current_max to 0
if (!allPrimes.has(arr[i]))
current_max = 0;
// If element is prime, then
// update current_max and
// max_so_far
else
{
current_max++;
max_so_far = Math.max(current_max,
max_so_far);
}
}
// Return the count of longest
// subarray
return max_so_far;
}
// Driver code
let arr = [ 1, 2, 4, 3, 29,
11, 7, 8, 9 ];
let n = arr.length;
// Find minimum and maximum element
let max_el = Number.MIN_VALUE;
let min_el = Number.MAX_VALUE;
for(let i = 0; i < n; i++)
{
if (arr[i] < min_el)
{
min_el = arr[i];
}
if (arr[i] > max_el)
{
max_el = arr[i];
}
}
// Find prime in the range
// [min_el, max_el]
primesInRange(min_el, max_el);
// Function call
document.write(maxPrimeSubarray(arr, n));
</script>
Time Complexity: O(N), where N is the length of the array.
Auxiliary Space: O(N), where N is the length of the array.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem