C++ Program to Find the maximum subarray sum using Binary Search approach


Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and conquer. For this algorithm to work properly, the data collection should be in the sorted form.

Binary search looks for a particular item by comparing the middle most item of the collection. If a match occurs, then the index of item is returned. If the middle item is greater than the item, then the item is searched in the sub-array to the left of the middle item. Otherwise, the item is searched for in the sub-array to the right of the middle item. This process continues on the sub-array as well until the size of the subarray reduces to zero.

This is the program to find the maximum subarray sum using Binary Search approach.

Algorithms

Begin
   Declare an integer function maximum() to find the maximum of two integers.
   Declare val1, val2 to the integer datatype.
   Pass them as parameter.
   Check the maximum between val1 and val2.
   Return the maximum value.
End
Begin
   Declare an integer function MCS() to find the maximum sum sub-array which includes medium of the sub-array.
   Declare an array array[] and variable l, m, h to the integer datatype.
   Pass them as parameter.
   Declare variable s, sum_of_left_part to the integer datatype.
      Initialize s = 0.
      Initialize sum_of_left_part = -1.
   for (int i = m; i >= l; i--)
      s = s + array[i].
   if (s > sum_of_left_part) then
      sum_of_left_part = s.
      s = 0
   Declare variable sum_of_right_part to the integer datatype.
      Initialize sum_of_right_part = -1.
   for (int i = m+1; i <= h; i++)
      s = s + array[i].
   if (s > sum_of_right_part) then
      sum_of_right_part = s.
   return sum_of_left_part + sum_of_right_part.
End
Begin
   Declare an integer function MaximumSum_of_SubArray().
   Declare an array array[] and variable l, h to the integer datatype.
      Pass them as parameter.
   Declare m to the integer datatype.
   if (l == h) then
      return array[l].
   m = (l + h)/2;
   return maximum(maximum(MaximumSum_of_SubArray(array, l, m), MaximumSum_of_SubArray(array, m+1, h)), MCS(array, l, m, h)).
   Declare number_of_elements and i to the integer datatype.
   Print “Enter the number of elements of array: ”.
   Enter the value of number_of_elements.
   Declare an array a[number_of_elements] to the integer datatype.
   for(i = 0; i < n; i++)
      Print “Enter the element of”.
      Enter the data element of array.
   Print “Maximum sum of Sub-Array is: ” .
      Print the result of MaximumSum_of_SubArray(a, 0, n-1).
End.

Example

 Live Demo

#include<iostream>
using namespace std;
int maximum(int val1, int val2) // find the maximum of two integers {
   return (val1 > val2)? val1:val2;
}
int MCS(int array[], int l, int m, int h) // find the maximum sum sub-array which includes medium of the sub-array. {
   int s = 0;
   int sum_of_left_part = -1;
   for (int i = m; i >= l; i--) {
      s = s + array[i];
      if (s > sum_of_left_part)
         sum_of_left_part = s;
   }
   s = 0;
   int sum_of_right_part = -1;
   for (int i = m+1; i <= h; i++) {
      s = s + array[i];
      if (s > sum_of_right_part)
         sum_of_right_part = s;
   }
   return sum_of_left_part + sum_of_right_part; // Return sum of elements on left and right of medium.
}
int MaximumSum_of_SubArray(int array[], int l, int h) {
   int m;
   if (l == h)
      return array[l];
   m = (l + h)/2;
   return maximum(maximum(MaximumSum_of_SubArray(array, l, m), MaximumSum_of_SubArray(array, m+1, h)), MCS(array, l, m, h));
}
int main() {
   int number_of_elements, i;
   cout<<"Enter the number of elements of array: ";
   cin>> number_of_elements;
   cout<<endl;
   int a[number_of_elements];
   for(i = 0; i < number_of_elements; i++) {
      cout<<"Enter the element of "<<i+1<<": ";
      cin>>a[i];
   }
   cout<<"\nMaximum sum of Sub-Array is: "<<MaximumSum_of_SubArray(a, 0, number_of_elements -1); // Print the maximum sum sub-array.
   return 0;
}

Output

Enter the number of elements of array: 5

Enter the element of 1: 12
Enter the element of 2: 45
Enter the element of 3: 56
Enter the element of 4: 48
Enter the element of 5: 75

Maximum sum of Sub-Array is: 236

Updated on: 30-Jul-2019

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements