Maximum Subarray Sum of Alternate Parity (original) (raw)
Last Updated : 18 Jan, 2024
Given array A[] of size **N. The Task for this problem is to find the maximum subarray (Subarrays are arrays within another array. Subarrays contain contiguous elements) sum such that adjacent elements of the subarray should have different parity.
**Examples:
**Input: A[] = {-1, 4, -1, 0, 5, -4}
**Output: 8
**Explanation: Subarray {4, -1, 0, 5} has maximum sum with consecutive elements has alternate parity (4 is even, -1 is odd, 0 is even, 5 is odd)**Input: A[] = {-1, 2, 4, -3}
**Output: 4
**Explanation: Subarray {4} has maximum sum.
**Approach: To solve the problem efficiently follow the below idea:
Kadane's Algorithm can be used to solve this problem, small change is that whenever our parity is not same we start new subarray with that point. Using this we can calculate maximum subarray sum with alternate parity with required subarray.
Below are the steps for the above approach:
- Initialize the variable **curSum = A[0]
- Initialize the **maximumSum = 0 that keeps track of the maximum subarray sum with alternate parity.
- Iterate over **N elements
- If the parity is the same then update **curSum with **A[i]
- else if the current sum is negative then update **curSum with A[i]
- else add **A[i] in **curSum
- in each iteration update **maximumSum with **curSum.
Below is the implementation of the above approach:
C++ `
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std;
// Function to Find Maximum subarray sum with // alternate parity int findMaxSubarraySum(int A[], int N) {
// for tracking current sum
int curSum = A[0];
// for tracking max subarray sum
int maximumSum = 0;
// iterating over all N elements
for (int i = 1; i < N; i++) {
// if adjacent elements have different parity
if (A[i] % 2 != A[i - 1] % 2) {
// if curSum with A[i] is greater than
// A[i] alone extend the solution by updating
// curSum with addtion of A[i]
if (curSum + A[i] > A[i]) {
// adding A[i] in curSum
curSum += A[i];
}
// in case if curSum is negative
else {
curSum = A[i];
}
}
else {
// update curSum
curSum = A[i];
}
// updating maximumsum
maximumSum = max(maximumSum, curSum);
}
// returning maximum subarray sum
return maximumSum;
}
// Driver Code int32_t main() {
// Input 1
int N = 6;
int A[] = { -1, 4, -1, 0, 5, -4 };
// Function Call
cout << findMaxSubarraySum(A, N) << endl;
// Input 2
int N1 = 4;
int A1[] = { -1, 2, 4, -3 };
// Function Call
cout << findMaxSubarraySum(A1, N1) << endl;
return 0;
}
Java
public class Main {
// Function to find maximum subarray sum with alternate parity
static int findMaxSubarraySum(int[] A, int N) {
// for tracking current sum
int curSum = A[0];
// for tracking max subarray sum
int maximumSum = 0;
// iterating over all N elements
for (int i = 1; i < N; i++) {
// if adjacent elements have different parity
if (A[i] % 2 != A[i - 1] % 2) {
// if curSum with A[i] is greater than A[i] alone
// extend the solution by updating curSum with addition of A[i]
if (curSum + A[i] > A[i]) {
// adding A[i] in curSum
curSum += A[i];
} else {
// in case if curSum is negative
curSum = A[i];
}
} else {
// update curSum
curSum = A[i];
}
// updating maximum sum
maximumSum = Math.max(maximumSum, curSum);
}
// returning maximum subarray sum
return maximumSum;
}
// Driver Code
public static void main(String[] args) {
// Input 1
int N = 6;
int[] A = { -1, 4, -1, 0, 5, -4 };
// Function Call
System.out.println(findMaxSubarraySum(A, N));
// Input 2
int N1 = 4;
int[] A1 = { -1, 2, 4, -3 };
// Function Call
System.out.println(findMaxSubarraySum(A1, N1));
}
}
Python3
def find_max_subarray_sum(arr, N): # For tracking current sum cur_sum = arr[0]
# For tracking max subarray sum
maximum_sum = 0
# Iterating over all N elements
for i in range(1, N):
# If adjacent elements have different parity
if arr[i] % 2 != arr[i - 1] % 2:
# If cur_sum with arr[i] is greater than arr[i] alone,
# extend the solution by updating cur_sum with addition of arr[i]
if cur_sum + arr[i] > arr[i]:
# Adding arr[i] to cur_sum
cur_sum += arr[i]
# In case if cur_sum is negative
else:
cur_sum = arr[i]
else:
# Update cur_sum
cur_sum = arr[i]
# Updating maximum_sum
maximum_sum = max(maximum_sum, cur_sum)
# Returning maximum subarray sum
return maximum_sum
Driver Code
if name == "main": # Input 1 N = 6 A = [-1, 4, -1, 0, 5, -4]
# Function Call
print(find_max_subarray_sum(A, N))
# Input 2
N1 = 4
A1 = [-1, 2, 4, -3]
# Function Call
print(find_max_subarray_sum(A1, N1))
C#
// C# code to implement the approach using System;
class GFG { // Function to Find Maximum subarray sum with alternate parity static int FindMaxSubarraySum(int[] A, int N) { // for tracking current sum int curSum = A[0];
// for tracking max subarray sum
int maximumSum = 0;
// iterating over all N elements
for (int i = 1; i < N; i++)
{
// if adjacent elements have different parity
if (A[i] % 2 != A[i - 1] % 2)
{
// if curSum with A[i] is greater than
// A[i] alone extend the solution by updating
// curSum with addtion of A[i]
if (curSum + A[i] > A[i])
{
// adding A[i] in curSum
curSum += A[i];
}
// in case if curSum is negative
else
{
curSum = A[i];
}
}
else
{
// update curSum
curSum = A[i];
}
// updating maximumsum
maximumSum = Math.Max(maximumSum, curSum);
}
// returning maximum subarray sum
return maximumSum;
}
// Driver Code
static void Main(string[] args)
{
// Input 1
int N = 6;
int[] A = { -1, 4, -1, 0, 5, -4 };
// Function Call
Console.WriteLine(FindMaxSubarraySum(A, N));
// Input 2
int N1 = 4;
int[] A1 = { -1, 2, 4, -3 };
// Function Call
Console.WriteLine(FindMaxSubarraySum(A1, N1));
}
}
JavaScript
// Function to find maximum subarray // sum with alternate parity function findMaxSubarraySum(A, N) { // For tracking current sum let curSum = A[0];
// For tracking max subarray sum
let maximumSum = 0;
// Iterating over all N elements
for (let i = 1; i < N; i++) {
// If adjacent elements have different parity
if (A[i] % 2 !== A[i - 1] % 2) {
// If curSum with A[i] is greater than A[i] alone,
// extend the solution by updating
// curSum with addition of A[i]
if (curSum + A[i] > A[i]) {
// Adding A[i] to curSum
curSum += A[i];
} else {
// In case if curSum is negative
curSum = A[i];
}
} else {
// Update curSum
curSum = A[i];
}
// Updating maximumsum
maximumSum = Math.max(maximumSum, curSum);
}
// Returning maximum subarray sum
return maximumSum;
}
// Driver Code
// Input 1 const N = 6; const A = [-1, 4, -1, 0, 5, -4];
// Function Call console.log(findMaxSubarraySum(A, N));
// Input 2 const N1 = 4; const A1 = [-1, 2, 4, -3];
// Function Call console.log(findMaxSubarraySum(A1, N1));
`
**Time Complexity: O(N)
**Auxiliary Space: O(1)
Similar Reads
- Maximum Sum Alternating Subarray Given an array arr[] of size N, the task is to find the maximum alternating sum of a subarray possible for a given array. Alternating Subarray Sum: Considering a subarray {arr[i], arr[j]}, alternating sum of the subarray is arr[i] - arr[i + 1] + arr[i + 2] - ........ (+ / -) arr[j]. Examples: Input: 6 min read
- Maximum sum subarray of size range [L, R] Given an integer array arr[] of size N and two integer L and R. The task is to find the maximum sum subarray of size between L and R (both inclusive). Example: Input: arr[] = {1, 2, 2, 1}, L = 1, R = 3 Output: 5 Explanation: Subarray of size 1 are {1}, {2}, {2}, {1} and maximum sum subarray = 2 for 8 min read
- Print subarray with maximum sum Given an array arr[], the task is to print the subarray having maximum sum.Examples:Input: arr[] = {2, 3, -8, 7, -1, 2, 3}Output: 11Explanation: The subarray {7, -1, 2, 3} has the largest sum 11.Input: arr[] = {-2, -5, 6, -2, -3, 1, 5, -6}Output: {6, -2, -3, 1, 5}Explanation: The subarray {6, -2, -3 13 min read
- Maximum sum bitonic subarray Given an array containing n numbers. The problem is to find the maximum sum bitonic subarray. A bitonic subarray is a subarray in which elements are first increasing and then decreasing. A strictly increasing or strictly decreasing subarray is also considered a bitonic subarray. Time Complexity of O 15+ min read
- Maximize product of subarray sum with its maximum element Given an array arr[] consisting of N positive integers, the task is to find the maximum product of the subarray sum with the maximum element of that subarray. Examples: Input: arr[] = {2, -3, 8, -2, 5}Output: 88Explanation:The required maximum product can be obtained using subarray {8, -2, 5}Therefo 8 min read
- Maximum Subarray Sum - Kadane's Algorithm Given an array arr[], the task is to find the subarray that has the maximum sum and return its sum.Examples:Input: arr[] = {2, 3, -8, 7, -1, 2, 3}Output: 11Explanation: The subarray {7, -1, 2, 3} has the largest sum 11.Input: arr[] = {-2, -4}Output: -2Explanation: The subarray {-2} has the largest s 9 min read
- CSES Solutions - Maximum Subarray Sum Given an array arr[] of N integers, your task is to find the maximum sum of values in a contiguous, nonempty subarray. Examples: Input: N = 8, arr[] = {-1, 3, -2, 5, 3, -5, 2, 2}Output: 9Explanation: The subarray with maximum sum is {3, -2, 5, 3} with sum = 3 - 2 + 5 + 3 = 9. Input: N = 6, arr[] = { 5 min read
- Longest subarray having maximum sum Given an array arr[] containing n integers. The problem is to find the length of the subarray having maximum sum. If there exists two or more subarrays with maximum sum then print the length of the longest subarray.Examples: Input : arr[] = {5, -2, -1, 3, -4}Output : 4There are two subarrays with ma 12 min read
- Maximum subarray sum modulo m Given an array of n elements and an integer m. The task is to find the maximum value of the sum of its subarray modulo m i.e find the sum of each subarray mod m and print the maximum value of this modulo operation.Examples: Input: arr[] = {10, 7, 18}, m = 13Output: 12Explanation: All subarrays and t 7 min read
- Sum of maximum of all subarrays | Divide and Conquer Given an array arr[] of length N, the task is to find the sum of the maximum elements of every possible sub-array of the array.Examples: Input : arr[] = {1, 3, 1, 7} Output : 42 Max of all sub-arrays: {1} - 1 {1, 3} - 3 {1, 3, 1} - 3 {1, 3, 1, 7} - 7 {3} - 3 {3, 1} - 3 {3, 1, 7} - 7 {1} - 1 {1, 7} - 12 min read