Assignment DSA
Assignment DSA
Designing Algorithm
Abraham Masresha
Student Code: OJ3914
1
Assignment 1
Description
The challenge involves the Maximum Contiguous Sum Problem, also referred to as the
Maximum Subarray Sum Problem, which revolves around determining the maximum sum within
a contiguous subarray in a given array of numbers. While existing algorithms for this problem
typically exhibit quadratic, cubic, or linear time complexities, the objective is to explore an
alternative approach that yields a more efficient solution. This task encourages students to delve
into creative algorithmic strategies to enhance efficiency in problem-solving. The aim is to
devise a distinctive solution that employs unconventional methods, allowing students to
showcase their comprehension of algorithm design and optimization principles. The assignment
specifically requires the development of an alternative algorithm for solving the Maximum
Contiguous Sum Problem, emphasizing a time complexity that differs from quadratic, cubic,
and linear approaches.
This assignment requires students to not only design an alternative algorithm for the Maximum
Contiguous Sum Problem with a time complexity distinct from quadratic, cubic, and linear approaches
but also to visually depict the growth rate of each algorithm (Algorithm1, Algorithm2 and Algorithm3
that we have discussed in Tutorial class and Algorithm4 , that you are going to design).
Deliverables
1. A not less than 5 pages of document that describes the assignment work, per the template
provided.
2. Source code of each Algorithm.
Note:
The assignment is expected to be submitted on or before Feb 29, 2024 12 p.m.
2
Input:
Output:
Steps:
1. Initialize:
2. Iterate:
1. Base Case:
2. Recursive Case:
Return the maximum of the three values: `current_sum` and the results of the
two recursive calls.
CODE:
#include <iostream>
return current_sum;
int max_so_far = 0;
int current_sum = 0;
current_sum += arr[i];
return max_so_far;
int main() {
cout << "Maximum contiguous sum: " << max_sum << endl;
return 0;
}
4
Time Complexity
1. Best Case O(n)):
There is a limit to the number of recursive calls made during an iteration if k is selected at
a modest and constant value.
The loop that iterates through the array n times and adds O(n) to the complexity emerges
as the major contributor.
Growth rate
Algorithm 1
5
Source code:
#include <iostream> }
#include <cstdlib> }
}
using namespace std; return maxSum;
}
int n;
int maxSubsequenceSum( int a[ ], int n){ int main(){
const int size = 10000;
int maxSum = 0; int arr[size];
int thisSum = 0;
for(int i=0;i<size;i++){
for( int i = 0; i < n; i++ ){ arr[i] = rand() % 100;
for( int j = i; j < n; j++ ){ }
int thisSum = 0; cout << maxSubsequenceSum(arr,
for( int k= i; k<= j; k++) size);
thisSum += a[ k ];
if( thisSum > maxSum ){ return 0;
maxSum = thisSum; }
Growth rate
The time complexity of the algorithm is O(n^3).
6
#include <iostream> }
#include <cstdlib> }
#include <bits/stdc++.h> return maxSum;
}
using namespace std;
int main(){
int maxSubsequenceSum( int a[ ], int n){ const int size = 10000;
int arr[size];
int maxSum = 0;
for(int i=0;i<size;i++){
for( int i = 0; i < n; i++ ){ arr[i] = rand() % 100;
int thisSum = 0; }
for( int j = i; j < n; j++ ){ cout << maxSubsequenceSum(arr,
thisSum += a[ j ]; size);
if( thisSum > maxSum ){
maxSum = thisSum; return 0;
}
}
Growth rate
The time complexity of the algorithm is O(n^2)
7
Algorithm 3
Source code:
#include <iostream> }
#include <cstdlib> }
#include <bits/stdc++.h> return maxSum;
}
using namespace std;
int main(){
int maxSubsequenceSum( int a[ ], int const int size = 10000;
n){ int arr[size];
Growth rate
The time complexity of the algorithm is O(n)
8