Skip to content

Commit 399728a

Browse files
committed
new question added
1 parent 3840536 commit 399728a

File tree

5 files changed

+254
-1
lines changed

5 files changed

+254
-1
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ value to be at the top of the tree with min edge length (or path to be traversed
124124
max edge or path to be traversed to minimize work.
125125
- Min cost spanning tree and shortest path problems are two different things. In shortest path a source will be given and we will have to reach the target in the shortest path possible given weight of each edge. In case of min cost spanning tree we have to construct a graph with min weight possible connecting ALL the nodes. Therefore priority of the algo varies
126126
- In greedy method, to min or max something (different methods), we can be greedy about something to solve the algo, and bring in cases which can defy out method. One of them will work.
127+
- Hash tables and mix and match of data structures like min and max heaps can also be used in solving greedy algos
128+
129+
### For divide and conquer
130+
131+
- In repeating elements with fixed size and ordered array we can use binary search. Also linear search can be
132+
used where if an element should be repeating a given number of times, you can check its value at i and then
133+
i+given number of times to see if thats true or not.
134+
127135

128136
# Topic0: Programming Questions
129137

@@ -187,6 +195,7 @@ max edge or path to be traversed to minimize work.
187195
- [Given n non negative integers representing an elevation map where width of each bar is 1. Find the amt. of water that is trapped in between these bars after raining](/arrays/question26.c)
188196
- [Given an unsorted array of +ve integers, find the number of triangles that can be formed with three different elements as three sides of the triangle](/arrays/question27.c)
189197
- [Given an array, find the smallest number that is not possible with the sum of numbers from the array](/arrays/question28.c)
198+
- [Write a program to do binary search in an array](/arrays/question29.c)
190199

191200
### Linked List
192201

@@ -301,6 +310,11 @@ max edge or path to be traversed to minimize work.
301310
- [Consider n-ropes with different length. Find algo to tie up all the rope into a single rope with min cost](/greedy/question9.c)
302311
- [Find max intervals from given intervals such that none of them are overlapping](/greedy/question10.c)
303312
- [Number of railway platforms](/greedy/question11.c)
313+
- [Rearrange the string such that same characters are d distance away](/greedy/question12.c)
314+
315+
### Divide and conquer
316+
317+
- [Find a majority element using linear search that occurs more than n/2 times](/divide-and-conquer/question1.c)
304318

305319
## Some important concepts to solve algos better
306320

arrays/question29.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Program to implement binary search in an array
3+
*/
4+
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
int main(){
9+
int arr[] = {};
10+
return 0;
11+
}

divide-and-conquer/question1.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Find a majority element using linear search that occurs more than n/2 times. The array is ordered
3+
4+
METHOD1:
5+
we can do a linear search for n elements and maintain a counter for each element which is reaches more than
6+
n/2 then we will return that element.
7+
Time complexity: O(n)
8+
Space complexity: O(1)
9+
10+
METHOD2:
11+
we can do a linear search of first n/2 elements and then see if the same element is present at ith and
12+
i+n/2 th place. If yes then that is the required element.
13+
Time complexity: O(n) //but overall time will be lesser still we are scanning till half
14+
Space complexity: O(1)
15+
16+
METHOD3:
17+
18+
19+
20+
*/
21+
//METHOD1
22+
// #include <stdio.h>
23+
// #include <stdlib.h>
24+
25+
// int findElm(int *arr, int size){
26+
// int i,counter = 1;
27+
// for(i=0;i<size-1;i++){
28+
// if(arr[i] == arr[i+1]){
29+
// counter++;
30+
// if(counter > size/2){
31+
// return arr[i];
32+
// }
33+
// }else{
34+
// counter = 1;
35+
// }
36+
// }
37+
// return -1;
38+
// }
39+
40+
// int main(){
41+
// int arr[] = {1,1,1,1,2,3,4};
42+
// int size = sizeof(arr)/sizeof(arr[0]);
43+
// int elm = findElm(arr,size);
44+
// if(elm < 0){
45+
// printf("element is not present\n");
46+
// }else{
47+
// printf("element is %d\n", elm);
48+
// }
49+
50+
// return 0;
51+
// }
52+
//=========================================================================================
53+
//METHOD2
54+
// #include <stdio.h>
55+
// #include <stdlib.h>
56+
57+
// int findElm(int *arr, int size){
58+
// int i=0;
59+
// while(i<=size/2){
60+
// if(arr[i] == arr[i+size/2]){
61+
// return arr[i];
62+
// }
63+
// i++;
64+
// }
65+
// return -1;
66+
// }
67+
68+
// int main(){
69+
// int arr[] = {1,2,3,4,4,4,4};
70+
// int size = sizeof(arr)/sizeof(arr[0]);
71+
// int elm = findElm(arr,size);
72+
// if(elm < 0){
73+
// printf("element is not present\n");
74+
// }else{
75+
// printf("element is %d\n", elm);
76+
// }
77+
78+
// return 0;
79+
// }
80+
//===============================================================================================
81+
//METHOD3
82+
#include <stdio.h>
83+
#include <stdlib.h>
84+
85+
int findElm(int *arr, int size){
86+
int i=0;
87+
while(i<=size/2){
88+
if(arr[i] == arr[i+size/2]){
89+
return arr[i];
90+
}
91+
i++;
92+
}
93+
return -1;
94+
}
95+
96+
int main(){
97+
int arr[] = {1,2,3,4,4,4,4};
98+
int size = sizeof(arr)/sizeof(arr[0]);
99+
int elm = findElm(arr,size);
100+
if(elm < 0){
101+
printf("element is not present\n");
102+
}else{
103+
printf("element is %d\n", elm);
104+
}
105+
106+
return 0;
107+
}
108+
109+
110+

greedy/question12.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
Rearrange the string such that same characters are d distance away
3+
4+
The idea is to pick the characters with the highest frequency first and rearrange them.
5+
Therefore we use a hashtable to store the frequencies of each character and build a max heap out of it.
6+
Then we extract max one by one and place it as closer as possible given value of d in the string.
7+
Then we keep doing that for all the freq in descending order
8+
9+
Time complexity: O(n) //since size of max heap is constant all operations will have cosntant time value.
10+
Space complexity: O(1) //since 256 will be the size of hash table at max which will also be the size of
11+
max heap and hence it is not dependent on the input
12+
*/
13+
14+
#include <stdio.h>
15+
#include <stdlib.h>
16+
#include <string.h>
17+
#define MAX 256
18+
19+
struct hash{
20+
char c;
21+
int count;
22+
};
23+
24+
void maxHeapify(struct hash *hashTable,int i, int size){
25+
int left = 2*i+1;
26+
int right = 2*i+2;
27+
int largest;
28+
if(left < size && hashTable[left].count > hashTable[i].count){
29+
largest = left;
30+
}else{
31+
largest = i;
32+
}
33+
if(right < size && hashTable[right].count > hashTable[largest].count){
34+
largest = right;
35+
}
36+
37+
if(largest < size && largest != i){
38+
int temp_count = hashTable[i].count;
39+
char temp_c = hashTable[i].c;
40+
hashTable[i].c = hashTable[largest].c;
41+
hashTable[i].count = hashTable[largest].count;
42+
hashTable[largest].c = temp_c;
43+
hashTable[largest].count = temp_count;
44+
45+
maxHeapify(hashTable,largest,size);
46+
}
47+
48+
}
49+
50+
void buildMaxHeap(struct hash *hashTable, int size){
51+
int index = (size/2)-1;
52+
int i;
53+
for(i=index; i>=0;i--){
54+
maxHeapify(hashTable,i,size);
55+
}
56+
}
57+
58+
void maxHeap(struct hash *hashTable){
59+
for(int i=0;i<MAX;i++){
60+
printf("%c , %d\n", hashTable[i].c, hashTable[i].count);
61+
}
62+
}
63+
64+
struct hash *extractMax(struct hash *hashTable, int *size){
65+
if(*size == 0){
66+
return NULL;
67+
}
68+
printf("extract running %d\n",hashTable[0].count);
69+
struct hash *temp = &hashTable[0];
70+
71+
72+
int count = hashTable[0].count;
73+
char c = hashTable[0].c;
74+
75+
hashTable[0].count = hashTable[*size-1].count;
76+
hashTable[0].c = hashTable[*size-1].c;
77+
*size = *size - 1;
78+
maxHeapify(hashTable,0,*size);
79+
80+
temp->count = count;
81+
temp->c = c;
82+
printf("value assigned is %d\n", temp->count);
83+
printf("char assigned is %c\n", temp->c);
84+
return temp;
85+
}
86+
87+
88+
void reArrange(char *arr, int d){
89+
int length = strlen(arr);
90+
int max = MAX;
91+
struct hash *hashTable = (struct hash *)calloc(max,sizeof(struct hash));
92+
int i,j;
93+
for(i=0;i<length;i++){
94+
hashTable[arr[i]].c = arr[i];
95+
hashTable[arr[i]].count++;
96+
arr[i]='\0';
97+
}
98+
99+
buildMaxHeap(hashTable, max);
100+
101+
printf("now extracting...\n");
102+
struct hash *temp = extractMax(hashTable, &max);
103+
printf("%d\n", temp->count);
104+
printf("-------------------------------------\n");
105+
temp = extractMax(hashTable, &max);
106+
printf("%d\n", temp->count);
107+
108+
109+
}
110+
111+
int main(){
112+
char arr[] = "aab";
113+
int d = 2;
114+
115+
reArrange(arr, d);
116+
return 0;
117+
}

nextquestions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,5 @@ TODO:
6464
- min heap implementation of PRIMS in greedy
6565
- solve KRUSKAL from greedy
6666
- solve DIJKSTRA alog from greedy
67-
- greedy question2 to be seen from geeks for geeks
67+
- greedy question2 to be seen from geeks for geeks
68+
- greedy question12 implementation

0 commit comments

Comments
 (0)