Maximum Subarray sum of Prime length
Given an array arr[] of size N, the task is to find the maximum subarray sum that can be obtained such that the length of the subarray should be prime.
Examples :
Input: arr[] = {2, -1, 3, -2, 1, -1}
Output: 4
The subarray {2, -1, 3} of size = 3 (prime number)input: arr[] = {-2, -3, 4, -1, -2, 1, 5, -3}
Output: 7
The subarray {4, -1, -2, 1, 5} of size = 5 (prime number)
Naive Approach: The idea is as follows:
Generate all possible subarrays and from them find the ones with prime length. Find the maximum sum among them.
Follow the given steps to solve the problem:
- Generate all possible subarrays of all lengths using nested for-loops.
- Find the sum of each prime length subarray.
- The numbers which are primes can be precomputed by Sieve algorithm
- Now for each prime length, calculate the sum and take the maximum of it
Below is the implementation of the above approach:
C++14
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to implement sieve of eratosthenesvoid sieve(int N, vector<bool>& prime){ prime[1] = false; prime[0] = false; for (int i = 2; i * i <= N; i++) { if (prime[i]) { for (int p = i * i; p <= N; p += i) { prime[p] = false; } } }} // Function to find the sum// of prime length subarraysint primeLenSum(int a[], int N){ vector<bool> prime(N + 1, true); sieve(N, prime); int ans = INT_MIN; // Traversing to find max sum // of prime subarray for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { if (prime[j - i]) { int sum = 0; for (int k = i; k <= j; k++) sum += a[k]; ans = max(ans, sum); } } } return ans;} // Driver codeint main(){ int arr[] = { 2, -1, 3, -2, 1, -1 }; int N = sizeof(arr) / sizeof(arr[0]); // Function call cout << primeLenSum(arr, N) << "\n"; return 0;} |
4
Time complexity: O(N3)
Auxiliary Space: O(N)
Efficient Approach: To solve the problem follow the below idea:
Use Kadane’s algorithm, and update the answer only if the length of the subarray is prime.
Follow the given steps to solve the problem:
- Initialize max_so_far = INT_MIN (since sum can be negative), and max_ending_here = 0 (to keep track of the current sum )
- Loop to iterate each element of the array:
- max_ending_here is equal to max_ending_here + arr[i]
- If max_so_far is less than max_ending_here then update max_so_far
- If max_ending_here is less than 0 then set max_ending_here = 0
- Return max_so_far
- Now for calculating the subarray sum of prime length we have to keep track of the subarray size and have to check whether the size is prime or not
Below is the implementation of the above idea :
C++14
// C++ code for the above approach #include <bits/stdc++.h>using namespace std; // Function to implement sieve of eratosthenesvoid sieve(int n, vector<bool>& prime){ prime[1] = false; prime[0] = false; for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int p = i * i; p <= n; p += i) { prime[p] = false; } } }} // Function to find the maximum subarray sum// having prime lengthint primeLenSum(int a[], int N){ vector<bool> prime(N + 1, true); sieve(N, prime); int max_ending_here = 0; int max_for_primes = 0, sz = 0; // Finding prime length subarray // with maximum sum for (int i = 0; i < N; i++) { max_ending_here += a[i]; sz = sz + 1; if (max_ending_here < 0) { max_ending_here = 0; sz = 0; } if (max_ending_here > max_for_primes && prime[sz]) max_for_primes = max_ending_here; } return max_for_primes;} // Driver codeint main(){ int arr[] = { 2, -1, 3, -2, 1, -1 }; int N = sizeof(arr) / sizeof(arr[0]); // Function call cout << primeLenSum(arr, N); return 0;} |
4
Time Complexity: O(N * log(logN))
Auxiliary Space: O(N)


.png)