Skip to content

Commit 20cbee6

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

File tree

8 files changed

+218
-128
lines changed

8 files changed

+218
-128
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ only if there is one number that repeats odd number of times
2222
which has to be returned
2323
- Sometimes if the algo seems to be complicated move to a generalized format where the result is assumed to be N and you are solving it for some x by going from solution to problem, and then try to figure out the algo. (refer question 28.c for more clarification)
2424
- Circular array can be used to implement a queue. Here the increment is not done just by incrementing by 1, but is done by incrementing by 1 and taking mod with array size. Like this you keep rotating the array. Refer question 2 stacks and queue for more info
25+
- For questions involving subarrays:
26+
- Naive approach
27+
- Can maintain another data structure (may be a queue) sometimes to solve the algo
28+
- Can maintain a hash Table to solve the algo
29+
- Can maintain multiple variables to solve the algo
30+
- Can maintain two pointers to solve the algo
2531

2632
### For Linked Lists: (methods that can be applied)
2733
- 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

arrays/a.exe

621 Bytes
Binary file not shown.

arrays/question18.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ Time complexity: O(nk)
77
Space complexity: o(1)
88
99
METHOD2:
10+
In this question we maintain a queue using a double linked list and each time if an element in the
11+
queue from the rear is lesser than the next element we move the rear to the prev node and free
12+
the memory. And in each iteration front is printed. Also if the last element in now no longer
13+
a part of the queue as it is not in the window, it has to be removed from the queue.
1014
15+
Time complexity: O(n)
16+
Space complexity: O(k) //queue will be of size k only in worst case
1117
1218
*/
19+
1320
//METHOD1
1421
// #include <stdio.h>
1522
// #include <stdlib.h>
@@ -118,6 +125,6 @@ void printMaxInK(int *arr, int size, int k){
118125
int main(){
119126
int arr[] = {8, 5, 10, 7, 9, 4, 15, 12, 90, 13};
120127
int size = sizeof(arr)/sizeof(arr[0]);
121-
int k = 3;
128+
int k = 4;
122129
printMaxInK(arr, size, k);
123130
}

arrays/question20.c

Lines changed: 84 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Time complexity: O(n^2)
99
Space complexity: O(1)
1010
1111
METHOD2 (only for positive numbers)
12-
Taking two pointers and scanning. Moving start index if sum is more, more end index if sum is less
12+
Taking two pointers and scanning. Moving start index if sum is more, move end index if sum is less
1313
Array in worst case will be traversed twice
1414
Time complexity: O(n)
1515
Space complexity: O(1)
@@ -23,74 +23,97 @@ Space complexity: O(n)
2323
*/
2424

2525
//METHOD1
26-
#include <stdio.h>
27-
#include <stdlib.h>
26+
// #include <stdio.h>
27+
// #include <stdlib.h>
2828

29-
void printSubArray(int *arr,int start, int end){
30-
for(int i=start; i<=end;i++){
31-
printf("%d", arr[i]);
32-
}
33-
}
29+
// void printSubArray(int *arr,int start, int end){
30+
// for(int i=start; i<=end;i++){
31+
// printf("%d", arr[i]);
32+
// }
33+
// }
3434

35-
int main(){
36-
int a[] = {5,4,6,7,9,8,3,1,2};
37-
int length = sizeof(a)/sizeof(a[0]);
38-
int start, end, sum;
39-
for(int i=0; i<length; i++){
40-
sum = 0;
41-
start = a[i];
42-
sum += start;
43-
for(int j=i+1; j<length; j++){
44-
end = a[j];
45-
sum += end;
46-
if(sum == 21){
47-
printSubArray(a,i,j);
48-
}
49-
}
50-
}
51-
}
35+
// int main(){
36+
// int a[] = {5,4,6,7,9,8,3,1,2};
37+
// int length = sizeof(a)/sizeof(a[0]);
38+
// int start, end, sum;
39+
// for(int i=0; i<length; i++){
40+
// sum = 0;
41+
// start = a[i];
42+
// sum += start;
43+
// for(int j=i+1; j<length; j++){
44+
// end = a[j];
45+
// sum += end;
46+
// if(sum == 21){
47+
// printSubArray(a,i,j);
48+
// }
49+
// }
50+
// }
51+
// }
5252

5353
//METHOD2
54+
// #include <stdio.h>
55+
// #include <stdlib.h>
56+
57+
// void printSubArray(int *arr,int start, int end){
58+
// for(int i=start; i<=end;i++){
59+
// printf("%d", arr[i]);
60+
// }
61+
// }
62+
63+
// int main(){
64+
// int a[] = {5,4,6,7,9,8,3,1,2};
65+
// int length = sizeof(a)/sizeof(a[0]);
66+
// int reqSum = 27;
67+
// int leftIndex = 0, rightIndex = 0, sum=0;
68+
69+
// if(a[leftIndex] == reqSum){
70+
// printf("%d\n", a[leftIndex]);
71+
// return 1;
72+
// }else{
73+
// rightIndex++;
74+
// sum = a[leftIndex] + a[rightIndex];
75+
// }
76+
77+
// while(rightIndex<=length){
78+
79+
// if(sum == reqSum){
80+
// printSubArray(a, leftIndex, rightIndex);
81+
// break;
82+
// }
83+
// if(sum < reqSum){
84+
// rightIndex++;
85+
// sum += a[rightIndex];
86+
// }else{
87+
// int temp = leftIndex;
88+
// leftIndex++;
89+
// sum = sum - a[temp];
90+
// }
91+
// }
92+
// if(sum != reqSum){
93+
// printf("sub array could not be found with sum given\n");
94+
// }
95+
96+
// }
97+
//METHOD3
5498
#include <stdio.h>
5599
#include <stdlib.h>
56100

57-
void printSubArray(int *arr,int start, int end){
58-
for(int i=start; i<=end;i++){
59-
printf("%d", arr[i]);
60-
}
101+
struct hash{
102+
int data;
103+
};
104+
105+
void findSubArray(int *a, int size, int sum){
106+
int sumSoFar = 0;
107+
struct hash *arr = (struct hash *)calloc(100, sizeof(struct hash));
108+
109+
//linear probing implementation to be done later
61110
}
62111

63112
int main(){
64-
int a[] = {5,4,6,7,9,8,3,1,2};
65-
int length = sizeof(a)/sizeof(a[0]);
66-
int reqSum = 27;
67-
int leftIndex = 0, rightIndex = 0, sum=0;
68-
69-
if(a[leftIndex] == reqSum){
70-
printf("%d\n", a[leftIndex]);
71-
return 1;
72-
}else{
73-
rightIndex++;
74-
sum = a[leftIndex] + a[rightIndex];
75-
}
76-
77-
while(rightIndex<=length){
78-
79-
if(sum == reqSum){
80-
printSubArray(a, leftIndex, rightIndex);
81-
break;
82-
}
83-
if(sum < reqSum){
84-
rightIndex++;
85-
sum += a[rightIndex];
86-
}else{
87-
int temp = leftIndex;
88-
leftIndex++;
89-
sum = sum - a[temp];
90-
}
91-
}
92-
if(sum != reqSum){
93-
printf("sub array could not be found with sum given\n");
94-
}
113+
int a[] = {8,5,-2,3,4,-5,7};
114+
int size = sizeof(a)/sizeof(a[0]);
115+
int reqSum = 10;
116+
findSubArray(arr,size, reqSum);
95117

96-
}
118+
return 0;
119+
}

arrays/question21.c

Lines changed: 65 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
2-
Consider and array which contains only 0's and 1's. Find largest sub array which contains only 0's and 1's
2+
Consider and array which contains only 0's and 1's.
3+
Find largest sub array which contains equal number of 0's and 1's
34
45
METHOD1:
56
finding all sub arrays possible and maintaining an array which counts number of 1s and another which counts number
@@ -19,69 +20,69 @@ Space complexity: O(n)
1920
*/
2021

2122
// METHOD1
22-
#include <stdio.h>
23-
#include <stdlib.h>
24-
25-
void printSubArray(int arr[],int start, int end){
26-
for(int i=start; i<=end; i++){
27-
printf("%d",arr[i]);
28-
}
29-
}
30-
31-
int main(){
32-
33-
int a[] = {1,1,1,1};
34-
int length = sizeof(a)/sizeof(a[0]);
35-
int start, end;
36-
37-
int count_one[length], count_zero[length];
38-
int ones = 0, zeroes = 0, max_zeroes = 0, max_ones = 0, curr_max_one = 0;
39-
int leftIndex, rightIndex, noSubArray;
40-
//for counting 1s in the end
41-
for(int i=0; i<length;i++){
42-
if(a[i] == 1){
43-
ones++;
44-
}
45-
count_one[i] = ones;
46-
}
47-
//for counting zeroes in the end
48-
for(int i=0; i<length;i++){
49-
if(a[i] == 0){
50-
zeroes++;
51-
}
52-
count_zero[i] = zeroes;
53-
}
54-
55-
for(int i=0; i<length;i++){
56-
start = i;
57-
for(int j=i+1; j<length;j++){
58-
end = j;
59-
if(start == 0){
60-
max_ones = count_one[end];
61-
max_zeroes = count_zero[end];
62-
}else{
63-
max_ones = count_one[end] - count_one[i-1];
64-
max_zeroes = count_zero[end] - count_zero[i-1];
65-
}
66-
if(max_ones == max_zeroes){
67-
if(curr_max_one < max_ones){
68-
curr_max_one = max_ones;
69-
leftIndex = start;
70-
rightIndex = end;
71-
noSubArray = 0;
72-
}else{
73-
noSubArray = 1;
74-
}
75-
}
76-
}
77-
}
78-
if(noSubArray){
79-
printf("no sub array found\n");
80-
}else{
81-
printSubArray(a,leftIndex,rightIndex);
82-
}
23+
// #include <stdio.h>
24+
// #include <stdlib.h>
25+
26+
// void printSubArray(int arr[],int start, int end){
27+
// for(int i=start; i<=end; i++){
28+
// printf("%d",arr[i]);
29+
// }
30+
// }
31+
32+
// int main(){
33+
34+
// int a[] = {1,1,1,1};
35+
// int length = sizeof(a)/sizeof(a[0]);
36+
// int start, end;
37+
38+
// int count_one[length], count_zero[length];
39+
// int ones = 0, zeroes = 0, max_zeroes = 0, max_ones = 0, curr_max_one = 0;
40+
// int leftIndex, rightIndex, noSubArray;
41+
// //for counting 1s in the end
42+
// for(int i=0; i<length;i++){
43+
// if(a[i] == 1){
44+
// ones++;
45+
// }
46+
// count_one[i] = ones;
47+
// }
48+
// //for counting zeroes in the end
49+
// for(int i=0; i<length;i++){
50+
// if(a[i] == 0){
51+
// zeroes++;
52+
// }
53+
// count_zero[i] = zeroes;
54+
// }
55+
56+
// for(int i=0; i<length;i++){
57+
// start = i;
58+
// for(int j=i+1; j<length;j++){
59+
// end = j;
60+
// if(start == 0){
61+
// max_ones = count_one[end];
62+
// max_zeroes = count_zero[end];
63+
// }else{
64+
// max_ones = count_one[end] - count_one[i-1];
65+
// max_zeroes = count_zero[end] - count_zero[i-1];
66+
// }
67+
// if(max_ones == max_zeroes){
68+
// if(curr_max_one < max_ones){
69+
// curr_max_one = max_ones;
70+
// leftIndex = start;
71+
// rightIndex = end;
72+
// noSubArray = 0;
73+
// }else{
74+
// noSubArray = 1;
75+
// }
76+
// }
77+
// }
78+
// }
79+
// if(noSubArray){
80+
// printf("no sub array found\n");
81+
// }else{
82+
// printSubArray(a,leftIndex,rightIndex);
83+
// }
8384

84-
}
85+
// }
8586

8687

8788
//METHOD2
@@ -106,7 +107,7 @@ void printLongestSubArray(int arr[],int size, int starts){
106107

107108
int main(){
108109

109-
int a[] = {0,0,0,1,1,1};
110+
int a[] = {0,1,0,1,1,1};
110111
int size = sizeof(a)/sizeof(a[0]);
111112

112113
int sum_arr[size];

dynamic-programming/a.exe

64 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)