Skip to content

Commit 765940a

Browse files
committed
new question added
1 parent 22e4046 commit 765940a

File tree

6 files changed

+267
-25
lines changed

6 files changed

+267
-25
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ use KMP
456456
- [Given a string of digits, find the length of the longest substring of a string, such that the length of the substring is '2k' digits and sum of left k digits is equal to the sum of right k digits](/dynamic-programming/question31.c)
457457
- [Given a rod of length 'n' inches and an array of prices that contains prices of all pieces of size smaller than n, find the max value obtainable by cutting the rod and selling the pieces](/dynamic-programming/question32.c)
458458
- [Fibonacci series](/dynamic-programming/question33.c)
459+
- [Max product sub array](/dynamic-programming/question34.c)
459460

460461
### Graphs
461462

dynamic-programming/question2.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int compareStrings(char *str1, char *str2, int len1, int len2){
3333
for(j=0;j<=len2;j++){
3434
if(i==0 || j==0){
3535
m[i][j] = 0;
36-
}else if(str1[i] == str2[j]){
36+
}else if(str1[i-1] == str2[j-1]){
3737
m[i][j] = 1 + m[i-1][j-1];
3838
}else{
3939
m[i][j] = max(m[i-1][j],m[i][j-1]);
@@ -54,4 +54,60 @@ int main(){
5454

5555
printf("length is %d\n", len);
5656
return 0;
57+
}
58+
59+
60+
//METHOD2 using recursion
61+
#include <stdio.h>
62+
#include <stdlib.h>
63+
#define MAX 200
64+
65+
int table[MAX][MAX];
66+
67+
int max(int a, int b){
68+
// printf("finding max between %d, %d \n",a,b);
69+
return a>b?a:b;
70+
}
71+
72+
int lcs(int start1, int start2, char *str1, char *str2, int len1, int len2){
73+
if(start1 == len1 || start2 == len2){
74+
return 0;
75+
}
76+
if(table[start1][start2] == -1){
77+
if(str1[start1] == str2[start2]){
78+
table[start1][start2] = 1 + lcs(start1+1,start2+1,str1, str2, len1, len2);
79+
}else{
80+
int a = lcs(start1+1,start2,str1,str2,len1,len2);
81+
int b = lcs(start1,start2+1,str1,str2,len1,len2);
82+
table[start1][start2] = max(a,b);
83+
}
84+
}
85+
return table[start1][start2];
86+
}
87+
88+
void init(){
89+
int i,j;
90+
for(i=0;i<MAX;i++){
91+
for(j=0;j<MAX;j++){
92+
table[i][j] = -1;
93+
}
94+
}
95+
96+
}
97+
98+
int main(){
99+
int cases;
100+
scanf("%d", &cases);
101+
int i;
102+
for(i=0;i<cases;i++){
103+
int len1, len2;
104+
scanf("%d %d", &len1, &len2);
105+
char str1[len1+1],str2[len2+1];
106+
scanf("%s",str1);
107+
scanf("%s",str2);
108+
init();
109+
int me = lcs(0,0,str1,str2, len1, len2);
110+
printf("%d\n", me);
111+
}
112+
return 0;
57113
}

dynamic-programming/question33.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,6 @@ int main(){
8282
printf("answer is %d\n", fib(n));
8383

8484
return 0;
85-
}
85+
}
86+
87+

dynamic-programming/question34.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
3+
Find the max product sub array
4+
5+
METHOD:
6+
Here we traverse the array from both the sides, handling corner cases.
7+
Also the algorithm takes into account the number of negative numbers
8+
9+
This is not a DP question.
10+
11+
Another method listed at GFG
12+
http://www.geeksforgeeks.org/maximum-product-subarray/
13+
14+
15+
16+
*/
17+
18+
#include <stdio.h>
19+
#include <stdlib.h>
20+
#include <limits.h>
21+
22+
void maxProductSubArray(int *arr, int size){
23+
int i;
24+
int countNegatives = 0;
25+
for(i=0;i<size;i++){
26+
if(arr[i] < 0){
27+
countNegatives++;
28+
}
29+
}
30+
int lastCountNegativesValues = countNegatives;
31+
32+
int curr = 1, max = INT_MIN;
33+
34+
for(i=0;i<size;i++){
35+
curr = curr*arr[i];
36+
37+
if(curr > max){
38+
max = curr;
39+
}
40+
41+
if(arr[i] < 0){
42+
countNegatives--;
43+
}
44+
45+
if((curr < 0 && countNegatives <= 0) || (curr == 0)){
46+
curr = 1;
47+
}
48+
}
49+
50+
curr = 1;
51+
countNegatives = lastCountNegativesValues;
52+
53+
for(i=size-1;i>=0;i--){
54+
curr = curr*arr[i];
55+
if(curr > max){
56+
max = curr;
57+
}
58+
59+
if(arr[i] < 0){
60+
countNegatives--;
61+
}
62+
63+
if((curr < 0 && countNegatives <= 0) || (curr == 0)){
64+
curr = 1;
65+
}
66+
}
67+
68+
printf("%d\n", max);
69+
}
70+
71+
int main(){
72+
int cases;
73+
scanf("%d",&cases);
74+
int i;
75+
for(i=0;i<cases;i++){
76+
int size;
77+
scanf("%d",&size);
78+
int j;
79+
int arr[size];
80+
for(j=0;j<size;j++){
81+
scanf("%d",&arr[j]);
82+
}
83+
maxProductSubArray(arr, size);
84+
85+
}
86+
return 0;
87+
}

dynamic-programming/question8.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,63 @@ int main(){
6363
maxSumSubArr(arr, size);
6464
return 0;
6565
}
66+
67+
//this will handle negative cases as well because in that case the answer will be the max number in the
68+
//array. Also this can be done by simply comparing the negative value of curr with max and then seeing
69+
//is the assignment should be done and later assigning 0 to curr
70+
#include <stdio.h>
71+
#include <stdlib.h>
72+
73+
void maxSumSubArray(int *arr, int size){
74+
int i;
75+
int max = arr[0];
76+
int curr = arr[0];
77+
int end = 0;
78+
79+
if(curr < 0){
80+
curr = 0;
81+
}
82+
83+
for(i=1;i<size;i++){
84+
curr += arr[i];
85+
86+
if(max < curr){
87+
max = curr;
88+
end = i;
89+
}
90+
91+
if(curr < 0){
92+
curr = 0;
93+
}
94+
}
95+
96+
printf("max is %d\n", max);
97+
printf("end is %d\n", end);
98+
99+
int temp = end;
100+
int sum = 0, start;
101+
while(temp >=0){
102+
sum += arr[temp];
103+
if(sum == max){
104+
start = temp;
105+
break;
106+
}
107+
temp--;
108+
}
109+
110+
for(i=start;i<=end;i++){
111+
printf("%d ", arr[i]);
112+
}
113+
114+
}
115+
116+
int main(){
117+
118+
int arr[] = {-2,-3,-4,-1,-2,-1,-5,-3};
119+
120+
int size = sizeof(arr)/sizeof(arr[0]);
121+
122+
maxSumSubArray(arr, size);
123+
124+
return 0;
125+
}

test1.c

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,70 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3+
#include <limits.h>
34

4-
struct node{
5-
int data;
6-
struct node *left;
7-
struct node *right;
8-
}*root = NULL, *root2 = NULL;
5+
void maxProductSubArray(int *arr, int size){
6+
int i;
7+
int countNegatives = 0;
8+
for(i=0;i<size;i++){
9+
if(arr[i] < 0){
10+
countNegatives++;
11+
}
12+
}
13+
int lastCountNegativesValues = countNegatives;
914

10-
struct node *newNode(int data){
11-
struct node *temp = (struct node *)malloc(sizeof(struct node));
12-
temp->data = data;
13-
temp->left = temp->right = NULL;
14-
return temp;
15-
}
15+
int curr = 1, max = INT_MIN;
1616

17-
struct node *mirror(struct node *root, struct node *root2){
18-
19-
}
17+
for(i=0;i<size;i++){
18+
curr = curr*arr[i];
2019

21-
int main(){
20+
if(curr > max){
21+
max = curr;
22+
}
23+
24+
if(arr[i] < 0){
25+
countNegatives--;
26+
}
27+
28+
if((curr < 0 && countNegatives <= 0) || (curr == 0)){
29+
curr = 1;
30+
}
31+
}
32+
33+
curr = 1;
34+
countNegatives = lastCountNegativesValues;
2235

23-
root = newNode(10);
24-
root->left = newNode(20);
25-
root->left->left = newNode(30);
26-
root->left->right = newNode(40);
27-
root->right = newNode(50);
28-
root->right->left = newNode(50);
29-
root->right->right = newNode(50);
36+
for(i=size-1;i>=0;i--){
37+
curr = curr*arr[i];
38+
if(curr > max){
39+
max = curr;
40+
}
3041

31-
root2 = mirror(root, root2);
42+
if(arr[i] < 0){
43+
countNegatives--;
44+
}
45+
46+
if((curr < 0 && countNegatives <= 0) || (curr == 0)){
47+
curr = 1;
48+
}
49+
}
50+
51+
printf("%d\n", max);
52+
}
53+
54+
int main(){
55+
int cases;
56+
scanf("%d",&cases);
57+
int i;
58+
for(i=0;i<cases;i++){
59+
int size;
60+
scanf("%d",&size);
61+
int j;
62+
int arr[size];
63+
for(j=0;j<size;j++){
64+
scanf("%d",&arr[j]);
65+
}
66+
maxProductSubArray(arr, size);
3267

68+
}
3369
return 0;
3470
}

0 commit comments

Comments
 (0)