Name: Harsh Dwivedi Reg. No.: 21BCE5373
Name: Harsh Dwivedi Reg. No.: 21BCE5373
DAA
1. The maximum Sum Subarray problem takes an array of positive and negative integers S as input
and finds the subarray of S having maximum sum. For example, consider an array with 16 elements,
13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7. Maximum subarray starts at index 7 and
ends at index 10 programming indices) and sum is 43. Write a C++ code to solve the problem using a
naïve approach. Print the starting index, end index, and sum of the subarray whose sum is maximum.
Find the time complexity of the algorithm.
Code:
#include<iostream>
#include<vector>
#include<climits>
#include<algorithm>
vector<int> max_sum_subarray(vector<int>arr){
int sum=INT_MIN;
int start;
int end;
for(int i=0;i<arr.size();i++){
int curr_sum=0;
for(int j=i;j<arr.size();j++){
curr_sum+=arr[j];
if(curr_sum>sum){
start=i;
end=j;
sum=curr_sum;
}
}
}
vector<int>ans{start, end, sum};
return ans;
}
int main(){
int n;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter all the elements: ";
vector<int>arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
vector<int>ans=max_sum_subarray(arr);
cout<<"Max sum is "<<ans[2]<<endl;
cout<<"Max sum subarray is ";
for(int i=ans[0];i<=ans[1];i++){
cout<<arr[i]<<" ";
}
}
Output:
Time Complexity:
Outer loop: n
Hence T(n)=n²
2. Develop an algorithm and implement it to solve the maximum sum subarray problem using a
divide-and-conquer approach. Print the starting index, end index, and sum of the subarray whose sum
is maximum. What is the time complexity of the algorithm, compare the performance of the naive
algorithm and divide and conquer algorithm for the size of inputs 100, 1000, 10000, 50000, 100000,
500000, 1000000.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include<climits>
using namespace std;
struct Result {
int maxSum;
int startIdx;
int endIdx;
};
Result maxCrossingSum(const vector<int>& arr, int left, int mid, int right) {
int sum = 0;
int leftSum = INT_MIN;
int leftStart = mid;
sum = 0;
int rightSum = INT_MIN;
int rightEnd = mid + 1;
int main() {
int n;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter all the elements: ";
vector<int>arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
return 0;
}
Output:
Analysis:
n1 time1 time2
0.00001 0.00004
100 1 2
0.00015 0.00321
1000 1 7
0.00094 0.26508
10000 8 1
0.00546 5.48742
50000 3 3
0.00983 24.5915
100000 1 8
0.06418
500000 8
100000 0.12238
0 5
Here Time 1 is of divide and conquer approach and time 2 is of Naïve approach
30
25
20
15 time1
time2
10
0
0 200000 400000 600000 800000 1000000 1200000
Clearly we can see that Divide and conquer approach is much better
3. Given an array of positive and negative numbers, develop a linear algorithm and write a C++ code
to print the start index, end index, and sum of the subarray which has maximum sum and maximum
length. Consider an array with 13 elements 4, -12, 3, 10, 4, 5, -15, 6, 7, 1, 3, 1, 4. Two subarrays have
the sum of 22, starting at 2 and ending at 5 with elements 3, 10, 4, 5, and another subarray that starts
at 7 and ends at 12 with elements 6, 7, 1, 3, 1, 4
Code:
#include <iostream>
#include <vector>
#include <climits> // For INT_MIN
// Function to find the maximum sum subarray with the maximum length
void maxSumSubarrayWithMaxLength(const vector<int>& arr) {
int n = arr.size();
if (n == 0) {
cout << "Array is empty." << endl;
return;
}
// Initialize variables
int maxSoFar = INT_MIN;
int maxEndingHere = 0;
int maxLength = 0;
int start = 0;
int end = 0;
int tempStart = 0; // Temporary start index for the current subarray
cout << "Sum of the maximum sum subarray: " << maxSoFar << endl;
cout << "Start index: " << start << endl;
cout << "End index: " << end << endl;
}
int main() {
int n;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter all the elements: ";
vector<int>arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
maxSumSubarrayWithMaxLength(arr);
return 0;
}
Output: