Given an array **arr[]** of **N** positive integers and a non negative integer **K**. The task is to delete exactly **K** sub-arrays from the array such that all the remaining elements of the array are prime and the size of the remaining array is maximum possible.

**Examples:**

Input:arr[] = {2, 4, 2, 2, 4, 2, 4, 2}, k = 2

Output:4

Delete the subarrays arr[1] and arr[4…6] and

the remaining prime array will be {2, 2, 2, 2}

Input:arr[] = {2, 4, 2, 2, 4, 2, 4, 2}, k = 3

Output:5

A **simple approach** would be to search in all the sub-arrays that would cost us **O(N ^{2})** time complexity and then keep track of the number of primes or composites in particular length of sub-array.

An **efficient approach** is to keep the track of number of primes between two consecutive composites.

**Preprocessing step:**Store all the primes in prime array using Sieve of Eratosthenes- Compute the indices of all composite numbers in a vector v.
- Compute the distance between two consecutive indices of above described vector in a vector diff as this will store the number of primes between any two consecutive composites.
- Sort this vector. After sorting we get the subarray that contain least no of primes to highest no of primes.
- Compute the prefix sum of this vector. Now each index of diff denotes the k value and value in diff denotes no of primes to be deleted when deleting k subarrays. 0th index denotes the largest k less than size of v, 1st index denotes second largest k and so on. So, from prefix sum vector we directly get the no of primes to be deleted.

After performing the above steps our solution depends on three cases:

- This is impossible case if k is 0 and there are composite integers in the array.
- If k is greater than or equal to no of composites then we can delete all composite integers and extra primes to equate the value k.These all subarrays are of size 1 which gives us optimal answer.
- If k is less than no of composite integers then we have to delete those subarrays which contain all the composite and no of primes falling in these subarrays.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `const` `int` `N = 1e7 + 5; ` `bool` `prime[N]; ` ` ` `// Sieve of Eratosthenes ` `void` `seive() ` `{ ` ` ` `for` `(` `int` `i = 2; i < N; i++) { ` ` ` `if` `(!prime[i]) { ` ` ` `for` `(` `int` `j = i + i; j < N; j += i) { ` ` ` `prime[j] = 1; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `prime[1] = 1; ` `} ` ` ` `// Function to return the size ` `// of the maximized array ` `int` `maxSizeArr(` `int` `* arr, ` `int` `n, ` `int` `k) ` `{ ` ` ` `vector<` `int` `> v, diff; ` ` ` ` ` `// Insert the indices of composite numbers ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(prime[arr[i]]) ` ` ` `v.push_back(i); ` ` ` `} ` ` ` ` ` `// Compute the number of prime between ` ` ` `// two consecutive composite ` ` ` `for` `(` `int` `i = 1; i < v.size(); i++) { ` ` ` `diff.push_back(v[i] - v[i - 1] - 1); ` ` ` `} ` ` ` ` ` `// Sort the diff vector ` ` ` `sort(diff.begin(), diff.end()); ` ` ` ` ` `// Compute the prefix sum of diff vector ` ` ` `for` `(` `int` `i = 1; i < diff.size(); i++) { ` ` ` `diff[i] += diff[i - 1]; ` ` ` `} ` ` ` ` ` `// Impossible case ` ` ` `if` `(k > n || (k == 0 && v.size())) { ` ` ` `return` `-1; ` ` ` `} ` ` ` ` ` `// Delete sub-arrays of length 1 ` ` ` `else` `if` `(v.size() <= k) { ` ` ` `return` `(n - k); ` ` ` `} ` ` ` ` ` `// Find the number of primes to be deleted ` ` ` `// when deleting the sub-arrays ` ` ` `else` `if` `(v.size() > k) { ` ` ` `int` `tt = v.size() - k; ` ` ` `int` `sum = 0; ` ` ` `sum += diff[tt - 1]; ` ` ` `int` `res = n - (v.size() + sum); ` ` ` `return` `res; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `seive(); ` ` ` `int` `arr[] = { 2, 4, 2, 2, 4, 2, 4, 2 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `k = 2; ` ` ` `cout << maxSizeArr(arr, n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `N = ` `10000005` `; ` `static` `int` `[]prime = ` `new` `int` `[N]; ` ` ` `// Sieve of Eratosthenes ` `static` `void` `seive() ` `{ ` ` ` `for` `(` `int` `i = ` `2` `; i < N; i++) ` ` ` `{ ` ` ` `if` `(prime[i] == ` `0` `) ` ` ` `{ ` ` ` `for` `(` `int` `j = i + i; j < N; j += i) ` ` ` `{ ` ` ` `prime[j] = ` `1` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `prime[` `1` `] = ` `1` `; ` `} ` ` ` `// Function to return the size ` `// of the maximized array ` `static` `int` `maxSizeArr(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `ArrayList<Integer> v = ` `new` `ArrayList<Integer>(); ` ` ` `ArrayList<Integer> diff = ` `new` `ArrayList<Integer>(); ` ` ` ` ` `// Insert the indices of composite numbers ` ` ` `int` `num = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `if` `(prime[arr[i]] == ` `1` `) ` ` ` `{ ` ` ` `v.add(i); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Compute the number of prime between ` ` ` `// two consecutive composite ` ` ` `num = ` `0` `; ` ` ` `for` `(` `int` `i = ` `1` `; i < v.size(); i++) ` ` ` `{ ` ` ` `diff.add(v.get(i) - v.get(i - ` `1` `) - ` `1` `); ` ` ` `} ` ` ` ` ` `// Sort the diff vector ` ` ` `Collections.sort(diff); ` ` ` ` ` `// Compute the prefix sum of diff vector ` ` ` `for` `(` `int` `i = ` `1` `; i < diff.size(); i++) ` ` ` `{ ` ` ` `diff.set(i, diff.get(i) + diff.get(i - ` `1` `)); ` ` ` `} ` ` ` ` ` `// Impossible case ` ` ` `if` `(k > n || (k == ` `0` `&& v.size() > ` `0` `)) ` ` ` `{ ` ` ` `return` `-` `1` `; ` ` ` `} ` ` ` ` ` `// Delete sub-arrays of length 1 ` ` ` `else` `if` `(v.size() <= k) ` ` ` `{ ` ` ` `return` `(n - k); ` ` ` `} ` ` ` ` ` `// Find the number of primes to be deleted ` ` ` `// when deleting the sub-arrays ` ` ` `else` `if` `(v.size() > k) ` ` ` `{ ` ` ` `int` `tt = v.size() - k; ` ` ` `int` `sum = ` `0` `; ` ` ` `sum += diff.get(tt - ` `1` `); ` ` ` `int` `res = n - (v.size() + sum); ` ` ` `return` `res; ` ` ` `} ` ` ` `return` `1` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `seive(); ` ` ` `int` `[]arr = { ` `2` `, ` `4` `, ` `2` `, ` `2` `, ` `4` `, ` `2` `, ` `4` `, ` `2` `}; ` ` ` `int` `n = arr.length; ` ` ` `int` `k = ` `2` `; ` ` ` `System.out.println(maxSizeArr(arr, n, k)); ` `} ` `} ` ` ` `// This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation of above approach ` ` ` `N ` `=` `10000005` `prime ` `=` `[` `False` `]` `*` `N ` ` ` `# Sieve of Eratosthenes ` `def` `seive(): ` ` ` `for` `i ` `in` `range` `(` `2` `,N): ` ` ` `if` `not` `prime[i]: ` ` ` `for` `j ` `in` `range` `(i` `+` `1` `,N): ` ` ` `prime[j] ` `=` `True` ` ` ` ` `prime[` `1` `] ` `=` `True` ` ` `# Function to return the size ` `# of the maximized array ` `def` `maxSizeArr(arr, n, k): ` ` ` `v, diff ` `=` `[], [] ` ` ` ` ` `# Insert the indices of composite numbers ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `prime[arr[i]]: ` ` ` `v.append(i) ` ` ` ` ` `# Compute the number of prime between ` ` ` `# two consecutive composite ` ` ` `for` `i ` `in` `range` `(` `1` `, ` `len` `(v)): ` ` ` `diff.append(v[i] ` `-` `v[i` `-` `1` `] ` `-` `1` `) ` ` ` ` ` `# Sort the diff vector ` ` ` `diff.sort() ` ` ` ` ` `# Compute the prefix sum of diff vector ` ` ` `for` `i ` `in` `range` `(` `1` `, ` `len` `(diff)): ` ` ` `diff[i] ` `+` `=` `diff[i` `-` `1` `] ` ` ` ` ` `# Impossible case ` ` ` `if` `k > n ` `or` `(k ` `=` `=` `0` `and` `len` `(v)): ` ` ` `return` `-` `1` ` ` ` ` `# Delete sub-arrays of length 1 ` ` ` `elif` `len` `(v) <` `=` `k: ` ` ` `return` `(n` `-` `k) ` ` ` ` ` `# Find the number of primes to be deleted ` ` ` `# when deleting the sub-arrays ` ` ` `elif` `len` `(v) > k: ` ` ` `tt ` `=` `len` `(v) ` `-` `k ` ` ` `s ` `=` `0` ` ` `s ` `+` `=` `diff[tt` `-` `1` `] ` ` ` `res ` `=` `n ` `-` `(` `len` `(v) ` `+` `s) ` ` ` `return` `res ` ` ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `seive() ` ` ` ` ` `arr ` `=` `[` `2` `, ` `4` `, ` `2` `, ` `2` `, ` `4` `, ` `2` `, ` `4` `, ` `2` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` `k ` `=` `2` ` ` ` ` `print` `(maxSizeArr(arr, n, k)) ` ` ` `# This code is contributed by ` `# sanjeev2552 ` |

*chevron_right*

*filter_none*

**Output:**

4

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:

- Find an array of size N having exactly K subarrays with sum S
- Maximum possible middle element of the array after deleting exactly k elements
- Count of subarrays having exactly K prime numbers
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Check if number can be made prime by deleting a single digit
- Number of subarrays having sum exactly equal to k
- Count of subarrays having exactly K distinct elements
- Count of subarrays having exactly K perfect square numbers
- Count of subarrays which contains a given number exactly K times
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Maximize cost to empty an array by removing contiguous subarrays of equal elements
- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Check if each element of the given array is the product of exactly K prime numbers
- Maximize the number of subarrays with XOR as zero
- Maximize count of non-overlapping subarrays with sum K
- Number of ways to erase exactly one element in the Binary Array to make XOR zero
- Minimum increments of Non-Decreasing Subarrays required to make Array Non-Decreasing
- Minimize count of increments of each element of subarrays required to make Array non-increasing
- Generate Array whose sum of all K-size subarrays divided by N leaves remainder X

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.