Skip to content

Commit f2c0ea8

Browse files
committed
new question added
1 parent 20cbee6 commit f2c0ea8

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ which has to be returned
2828
- Can maintain a hash Table to solve the algo
2929
- Can maintain multiple variables to solve the algo
3030
- Can maintain two pointers to solve the algo
31+
- Kabane's algo
3132

3233
### For Linked Lists: (methods that can be applied)
3334
- Use multiple variables to not loose track of the linked list and keep moving them ahead in a manner such that various operations can be done on a linked list
@@ -357,7 +358,8 @@ i+given number of times to see if thats true or not.
357358
- [Find a subset in an array whose sum is w](/dynamic-programming/question5.c)
358359
- [Travelling salesman problem](/dynamic-programming/question6.c)
359360
- [All pair shortest path algorithm](/dynamic-programming/question7.c)
360-
- [Find the maximum sum sub array](/dynamic-programming/question7.c)
361+
- [Find the maximum sum sub array](/dynamic-programming/question8.c)
362+
- [Find the maximum sum sub array](/dynamic-programming/question9.c)
361363

362364
## Some important concepts to solve algos better
363365

dynamic-programming/a.exe

-45 Bytes
Binary file not shown.

dynamic-programming/question9.c

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Max sum increasing subsequence
3+
4+
In this we need to find a subsequence which is increasing and has the max sum
5+
6+
METHOD:
7+
We will maintain another array here and for each index this array will have the max sum resulting
8+
from adding numbers satisfying criteria given in the question by traversing back.
9+
Each time we keep doing that for all the numbers and then we will have the max sum possible
10+
11+
Time complexity: O(n^2)
12+
Space complexity: O(n)
13+
*/
14+
15+
#include <stdio.h>
16+
#include <stdlib.h>
17+
18+
int getMaxSum(int *arr, int i){
19+
int index, sum = 0, maxSum = 0;
20+
for(index = i-1; index >=0; index--){
21+
if(arr[i] > arr[index]){
22+
sum = arr[i]+arr[index];
23+
}
24+
if(sum > maxSum){
25+
maxSum = sum;
26+
}
27+
}
28+
return maxSum;
29+
}
30+
31+
void maxIncreaseSubSequence(int *arr, int size){
32+
int sum;
33+
int cumulative[size];
34+
int i, j, index;
35+
int maxSum = 0;
36+
cumulative[0] = arr[0];
37+
for(i=1; i<size;i++){
38+
cumulative[i] = getMaxSum(arr,i);
39+
if(maxSum < cumulative[i]){
40+
maxSum = cumulative[i];
41+
index = i;
42+
}
43+
}
44+
printf("max sum possible in this array is %d at index %d\n", maxSum, index);
45+
}
46+
47+
int main(){
48+
int arr[] = {20,3,1,15,16,2,12,13};
49+
50+
int size = sizeof(arr)/sizeof(arr[0]);
51+
52+
maxIncreaseSubSequence(arr, size);
53+
return 0;
54+
}

0 commit comments

Comments
 (0)