Maximize the subarray sum after multiplying all elements of any subarray with X in C++

C++Server Side ProgrammingProgramming

We are given with an integer array and an integer variable i.e. ‘X’. The task is to firstly form the subarray from the given array and then multiply all the elements of a subarray with an integer X. At last find out the elements that will contribute the maximum sum.

Let us see various input output scenarios for this -

In − int arr[] = {2, 4, 1, -5, -2}, X = 3

Out  − Maximize the subarray sum after multiplying all elements of any subarray with X are: 21

Explanation − we are given with an array and an integer variable as X. Firstly, fetch the subarray from the given array let’s say, {2, 4, 1}. Now multiply all the elements of a subarray with X i.e. 3 so the array will be {6, 12, 3, -5, -2}. At last, check for the maximum subarray sum which will be returned by 6 + 12 + 3 = 21.

In   − int arr[] = {-1, 2, -6, 3, -4}, x= -1

Out − Maximize the subarray sum after multiplying all elements of any subarray with X are: 11

Explanation − we are given with an array and an integer variable as X. Firstly, fetch the subarray from the given array let’s say, {-1, -6, -4}. Now multiply all the elements of a subarray with X i.e. -1 so the array will be {1, 2, 6, 3, 4}. At last, check for the maximum subarray sum which will be returned by 1 + 6 + 4 = 11.

Approach used in the below program is as follows

  • Input an integer array and an integer variable as ‘X’. calculate the size of an array and pass the data to the function Max_Subarray(arr, size, x).

  • Inside the function Max_Subarray(arr, size, x)

    • Declare an array as int arr_2[size][3] and a temporary variable as temp to 0.

    • Initialises all the elements of an array ‘arr_2’ with -1 using memset() method in C++.

    • Start loop FOR from i to 0 till the size of an array. Inside the loop, set temp to the call to the function max(temp, check(i, 0, arr, arr_2, size, x))

    • Return temp.

  • Inside the function int check(int first, int last, int arr[], int arr_2[Max_size][3], int size, int x)

    • Declare a temporary variable as count to 0.

    • Check IF first = size then return 0

    • Check IF arr_2[first][last] != -1 then return arr_2[first][last].

    • Check IF last = 0 then call the inbuilt max function of C++ to find out the maximum value as max(count, arr[first] + check(first + 1, 0, arr, arr_2, size, x)) and also set count = max(count, x * arr[first] + check(first + 1, 1, arr, arr_2, size, x))

    • ELSE IF check last = 1 then set count to max(count, x * arr[first] + check(first + 1, 1, arr, arr_2, size, x)) and set count to max(count, arr[first] + check(first + 1, 2, arr, arr_2, size, x))

    • ELSE, set count to max(count, arr[first] + check(first + 1, 2, arr, arr_2, size, x));

    • Return arr_2[first][last] to count.

  • Print the result.

Example

#include <bits/stdc++.h>
using namespace std;
#define Max_size 5

int check(int first, int last, int arr[], int arr_2[Max_size][3], int size, int x){
   int count = 0;
      if(first == size){
         return 0;
      }
      if(arr_2[first][last] != -1){
         return arr_2[first][last];}
      if (last == 0){
         count = max(count, arr[first] + check(first + 1, 0, arr, arr_2, size, x));
         count = max(count, x * arr[first] + check(first + 1, 1, arr, arr_2, size, x));
      }
      else if(last == 1){
         count = max(count, x * arr[first] + check(first + 1, 1, arr, arr_2, size, x));
         count = max(count, arr[first] + check(first + 1, 2, arr, arr_2, size, x));
      }
      else{
         count = max(count, arr[first] + check(first + 1, 2, arr, arr_2, size, x));
      }
      return arr_2[first][last] = count;
}
int Max_Subarray(int arr[], int size, int x){
   int arr_2[size][3];
   int temp = 0;
   memset(arr_2, -1, sizeof arr_2);
   for(int i = 0; i < size; i++){
      temp = max(temp, check(i, 0, arr, arr_2, size, x));
   }
   return temp;
}
int main(){
   int arr[] = {2, 4, 1, -5, -2};
   int size = sizeof(arr) / sizeof(arr[0]);
   int x = 3;
   cout<<"Maximize the subarray sum after multiplying all elements of any subarray with X are: "<<Max_Subarray(arr, size, x);
   return 0;
}

Output

If we run the above code it will generate the following Output

Maximize the subarray sum after multiplying all elements of any subarray with X are: 21
raja
Updated on 22-Oct-2021 07:42:24

Advertisements