🗽
🗽
🗽
🗽
C/Cpp and OS
Search…
Guide
Templates and methods
Linked List with C
Linked List with C++
Stacks with C
Queues with C
Graphs with C/C++
Trees with C/C++
DS with C
Reverse Linked List
Implement Queue with LL
Find mid of LL without traverse
Delete a Node from LL
Create a Binary search tree
String and Array
Reverse string
implement strcmp in C
Maximum Subarray
CTCI
Ch 01 - Arrays and Strings
Ch 02 - Linked List
Algorithm
Bubble Sort
Insertion Sort
Merge Sort
Count Prime
Basic Concept in C
Understand about pointer
Pre/Post Increment Operator
string and constant charaters
buffer overflow
Short-circuit evaluation
Allocate memory for 2D array dynamically
OS review
For beginner
Reference
Question Collecting:
References
Template on Linux
Powered By
GitBook
Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
https://leetcode.com/problems/maximum-subarray/
Explanation:
Algorithm:
1
#include <stdio.h>
2
3
int maximumSubarray(int* array, int len){
4
int tmpMax = array[0];
5
6
int finalMax = tmpMax;
7
for(int i = 1; i < len; i++){
8
if(tmpMax + array[i] < array[i]){
9
tmpMax = array[i];
10
}else{
11
tmpMax += array[i];
12
}
13
14
if(tmpMax > finalMax){
15
finalMax = tmpMax;
16
}
17
}
18
return finalMax;
19
}
Copied!
Test:
1
int main(){
2
int array[] = {1, 2, 3, 4, 5, -10, 20, 30, -40, 10};
3
int len = sizeof(array) / sizeof(array[0]);
4
5
printf("Result of Maximum Subarray is : %d \n", maximumSubarray(array, len));
6
7
return 0;
8
}
Copied!
String and Array - Previous
implement strcmp in C
Next - CTCI
Ch 01 - Arrays and Strings
Last modified
3mo ago
Copy link
Contents
Explanation:
Algorithm:
Test: