Skip to content

Commit 461bf0a

Browse files
committed
new question added
1 parent 399728a commit 461bf0a

File tree

11 files changed

+152
-125
lines changed

11 files changed

+152
-125
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ which has to be returned
3636
- To make games like snakes & ladders, we can use a linked list with a random pointer, next pointer and data.
3737
Whenever there is a ladder or snake, the random pointer will point there else it will be NULL.
3838
- Consider making additional connections (links to the new list or old list) for traversing or reference point of view when there are multiple things involved (random node eg:). New node sometimes can be added in the middle of the two nodes to maintain a connection and so on.
39+
- In program involving partition or merging always take mid as start+end/2 and not subtraction because this will always give the correct value. Subtraction may not give the correct value as you keep breaking the array into smaller parts
3940

4041
### For Hashing: (methods that can be applied)
4142
- When solving questions divide value with the size of the hashTable. Keep the size of the hash table one
@@ -131,7 +132,7 @@ max edge or path to be traversed to minimize work.
131132
- In repeating elements with fixed size and ordered array we can use binary search. Also linear search can be
132133
used where if an element should be repeating a given number of times, you can check its value at i and then
133134
i+given number of times to see if thats true or not.
134-
135+
- In divide and conquer even number of multiplications can be reduced from n to logn to get the same result. Example is the pow function where the base value is square everytime and power is halved everytime to get the same answer in logn multiplications. In case the power value is odd, the result is given the base value such that power value is converted to even and then same operations are applied.
135136

136137
# Topic0: Programming Questions
137138

@@ -315,6 +316,9 @@ i+given number of times to see if thats true or not.
315316
### Divide and conquer
316317

317318
- [Find a majority element using linear search that occurs more than n/2 times](/divide-and-conquer/question1.c)
319+
- [Nuts and bolts problem](/divide-and-conquer/question2.c)
320+
- [Write a custom C function to implement a pow function](/divide-and-conquer/question3.c)
321+
318322

319323
## Some important concepts to solve algos better
320324

arrays/a.exe

30.5 KB
Binary file not shown.

arrays/question29.c

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,54 @@ Program to implement binary search in an array
55
#include <stdio.h>
66
#include <stdlib.h>
77

8+
int findElmIterative(int *arr, int start, int end, int elm){
9+
while(start < end){
10+
int mid = (start+end)/2;
11+
if(arr[mid] > elm){
12+
end = mid - 1;
13+
}else if(arr[mid] < elm){
14+
start = mid + 1;
15+
}else{
16+
return mid;
17+
}
18+
}
19+
return -1;
20+
}
21+
22+
int findElmRecursive(int *arr, int start, int end, int elm){
23+
if(start >= end){
24+
return -1;
25+
}
26+
int mid = (start + end)/2;
27+
if(arr[mid] > elm){
28+
return findElmRecursive(arr,start,mid-1,elm);
29+
}
30+
if(arr[mid] < elm){
31+
return findElmRecursive(arr,mid+1,end,elm);
32+
}
33+
return mid;
34+
}
35+
836
int main(){
9-
int arr[] = {};
37+
int arr[] = {2,5,8,12,16,23,38,56,72,91};
38+
int size = sizeof(arr)/sizeof(arr[0]);
39+
int elm = 23;
40+
int step;
41+
printf("1. Do Iteration\n");
42+
printf("2. Do Recursion\n");
43+
scanf("%d",&step);
44+
int index;
45+
switch(step){
46+
case 1: index = findElmIterative(arr,0, size-1, elm);
47+
break;
48+
case 2: index = findElmRecursive(arr,0,size-1,elm);
49+
break;
50+
}
51+
if(index < 0){
52+
printf("no such element is present\n");
53+
}else{
54+
printf("element is present at index %d\n", index);
55+
}
56+
1057
return 0;
1158
}

code/test1.c

Lines changed: 0 additions & 6 deletions
This file was deleted.

code/test2.c

Lines changed: 0 additions & 13 deletions
This file was deleted.

code/test3.c

Lines changed: 0 additions & 88 deletions
This file was deleted.

divide-and-conquer/a.exe

29.8 KB
Binary file not shown.

divide-and-conquer/question1.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@ Time complexity: O(n) //but overall time will be lesser still we are scanning ti
1414
Space complexity: O(1)
1515
1616
METHOD3:
17-
18-
17+
We will do a binary search. The thing to note is that the element that is repeating the majority number
18+
of times n/2, will alway touch the middle part of the array No matter where it starts or end.
19+
Therefore, if we start from the middle and try to see if the left element ot the middle is equal
20+
to that element, if yes, then we search in the left array and repeat this exercise, otherwise we assume
21+
that the middle element is the starting element.
22+
Once we have the starting element we can just go to index +n/2 to see if the end index is that to finalize
23+
that the element is repeating n/2 times, otherwise elm is not repeating that many times. If the question says
24+
that find how many times is the element repeating given it is repeating more than n/2 times, then it will
25+
definately touch the middle, then we find its end index also using binary search as we did for start index
26+
Time complexity: O(logn)
27+
Space complexity: O(logn) //if recursive else O(1) if iterative
1928
2029
*/
2130
//METHOD1
@@ -82,25 +91,18 @@ Space complexity: O(1)
8291
#include <stdio.h>
8392
#include <stdlib.h>
8493

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+
int findElm(int *arr, int start, int end,int size){
95+
9496
}
9597

9698
int main(){
97-
int arr[] = {1,2,3,4,4,4,4};
99+
int arr[] = {1,2,2,2,2,3,4};
98100
int size = sizeof(arr)/sizeof(arr[0]);
99-
int elm = findElm(arr,size);
100-
if(elm < 0){
101+
int index = findElm(arr,0,size-1, size);
102+
if(index < 0){
101103
printf("element is not present\n");
102104
}else{
103-
printf("element is %d\n", elm);
105+
printf("element is %d\n", arr[index]);
104106
}
105107

106108
return 0;

divide-and-conquer/question2.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
Given two array with nuts and bolts each, and given that each nut only maps to a single bolt and vice
3+
versa, find each pair
4+
5+
METHOD:
6+
sort and then compare
7+
Time complexity: O(nlogn)
8+
9+
METHOD:
10+
quick sort way??
11+
http://www.geeksforgeeks.org/nuts-bolts-problem-lock-key-problem/
12+
*/

divide-and-conquer/question3.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Custom C function to implement a pow function
3+
*/
4+
5+
/*
6+
The function is buggy because it can overflow, cannot work for negative value of power. Also it cannot work
7+
for decimals. It is also slow because it is doing a particular multiplications so many times.
8+
It also does not handle fractional power
9+
*/
10+
11+
// #include <stdio.h>
12+
// #include <stdlib.h>
13+
14+
// int customPow(int base, int power){
15+
// int i;
16+
// int temp = 1;
17+
// for(i=0;i<power;i++){
18+
// temp *= base;
19+
// }
20+
// return temp;
21+
// }
22+
23+
// int main(){
24+
// printf("value is %d\n", customPow(10,4));
25+
// return 0;
26+
// }
27+
28+
/*
29+
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
31+
*/
32+
#include <stdio.h>
33+
#include <stdlib.h>
34+
35+
//iterative
36+
int ipow(int base, int exp){
37+
int result = 1;
38+
while(exp){
39+
if(exp & 1){ //will return 0 if number is even and will return 1 if number if odd
40+
//therefore odd value is taken out of the bracket and remaning operations are done on
41+
//even value
42+
result *= base;
43+
}
44+
exp >>= 1; //used to convert exp value to half
45+
base *= base;
46+
}
47+
return result;
48+
}
49+
50+
//recursive
51+
int ipowRecursive(int base, int exp){
52+
if(exp == 0){
53+
return 1;
54+
}
55+
int result = ipowRecursive(base,exp/2);
56+
if(exp & 1){
57+
return result*result*base;
58+
}
59+
return result*result;
60+
}
61+
62+
int main(){
63+
// int a = 50;
64+
printf("value is %d\n", ipowRecursive(10,4));
65+
return 0;
66+
}

0 commit comments

Comments
 (0)