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

Assignment DSA

Uploaded by

RDY Player
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignment DSA

Uploaded by

RDY Player
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

DSA Assignment

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

Iterative with Controlled Recursion Approach


This method uses controlled recursion to look at possible subarrays while iterating across the
array. By restricting the recursion depth to a certain value (k), the number of subarrays that can
be searched is successfully controlled, therefore avoiding exponential growth.

Algorithm: Maximum Contiguous Sum with Controlled Recursion

Input:

`arr`: An array of integers.

`n`: The size of the array.

`k`: The maximum recursion depth (a constant value).

Output:

The maximum sum of a contiguous subarray in the array.

Steps:

1. Initialize:

`max_so_far`: Set to 0 (stores the maximum sum found so far).

`current_sum`: Set to 0 (stores the sum of the current subarray).

2. Iterate:

For each element `arr[i]` in the array:

Add `arr[i]` to `current_sum`.



Call the recursive helper function `find_max_sum_recursive(i,
 k,
current_sum)` with the current index (`i`), `k`, and `current_sum`.
 Update `max_so_far` if `current_sum` is greater than `max_so_far`.
3. Return: `max_so_far`.

Recursive Helper Function (`find_max_sum_recursive`):

1. Base Case:

If `k` is zero or `i` is out of bounds of the array, return `current_sum`.

2. Recursive Case:

 Call the function recursively twice:


 Once with `k` decreased by 1 and `i` incremented by 1:
`find_max_sum_recursive(i + 1, k - 1, current_sum)`.
 Once with `i` incremented by 1 but keeping `k` the same:
`find_max_sum_recursive(i + 1, k, current_sum + arr[i + 1])`.
3

 Return the maximum of the three values: `current_sum` and the results of the
two recursive calls.

CODE:

#include <iostream>

using namespace std;

int find_max_sum_recursive(int arr[], int i, int k, int current_sum) {

if (k == 0 || i >= sizeof(arr) / sizeof(arr[0])) {

return current_sum;

int option1 = find_max_sum_recursive(arr, i + 1, k - 1, current_sum);

int option2 = find_max_sum_recursive(arr, i + 1, k, current_sum + arr[i + 1]);

return max(current_sum, max(option1, option2));

int find_max_contiguous_sum(int arr[], int n, int k) {

int max_so_far = 0;

int current_sum = 0;

for (int i = 0; i < n; i++) {

current_sum += arr[i];

max_so_far = max(max_so_far, find_max_sum_recursive(arr, i, k, current_sum));

return max_so_far;

int main() {

int arr[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4};

int n = sizeof(arr) / sizeof(arr[0]);

int k = 2; // Choose an appropriate value for k

int max_sum = find_max_contiguous_sum(arr, n, k);

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.

2. Worst Case (O (n * 2^(k))):


 The recursive calls can examine a huge number of subarrays if the value of k is selected
in an excessively big or unsuitable manner for the magnitude of the problem.
 Assuming the worst scenario, every element has the potential to initiate up to 2^(k)
recursive calls, resulting in n * 2^(k) calls throughout the loop iterations.
 In the worst-case situation, the time complexity is O(n * 2^(k)), which, for bigger values
of k, may be closer to O(n^2).

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

Growth rate for O(n^3)


Algorithm 2
Source code:

#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

Growth rate for O(n^2)

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

int maxSum = 0; for(int i=0;i<size;i++){


arr[i] = rand() % 100;
for( int i = 0; i < n; i++ ){ }
int thisSum = 0; cout << maxSubsequenceSum(arr,
for( int j = i; j < n; j++ ){ size);
thisSum += a[ j ];
if( thisSum > maxSum ){ return 0;
maxSum = thisSum; }

Growth rate
The time complexity of the algorithm is O(n)
8

Growth rate for O(n)

You might also like