Maximum Subarray Sum Excluding Certain Elements in C++ program

C++Server Side ProgrammingProgramming

In this problem, we are given two arrays arr1[] of size n and arr2[] of size m. Our task is to create a program to find the maximum subarray sum excluding certain elements.

Problem Description − We need to find the maximum subarray sum from elements of the array arr1[] that are not present in arr2[].

Let’s take an example to understand the problem,

Input

arr1[] = {4, 5, 7, 2, 9}, arr2[] = {1, 9, 2, 7}

Output

9

Explanation

arr1 after removal of elements of arr2[] = {4, 5}
Both can form a subarray, hence sum = 4+5 = 9.

Solution Approach

A simple solution to the problem is using Kadane’s Algorithm in which we can find all positive contagious sequences of arrays. Find the sum of all elements of this subsequence and then return the maximum of them. We will update the algorithm based on the fact that we need not consider arr2[] elements for the max subarray and for this we will search the element using the searching algorithm. If it is present in the arr2, we will clear the current window and reset the window. Check if the sum is less than maxSum, maxSum = sum.

Example

 Live Demo

Program to illustrate the working of our solution,

#include <iostream>
using namespace std;
int isInArr2(int arr2[], int start, int end, int searchEle){
   if (end >= start) {
      int mid = start + (end − start) / 2;
      if (arr2[mid] == searchEle)
      return true;
      if (arr2[mid] > searchEle)
      return isInArr2(arr2, start, mid − 1, searchEle);
      return isInArr2(arr2, mid + 1, end, searchEle);
   }
   return false;
}
int calcMaxSubArraySum(int arr1[], int arr2[], int n, int m){
   int maxSum = −1, sum = 0;
   for (int i = 0; i < n; i++) {
      if (isInArr2(arr2, 0, m, arr1[i])) {
         sum = 0;
         continue;
      }
      sum = max(arr1[i], sum + arr1[i]);
      maxSum = max(maxSum, sum);
   }
   return maxSum;
}
int main(){
   int arr1[] = { 5, 4, 7, 2, 9 };
   int arr2[] = { 1, 9, 2, 7 };
   int n = sizeof(arr1) / sizeof(arr1[0]);
   int m = sizeof(arr2) / sizeof(arr2[0]);
   cout<<"The maximum Subarray Sum Excluding Certain Elements is
   "<<calcMaxSubArraySum(arr1, arr2, n, m);
   return 0;
}

Output

The maximum Subarray Sum Excluding Certain Elements is 9

This solution is effective but there can be a better approach to check for the presence of elements in the second array which will save some computation time. Here is a way to do it using a map −

In this approach, we will use our updated Kadane’s Algorithm and one more update will be done by replacing our searching algorithm with a map−based check for the presence of the element in the array which will be effective.

Example

Program to illustrate the working of our solution,

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int calcMaxSubArraySum(int arr1[], int arr2[], int n, int m){
   unordered_map<int,int> checkVal;
   for(int i=0;i<m;i++)
   checkVal[arr2[i]] = 1;
   int maxSum = −1, sum = 0;
   for (int i = 0; i < n; i++) {
      if (checkVal[arr1[i]]==1) {
         sum = 0;
         continue;
      }
      sum = max(arr1[i], sum + arr1[i]);
      maxSum = max(maxSum, sum);
   }
   return maxSum;
}
int main(){
   int arr1[] = { 5, 4, 7, 2, 9 };
   int arr2[] = { 1, 9, 2, 7 };
   int n = sizeof(arr1) / sizeof(arr1[0]);
   int m = sizeof(arr2) / sizeof(arr2[0]);
   cout<<"The maximum Subarray Sum Excluding Certain Elements is "<<calcMaxSubArraySum(arr1, arr2, n, m);
   return 0;
}

Output

The maximum Subarray Sum Excluding Certain Elements is 9
raja
Published on 09-Dec-2020 17:20:34
Advertisements