Skip to content

Commit 6ae916a

Browse files
committed
new questions added
1 parent c57db67 commit 6ae916a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+3620
-221
lines changed

README.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,15 @@ use KMP
400400
- [Make a program to implement huffman encoding](/greedy/question2.c)
401401
- [Make a program to sequence given jobs with deadlines to maximize profits](/greedy/question3.c)
402402
- [Optimal merge patterns](/greedy/question4.c)
403-
- [Program for PRIMS algorithm](/greedy/question5.c)
403+
- [Program for PRIMS algorithm without min heap](/greedy/question5.c)
404404
- [Program for KRUSKALS algorithm](/greedy/question6.c)
405405
- [Program for DIJKSTRA algorithm](/greedy/question7.c)
406406
- [Program to implement a simple graph](/greedy/question8.c)
407407
- [Consider n-ropes with different length. Find algo to tie up all the rope into a single rope with min cost](/greedy/question9.c)
408408
- [Find max intervals from given intervals such that none of them are overlapping](/greedy/question10.c)
409409
- [Number of railway platforms](/greedy/question11.c)
410410
- [Rearrange the string such that same characters are d distance away](/greedy/question12.c)
411+
- [Program for PRIMS algorithm using min heap](/greedy/question13.c)
411412

412413
### Divide and conquer
413414

@@ -458,6 +459,9 @@ use KMP
458459
- [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)
459460
- [Fibonacci series](/dynamic-programming/question33.c)
460461
- [Max product sub array](/dynamic-programming/question34.c)
462+
- [Finding the maximum square sub-matrix with all equal elements](/dynamic-programming/question35.c)
463+
- [All pair shortest path algorithm (Floyd Warshall)](/dynamic-programming/question36.c)
464+
- [Collect max coins from the grid in two traversals](/dynamic-programming/question37.c)
461465

462466
### Graphs
463467

@@ -472,7 +476,13 @@ use KMP
472476
- [Kosaraju algorithm to find SCCs](/graphs/question9.c)
473477
- [Find if the given directed graph is eulerian](/graphs/question10.c)
474478
- [Find if the given undirected graph is eulerian](/graphs/question11.c)
475-
- [Dijkstra algorithm](/graphs/question12.c)
479+
- [Dijkstra algorithm for matrix](/graphs/question12.c)
480+
- [Dijkstra algorithm for adjacency list](/graphs/question13.c)
481+
- [Articulation points and bridges in a graph](/graphs/question13.c)
482+
- [Print all jumping numbers smaller than or equal to a given value](/graphs/question15.c)
483+
- [Shortest path in directed acyclic graph](/graphs/question16.c)
484+
- [Longest path in directed acyclic graph](/graphs/question17.c)
485+
- [Hamiltonian cycle in a graph](/graphs/question18.c)
476486

477487
### Pattern Matching
478488

@@ -491,12 +501,19 @@ use KMP
491501

492502
- [Test whether given two strings are isomorphic or not](/misc/question1.c)
493503
- [Find the length of the longest suffix which is also a prefix](/misc/question2.c)
504+
- [Sort a stack](/misc/question66.c)
505+
- [Given two strings a and b print whether they contain any common subsequence (non empty) or not.](/misc/question67.c)
506+
494507

495508
### Bit Manipulation
496509

497510
- [Find set bits in an integer](/bit-manipulation/question1.c)
498511
- [Find if a number is a power of 2](/bit-manipulation/question2.c)
499512

513+
514+
### Trie
515+
516+
500517
## Some important concepts to solve algos better
501518

502519
- For extreme values refer to limits.h constants given by C

arrays/question3.c

