Skip to content

Commit 31269e0

Browse files
committed
new question added
1 parent 81ca644 commit 31269e0

File tree

4 files changed

+126
-19
lines changed

4 files changed

+126
-19
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
### For Arrays: (methods that can be applied)
44

5-
- sorting and then doing something, hashtable, two pointers in a loop are some of the operations that are popular to solve algos
5+
- sorting and then doing something, hashtable, two pointers in a loop are some of the operations that are popular to solve algos.
6+
Hash table can also be made for storing sum etc.
67
- to find all combinations use nested for loops (worst algos)
78
- another way is to stop at an element and traverse all its previous elements
89
- fixing one number and having two pointers one at start and one at end to make some operation
@@ -13,13 +14,17 @@ only if there is one number that repeats odd number of times
1314
- If element in an array is to be found, and length is known apply binary search. Will complete the search in O(logn) times. If binary search cant be applied look in sizes of k at a time to limit the search set.
1415
- We cannot find time complexity of something whose input size is unknown.
1516

17+
# General hash functions
18+
- take mod with number of elements present
19+
1620
#Topic0: Programming Questions
1721

1822
TODO:
1923
- question14.c (general),
2024
- question17.c (arrays), to be done after trees and linkedlist is done
2125
- question18.c (arrays), to be done after linkedlist is done
2226
- question19.c (arrays): to be done after trees is done
27+
- question20.c (arrays): to be done after hashing (approach 2 and 3)
2328

2429
## Note:
2530
**Questions statements are included in the file itself**
@@ -79,10 +84,11 @@ TODO:
7984
- [Given an array A, find two elements whose sum is closest to zero](/arrays/question14.c)
8085
- [Find the triplet in given array that sum to given value 'x'](/arrays/question15.c)
8186
- [Find the equilibrium index of an array](/arrays/question16.c)
82-
- [In an array of unknown size having all 0s at one side and all 1s at other, find the index where 1st 1 exists.](/arrays/question17.c) - STILL TO BE DONE
87+
- [In an array of unknown size having all 0s at one side and all 1s at other, find the index where 1st 1 exists.](/arrays/question17.c)
8388
- [Given an array and an integer K, find max element for each and every contiguous subarray of size k](/arrays/question18.c)
8489
- [Count number of smaller elements on the right of each element in an array](/arrays/question19.c)
8590
- [Find the subarray with the given sum](/arrays/question20.c)
91+
- [Consider and array which contains only 0's and 1's. Find largest sub array which contains only 0's and 1's](/arrays/question21.c)
8692

8793
## Some important concepts to solve algos better
8894

arrays/question19.c

+17-17
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ Time complexity: O(n^2)
1313
Time complexity: O(1)
1414
*/
1515

16-
// //METHOD1
17-
// #include <stdio.h>
18-
// int main(){
19-
// int a[] = {10,3,4,5,7,1,3,2};
20-
// int length = sizeof(a)/sizeof(a[0]);
21-
// int elm1, counter;
22-
// for(int i=0; i<length; i++){
23-
// elm1 = a[i];
24-
// counter = 0;
25-
// for(int j=i+1;j<length;j++){
26-
// if(elm1 > a[j]){
27-
// counter++;
28-
// }
29-
// }
30-
// printf("%d ", counter);
31-
// }
32-
// }
16+
//METHOD1
17+
#include <stdio.h>
18+
int main(){
19+
int a[] = {10,3,4,5,7,1,3,2};
20+
int length = sizeof(a)/sizeof(a[0]);
21+
int elm1, counter;
22+
for(int i=0; i<length; i++){
23+
elm1 = a[i];
24+
counter = 0;
25+
for(int j=i+1;j<length;j++){
26+
if(elm1 > a[j]){
27+
counter++;
28+
}
29+
}
30+
printf("%d ", counter);
31+
}
32+
}
3333

3434
//METHOD2
3535
#include <stdio.h>

arrays/question20.c

+91
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,96 @@
11
/*
22
Find the subarray with the given sum
33
4+
SUBARRAY: part of the array which is contiguous and satisfied the given condition
5+
6+
METHOD1
7+
Take all possible combos
8+
Time complexity: O(n^2)
9+
Space complexity: O(1)
10+
11+
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
13+
Array in worst case will be traversed twice
14+
Time complexity: O(n)
15+
Space complexity: O(1)
16+
17+
METHOD3(for both negative and positive)
18+
Maintaining a hash for sum so far and subtracting given sum each time to see if the result is present in the hash table
19+
If present that means sub array exists which if subtracted can give a subarray which has the req sum
20+
Time complexity: O(n)
21+
Space complexity: O(n)
422
523
*/
24+
25+
//METHOD1
26+
#include <stdio.h>
27+
#include <stdlib.h>
28+
29+
void printSubArray(int *arr,int start, int end){
30+
for(int i=start; i<=end;i++){
31+
printf("%d", arr[i]);
32+
}
33+
}
34+
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+
}
52+
53+
//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+
}

arrays/question21.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
Consider and array which contains only 0's and 1's. Find largest sub array which contains only 0's and 1's
3+
*/
4+
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
int main(){
9+
10+
}

0 commit comments

Comments
 (0)