Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions coding_freshmen/C/AbhiSS01/MAXIMUM_SUM.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>

#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])

int maxSubArraySum (int arr[], int n)
{
int i = 0;
int max_so_far = 0;
int max_ending_here = 0;

for (i = 0; i < n; i++)
{
max_ending_here = max_ending_here + arr[i];
if (max_ending_here < 0)
{
max_ending_here = 0;
}
if (max_so_far < max_ending_here)
{
max_so_far = max_ending_here;
}
}
return max_so_far;
}

int main ()
{
int arr[] = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
int arr_size = ARRAY_SIZE (arr);
const int maxSum = maxSubArraySum (arr, arr_size);
printf ("Largest Sum Contiguous Sub-Array : %d ", maxSum);
return 0;
}
36 changes: 36 additions & 0 deletions coding_freshmen/C/AbhiSS01/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# <MAXIMUM_SUM>
< Given an array arr[] of size N. The task is to find the sum of the contiguous subarray within a arr[] with the largest sum.>

# Problem Explanation 🚀
we need to find the subarray in a given array that has the maximum sum of its elements. In other words, we are looking for a contiguous subarray in the given array whose sum is maximum.

For example, consider the following array:

arr = [-2, -3, 4, -1, -2, 1, 5, -3]
The largest sum contiguous subarray in this array is [4, -1, -2, 1, 5], which has a sum of 7.

The problem of finding the largest sum contiguous subarray has a well-known algorithm called Kadane’s algorithm.

# Your logic 🤯
* Approach
Initialize the variables max_so_far = INT_MIN and max_ending_here = 0
Run a for loop from 0 to N-1 and for each index i:
Add the arr[i] to max_ending_here.
If max_so_far is less than max_ending_here then update max_so_far to max_ending_here.
If max_ending_here < 0 then update max_ending_here = 0
Return max_so_far

* Own test cases if any
If we are entering 8 elements (N = 8), with array element values as -1,-5,5,3,-2,5,4 and 1 then,
The largest contiguous subarray is: 5 3 -2 5 4 1
The sum of the largest contiguous subarray is: 16
* Code Structure and Libraries used
<stdio.h>


# Time Complexity and Space Complexity

Time Complexity -> O(N)
Space Complexity -> O(1)

```