Longest Prime Subarray after removing one element
Given an array A of integers. We can remove at most one index from the array. Our goal is to maximize the length of the subarray that contains all primes. Print the largest length subarray that you can achieve by removing exactly one element from the array .
Examples:
Input : arr[] = { 2, 8, 5, 7, 9, 5, 7 } Output : 4 Explanation :If we remove the number 9 which is at index 5 then the remaining array contains a subarray whose length is 4 which is maximum. Input : arr[] = { 2, 3, 5, 7 } Output : 3 If we remove the number 3 which is at index 1 then the remaining array contains a subarray whose length is 3 which is maximum.
The idea is to count contiguous primes just before every index and just after every index. Now traverse the array again and find an index for which sum counts of primes after and before is maximum.
C++
// CPP program to find length of the longest // subarray with all primes except possibily // one. #include <bits/stdc++.h> using namespace std; #define N 100000 bool prime[N]; void SieveOfEratosthenes() { // Create a boolean array "prime[0..n]" and // initialize all entries it as true. A value // in prime[i] will finally be false if i is // Not a prime, else true. memset (prime, true , sizeof (prime)); for ( int p = 2; p * p <= N; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true ) { // Update all multiples of p for ( int i = p * 2; i <= N; i += p) prime[i] = false ; } } } int longestPrimeSubarray( int arr[], int n) { int left[n], right[n]; int primecount = 0, res = 0; // left array used to count number of // continuous prime numbers starting // from left of current element for ( int i = 0; i < n; i++) { left[i] = primecount; if (prime[arr[i]]) { primecount++; } else primecount = 0; } // right array used to count number of // continuous prime numbers starting from // right of current element primecount = 0; for ( int i = n - 1; i >= 0; i--) { right[i] = primecount; if (prime[arr[i]]) { primecount++; } else primecount = 0; } for ( int i = 0; i < n; i++) res = max(res, left[i] + right[i]); return res; } // Driver code int main() { int arr[] = { 2, 8, 5, 7, 9, 5, 7 }; // used of SieveOfEratosthenes method to // detect a number prime or not SieveOfEratosthenes(); int n = sizeof (arr) / sizeof (arr[0]); cout << "largest length of PrimeSubarray " << longestPrimeSubarray(arr, n) << endl; return 0; } |
Python3
# Python 3 program to find length of the
# longest subarray with all primes except
# possibily one.
from math import sqrt
N = 100000
prime = [True for i in range(N + 1)]
def SieveOfEratosthenes():
# Create a boolean array “prime[0..n]”
# and initialize all entries it as true.
# A value in prime[i] will finally be
# false if i is Not a prime, else true.
k = int(sqrt(N)) + 1
for p in range(2, k, 1):
# If prime[p] is not changed,
# then it is a prime
if (prime[p] == True):
# Update all multiples of p
for i in range(p * 2, N + 1, p):
prime[i] = False
def longestPrimeSubarray(arr, n):
left = [0 for i in range(n)]
right = [0 for i in range(n)]
primecount = 0
res = 0
# left array used to count number of
# continuous prime numbers starting
# from left of current element
for i in range(n):
left[i] = primecount
if (prime[arr[i]]):
primecount += 1
else:
primecount = 0
# right array used to count number of
# continuous prime numbers starting
# from right of current element
primecount = 0
i = n – 1
while(i >= 0):
right[i] = primecount
if (prime[arr[i]]):
primecount += 1
else:
primecount = 0
i -= 1
for i in range(n):
res = max(res, left[i] + right[i])
return res
# Driver code
if __name__ == ‘__main__’:
arr = [2, 8, 5, 7, 9, 5, 7]
# used of SieveOfEratosthenes method
# to detect a number prime or not
SieveOfEratosthenes()
n = len(arr)
print(“largest length of PrimeSubarray”,
longestPrimeSubarray(arr, n))
# This code is contributed by
# Surendra_Gangwar
PHP
<?php // PHP program to find length of // the longest subarray with all at most // primes except possibily one. $N = 100000; $prime = array_fill (0, $N , true); function SieveOfEratosthenes() { // Create a boolean array "prime[0..n]" // and initialize all entries it as // true. A value in prime[i] will // finally be false if i is Not a // prime, else true. global $prime , $N ; for ( $p = 2; $p * $p <= $N ; $p ++) { // If prime[p] is not changed, // then it is a prime if ( $prime [ $p ] == true) { // Update all multiples of p for ( $i = $p * 2; $i <= $N ; $i += $p ) $prime [ $i ] = false; } } } function longestPrimeSubarray( $arr , $n ) { global $prime , $N ; $left = array ( $n ); $right = array ( $n ); $primecount = 0; $res = 0; // left array used to count number of // continuous prime numbers starting // from left of current element for ( $i = 0; $i < $n ; $i ++) { $left [ $i ] = $primecount ; if ( $prime [ $arr [ $i ]]) { $primecount ++; } else $primecount = 0; } // right array used to count number // of continuous prime numbers starting // from right of current element $primecount = 0; for ( $i = $n - 1; $i >= 0; $i --) { $right [ $i ] = $primecount ; if ( $prime [ $arr [ $i ]]) { $primecount ++; } else $primecount = 0; } for ( $i = 0; $i < $n ; $i ++) $res = max( $res , $left [ $i ] + $right [ $i ]); return $res ; } // Driver Code $arr = array (2, 8, 5, 7, 9, 5, 7); // used of SieveOfEratosthenes method // to detect a number prime or not SieveOfEratosthenes(); $n = count ( $arr ); echo "largest length of PrimeSubarray " . longestPrimeSubarray( $arr , $n ); // This code is contributed by mits ?> |
largest length of PrimeSubarray 4
Recommended Posts:
- Maximum sum subarray removing at most one element
- Longest Subarray with first element greater than or equal to Last element
- K-th smallest element after removing given integers from natural numbers | Set 2
- Longest subarray having maximum sum
- Longest subarray with sum divisible by k
- Longest increasing subarray
- Longest subarray such that the difference of max and min is at-most one
- Longest Subarray with Sum greater than Equal to Zero
- Longest subarray in which all elements are greater than K
- Longest subarray with elements divisible by k
- Length of the longest Subarray with only Even Elements
- Longest subarray not having more than K distinct elements
- Longest Subarray of non-negative Integers
- Length of the longest alternating subarray
- Longest Subarray having sum of elements atmost 'k'
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.