Skip to content

Commit 6ccdc4e

Browse files
committed
new miscellaneous questions added
1 parent 8249e5b commit 6ccdc4e

Some content is hidden

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

75 files changed

+5191
-92
lines changed

Diff for: README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ use KMP
259259
- [question22.c](/general/question22.c)
260260
- [question23.c](/general/question23.c)
261261
- [question24.c](/general/question24.c)
262+
- [Find GCD and LCM of two numbers](/general/question25.c)
262263

263264
### Arrays
264265

@@ -448,7 +449,7 @@ use KMP
448449
- [Longest non-overlapping repeating sub string](/dynamic-programming/question23.c)
449450
- [Given two strings X and Y, find the minimum cost to make two strings equal using only delete operations. Cost to delete character in X is S and in Y is P](/dynamic-programming/question24.c)
450451
- [Count the number of times string occured as the subsequence of the other string](/dynamic-programming/question25.c)
451-
- [Given an amount and some coints, find out in how many ways can this amount be formed](/dynamic-programming/question26.c)
452+
- [Given an amount and some coins, find out in how many ways can this amount be formed](/dynamic-programming/question26.c)
452453
- [Given a 2xn board and tiles of size 2x1, count the number of ways to fill the board using 2x1 tiles](/dynamic-programming/question27.c)
453454
- [Given a cost matrix mxn having a cost at each cell. Find the min cost that it will take to reach cell (m-1,n-1) from top left corner cell (0,0) if the only allowed directions to move are right, down and diagnal down](/dynamic-programming/question28.c)
454455
- [Given a string, find out if it becomes a palindrome or not after doing atmost k deletions](/dynamic-programming/question29.c)
@@ -468,7 +469,10 @@ use KMP
468469
- [Given a 2D boolean matrix, find the number of islands](/graphs/question6.c)
469470
- [Check whether a given graph is biparite or not](/graphs/question7.c)
470471
- [Detect a cycle in a directed graph](/graphs/question8.c)
471-
472+
- [Kosaraju algorithm to find SCCs](/graphs/question9.c)
473+
- [Find if the given directed graph is eulerian](/graphs/question10.c)
474+
- [Find if the given undirected graph is eulerian](/graphs/question11.c)
475+
- [Dijkstra algorithm](/graphs/question12.c)
472476

473477
### Pattern Matching
474478

@@ -488,6 +492,11 @@ use KMP
488492
- [Test whether given two strings are isomorphic or not](/misc/question1.c)
489493
- [Find the length of the longest suffix which is also a prefix](/misc/question2.c)
490494

495+
### Bit Manipulation
496+
497+
- [Find set bits in an integer](/bit-manipulation/question1.c)
498+
- [Find if a number is a power of 2](/bit-manipulation/question2.c)
499+
491500
## Some important concepts to solve algos better
492501

493502
- For extreme values refer to limits.h constants given by C
@@ -580,6 +589,7 @@ Even if there are repetitions, they are going to be same.
580589
if they dont have edge it is infinity.
581590
- Travelling salesman problem time complexity O(n^2 ^ 2^n)
582591
- Ugly number is a one which can be written as a product of 2,3 or 5 or a combination of these numbers. 1 is also considered as ugly number as it is an exception.
592+
- Whenever highest or lowest power of two is to be found, use log operations
583593

584594

585595
# C Programming - Topic1: Introduction

Diff for: back-tracking/question3.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ void init(int n, int arr[n][n]){
5858
}
5959
}
6060