+59-48
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,69 @@ Therefore: O(nlogn)
99
1010
*/
1111
#include <stdio.h>
12-
#include <math.h>
13-
14-
void merge(int arr[],int p, int q, int r){
15-
long unsigned int f = INFINITY;
16-
int n1= q-p+1;
17-
int n2 = r-q;
18-
int left[n1+1], right[n2+1];
19-
int i,j,k;
20-
21-
for(i=0;i<n1;i++){
22-
left[i]=arr[p+i];
23-
printf("%d ", left[i]);
24-
}
25-
26-
for(j=0;j<n2;j++){
27-
right[j]=arr[q+j+1];
28-
printf("%d ", right[j]);
29-
}
30-
31-
left[n1] = right[n2] = f;
32-
i=j=0;
33-
for(k=p;k<(r+1);k++){
34-
if(left[i] < right[j]){
35-
arr[k] = left[i];
36-
i++;
37-
}else{
38-
arr[k] = right[j];
39-
j++;
12+
#include <stdlib.h>
13+
#include <limits.h>
14+
15+
16+
void merge(int *arr, int start, int mid, int end) {
17+
18+
int len1 = mid - start + 2;
19+
int len2 = end - mid + 1;
20+
21+
int left[len1], right[len2];
22+
23+
int i;
24+
for(i=0; i<len1-1; i++) {
25+
left[i] = arr[start + i];
26+
}
27+
28+
for(i=0; i<len2-1; i++) {
29+
right[i] = arr[mid + i + 1];
4030
}
41-
}
42-
43-
for(int z=0; z<(r+1);z++){
44-
printf("%d ", arr[z]);
45-
}
46-
31+
32+
left[len1-1] = INT_MAX;
33+
right[len2-1] = INT_MAX;
34+
35+
int k;
36+
int j;
37+
i=0,j=0;
38+
for(k=start; k<=end; k++) {
39+
if(left[i] < right[j]) {
40+
arr[k] = left[i];
41+
i++;
42+
}else{
43+
arr[k] = right[j];
44+
j++;
45+
}
46+
}
47+
4748
}
4849

49-
void merge_sort(int arr[], int p, int r){
50-
51-
int q;
52-
if(p<r){
53-
q = (p+r)/2;
54-
merge_sort(arr,p,q);
55-
merge_sort(arr,q+1,r);
56-
merge(arr,p,q,r);
57-
}
50+
void mergeSort(int *arr, int start, int end) {
51+
int mid;
52+
if(start < end) {
53+
mid = (start + end)/2;
54+
mergeSort(arr, start, mid);
55+
mergeSort(arr, mid + 1, end);
56+
57+
merge(arr, start, mid, end);
58+
}
5859
}
5960

60-
int main(){
61-
62-
int arr[] = {9,6,5,0,8,2};
63-
int length = sizeof(arr)/sizeof(arr[0]);
64-
merge_sort(arr,0,length-1);
61+
void display(int *arr, int size) {
62+
int i;
63+
for(i=0;i<size;i++) {
64+
printf("%d ", arr[i]);
65+
}
66+
printf("\n");
67+
}
68+
69+
int main() {
70+
int arr[] = {9,6,5,0,8,2};
71+
int size = sizeof(arr)/sizeof(arr[0]);
72+
73+
mergeSort(arr,0,size-1);
6574

75+
display(arr, size);
76+
return 0;
6677
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct node{
5+
int data;
6+
struct node *left;
7+
struct node *right;
8+
};
9+
10+
struct node *root;
11+
12+
struct node *newNode(int data){
13+
struct node *temp = (struct node *)malloc(sizeof(struct node));
14+
temp->data = data;
15+
temp->left = NULL;
16+
temp->right = NULL;
17+
return temp;
18+
}
19+
20+
void search(struct node *root, struct node *temp){
21+
if(temp->data < root->data && root->left){
22+
search(root->left, temp);
23+
}else if(temp->data < root->data && !root->left){
24+
root->left = temp;
25+
}else if(temp->data > root->data && root->right){
26+
search(root->right, temp);
27+
}else if(temp->data > root->data && !root->right){
28+
root->right = temp;
29+
}
30+
}
31+
32+
int findMax(int a, int b){
33+
return a > b ? a : b;
34+
}
35+
36+
void addNode(int data){
37+
struct node *temp = newNode(data);
38+
if(root){
39+
search(root, temp);
40+
}else{
41+
root = temp;
42+
}
43+
}
44+
45+
void inorder(struct node *root){
46+
if(root){
47+
inorder(root->left);
48+
printf("%d ",root->data);
49+
inorder(root->right);
50+
}
51+
}
52+
53+
int totalNodes(struct node *root){
54+
int count = 0;
55+
if(root){
56+
count = count + 1;
57+
count += totalNodes(root->left);
58+
count += totalNodes(root->right);
59+
}
60+
return count;
61+
}
62+
63+
int numLeaves(struct node *root){
64+
if(!root){
65+
return 0;
66+
}
67+
if(!root->left && !root->right){
68+
return 1;
69+
}
70+
int left = numLeaves(root->left);
71+
int right = numLeaves(root->right);
72+
73+
return left + right;
74+
}
75+
76+
int fullNodes(struct node *root){
77+
if(!root){
78+
return 0;
79+
}
80+
if(root->left && root->right){
81+
return 1;
82+
}
83+
int left = fullNodes(root->left);
84+
int right = fullNodes(root->right);
85+
return left + right;
86+
}
87+
88+
int height(struct node *root){
89+
if(!root){
90+
return 0;
91+
}
92+
93+
int left = height(root->left);
94+
int right = height(root->right);
95+
96+
return 1 + findMax(left, right);
97+
}
98+
99+
int searchMin(struct node *root){
100+
if(!root){
101+
return 0;
102+
}
103+
while(root->left){
104+
root = root->left;
105+
}
106+
return root->data;
107+
}
108+
109+
int searchMax(struct node *root){
110+
if(!root){
111+
return 0;
112+
}
113+
while(root->right){
114+
root = root->right;
115+
}
116+
return root->data;
117+
}
118+
119+
int isComplete(struct node *root){
120+
if(!root){
121+
return 0;
122+
}
123+
if((!root->left && !root->right) || (root->left && root->right)){
124+
return 1;
125+
}
126+
if((!root->left && root->right) || (root->left && !root->right) ){
127+
return 0;
128+
}
129+
int left = isComplete(root->left);
130+
int right = isComplete(root->right);
131+
132+
return left && right;
133+
}
134+
135+
struct node *getInorderSuccessor(struct node *root){
136+
if(root){
137+
while(root->left){
138+
root = root->left;
139+
}
140+
return root;
141+
}
142+
return NULL;
143+
}
144+
145+
struct node *findAndDelete(struct node *root, int data){
146+
147+
if(!root){
148+
return NULL;
149+
}
150+
if(data < root->data){
151+
root->left = findAndDelete(root->left, data);
152+
}else if(data > root->data) {
153+
root->right = findAndDelete(root->right, data);
154+
}else {
155+
156+
//if node has only one child or not child
157+
if(!root->left){
158+
struct node *temp = root->right;
159+
free(root);
160+
return temp;
161+
}
162+
if(!root->right){
163+
struct node *temp = root->left;
164+
free(root);
165+
return temp;
166+
}
167+
168+
//if it has two children
169+
struct node *temp = getInorderSuccessor(root->right);
170+
171+
root->data = temp->data;
172+
173+
root->right = findAndDelete(root->right, temp->data);
174+
175+
}
176+
return root;
177+
178+
}
179+
180+
int main(){
181+
182+
int arr[] = {40,11,18,12,19,21,4,8};
183+
int i;
184+
int size =sizeof(arr)/sizeof(arr[0]);
185+
for(i=0;i<size;i++) {
186+
addNode(arr[i]);
187+
}
188+
printf("inorder is...\n");
189+
inorder(root);
190+
191+
printf("\n no of nodes are: %d\n", totalNodes(root));
192+
printf("\n no of leaves are: %d\n", numLeaves(root));
193+
printf("\n no of non-leaves are: %d\n", totalNodes(root) - numLeaves(root));
194+
printf("\n no of full-nodes are: %d\n", fullNodes(root));
195+
printf("\n height is: %d\n", height(root));
196+
printf("\n min is: %d\n", searchMin(root));
197+
printf("\n max is: %d\n", searchMax(root));
198+
printf("\n tree is complete: %d\n", isComplete(root));
199+
200+
//delete a node
201+
// printf("root of data is %d\n", root->data);
202+
root = findAndDelete(root, 11);
203+
printf("\ninorder is...\n");
204+
inorder(root);
205+
206+
root = findAndDelete(root, 19);
207+
printf("\ninorder is...\n");
208+
inorder(root);
209+
210+
root = findAndDelete(root, 40);
211+
printf("\ninorder is...\n");
212+
inorder(root);
213+
214+
return 0;
215+
}

0 commit comments

Comments
 (0)