0% found this document useful (0 votes)
8 views

Name: Harsh Dwivedi Reg. No.: 21BCE5373

tryrth

Uploaded by

yash186907
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Name: Harsh Dwivedi Reg. No.: 21BCE5373

tryrth

Uploaded by

yash186907
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

LPS: 3

DAA

Name: Harsh Dwivedi

Reg. No.: 21BCE5373

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>

using namespace std;

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

Inner loop: n-i

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;

for (int i = mid; i >= left; --i) {


sum += arr[i];
if (sum > leftSum) {
leftSum = sum;
leftStart = i;
}
}

sum = 0;
int rightSum = INT_MIN;
int rightEnd = mid + 1;

for (int i = mid + 1; i <= right; ++i) {


sum += arr[i];
if (sum > rightSum) {
rightSum = sum;
rightEnd = i;
}
}

return {leftSum + rightSum, leftStart, rightEnd};


}

Result maxSubArraySum(const vector<int>& arr, int left, int right) {


if (left == right) {
return {arr[left], left, left};
}

int mid = (left + right) / 2;

Result leftResult = maxSubArraySum(arr, left, mid);


Result rightResult = maxSubArraySum(arr, mid + 1, right);
Result crossResult = maxCrossingSum(arr, left, mid, right);

if (leftResult.maxSum >= rightResult.maxSum && leftResult.maxSum >=


crossResult.maxSum) {
return leftResult;
} else if (rightResult.maxSum >= leftResult.maxSum && rightResult.maxSum >=
crossResult.maxSum) {
return rightResult;
} else {
return crossResult;
}
}

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];
}

Result result = maxSubArraySum(arr, 0, n - 1);


cout << "Maximum subarray sum is " << result.maxSum << endl;
cout << "Subarray starts at index " << result.startIdx << " and ends at
index " << result.endIdx << endl;

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

T(n) of divide and conquer approach is: n log(n)

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

using namespace std;

// 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

// Traverse the array


for (int i = 0; i < n; i++) {
maxEndingHere += arr[i];

// Check if the current subarray sum is greater than maxSoFar or


// if it is equal but the length of the current subarray is greater
if (maxEndingHere > maxSoFar || (maxEndingHere == maxSoFar && (i -
tempStart + 1) > maxLength)) {
maxSoFar = maxEndingHere;
start = tempStart;
end = i;
maxLength = end - start + 1;
}

// Reset the subarray if the current element is negative


if (maxEndingHere < 0) {
maxEndingHere = 0;
tempStart = i + 1; // Potential start index for the next subarray
}
}

// Output the results


cout << "Maximum sum subarray is: ";
for (int i = start; i <= end; i++) {
cout << arr[i] << " ";
}
cout << endl;

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:

You might also like