61+
62+
6163
int main(){
6264
int n;
6365
printf("enter the value of n\n");
@@ -67,7 +69,7 @@ int main(){
6769

6870
init(n,arr);
6971

70-
findNQueen()
72+
findNQueen(n, arr);
7173

7274
return 0;
7375
}

Diff for: bit-manipulation/question1.c

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Count the number of set bits in an integer
3+
4+
If number is given, simply divide it by 2 in a loop and incremenet count if rem is 1
5+
TC: logn
6+
*/
7+
8+
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
12+
int setBits(int num){
13+
int rem;
14+
15+
int count = 0;
16+
while(num){
17+
18+
19+
rem = num%2;
20+
if(rem){
21+
count++;
22+
}
23+
24+
num = num/2;
25+
26+
27+
28+
}
29+
30+
return count;
31+
}
32+
33+
34+
int main(){
35+
36+
int n;
37+
printf("enter the number\n");
38+
scanf("%d",&n);
39+
40+
printf("num set bits are: %d\n", setBits(n));
41+
42+
}

Diff for: bit-manipulation/question2.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
find if a number if power of 2
3+
4+
If we subtract a power of 2 numbers by 1 then all unset bits after the only set bit become set;
5+
and the set bit become unset.
6+
7+
So, if a number n is a power of 2 then bitwise & of n and n-1 will be zero.
8+
We can say n is a power of 2 or not based on value of n&(n-1).
9+
The expression n&(n-1) will not work when n is 0.
10+
To handle this case also, our expression will become n && (!n&(n-1))
11+
*/
12+
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
16+
int isPowerOfTwo(int n){
17+
return n && !(n & (n-1));
18+
}
19+
20+
int main(){
21+
int n;
22+
printf("enter the number\n");
23+
scanf("%d",&n);
24+
25+
if(isPowerOfTwo(n)){
26+
printf("number is pwr\n");
27+
}else{
28+
printf("no.\n");
29+
}
30+
return 0;
31+
}
32+

Diff for: complex-datastructures/making-a-graph.c

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct adjListNode{
5+
int data;
6+
struct adjListNode *next;
7+
};
8+
9+
struct adjList{
10+
struct adjListNode *head;
11+
};
12+
13+
struct graph{
14+
int vertices;
15+
struct adjList *arr;
16+
};
17+
18+
struct adjListNode *newNode(int data){
19+
struct adjListNode *temp = (struct adjListNode *)malloc(sizeof(struct adjListNode));
20+
temp->data = data;
21+
temp->next = NULL;
22+
return temp;
23+
}
24+
25+
void addEdge(struct graph *newGraph,int start, int end){
26+
struct adjListNode *temp = newNode(start);
27+
28+
if(newGraph->arr[end].head){
29+
temp->next = newGraph->arr[end].head;
30+
newGraph->arr[end].head = temp;
31+
32+
}else{
33+
newGraph->arr[end].head = temp;
34+
}
35+
36+
temp = newNode(end);
37+
38+
if(newGraph->arr[start].head){
39+
temp->next = newGraph->arr[start].head;
40+
newGraph->arr[start].head = temp;
41+
}else{
42+
newGraph->arr[start].head = temp;
43+
}
44+
}
45+
46+
void display(struct graph *newGraph){
47+
int i;
48+
struct adjListNode *temp;
49+
for(i=0;i<newGraph->vertices;i++){
50+
temp = newGraph->arr[i].head;
51+
printf("%d --> ", i);
52+
while(temp){
53+
printf("%d ", temp->data);
54+
temp = temp->next;
55+
}
56+
printf("\n");
57+
}
58+
}
59+
60+
int main(){
61+
62+
struct graph *newGraph = (struct graph *)malloc(sizeof(struct graph));
63+
int vertices = 4;
64+
newGraph->vertices = 4;
65+
66+
newGraph->arr = (struct adjList *)calloc(vertices,sizeof(struct adjList));
67+
68+
addEdge(newGraph, 0, 1);
69+
addEdge(newGraph, 0, 2);
70+
addEdge(newGraph, 1, 3);
71+
addEdge(newGraph, 2, 3);
72+
73+
display(newGraph);
74+
75+
return 0;
76+
}

Diff for: complex-datastructures/making-a-tree.c

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 *newNode(int data){
11+
struct node *temp = (struct node *)malloc(sizeof(struct node));
12+
temp->data = data;
13+
temp->left = NULL;
14+
temp->right = NULL;
15+
return temp;
16+
}
17+
18+
void inorder(struct node *root){
19+
if(root){
20+
inorder(root->left);
21+
printf("%d ", root->data);
22+
inorder(root->right);
23+
}
24+
}
25+
26+
void displayTree(struct node *root){
27+
struct node *temp = root;
28+
inorder(temp);
29+
}
30+
31+
int main(){
32+
33+
struct node *root = newNode(10);
34+
35+
root->left = newNode(20);
36+
root->right = newNode(30);
37+
root->left->left = newNode(40);
38+
root->left->right = newNode(50);
39+
40+
root->right->left = newNode(60);
41+
root->right->right = newNode(70);
42+
43+
displayTree(root);
44+
45+
return 0;
46+
}

Diff for: divide-and-conquer/question3.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ It also does not handle fractional power
2727

2828
/*
2929
Here is a better function that runs faster and handle all the cases
30-
In this case each time exp value is converted to half and base value is doubled
30+
In this case each time exp value is converted to half and base value is squared
3131
*/
3232
#include <stdio.h>
3333
#include <stdlib.h>

Diff for: dynamic-programming/question13.c

+61-30
Original file line numberDiff line numberDiff line change
@@ -41,49 +41,80 @@ Therefore answer here is 4. (longest possible subsequence)
4141
Time complexity: Since each element will be compared with its all prev elements in the newly formed
4242
array O(n^2)
4343
Space complexity: O(n) //table size is reduced here as compared to method1
44+
45+
METHOD3:
46+
Just like a deck of cards
47+
http://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/
48+
49+
Time complexity: O(nlogn)
50+
Space complexity: O(1)
51+
4452
*/
4553

4654
//METHOD2
55+
// #include <stdio.h>
56+
// #include <stdlib.h>
57+
58+
// void longestIncreasingSub(int *arr, int size){
59+
// int sol[size];
60+
// int i,j;
61+
// sol[0] = 1; //base solution cannot be greater than 1.
62+
// int maxLength = 1;
63+
// int start = 0, end = 0;
64+
// int overallMax = 1;
65+
// for(i=1;i<size;i++){
66+
// int currentMax = 1;
67+
// for(j=i-1;j>=0;j--){
68+
// int key = 1;
69+
// if(arr[i] > arr[j]){
70+
// key = key + sol[j];
71+
// if(currentMax < key){
72+
// currentMax = key;
73+
// }
74+
// }
75+
// sol[i] = currentMax;
76+
// }
77+
// if(overallMax < sol[i]){
78+
// overallMax = sol[i];
79+
// end = i;
80+
// }
81+
// }
82+
83+
// printf("total length of subsequence is: %d\n", overallMax);
84+
// printf("subsequence is:\n");
85+
86+
// //for printing maybe a struct can be taken and element index for which current max
87+
// //is computed is noted down. That will be the answer Iterate like this backwards for all
88+
89+
// }
90+
91+
// int main(){
92+
// int arr[] = {2,3,1,5,12,10,11};
93+
// int size = sizeof(arr)/sizeof(arr[0]);
94+
95+
// longestIncreasingSub(arr,size);
96+
// return 0;
97+
// }
98+
4799
#include <stdio.h>
48100
#include <stdlib.h>
49101

50-
void longestIncreasingSub(int *arr, int size){
51-
int sol[size];
52-
int i,j;
53-
sol[0] = 1; //base solution cannot be greater than 1.
54-
int maxLength = 1;
55-
int start = 0, end = 0;
56-
int overallMax = 1;
57-
for(i=1;i<size;i++){
58-
int currentMax = 1;
59-
for(j=i-1;j>=0;j--){
60-
int key = 1;
61-
if(arr[i] > arr[j]){
62-
key = key + sol[j];
63-
if(currentMax < key){
64-
currentMax = key;
65-
}
66-
}
67-
sol[i] = currentMax;
68-
}
69-
if(overallMax < sol[i]){
70-
overallMax = sol[i];
71-
end = i;
72-
}
73-
}
102+
int longestIncreasingSub(int *arr, int size){
103+
int aux[size];
74104

75-
printf("total length of subsequence is: %d\n", overallMax);
76-
printf("subsequence is:\n");
105+
int i;
106+
for(i=0;i<size;i++){
107+
aux[i] = -1;
108+
}
77109

78-
//for printing maybe a struct can be taken and element index for which current max
79-
//is computed is noted down. That will be the answer Iterate like this backwards for all
110+
80111

81112
}
82113

83114
int main(){
84115
int arr[] = {2,3,1,5,12,10,11};
85-
int size = sizeof(arr)/sizeof(arr[0]);
116+
int size =sizeof(arr)/sizeof(arr[0]);
86117

87-
longestIncreasingSub(arr,size);
118+
longestIncreasingSub(arr, size);
88119
return 0;
89120
}

0 commit comments

Comments
 (0)