├── Basic-Programs ├── Basic │ ├── Sum-of-First-N.c │ ├── Rem-Quo.c │ ├── Odd-Even.c │ ├── Swap-Two-Numbers.c │ ├── Factorial.c │ ├── Pow-of-Int.c │ ├── Rev-Int.c │ ├── Prime-Num.c │ ├── Fibo.c │ └── Greatest-of-three.c ├── Basic-Program2 │ ├── pencil-count.c │ ├── nos-not-div-by-n.c │ ├── letter-colour.c │ ├── letter-social-media.c │ └── admission-eligibility.c ├── Basic-Program1 │ ├── quo-and-rem.c │ ├── swap-two-nos.c │ ├── pow-of.c │ ├── odd-or-even.c │ └── fibo.c ├── Basic-Program3 │ ├── pow-of-two.c │ ├── single-char.c │ ├── missing_integer.c │ ├── locker.c │ ├── keystrokes.c │ ├── isPow.c │ ├── caterpillar_leaves.c │ ├── nums_maxes.c │ ├── duplicate_prod.c │ └── sort-stock.c └── README.md ├── Dynamic-Programming ├── Playing-With-Numbers.c ├── Longest-Common-Subsequence.c ├── Longest-non-Decreasing-Subsquence.c ├── Playing-with-Chessboard.c └── README.md ├── Finding-Time-Complexity-of-Algorithms ├── Problem-2-Finding-Complexity-using-Counter-Method.c ├── Problem-4-Finding-Complexity-using-Counter-Method.c ├── Problem-6-Finding-Complexity-using-Counter-Method.c ├── Problem-5-Finding-Complexity-using-Counter-Method.c ├── Problem-3-Finding-Complexity-using-Counter-Method.c ├── Problem-1-Comparing-Complexity-Quiz.c └── README.md ├── Greedy-Algorithms ├── Problem-1.c ├── Problem-4.c ├── Problem-5.c ├── Problem-2.c ├── Problem-6.c ├── Problem-3.c └── README.md ├── Brute-Force-Strategy-Simple-Programs ├── Finding-Duplicates.c ├── String-Matching.c ├── Pair-with-Difference.c ├── Add-Array.c ├── Bubble-Sort.c ├── Print-intersection-of-2-sorted-arrays.c ├── Subset-Sum.c ├── Find-Words.c ├── Assignment-Prob.c └── README.md ├── Divide-and-Conquer ├── Majority-Element.c ├── Number-of-zeroes.c ├── Finding-Floor-Value.c ├── Finding-element-in-sorted-matrix.c ├── Two-elements-sum-to-x.c └── README.md ├── Backtracking ├── PP3-Subset-Sum.c ├── PP1-Hamiltonian-Cycle.c └── README.md ├── String-Matching ├── Rabin-Karp.c ├── KMP-Algo.c └── README.md ├── cryptic-fate5.svg └── README.md /Basic-Programs/Basic/Sum-of-First-N.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n; 5 | scanf("%d",&n); 6 | printf("%d",n*(n+1)/2); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic/Rem-Quo.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a,b; 5 | scanf("%d %d",&a,&b); 6 | printf("%d\n%d",a/b,a%b); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program2/pencil-count.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n, e; 5 | scanf("%d %d", &n, &e); 6 | printf("%d", n - e); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic/Odd-Even.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a; 5 | scanf("%d",&a); 6 | if((a&1)==0) printf("Even"); 7 | else printf("Odd"); 8 | return 0; 9 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic/Swap-Two-Numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a,b; 5 | scanf("%d %d",&a,&b); 6 | a=a^b; 7 | b=a^b; 8 | a=a^b; 9 | printf("%d %d",a,b); 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic/Factorial.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n,fact=1; 5 | scanf("%d",&n); 6 | for(int i=2;i<=n;i++){ 7 | fact*=i; 8 | } 9 | printf("%d",fact); 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program1/quo-and-rem.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int x, y, quo, rem; 5 | scanf("%d %d", &x, &y); 6 | quo = x / y; 7 | rem = x % y; 8 | printf("%d\n%d", quo, rem); 9 | return 0; 10 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program1/swap-two-nos.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int x, y; 5 | scanf("%d %d", &x, &y); 6 | x = x ^ y; 7 | y = x ^ y; 8 | x = x ^ y; 9 | printf("%d %d", x, y); 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic/Pow-of-Int.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a,b,c; 5 | scanf("%d %d",&a,&b); 6 | b--; 7 | c=a; 8 | while(b--){ 9 | a*=c; 10 | } 11 | printf("%d",a); 12 | return 0; 13 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic/Rev-Int.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n,ans=0; 5 | scanf("%d",&n); 6 | while(n>0){ 7 | ans=ans*10+n%10; 8 | n/=10; 9 | } 10 | printf("%d",ans); 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program1/pow-of.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | int ans = 1; 7 | while (b--) 8 | { 9 | ans *= a; 10 | } 11 | printf("%d", ans); 12 | return 0; 13 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program1/odd-or-even.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n; 5 | scanf("%d", &n); 6 | if ((n & 1) == 0) 7 | { 8 | printf("Even"); 9 | } 10 | else 11 | { 12 | printf("Odd"); 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic/Prime-Num.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n; 5 | scanf("%d",&n); 6 | for(int i=2;i 2 | int main() 3 | { 4 | int a, b, n; 5 | scanf("%d %d %d", &a, &b, &n); 6 | for (int i = a; i <= b; i++) 7 | { 8 | if (i % n != 0) 9 | { 10 | printf("%d ", i); 11 | } 12 | } 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program3/pow-of-two.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n; 5 | scanf("%d", &n); 6 | if ((n & (n - 1)) == 0) 7 | { 8 | printf("%d is a number that can be expressed as power of 2.", n); 9 | } 10 | else 11 | { 12 | printf("%d cannot be expressed as power of 2.", n); 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic/Fibo.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n,prev1=1,prev2=0,curr; 5 | scanf("%d",&n); 6 | if(n==0){ 7 | printf("%d",0); 8 | return 0; 9 | } 10 | for(int i=1;i 2 | int fibo(int n) 3 | { 4 | if (n == 0) 5 | { 6 | return 0; 7 | } 8 | else if (n == 1 || n == 2) 9 | { 10 | return 1; 11 | } 12 | return fibo(n - 1) + fibo(n - 2); 13 | } 14 | int main() 15 | { 16 | int n; 17 | scanf("%d", &n); 18 | int ans = fibo(n); 19 | printf("%d", ans); 20 | } -------------------------------------------------------------------------------- /Dynamic-Programming/Playing-With-Numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n; 6 | scanf("%d", &n); 7 | long long int prev1 = 1, prev2 = 1, prev3 = 1, curr; 8 | for (int i = 3; i <= n; i++) 9 | { 10 | curr = prev1 + prev3; 11 | prev3 = prev2; 12 | prev2 = prev1; 13 | prev1 = curr; 14 | } 15 | printf("%lld", prev1); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Finding-Time-Complexity-of-Algorithms/Problem-2-Finding-Complexity-using-Counter-Method.c: -------------------------------------------------------------------------------- 1 | #include 2 | void function(int n,int*c){ 3 | int i=1,s=1; 4 | (*c)+=2; 5 | while(s<=n){ 6 | (*c)++; 7 | i++; 8 | (*c)++; 9 | s+=i; 10 | (*c)++; 11 | 12 | } 13 | (*c)++; 14 | } 15 | int main(){ 16 | int n; 17 | scanf("%d",&n); 18 | int c=0; 19 | function(n,&c); 20 | printf("%d",c); 21 | } 22 | -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program3/single-char.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main() 5 | { 6 | char st[1000000]; // declaring as 10 power 6 to avoid declarations of long int even though 10 power 8 is given as constraint 7 | scanf("%s", st); 8 | int x = 0; 9 | for (int i = 0; i < strlen(st); i++) 10 | { 11 | int y = (int)st[i]; 12 | x = x ^ y; 13 | } 14 | printf("%c", x); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program2/letter-colour.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | char c; 5 | scanf("%c", &c); 6 | if (c == 'r' || c == 'R') 7 | { 8 | printf("RED"); 9 | } 10 | else if (c == 'g' || c == 'G') 11 | { 12 | printf("GREEN"); 13 | } 14 | else if (c == 'b' || c == 'B') 15 | { 16 | printf("BLUE"); 17 | } 18 | else 19 | { 20 | printf("UNDEFINED"); 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic/Greatest-of-three.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a,b,c; 5 | scanf("%d %d %d",&a,&b,&c); 6 | if(a>b){ 7 | if(a>c){ 8 | printf("%d",a); 9 | } 10 | else{ 11 | printf("%d",c); 12 | } 13 | } 14 | else{ 15 | if(b>c){ 16 | printf("%d",b); 17 | } 18 | else{ 19 | printf("%d",c); 20 | } 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Finding-Time-Complexity-of-Algorithms/Problem-4-Finding-Complexity-using-Counter-Method.c: -------------------------------------------------------------------------------- 1 | #include 2 | void factor(int n,int *c) { 3 | (*c)++; 4 | for (int i = 1; i <=n;i++){ 5 | (*c)++; 6 | (*c)++; 7 | if (n % i== 0){ 8 | // printf("%d ", i); 9 | } 10 | } 11 | (*c)++; 12 | // return 0; 13 | } 14 | int main(){ 15 | int n,c=0; 16 | scanf("%d",&n); 17 | factor(n,&c); 18 | printf("%d",c); 19 | } 20 | -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program2/letter-social-media.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | char c; 5 | scanf("%c", &c); 6 | if (c == 'w' || c == 'W') 7 | { 8 | printf("whatsapp"); 9 | } 10 | else if (c == 't' || c == 'T') 11 | { 12 | printf("twitter"); 13 | } 14 | else if (c == 'f' || c == 'F') 15 | { 16 | printf("facebook"); 17 | } 18 | else 19 | { 20 | printf("undefined"); 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program2/admission-eligibility.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int m, p, c, total; 5 | scanf("%d %d %d", &m, &p, &c); 6 | total = m + p + c; 7 | if (m >= 65 && p >= 55 && c >= 50) 8 | { 9 | printf("The candidate is eligible"); 10 | } 11 | else if (total >= 180) 12 | { 13 | printf("The candidate is eligible"); 14 | } 15 | else 16 | { 17 | printf("The candidate is not eligible"); 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Greedy-Algorithms/Problem-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n, ans = 0; 6 | scanf("%d", &n); 7 | int arr[] = {1000, 500, 100, 50, 20, 10, 5, 2, 1}; 8 | for (int i = 0; i < 9; i++) 9 | { 10 | if ((arr[i] % n) == arr[i]) 11 | { 12 | n -= arr[i]; 13 | i = 0; 14 | ans++; 15 | } 16 | if (n == 0) 17 | { 18 | break; 19 | } 20 | } 21 | printf("%d", ans); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Brute-Force-Strategy-Simple-Programs/Finding-Duplicates.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n; 5 | scanf("%d", &n); 6 | int arr[n]; 7 | for (int i = 0; i < n; i++) 8 | scanf("%d", &arr[i]); 9 | for (int i = 0; i < n; i++) 10 | { 11 | for (int j = i + 1; j < n; j++) 12 | { 13 | if (arr[i] == arr[j]) 14 | { 15 | printf("%d", arr[i]); 16 | return 0; 17 | } 18 | } 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Brute-Force-Strategy-Simple-Programs/String-Matching.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | char st[50], sub[50]; 6 | scanf("%s", st); 7 | scanf("%s", sub); 8 | for (int i = 0; i < strlen(st); i++) 9 | { 10 | char s[strlen(sub)]; 11 | memcpy(s, &st[i], strlen(sub)); 12 | s[strlen(sub)] = '\0'; 13 | if (strcmp(s, sub) == 0) 14 | { 15 | printf("1"); 16 | return 0; 17 | } 18 | } 19 | printf("0"); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Finding-Time-Complexity-of-Algorithms/Problem-6-Finding-Complexity-using-Counter-Method.c: -------------------------------------------------------------------------------- 1 | #include 2 | void reverse(int n,int*c){ 3 | int rev = 0, remainder; 4 | (*c)++; 5 | while (n != 0) { 6 | (*c)++; 7 | remainder = n % 10; 8 | (*c)++; 9 | rev = rev * 10 + remainder; 10 | (*c)++; 11 | n/= 10; 12 | (*c)++; 13 | } 14 | (*c)++; 15 | // printf("%d",rev); 16 | (*c)++; 17 | } 18 | int main(){ 19 | int n,c=0; 20 | scanf("%d",&n); 21 | reverse(n,&c); 22 | printf("%d",c); 23 | } 24 | -------------------------------------------------------------------------------- /Brute-Force-Strategy-Simple-Programs/Pair-with-Difference.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n; 5 | scanf("%d", &n); 6 | int arr[n]; 7 | for (int i = 0; i < n; i++) 8 | { 9 | scanf("%d", &arr[i]); 10 | } 11 | int k; 12 | scanf("%d", &k); 13 | for (int i = 0; i < n; i++) 14 | { 15 | for (int j = 0; j < n; j++) 16 | { 17 | if (i != j && (arr[i] - arr[j] == k)) 18 | { 19 | printf("1"); 20 | return 0; 21 | } 22 | } 23 | } 24 | printf("0"); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Divide-and-Conquer/Majority-Element.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n; 6 | scanf("%d", &n); 7 | int arr[n]; 8 | for (int i = 0; i < n; i++) 9 | { 10 | scanf("%d", &arr[i]); 11 | } 12 | for (int i = 0; i < n - 1; i++) 13 | { 14 | int c = 0; 15 | for (int j = i + 1; j < n; j++) 16 | { 17 | if (arr[i] == arr[j]) 18 | { 19 | c++; 20 | } 21 | } 22 | if (c >= n / 2) 23 | { 24 | printf("%d", arr[i]); 25 | return 0; 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Finding-Time-Complexity-of-Algorithms/Problem-5-Finding-Complexity-using-Counter-Method.c: -------------------------------------------------------------------------------- 1 | #include 2 | void function(int n,int *c1){ 3 | int c= 0; 4 | (*c1)++; 5 | for(int i=n/2; i 2 | 3 | void find(int *arr, int *c, int low, int high, int mid) 4 | { 5 | if (arr[mid] == 0 && low <= high) 6 | { 7 | (*c)++; 8 | find(arr, c, low, mid - 1, (low + mid - 1) / 2); 9 | } 10 | if (low <= high) 11 | { 12 | find(arr, c, mid + 1, high, (mid + 1 + high) / 2); 13 | } 14 | } 15 | int main() 16 | { 17 | int n; 18 | scanf("%d", &n); 19 | int arr[n]; 20 | for (int i = 0; i < n; i++) 21 | { 22 | scanf("%d", &arr[i]); 23 | } 24 | int c = 0; 25 | find(arr, &c, 0, n - 1, n / 2); 26 | printf("%d", c); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Backtracking/PP3-Subset-Sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int help(int *set, int n, int k, int j) 4 | { 5 | if (k == 0 || j == n) 6 | { 7 | if (k == 0) 8 | return 1; 9 | return 0; 10 | } 11 | if (help(set, n, k - set[j], j + 1)) 12 | return 1; 13 | if (help(set, n, k, j + 1)) 14 | return 1; 15 | return 0; 16 | } 17 | int main() 18 | { 19 | int n, k; 20 | scanf("%d %d", &n, &k); 21 | int set[n]; 22 | for (int i = 0; i < n; i++) 23 | { 24 | scanf("%d", &set[i]); 25 | } 26 | if (help(set, n, k, 0)) 27 | { 28 | printf("True"); 29 | } 30 | else 31 | { 32 | printf("False"); 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Brute-Force-Strategy-Simple-Programs/Add-Array.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | long int sub = 0, n, k, ans; 5 | scanf("%ld", &n); 6 | long int y = n; 7 | while (n--) 8 | { 9 | long int x, pos = 1; 10 | scanf("%ld", &x); 11 | for (int i = 0; i < n; i++) 12 | { 13 | pos *= 10; 14 | } 15 | sub += (pos * x); 16 | } 17 | scanf("%ld", &k); 18 | ans = k + sub; 19 | long int arrans[y + 10]; 20 | long q = 0; 21 | while (ans > 0) 22 | { 23 | arrans[q] = ans % 10; 24 | ans /= 10; 25 | q++; 26 | } 27 | for (long int i = q - 1; i >= 0; i--) 28 | { 29 | printf("%ld ", arrans[i]); 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Brute-Force-Strategy-Simple-Programs/Bubble-Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | void bubbleSort(int *arr, int n) 3 | { 4 | for (int i = 0; i < n - 1; i++) 5 | { 6 | for (int j = 0; j < n - i - 1; j++) 7 | { 8 | if (arr[j] > arr[j + 1]) 9 | { 10 | int temp = arr[j]; 11 | arr[j] = arr[j + 1]; 12 | arr[j + 1] = temp; 13 | } 14 | } 15 | } 16 | } 17 | int main() 18 | { 19 | int n; 20 | scanf("%d", &n); 21 | int arr[n]; 22 | for (int i = 0; i < n; i++) 23 | { 24 | scanf("%d", &arr[i]); 25 | } 26 | bubbleSort(arr, n); 27 | for (int i = 0; i < n; i++) 28 | { 29 | printf("%d ", arr[i]); 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Finding-Time-Complexity-of-Algorithms/Problem-3-Finding-Complexity-using-Counter-Method.c: -------------------------------------------------------------------------------- 1 | #include 2 | void func(int n,int*c){ 3 | if(n==1){ 4 | (*c)++; 5 | // printf(" "); 6 | (*c)++; 7 | } 8 | else{ 9 | (*c)++; 10 | for(int i=1;i<=n;i++){ 11 | (*c)++; 12 | for(int j=1;j<=n;j++){ 13 | (*c)++; 14 | // printf(" "); 15 | (*c)++; 16 | // printf(" "); 17 | (*c)++; 18 | break; 19 | } 20 | (*c)++; 21 | } 22 | (*c)++; 23 | } 24 | } 25 | int main(){ 26 | int n; 27 | scanf("%d",&n); 28 | int c=0; 29 | func(n,&c); 30 | printf("%d",c); 31 | } 32 | -------------------------------------------------------------------------------- /Dynamic-Programming/Longest-Common-Subsequence.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int max(int a, int b) 5 | { 6 | if (a > b) 7 | return a; 8 | return b; 9 | } 10 | int help(int a, int b, char *x, char *y, int dp[][b + 1]) 11 | { 12 | if (a < 0 || b < 0) 13 | return 0; 14 | if (dp[a][b] != -1) 15 | return dp[a][b]; 16 | if (x[a] == y[b]) 17 | return 1 + help(a - 1, b - 1, x, y, dp); 18 | return dp[a][b] = max(help(a - 1, b, x, y, dp), help(a, b - 1, x, y, dp)); 19 | } 20 | int main() 21 | { 22 | char x[100], y[100]; 23 | scanf("%s %s", x, y); 24 | int a = strlen(x), b = strlen(y); 25 | int dp[a][b]; 26 | memset(dp, -1, sizeof(dp)); 27 | printf("%d", help(strlen(x) - 1, strlen(y) - 1, x, y, dp)); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Divide-and-Conquer/Finding-Floor-Value.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void help(int *arr, int n, int x, int *max, int low, int high, int mid) 4 | { 5 | if (arr[mid] <= x && arr[mid] > (*max)) 6 | { 7 | *max = arr[mid]; 8 | } 9 | if (low > high) 10 | return; 11 | if (x < arr[mid]) 12 | help(arr, n, x, max, low, mid - 1, (low + mid - 1) / 2); 13 | if (x > arr[mid]) 14 | help(arr, n, x, max, mid + 1, high, (high + mid + 1) / 2); 15 | } 16 | int main() 17 | { 18 | int n; 19 | scanf("%d", &n); 20 | int arr[n]; 21 | for (int i = 0; i < n; i++) 22 | { 23 | scanf("%d", &arr[i]); 24 | } 25 | int x; 26 | scanf("%d", &x); 27 | int max = 0; 28 | help(arr, n, x, &max, 0, n - 1, n / 2); 29 | printf("%d", max); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Finding-Time-Complexity-of-Algorithms/Problem-1-Comparing-Complexity-Quiz.c: -------------------------------------------------------------------------------- 1 | #include 2 | void gcdIter(int a,int b,int*gcd,int*c){ 3 | for(int i=b;i>1;i--){ 4 | if(a%i==0&&b%i==0){ 5 | (*gcd)=i; 6 | break; 7 | } 8 | (*c)++; 9 | } 10 | } 11 | int gcdEucl(int a,int b,int *c){ 12 | if(b==0){ 13 | return a; 14 | } 15 | (*c)++; 16 | return gcdEucl(b,a%b,c); 17 | } 18 | int main(){ 19 | int a,b; 20 | scanf("%d %d",&a,&b); 21 | int gcd=0,c=1; 22 | gcdIter(a,b,&gcd,&c); 23 | printf("%d\n",gcd); 24 | int c1=0; 25 | printf("%d\n",gcdEucl(a,b,&c1)); 26 | printf("%d\n",c); 27 | printf("%d\n",c1); 28 | if(c1 2 | 3 | int binary_search(int *arr, int x, int low, int high, int mid) 4 | { 5 | if (low > high) 6 | return -1; 7 | if (arr[mid] == x) 8 | return 1; 9 | else if (arr[mid] > x) 10 | return binary_search(arr, x, low, mid - 1, (low + mid - 1) / 2); 11 | else 12 | return binary_search(arr, x, mid + 1, high, (high + mid + 1) / 2); 13 | } 14 | int main() 15 | { 16 | int n; 17 | scanf("%d", &n); 18 | int arr[n * n]; 19 | for (int i = 0; i < n * n; i++) 20 | { 21 | scanf("%d", &arr[i]); 22 | } 23 | int x; 24 | scanf("%d", &x); 25 | if (binary_search(arr, x, 0, n * n - 1, n * n / 2)==1) 26 | { 27 | printf("FOUND"); 28 | } 29 | else 30 | { 31 | printf("NOT FOUND"); 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Brute-Force-Strategy-Simple-Programs/Print-intersection-of-2-sorted-arrays.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int t; 5 | scanf("%d", &t); 6 | while (t--) 7 | { 8 | int x, y; 9 | scanf("%d", &x); 10 | int arr1[x]; 11 | for (int i = 0; i < x; i++) 12 | { 13 | scanf("%d", &arr1[i]); 14 | } 15 | scanf("%d", &y); 16 | int arr2[y]; 17 | for (int i = 0; i < y; i++) 18 | { 19 | scanf("%d", &arr2[i]); 20 | } 21 | for (int i = 0; i < x; i++) 22 | { 23 | for (int j = 0; j < y; j++) 24 | { 25 | if (arr1[i] == arr2[j]) 26 | { 27 | printf("%d ", arr1[i]); 28 | break; 29 | } 30 | } 31 | } 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Greedy-Algorithms/Problem-4.c: -------------------------------------------------------------------------------- 1 | #include 2 | void sort(int *arr, int n) 3 | { 4 | for (int i = 0; i < n - 1; i++) 5 | { 6 | for (int j = 0; j < n - i - 1; j++) 7 | { 8 | if (arr[j] < arr[j + 1]) 9 | { 10 | int temp = arr[j]; 11 | arr[j] = arr[j + 1]; 12 | arr[j + 1] = temp; 13 | } 14 | } 15 | } 16 | } 17 | int power(int a) 18 | { 19 | if (a == 0) 20 | return 1; 21 | return 3 * power(a - 1); 22 | } 23 | int main() 24 | { 25 | int n; 26 | scanf("%d", &n); 27 | int arr[n]; 28 | for (int i = 0; i < n; i++) 29 | { 30 | scanf("%d", &arr[i]); 31 | } 32 | sort(arr, n); 33 | int ans = 0; 34 | for (int i = 0; i < n; i++) 35 | { 36 | ans += power(i) * arr[i]; 37 | } 38 | printf("%d", ans); 39 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program3/missing_integer.c: -------------------------------------------------------------------------------- 1 | // You are given a sequence of n-1 distinct positive integers, all of which are less than or equal to a integer ‘n’. You have to find the integer that is missing from the range [1,2, . . . n]. Solve the question without using arrays. 2 | 3 | // Input Format: 4 | // One line containing the integer ‘n’ where 2<=n<=10,000 5 | // First line is followed by a sequence of ‘n-1’ distinct positive integers. Note that the sequence may not be in any particular order. 6 | 7 | // Output Format: 8 | // One line containing the missing number 9 | 10 | #include 11 | int main() 12 | { 13 | int n; 14 | scanf("%d", &n); 15 | int actual_sum = n * (n + 1) / 2, given_sum = 0; 16 | while (n != 1) 17 | { 18 | int k; 19 | scanf("%d", &k); 20 | given_sum += k; 21 | n--; 22 | } 23 | printf("%d", actual_sum - given_sum); 24 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program3/locker.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int n; 6 | scanf("%d", &n); 7 | int arr[n + 1]; 8 | for (int i = 1; i <= n; i++) 9 | { 10 | arr[i] = 0; 11 | } 12 | for (int i = 1; i <= n; i++) 13 | { 14 | for (int j = 1; j <= n; j++) 15 | { 16 | if (j % i == 0) 17 | { 18 | if (arr[j] == 0) 19 | { 20 | arr[j] = 1; 21 | } 22 | else 23 | { 24 | arr[j] = 0; 25 | } 26 | } 27 | } 28 | } 29 | int open = 0; 30 | for (int i = 1; i <= n; i++) 31 | { 32 | if (arr[i] == 1) 33 | { 34 | open++; 35 | } 36 | } 37 | printf("open = %d\nclose = %d", open, abs(n - open)); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Brute-Force-Strategy-Simple-Programs/Subset-Sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | int help(int *arr, int n, int j, int sum,int asum) 3 | { 4 | if (j == n) 5 | { 6 | if (sum == asum/2) 7 | return 1; 8 | if (j == n) 9 | return 0; 10 | } 11 | if (help(arr, n, j + 1, sum - arr[j],asum)) 12 | return 1; 13 | if (help(arr, n, j + 1, sum,asum)) 14 | return 1; 15 | return 0; 16 | } 17 | int main() 18 | { 19 | int n, sum = 0; 20 | scanf("%d", &n); 21 | int arr[n]; 22 | for (int i = 0; i < n; i++) 23 | { 24 | scanf("%d", &arr[i]); 25 | sum += arr[i]; 26 | } 27 | if (sum % 2 == 1) 28 | { 29 | printf("false"); 30 | return 0; 31 | } 32 | if (help(arr, n, 0, sum,sum)) 33 | { 34 | printf("true"); 35 | } 36 | else 37 | { 38 | printf("false"); 39 | } 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Dynamic-Programming/Longest-non-Decreasing-Subsquence.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int max(int a, int b) 5 | { 6 | if (a > b) 7 | return a; 8 | return b; 9 | } 10 | int help(int *arr, int n, int i, int prev, int dp[][n + 1]) 11 | { 12 | if (i == n) 13 | return 0; 14 | if (dp[i][prev + 1] != -1) 15 | return dp[i][prev + 1]; 16 | int l = help(arr, n, i + 1, prev, dp); 17 | int r = INT_MIN; 18 | if (prev == -1 || arr[i] >= arr[prev]) 19 | r = 1 + help(arr, n, i + 1, i, dp); 20 | return dp[i][prev + 1] = max(l, r); 21 | } 22 | int main() 23 | { 24 | int n; 25 | scanf("%d", &n); 26 | int arr[n]; 27 | for (int i = 0; i < n; i++) 28 | scanf("%d", &arr[i]); 29 | int dp[n][n + 1]; 30 | memset(dp, -1, sizeof(dp)); 31 | int ans = help(arr, n, 0, -1, dp); 32 | printf("%d", ans); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Dynamic-Programming/Playing-with-Chessboard.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int max(int a, int b) 5 | { 6 | if (a > b) 7 | return a; 8 | return b; 9 | } 10 | 11 | int help(int n, int chess[][n], int i, int j, int dp[][n]) 12 | { 13 | if (i == n - 1 && j == n - 1) 14 | return chess[n - 1][n - 1]; 15 | if (dp[i][j] != -1) 16 | return dp[i][j]; 17 | int l = 0, r = 0; 18 | if (i < n - 1) 19 | l = chess[i][j] + help(n, chess, i + 1, j, dp); 20 | if (j < n - 1) 21 | r = chess[i][j] + help(n, chess, i, j + 1, dp); 22 | return dp[i][j] = max(l, r); 23 | } 24 | 25 | int main() 26 | { 27 | int n; 28 | scanf("%d", &n); 29 | int chess[n][n]; 30 | for (int i = 0; i < n; i++) 31 | { 32 | for (int j = 0; j < n; j++) 33 | scanf("%d", &chess[i][j]); 34 | } 35 | int dp[n][n]; 36 | memset(dp, -1, sizeof(dp)); 37 | printf("%d", help(n, chess, 0, 0, dp)); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Greedy-Algorithms/Problem-5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void sort(int *arr, int n) 4 | { 5 | for (int i = 0; i < n - 1; i++) 6 | { 7 | for (int j = 0; j < n - i - 1; j++) 8 | { 9 | if (arr[j] > arr[j + 1]) 10 | { 11 | int temp = arr[j]; 12 | arr[j] = arr[j + 1]; 13 | arr[j + 1] = temp; 14 | } 15 | } 16 | } 17 | } 18 | int main() 19 | { 20 | int n, m, min = 0, max = 0; 21 | scanf("%d", &n); 22 | int arr[n]; 23 | for (int i = 0; i < n; i++) 24 | { 25 | scanf("%d", &arr[i]); 26 | } 27 | scanf("%d", &m); 28 | sort(arr, n); 29 | int k = n; 30 | for (int i = 0; i < k; i++) 31 | { 32 | k -= m; 33 | min += arr[i]; 34 | } 35 | k = 0; 36 | for (int i = n - 1; i >= k; i--) 37 | { 38 | k += m; 39 | max += arr[i]; 40 | } 41 | printf("%d\n%d", min, max); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Greedy-Algorithms/Problem-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void sort(int *arr, int n) 4 | { 5 | for (int i = 0; i < n - 1; i++) 6 | { 7 | for (int j = 0; j < n - i - 1; j++) 8 | { 9 | if (arr[j] > arr[j + 1]) 10 | { 11 | int temp = arr[j]; 12 | arr[j] = arr[j + 1]; 13 | arr[j + 1] = temp; 14 | } 15 | } 16 | } 17 | } 18 | int main() 19 | { 20 | int x, y, ans = 0, k = 0; 21 | scanf("%d", &x); 22 | int child[x]; 23 | for (int i = 0; i < x; i++) 24 | { 25 | scanf("%d", &child[i]); 26 | } 27 | scanf("%d", &y); 28 | int cookies[y]; 29 | for (int i = 0; i < y; i++) 30 | { 31 | scanf("%d", &cookies[i]); 32 | } 33 | sort(child, x); 34 | sort(cookies, y); 35 | for (int i = 0; i < x; i++) 36 | { 37 | for (int j = k; j < y; j++) 38 | { 39 | if (child[i] <= cookies[j]) 40 | { 41 | ans++; 42 | k = j + 1; 43 | break; 44 | } 45 | } 46 | } 47 | printf("%d", ans); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Brute-Force-Strategy-Simple-Programs/Find-Words.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main() 5 | { 6 | int n; 7 | scanf("%d", &n); 8 | char words[n][100]; 9 | int i = 0; 10 | while (i < n) 11 | { 12 | scanf("%s", words[i]); 13 | i++; 14 | } 15 | char chars[100]; 16 | scanf("%s", chars); 17 | int ans = 0; 18 | for (int i = 0; i < n; i++) 19 | { 20 | int mpp[26] = {0}; 21 | for (int i = 0; i < strlen(chars); i++) 22 | { 23 | mpp[chars[i] - 'a'] += 1; 24 | } 25 | int c = 0; 26 | for (int j = 0; j < strlen(words[i]); j++) 27 | { 28 | for (int k = 0; k < strlen(chars); k++) 29 | { 30 | if (words[i][j] == chars[k] && mpp[chars[k] - 'a'] > 0) 31 | { 32 | c++; 33 | mpp[chars[k] - 'a']--; 34 | break; 35 | } 36 | } 37 | } 38 | if (c == strlen(words[i])) 39 | { 40 | ans += c; 41 | } 42 | } 43 | printf("%d", ans); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Greedy-Algorithms/Problem-6.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void sort(int n, int *arr) 4 | { 5 | for (int i = 0; i < n - 1; i++) 6 | { 7 | for (int j = 0; j < n - i - 1; j++) 8 | { 9 | if (arr[j] > arr[j + 1]) 10 | { 11 | int temp = arr[j]; 12 | arr[j] = arr[j + 1]; 13 | arr[j + 1] = temp; 14 | } 15 | } 16 | } 17 | } 18 | int min(int a, int b) 19 | { 20 | if (a < b) 21 | return a; 22 | return b; 23 | } 24 | int max(int a, int b) 25 | { 26 | if (a > b) 27 | return a; 28 | return b; 29 | } 30 | int main() 31 | { 32 | int n, m; 33 | scanf("%d", &n); 34 | int arr[n]; 35 | for (int i = 0; i < n; i++) 36 | scanf("%d", &arr[i]); 37 | scanf("%d", &m); 38 | sort(n, arr); 39 | int ans = arr[n - 1] - arr[0]; 40 | for (int i = 1; i < n; i++) 41 | { 42 | int x = max(arr[n - 1] - m, arr[i - 1] + m); 43 | int y = min(arr[0] + m, arr[i] - m); 44 | int d = x - y; 45 | if (d < 0) 46 | continue; 47 | ans = min(ans, d); 48 | } 49 | printf("%d", ans); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Greedy-Algorithms/Problem-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n, count5 = 0, count10 = 0; 6 | scanf("%d", &n); 7 | int bills[n]; 8 | for (int i = 0; i < n; i++) 9 | { 10 | scanf("%d", &bills[i]); 11 | } 12 | for (int i = 0; i < n; i++) 13 | { 14 | if (bills[i] == 5) 15 | { 16 | count5++; 17 | } 18 | else if (bills[i] == 10) 19 | { 20 | count10++; 21 | if (count5 > 0) 22 | { 23 | count5--; 24 | } 25 | else 26 | { 27 | printf("false"); 28 | return 0; 29 | } 30 | } 31 | else if (bills[i] == 20) 32 | { 33 | if (count10 > 0 && count5 > 0) 34 | { 35 | count10--; 36 | count5--; 37 | } 38 | else if (count5 >= 3) 39 | { 40 | count5 -= 3; 41 | } 42 | else if (count5 < 0) 43 | { 44 | printf("false"); 45 | return 0; 46 | } 47 | } 48 | } 49 | printf("true"); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Divide-and-Conquer/Two-elements-sum-to-x.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int help(int *arr, int n, int *map, int j, int x) 5 | { 6 | if (j == 2) 7 | { 8 | if (x == 0) 9 | { 10 | return 1; 11 | } 12 | return 0; 13 | } 14 | for (int i = 0; i < n; i++) 15 | { 16 | if (map[i] == 0) 17 | { 18 | map[i] = 1; 19 | x -= arr[i]; 20 | if (help(arr, n, map, j + 1, x)) 21 | return 1; 22 | map[i] = 0; 23 | x += arr[i]; 24 | } 25 | } 26 | return 0; 27 | } 28 | int main() 29 | { 30 | int n; 31 | scanf("%d", &n); 32 | int arr[n]; 33 | for (int i = 0; i < n; i++) 34 | { 35 | scanf("%d", &arr[i]); 36 | } 37 | int x; 38 | scanf("%d", &x); 39 | int map[n]; 40 | memset(map, 0, sizeof(map)); 41 | if (help(arr, n, map, 0, x)) 42 | { 43 | for (int i = 0; i < n; i++) 44 | { 45 | if (map[i] == 1) 46 | { 47 | printf("%d\n", arr[i]); 48 | } 49 | } 50 | } 51 | else 52 | { 53 | printf("No"); 54 | } 55 | return 0; 56 | } -------------------------------------------------------------------------------- /String-Matching/Rabin-Karp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define d 256 4 | int search(char pat[], char txt[], int q) 5 | { 6 | int M = strlen(pat); 7 | int N = strlen(txt); 8 | int i, j; 9 | int p = 0; 10 | int t = 0; 11 | int h = 1; 12 | for (i = 0; i < M - 1; i++) 13 | h = (h * d) % q; 14 | for (i = 0; i < M; i++) 15 | { 16 | p = (d * p + pat[i]) % q; 17 | t = (d * t + txt[i]) % q; 18 | } 19 | for (i = 0; i <= N - M; i++) 20 | { 21 | if (p == t) 22 | { 23 | for (j = 0; j < M; j++) 24 | { 25 | if (txt[i + j] != pat[j]) 26 | break; 27 | } 28 | if (j == M) 29 | { 30 | printf("%d", i); 31 | return 1; 32 | } 33 | } 34 | if (i < N - M) 35 | { 36 | t = (d * (t - txt[i] * h) + txt[i + M]) % q; 37 | if (t < 0) 38 | t = (t + q); 39 | } 40 | } 41 | printf("-1"); 42 | return 0; 43 | } 44 | int main() 45 | { 46 | char txt[50]; 47 | char pat[50]; 48 | scanf("%s", txt); 49 | scanf("%s", pat); 50 | int q = 101; 51 | search(pat, txt, q); 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program3/keystrokes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | void delete (char arr[], int n, int pos) 6 | { 7 | for (int i = pos; i < n; i++) 8 | { 9 | arr[i] = arr[i + 1]; 10 | } 11 | } 12 | int main() 13 | { 14 | int t; 15 | scanf("%d", &t); 16 | while (t--) 17 | { 18 | fflush(stdin); 19 | char st[100]; 20 | scanf("%[^\n]s", st); 21 | int len = strlen(st); 22 | for (int i = len - 1; i >= 0; i--) 23 | { 24 | if (st[i] == '^') 25 | { 26 | delete (st, len, i); 27 | len--; 28 | for (int j = i - 1; j >= 0; j--) 29 | { 30 | if (isalpha(st[j]) || isspace(st[j])) 31 | { 32 | delete (st, len, j); 33 | len--; 34 | break; 35 | } 36 | } 37 | } 38 | } 39 | if (len == 0) 40 | { 41 | printf("-1\n"); 42 | continue; 43 | } 44 | for (int i = 0; i < len; i++) 45 | { 46 | printf("%c", st[i]); 47 | } 48 | printf("\n"); 49 | } 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Brute-Force-Strategy-Simple-Programs/Assignment-Prob.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void assignment(int *initial, int row, int n, int j, int arr[][n], int sub, int *ans, int map[]) 4 | { 5 | if (j == n) 6 | { 7 | if ((*initial) == 1) 8 | { 9 | *initial = 0; 10 | (*ans) = sub; 11 | } 12 | else 13 | { 14 | if (sub < (*ans)) 15 | { 16 | (*ans) = sub; 17 | } 18 | } 19 | return; 20 | } 21 | for (int i = 0; i < n; i++) 22 | { 23 | if (map[i] == 1) 24 | { 25 | map[i] = 0; 26 | sub += arr[row][i]; 27 | assignment(initial, row + 1, n, j + 1, arr, sub, ans, map); 28 | map[i] = 1; 29 | sub -= arr[row][i]; 30 | } 31 | } 32 | } 33 | int main() 34 | { 35 | int n; 36 | scanf("%d", &n); 37 | int arr[n][n]; 38 | for (int i = 0; i < n; i++) 39 | { 40 | for (int j = 0; j < n; j++) 41 | { 42 | scanf("%d", &arr[i][j]); 43 | } 44 | } 45 | int map[n]; 46 | for (int i = 0; i < n; i++) 47 | { 48 | map[i] = 1; 49 | } 50 | int ans = 0, initial = 1; 51 | assignment(&initial, 0, n, 0, arr, 0, &ans, map); 52 | printf("%d", ans); 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program3/isPow.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Complete the 'isPower' function below. 3 | * 4 | * The function is expected to return an INTEGER_ARRAY. 5 | * The function accepts INTEGER_ARRAY arr as parameter. 6 | */ 7 | 8 | /* 9 | * To return the integer array from the function, you should: 10 | * - Store the size of the array to be returned in the result_count variable 11 | * - Allocate the array statically or dynamically 12 | * 13 | * For example, 14 | * int* return_integer_array_using_static_allocation(int* result_count) { 15 | * *result_count = 5; 16 | * 17 | * static int a[5] = {1, 2, 3, 4, 5}; 18 | * 19 | * return a; 20 | * } 21 | * 22 | * int* return_integer_array_using_dynamic_allocation(int* result_count) { 23 | * *result_count = 5; 24 | * 25 | * int *a = malloc(5 * sizeof(int)); 26 | * 27 | * for (int i = 0; i < 5; i++) { 28 | * *(a + i) = i + 1; 29 | * } 30 | * 31 | * return a; 32 | * } 33 | * 34 | */ 35 | int *isPower(int arr_count, int *arr, int *result_count) 36 | { 37 | int *result = malloc(arr_count * sizeof(int)); 38 | *result_count = 0; 39 | for (int i = 0; i < arr_count; i++) 40 | { 41 | int n = arr[i]; 42 | if ((n & (n - 1)) == 0) 43 | { 44 | result[*result_count] = 1; 45 | } 46 | else 47 | { 48 | result[*result_count] = 0; 49 | } 50 | (*result_count)++; 51 | } 52 | return result; 53 | } 54 | -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program3/caterpillar_leaves.c: -------------------------------------------------------------------------------- 1 | // k caterpillars are eating their way through n leaves. Each caterpillar falls from leaf to leaf in a unique sequence. All caterpillars start at a twig in position 0 and fall onto the leaves at positions between 1 and n. Each caterpillar i has an associated 'jump-number' ai. A caterpillar with jump number j eats leaves at positions that are multiples of j. It will proceed in the order j, 2j, 3j, ... till it reaches the end of the leaves, then it stops and builds a cocoon. 2 | 3 | // Input Format 4 | // The first line of the input is an integer n, total number of leaves. The second line of the input is an integer k, total number of caterpillars. Each of the next k lines contains a single integer ai. 5 | 6 | // Constraints 7 | // · 1 ≤ n ≤ 2 x 109 8 | // · 1 ≤ k ≤ 18 9 | // · 2 ≤ ai ≤ 22, 0 ≤ i < k 10 | 11 | // Output Format 12 | // The function should return the number of uneaten leaves 13 | 14 | #include 15 | int main() 16 | { 17 | int n, k; 18 | scanf("%d %d", &n, &k); 19 | int arr[n + 1]; 20 | for (int i = 1; i <= n; i++) 21 | { 22 | arr[i] = 1; 23 | } 24 | while (k != 0) 25 | { 26 | int l; 27 | scanf("%d", &l); 28 | for (int i = l; i <= n; i += l) 29 | { 30 | arr[i] = 0; 31 | } 32 | k--; 33 | } 34 | int c = 0; 35 | for (int i = 1; i <= n; i++) 36 | { 37 | if (arr[i] == 1) 38 | { 39 | c++; 40 | } 41 | } 42 | printf("%d", c); 43 | } -------------------------------------------------------------------------------- /String-Matching/KMP-Algo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void computeLPSArray(char *pat, int M, int *lps); 4 | void KMPSearch(char *pat, char *txt) 5 | { 6 | int M = strlen(pat); 7 | int N = strlen(txt); 8 | int lps[M]; 9 | computeLPSArray(pat, M, lps); 10 | int i = 0; 11 | int j = 0; 12 | while ((N - i) >= (M - j)) 13 | { 14 | if (pat[j] == txt[i]) 15 | { 16 | j++; 17 | i++; 18 | } 19 | if (j == M) 20 | { 21 | printf("%d ", i - j); 22 | j = lps[j - 1]; 23 | } 24 | else if (i < N && pat[j] != txt[i]) 25 | { 26 | if (j != 0) 27 | j = lps[j - 1]; 28 | else 29 | i = i + 1; 30 | } 31 | } 32 | printf("\n"); 33 | for (int i = 0; i < M; i++) 34 | printf("%d ", lps[i]); 35 | } 36 | void computeLPSArray(char *pat, int M, int *lps) 37 | { 38 | int len = 0; 39 | lps[0] = 0; 40 | int i = 1; 41 | while (i < M) 42 | { 43 | if (pat[i] == pat[len]) 44 | { 45 | len++; 46 | lps[i] = len; 47 | i++; 48 | } 49 | else 50 | { 51 | if (len != 0) 52 | len = lps[len - 1]; 53 | else 54 | { 55 | lps[i] = 0; 56 | i++; 57 | } 58 | } 59 | } 60 | } 61 | int main() 62 | { 63 | char txt[50]; 64 | char pat[50]; 65 | scanf("%[^\n]s", txt); 66 | scanf("%s", pat); 67 | KMPSearch(pat, txt); 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Backtracking/PP1-Hamiltonian-Cycle.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int help(int k, int n, int *vis, int c, int graph[][n]) 5 | { 6 | if (c == n - 1) 7 | { 8 | if (graph[k][0] == 1) 9 | { 10 | int l = 0; 11 | for (int i = 0; i < n; i++) 12 | { 13 | if (vis[i] != -1) 14 | l++; 15 | } 16 | if (l == n) 17 | { 18 | return 1; 19 | } 20 | } 21 | return 0; 22 | } 23 | for (int i = 0; i < n; i++) 24 | { 25 | if (vis[i] == -1 && graph[k][i] == 1) 26 | { 27 | if (k == 0) 28 | vis[k] = 0; 29 | c++; 30 | vis[i] = c; 31 | if (help(i, n, vis, c, graph)) 32 | return 1; 33 | c--; 34 | vis[i] = -1; 35 | } 36 | } 37 | return 0; 38 | } 39 | int main() 40 | { 41 | int n; 42 | scanf("%d", &n); 43 | int graph[n][n]; 44 | for (int i = 0; i < n; i++) 45 | { 46 | for (int j = 0; j < n; j++) 47 | { 48 | scanf("%d", &graph[i][j]); 49 | } 50 | } 51 | int vis[n]; 52 | memset(vis, -1, sizeof(vis)); 53 | int path[n]; 54 | int j = 0; 55 | if (help(0, n, vis, 0, graph)) 56 | { 57 | printf("The hamiltonian cycle is "); 58 | for (int i = 0; i < n; i++) 59 | { 60 | path[vis[i]] = j; 61 | j++; 62 | } 63 | for (int i = 0; i < n; i++) 64 | { 65 | printf("%d ", path[i]); 66 | } 67 | printf("0"); 68 | } 69 | else 70 | { 71 | printf("The hamiltonian cycle does not exist"); 72 | } 73 | return 0; 74 | } -------------------------------------------------------------------------------- /Basic-Programs/README.md: -------------------------------------------------------------------------------- 1 | # Basic Programs 2 | --- 3 | ## Basic 4 | 5 | - [Swap Two Numbers](Basic/Swap-Two-Numbers.c) 6 | - [Remainder and Quotient of given integers](Basic/Rem-Quo.c) 7 | - [Greatest of three integers](Basic/Greatest-of-three.c) 8 | - [Odd or Even](Basic/Odd-Even.c) 9 | - [Factorial of a Number](Basic/Factorial.c) 10 | - [Sum of first N natural numbers](Basic/Sum-of-First-N.c) 11 | - [Fibonacci Nth term](Basic/Fibo.c) 12 | - [Power of Integer](Basic/Pow-of-Int.c) 13 | - [Prime Number](Basic/Prime-Num.c) 14 | - [Reverse an Integer](Basic/Rev-Int.c) 15 | --- 16 | 17 | ## Basic Programs-1 18 | 19 | - [nth term of Fibonacci series](Basic-Program1/fibo.c) 20 | - [Find quotient and remainder](Basic-Program1/quo-and-rem.c) 21 | - [Odd or Even](Basic-Program1/odd-or-even.c) 22 | - [Swap two numbers](Basic-Program1/swap-two-nos.c) 23 | - [Power of integers](Basic-Program1/pow-of.c) 24 | 25 | --- 26 | 27 | ## Basic Programs-2 28 | 29 | - [Range of Numbers not divisible by N](Basic-Program2/nos-not-div-by-n.c) 30 | - [Admission Eligibility](Basic-Program2/admission-eligibility.c) 31 | - [Pencils Sold](Basic-Program2/pencil-count.c) 32 | - [Print Social Media by letter](Basic-Program2/letter-social-media.c) 33 | - [Print colour by character](Basic-Program2/letter-colour.c) 34 | 35 | --- 36 | 37 | ## Basic Programs-3 38 | 39 | - [Keystrokes Problem](Basic-Program3/keystrokes.c) 40 | - [Locker open/close Problem](Basic-Program3/locker.c) 41 | - [Power of Two](Basic-Program3/pow-of-two.c) 42 | - [Sorted Stock](Basic-Program3/sort-stock.c) 43 | - [Odd appearing character](Basic-Program3/single-char.c) 44 | - [Nums and Maxes](Basic-Program3/nums_maxes.c) 45 | - [Caterpillar and Leaves](Basic-Program3/caterpillar_leaves.c) 46 | - [Missing Integer in Sequence](Basic-Program3/missing_integer.c) 47 | - [Duplicate Products](Basic-Program3/duplicate_prod.c) 48 | - [Power of Two- isPower function](Basic-Program3/isPow.c) 49 | 50 | --- -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program3/nums_maxes.c: -------------------------------------------------------------------------------- 1 | // Given two arrays of positive integers, for each element in the second array, find the total number of elements in the first array which are less than or equal to that element. Store the values determined in an array. 2 | // For example, if the first array is [1, 2, 3] and the second array is [2, 4], then there are 2 elements in the first array less than or equal to 2. There are 3 elements in the first array which are less than or equal to 4. We can store these answers in an array, answer = [2, 3]. 3 | 4 | // Program Description 5 | // The program must return an array of m positive integers, one for each maxes[i] representing the total number of elements nums[j] satisfying nums[j] ≤ maxes[i] where 0 ≤ j < n and 0 ≤ i < m, in the given order. 6 | 7 | // The program has the following: 8 | 9 | // nums[nums[0],...nums[n-1]]: first array of positive integers 10 | 11 | // maxes[maxes[0],...maxes[n-1]]: second array of positive integers 12 | 13 | // Constraints 14 | // · 2 ≤ n, m ≤ 105 15 | // · 1 ≤ nums[j] ≤ 109, where 0 ≤ j < n. 16 | // · 1 ≤ maxes[i] ≤ 109, where 0 ≤ i < m. 17 | 18 | // Input Format For Custom Testing 19 | 20 | // Input from stdin will be processed as follows and passed to the program. 21 | 22 | // The first line contains an integer n, the number of elements in nums. 23 | // The next n lines each contain an integer describing nums[j] where 0 ≤ j < n. 24 | // The next line contains an integer m, the number of elements in maxes. 25 | // The next m lines each contain an integer describing maxes[i] where 0 ≤ i < m. 26 | 27 | #include 28 | int main() 29 | { 30 | int n, m; 31 | scanf("%d", &n); 32 | int nums[n]; 33 | for (int i = 0; i < n; i++) 34 | { 35 | scanf("%d", &nums[i]); 36 | } 37 | scanf("%d", &m); 38 | int maxes[m]; 39 | for (int i = 0; i < m; i++) 40 | { 41 | scanf("%d", &maxes[i]); 42 | } 43 | for (int i = 0; i < m; i++) 44 | { 45 | int c = 0; 46 | for (int j = 0; j < n; j++) 47 | { 48 | if (maxes[i] >= nums[j]) 49 | { 50 | c++; 51 | } 52 | } 53 | printf("%d\n", c); 54 | } 55 | return 0; 56 | } -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program3/duplicate_prod.c: -------------------------------------------------------------------------------- 1 | // Given n complex products, each with name, price and weight, find out how many duplicates of the original product are present within the products. Here, a duplicate is a product with all parameters, i.e. name, price and weight, equal to some other product. 2 | // Complete the code in the editor below. The program has to return a single integer denoting the number of duplicates within the products. 3 | 4 | // It has the following: 5 | // names: string array of size n, where namesi denotes the name of the ith product 6 | // prices: int array of size n, where pricesi denotes the price of the ith product 7 | // weights: int array of size n, where weightsi denotes the weight of the ith product 8 | 9 | // Constraints 10 | // · 1 ≤ n ≤ 105 11 | // · namesi is non-empty, has at most 10 characters, and all its characters are lowercase english letters 12 | // · 1 ≤ pricesi, weightsi ≤ 1000 13 | 14 | // Input Format Format for Custom Testing 15 | // Input from stdin will be processed as follows and passed to the function: 16 | // In the first line, there is a single integer n. 17 | // Then, n lines follow. In the ith of them, there is a single string namesi 18 | // In the next line, there is a single integer n. 19 | // Then, n lines follow. In the ith of them, there is a single integer pricesi 20 | // In the next line, there is a single integer n. 21 | // Then, n lines follow. In the ith of them, there is a single integer weightsi 22 | 23 | #include 24 | #include 25 | int main() 26 | { 27 | int n; 28 | scanf("%d", &n); 29 | char names[n][10]; 30 | int prices[n]; 31 | int weights[n]; 32 | for (int i = 0; i < n; i++) 33 | { 34 | scanf("%s", names[i]); 35 | } 36 | scanf("%d", &n); 37 | for (int i = 0; i < n; i++) 38 | { 39 | scanf("%d", &prices[i]); 40 | } 41 | scanf("%d", &n); 42 | for (int i = 0; i < n; i++) 43 | { 44 | scanf("%d", &weights[i]); 45 | } 46 | int c = 0; 47 | for (int i = 0; i < n - 1; i++) 48 | { 49 | for (int j = i + 1; j < n; j++) 50 | { 51 | if (strcmp(names[i], names[j]) == 0 && prices[i] == prices[j] && weights[i] == weights[j]) 52 | { 53 | c++; 54 | break; 55 | } 56 | } 57 | } 58 | printf("%d", c); 59 | return 0; 60 | } -------------------------------------------------------------------------------- /cryptic-fate5.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /String-Matching/README.md: -------------------------------------------------------------------------------- 1 | # String Matching 2 | --- 3 | ## 🌟 Problem 1: Rabin Karp Algorithm Implementation 4 | ### ❓ Question: 5 | 6 | Given two strings text and pattern, return the index of the first occurrence of pattern in text, or -1 if pattern is not part of text. 7 | 8 | Use Rabin Karp algorithm to solve the problem. 9 | 10 | 11 | ### 💻 Code: 12 | 13 | #include 14 | #include 15 | #define d 256 16 | int search(char pat[], char txt[], int q) 17 | { 18 | int M = strlen(pat); 19 | int N = strlen(txt); 20 | int i, j; 21 | int p = 0; 22 | int t = 0; 23 | int h = 1; 24 | for (i = 0; i < M - 1; i++) 25 | h = (h * d) % q; 26 | for (i = 0; i < M; i++) 27 | { 28 | p = (d * p + pat[i]) % q; 29 | t = (d * t + txt[i]) % q; 30 | } 31 | for (i = 0; i <= N - M; i++) 32 | { 33 | if (p == t) 34 | { 35 | for (j = 0; j < M; j++) 36 | { 37 | if (txt[i + j] != pat[j]) 38 | break; 39 | } 40 | if (j == M) 41 | { 42 | printf("%d", i); 43 | return 1; 44 | } 45 | } 46 | if (i < N - M) 47 | { 48 | t = (d * (t - txt[i] * h) + txt[i + M]) % q; 49 | if (t < 0) 50 | t = (t + q); 51 | } 52 | } 53 | printf("-1"); 54 | return 0; 55 | } 56 | int main() 57 | { 58 | char txt[50]; 59 | char pat[50]; 60 | scanf("%s", txt); 61 | scanf("%s", pat); 62 | int q = 101; 63 | search(pat, txt, q); 64 | return 0; 65 | } 66 | 67 | ### 🧐 Explanation: 68 | 69 | - 70 | 71 | --- 72 | ## 🌟 Problem 2: KMP Algorithm Implementation 73 | ### ❓ Question: 74 | 75 | 76 | Given two strings text and pattern, return the index of the all occurrence of pattern in text, or -1 if pattern is not part of text. 77 | 78 | Also Print the LPS array. 79 | 80 | Use KMP algorithm to solve the problem. 81 | 82 | ### 💻 Code: 83 | 84 | #include 85 | #include 86 | void computeLPSArray(char *pat, int M, int *lps); 87 | void KMPSearch(char *pat, char *txt) 88 | { 89 | int M = strlen(pat); 90 | int N = strlen(txt); 91 | int lps[M]; 92 | computeLPSArray(pat, M, lps); 93 | int i = 0; 94 | int j = 0; 95 | while ((N - i) >= (M - j)) 96 | { 97 | if (pat[j] == txt[i]) 98 | { 99 | j++; 100 | i++; 101 | } 102 | if (j == M) 103 | { 104 | printf("%d ", i - j); 105 | j = lps[j - 1]; 106 | } 107 | else if (i < N && pat[j] != txt[i]) 108 | { 109 | if (j != 0) 110 | j = lps[j - 1]; 111 | else 112 | i = i + 1; 113 | } 114 | } 115 | printf("\n"); 116 | for (int i = 0; i < M; i++) 117 | printf("%d ", lps[i]); 118 | } 119 | void computeLPSArray(char *pat, int M, int *lps) 120 | { 121 | int len = 0; 122 | lps[0] = 0; 123 | int i = 1; 124 | while (i < M) 125 | { 126 | if (pat[i] == pat[len]) 127 | { 128 | len++; 129 | lps[i] = len; 130 | i++; 131 | } 132 | else 133 | { 134 | if (len != 0) 135 | len = lps[len - 1]; 136 | else 137 | { 138 | lps[i] = 0; 139 | i++; 140 | } 141 | } 142 | } 143 | } 144 | int main() 145 | { 146 | char txt[50]; 147 | char pat[50]; 148 | scanf("%[^\n]s", txt); 149 | scanf("%s", pat); 150 | KMPSearch(pat, txt); 151 | return 0; 152 | } 153 | 154 | ### 🧐 Explanation: 155 | 156 | - 157 | 158 | --- -------------------------------------------------------------------------------- /Basic-Programs/Basic-Program3/sort-stock.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Complete the 'itemsSort' function below. 3 | * 4 | * The function is expected to return an INTEGER_ARRAY. 5 | * The function accepts INTEGER_ARRAY items as parameter. 6 | */ 7 | 8 | /* 9 | * To return the integer array from the function, you should: 10 | * - Store the size of the array to be returned in the result_count variable 11 | * - Allocate the array statically or dynamically 12 | * 13 | * For example, 14 | * int* return_integer_array_using_static_allocation(int* result_count) { 15 | * *result_count = 5; 16 | * 17 | * static int a[5] = {1, 2, 3, 4, 5}; 18 | * 19 | * return a; 20 | * } 21 | * 22 | * int* return_integer_array_using_dynamic_allocation(int* result_count) { 23 | * *result_count = 5; 24 | * 25 | * int *a = malloc(5 * sizeof(int)); 26 | * 27 | * for (int i = 0; i < 5; i++) { 28 | * *(a + i) = i + 1; 29 | * } 30 | * 31 | * return a; 32 | * } 33 | * 34 | */ 35 | #include 36 | #include 37 | struct node 38 | { 39 | int key; 40 | int val; 41 | struct node *next; 42 | }; 43 | typedef struct node Map; 44 | void insert(Map *mpp, int key, int val) 45 | { 46 | Map *newMap = malloc(sizeof(Map)); 47 | newMap->key = key; 48 | newMap->val = val; 49 | newMap->next = NULL; 50 | Map *pos = mpp; 51 | if (pos->next != NULL) 52 | { 53 | while (pos->next != NULL) 54 | { 55 | pos = pos->next; 56 | } 57 | pos->next = newMap; 58 | } 59 | else 60 | { 61 | pos->next = newMap; 62 | } 63 | } 64 | void delete (Map *mpp, int val) 65 | { 66 | Map *pos = mpp; 67 | while (pos != NULL && pos->next->val != val) 68 | { 69 | pos = pos->next; 70 | } 71 | Map *temp = pos->next; 72 | pos->next = temp->next; 73 | free(temp); 74 | } 75 | void incCount(Map *mpp, int key) 76 | { 77 | Map *pos = mpp; 78 | while (pos != NULL && pos->key != key) 79 | { 80 | pos = pos->next; 81 | } 82 | pos->val = pos->val + 1; 83 | } 84 | int find(Map *mpp, int key) 85 | { 86 | Map *pos = mpp; 87 | while (pos != NULL && pos->key != key) 88 | { 89 | pos = pos->next; 90 | } 91 | if (pos != NULL) 92 | { 93 | return 1; 94 | } 95 | return 0; 96 | } 97 | void sort(int items[], int items_count) 98 | { 99 | for (int i = 0; i < items_count - 1; i++) 100 | { 101 | for (int j = 0; j < items_count - i - 1; j++) 102 | { 103 | if (items[j] > items[j + 1]) 104 | { 105 | int temp = items[j]; 106 | items[j] = items[j + 1]; 107 | items[j + 1] = temp; 108 | } 109 | } 110 | } 111 | } 112 | Map *findVal(Map *mpp, int val) 113 | { 114 | Map *pos = mpp; 115 | while (pos != NULL && pos->val != val) 116 | { 117 | pos = pos->next; 118 | } 119 | return pos; 120 | } 121 | int *itemsSort(int items_count, int *items, int *result_count) 122 | { 123 | int *result = malloc((items_count) * sizeof(int)); 124 | sort(items, items_count); 125 | Map *mpp = malloc(sizeof(Map)); 126 | mpp->next = NULL; 127 | int c = 0; 128 | for (int i = 0; i < items_count; i++) 129 | { 130 | if (find(mpp, items[i])) 131 | { 132 | incCount(mpp, items[i]); 133 | } 134 | else 135 | { 136 | insert(mpp, items[i], 1); 137 | c++; 138 | } 139 | } 140 | int count[c]; 141 | int k = 0; 142 | Map *pos = mpp->next; 143 | while (pos != NULL) 144 | { 145 | count[k] = pos->val; 146 | pos = pos->next; 147 | k++; 148 | } 149 | sort(count, c); 150 | int f = 0; 151 | for (int i = 0; i < c; i++) 152 | { 153 | Map *val = findVal(mpp, count[i]); 154 | int v = val->val; 155 | while (v != 0) 156 | { 157 | result[f] = val->key; 158 | f++; 159 | v--; 160 | } 161 | delete (mpp, val->val); 162 | } 163 | *result_count = items_count; 164 | return result; 165 | } 166 | // int main() 167 | // { 168 | // int items_count; 169 | // scanf("%d", &items_count); 170 | // int items[items_count]; 171 | // for (int i = 0; i < items_count; i++) 172 | // { 173 | // scanf("%d", &items[i]); 174 | // } 175 | // int *res; 176 | // res = itemsSort(items_count, items, res); 177 | // for (int i = 0; i < items_count; i++) 178 | // { 179 | // printf("%d\n", res[i]); 180 | // } 181 | // return 0; 182 | // } 183 | -------------------------------------------------------------------------------- /Backtracking/README.md: -------------------------------------------------------------------------------- 1 | # Backtracking 2 | --- 3 | ## 🌟 Problem 1: 4 | ### ❓ Question: 5 | 6 | Hamiltonian Path in an undirected graph is a path that visits each vertex exactly once. A Hamiltonian cycle (or Hamiltonian circuit) is a Hamiltonian Path such that there is an edge (in the graph) from the last vertex to the first vertex of the Hamiltonian Path. Determine whether a given graph contains Hamiltonian Cycle or not. If it contains, then prints the path. Following are the input and output of the required function. 7 | 8 | Input: 9 | First the number of vertices in the graph V. 10 | Next A 2D array graph[V][V] where V is the number of vertices in graph and graph[V][V] is adjacency matrix representation of the graph. 11 | A value graph[i][j] is 1 if there is a direct edge from i to j, otherwise graph[i][j] is 0. 12 | 13 | Output: 14 | An array path[V] that should contain the Hamiltonian Path. path[i] should represent the ith vertex in the Hamiltonian Path. 15 | The code should also return false if there is no Hamiltonian Cycle in the graph. 16 | 17 | 18 | ### 💻 Code: 19 | 20 | #include 21 | #include 22 | 23 | int help(int k, int n, int *vis, int c, int graph[][n]) 24 | { 25 | if (c == n - 1) 26 | { 27 | if (graph[k][0] == 1) 28 | { 29 | int l = 0; 30 | for (int i = 0; i < n; i++) 31 | { 32 | if (vis[i] != -1) 33 | l++; 34 | } 35 | if (l == n) 36 | return 1; 37 | } 38 | return 0; 39 | } 40 | for (int i = 0; i < n; i++) 41 | { 42 | if (vis[i] == -1 && graph[k][i] == 1) 43 | { 44 | if (k == 0) 45 | vis[k] = 0; 46 | c++; 47 | vis[i] = c; 48 | if (help(i, n, vis, c, graph)) 49 | return 1; 50 | c--; 51 | vis[i] = -1; 52 | } 53 | } 54 | return 0; 55 | } 56 | int main() 57 | { 58 | int n; 59 | scanf("%d", &n); 60 | int graph[n][n]; 61 | for (int i = 0; i < n; i++) 62 | { 63 | for (int j = 0; j < n; j++) 64 | { 65 | scanf("%d", &graph[i][j]); 66 | } 67 | } 68 | int vis[n]; 69 | memset(vis, -1, sizeof(vis)); 70 | int path[n]; 71 | int j = 0; 72 | if (help(0, n, vis, 0, graph)) 73 | { 74 | printf("The hamiltonian cycle is "); 75 | for (int i = 0; i < n; i++) 76 | { 77 | path[vis[i]] = j; 78 | j++; 79 | } 80 | for (int i = 0; i < n; i++) 81 | { 82 | printf("%d ", path[i]); 83 | } 84 | printf("0"); 85 | } 86 | else 87 | { 88 | printf("The hamiltonian cycle does not exist"); 89 | } 90 | return 0; 91 | } 92 | 93 | ### ⌚🚀 Complexity: 94 | 95 | - Time Complexity: O(nn) 96 | - Space Complexity: O(n2)+O(n)+O(n) 97 | 98 | ### 🧐 Explanation: 99 | 100 | - 101 | 102 | --- 103 | ## 🌟 Problem 2: 104 | ### ❓ Question: 105 | 106 | A proper coloring of a graph is an assignment of colors to the vertices of the graph so that no two adjacent vertices have the same color. Given an undirected graph and a number m, determine if the graph can be coloured with at most m colours such that no two adjacent vertices of the graph are colored with the same color. Here coloring of a graph means the assignment of colors to all vertices. 107 | 108 | Input: 109 | An integer V is the number of vertices in the graph. 110 | An integer m is the maximum number of colors that can be used. 111 | A 2D array graph[V][V] where V is the number of vertices in graph and graph[V][V] is an adjacency matrix representation of the graph. 112 | A value graph[i][j] is 1 if there is a direct edge from i to j, otherwise graph[i][j] is 0. 113 | 114 | Output: 115 | An array color[V] that should have numbers from 1 to m. color[i] should represent the color assigned to the ith vertex. The code should also return false if the graph cannot be colored with m colors. 116 | 117 | ### 💻 Code: 118 | 119 | 120 | 121 | ### ⌚🚀 Complexity: 122 | 123 | - Time Complexity: 124 | - Space Complexity: 125 | ### 🧐 Explanation: 126 | 127 | - 128 | 129 | --- 130 | 131 | 132 | ## 🌟 Problem 3: 133 | ### ❓ Question: 134 | 135 | Subset sum problem is to find subset of elements that are selected from a given set whose sum adds up to a given number K. Given a set of non-negative numbers and the value K, determine if there is a subset of numbers whose sum value is equal to K. It is assumed that the input set is unique (no duplicates are presented). 136 | 137 | Input: 138 | An integer which is equal to the number of input non-negative integers. 139 | An integer which is equal to sum value. 140 | The set of non-negative integers 141 | 142 | Output: 143 | True/False - Saying whether the subset exists or not. 144 | 145 | 146 | ### 💻 Code: 147 | 148 | #include 149 | 150 | int help(int *set, int n, int k, int j) 151 | { 152 | if (k == 0 || j == n) 153 | { 154 | if (k == 0) 155 | return 1; 156 | return 0; 157 | } 158 | if (help(set, n, k - set[j], j + 1)) 159 | return 1; 160 | if (help(set, n, k, j + 1)) 161 | return 1; 162 | return 0; 163 | } 164 | int main() 165 | { 166 | int n, k; 167 | scanf("%d %d", &n, &k); 168 | int set[n]; 169 | for (int i = 0; i < n; i++) 170 | { 171 | scanf("%d", &set[i]); 172 | } 173 | if (help(set, n, k, 0)) 174 | { 175 | printf("True"); 176 | } 177 | else 178 | { 179 | printf("False"); 180 | } 181 | return 0; 182 | } 183 | 184 | 185 | ### ⌚🚀 Complexity: 186 | 187 | - Time Complexity: O(2n) 188 | - Space Complexity: O(n)+O(n) 189 | 190 | ### 🧐 Explanation: 191 | 192 | - 193 | 194 | --- 195 | 196 | 197 | -------------------------------------------------------------------------------- /Dynamic-Programming/README.md: -------------------------------------------------------------------------------- 1 | # Dynamic Programming 2 | --- 3 | ## 🌟 Problem 1: 4 | ### ❓ Question: 5 | 6 | Ram and Sita are playing with numbers by giving puzzles to each other. Now it was Ram term, so he gave Sita a positive integer ‘n’ and two numbers 1 and 3. He asked her to find the possible ways by which the number n can be represented using 1 and 3.Write any efficient algorithm to find the possible ways. 7 | 8 | Example 1: 9 | 10 | Input: 6 11 | Output:6 12 | Explanation: There are 6 ways to 6 represent number with 1 and 3 13 | 1+1+1+1+1+1 14 | 3+3 15 | 1+1+1+3 16 | 1+1+3+1 17 | 1+3+1+1 18 | 3+1+1+1 19 | 20 | Input Format 21 | First Line contains the number n. 22 | 23 | Output Format 24 | Print: The number of possible ways ‘n’ can be represented using 1 and 3. 25 | 26 | 27 | ### 💻 Code: 28 | 29 | #include 30 | 31 | int main() 32 | { 33 | int n; 34 | scanf("%d", &n); 35 | long long int prev1 = 1, prev2 = 1, prev3 = 1, curr; 36 | for (int i = 3; i <= n; i++) 37 | { 38 | curr = prev1 + prev3; 39 | prev3 = prev2; 40 | prev2 = prev1; 41 | prev1 = curr; 42 | } 43 | printf("%lld", prev1); 44 | return 0; 45 | } 46 | 47 | ### ⌚🚀 Complexity: 48 | 49 | - Time Complexity: O(n) 50 | - Space Complexity: O(1) 51 | 52 | ### 🧐 Explanation: 53 | 54 | - 55 | 56 | --- 57 | ## 🌟 Problem 2: Longest Common Subsequence 58 | ### ❓ Question: 59 | 60 | Given two strings find the length of the common longest subsequence(need not be contiguous) between the two. 61 | 62 | Example: 63 | 64 | s1: ggtabe 65 | s2: tgatasb 66 | 67 | 68 | The length is 4 69 | 70 | 71 | ### 💻 Code: 72 | 73 | #include 74 | #include 75 | 76 | int max(int a, int b) 77 | { 78 | if (a > b) 79 | return a; 80 | return b; 81 | } 82 | int help(int a, int b, char *x, char *y, int dp[][b + 1]) 83 | { 84 | if (a < 0 || b < 0) 85 | return 0; 86 | if (dp[a][b] != -1) 87 | return dp[a][b]; 88 | if (x[a] == y[b]) 89 | return 1 + help(a - 1, b - 1, x, y, dp); 90 | return dp[a][b] = max(help(a - 1, b, x, y, dp), help(a, b - 1, x, y, dp)); 91 | } 92 | int main() 93 | { 94 | char x[100], y[100]; 95 | scanf("%s %s", x, y); 96 | int a = strlen(x), b = strlen(y); 97 | int dp[a][b]; 98 | memset(dp, -1, sizeof(dp)); 99 | printf("%d", help(strlen(x) - 1, strlen(y) - 1, x, y, dp)); 100 | return 0; 101 | } 102 | ### ⌚🚀 Complexity: 103 | 104 | - Time Complexity: O(n*m) 105 | - Space Complexity: O(n*m) 106 | ### 🧐 Explanation: 107 | 108 | - 109 | 110 | --- 111 | ## 🌟 Problem 3: Longest non-decreasing Subsequence 112 | ### ❓ Question: 113 | 114 | 115 | Find the length of the Longest Non-decreasing Subsequence in a given Sequence. 116 | 117 | Eg: 118 | 119 | Input:9 120 | 121 | Sequence:[-1,3,4,5,2,2,2,2,3] 122 | The subsequence is: [-1,2,2,2,2,3] 123 | 124 | Output:6 125 | 126 | 127 | ### 💻 Code: 128 | 129 | #include 130 | #include 131 | #include 132 | int max(int a, int b) 133 | { 134 | if (a > b) 135 | return a; 136 | return b; 137 | } 138 | int help(int *arr, int n, int i, int prev, int dp[][n + 1]) 139 | { 140 | if (i == n) 141 | return 0; 142 | if (dp[i][prev + 1] != -1) 143 | return dp[i][prev + 1]; 144 | int l = help(arr, n, i + 1, prev, dp); 145 | int r = INT_MIN; 146 | if (prev == -1 || arr[i] >= arr[prev]) 147 | r = 1 + help(arr, n, i + 1, i, dp); 148 | return dp[i][prev + 1] = max(l, r); 149 | } 150 | int main() 151 | { 152 | int n; 153 | scanf("%d", &n); 154 | int arr[n]; 155 | for (int i = 0; i < n; i++) 156 | scanf("%d", &arr[i]); 157 | int dp[n][n + 1]; 158 | memset(dp, -1, sizeof(dp)); 159 | int ans = help(arr, n, 0, -1, dp); 160 | printf("%d", ans); 161 | return 0; 162 | } 163 | 164 | ### ⌚🚀 Complexity: 165 | 166 | - Time Complexity: O(n2) 167 | - Space Complexity: O(n2) 168 | 169 | ### 🧐 Explanation: 170 | 171 | - 172 | 173 | --- 174 | ## 🌟 Problem 4: Playing With Chessboard 175 | ### ❓ Question: 176 | 177 | Ram is given with an n*n chessboard with each cell with a monetary value. Ram stands at the (0,0), that the position of the top left white rook. He is been given a task to reach the bottom right black rook position (n-1, n-1) constrained that he needs to reach the position by traveling the maximum monetary path under the condition that he can only travel one step right or one step down the board. Help ram to achieve it by providing an efficient DP algorithm. 178 | 179 | 180 | Example: 181 | Input 182 | 3 183 | 1 2 4 184 | 2 3 4 185 | 8 7 1 186 | Output: 187 | 19 188 | 189 | Explanation: 190 | Totally there will be 6 paths among that the optimal is: 191 | Optimal path value- 1+2+8+7+1=19 192 | 193 | 194 | ### 💻 Code: 195 | 196 | #include 197 | #include 198 | 199 | int max(int a, int b) 200 | { 201 | if (a > b) 202 | return a; 203 | return b; 204 | } 205 | 206 | int help(int n, int chess[][n], int i, int j, int dp[][n]) 207 | { 208 | if (i == n - 1 && j == n - 1) 209 | return chess[n - 1][n - 1]; 210 | if (dp[i][j] != -1) 211 | return dp[i][j]; 212 | int l = 0, r = 0; 213 | if (i < n - 1) 214 | l = chess[i][j] + help(n, chess, i + 1, j, dp); 215 | if (j < n - 1) 216 | r = chess[i][j] + help(n, chess, i, j + 1, dp); 217 | return dp[i][j] = max(l, r); 218 | } 219 | 220 | int main() 221 | { 222 | int n; 223 | scanf("%d", &n); 224 | int chess[n][n]; 225 | for (int i = 0; i < n; i++) 226 | { 227 | for (int j = 0; j < n; j++) 228 | scanf("%d", &chess[i][j]); 229 | } 230 | int dp[n][n]; 231 | memset(dp, -1, sizeof(dp)); 232 | printf("%d", help(n, chess, 0, 0, dp)); 233 | return 0; 234 | } 235 | 236 | ### ⌚🚀 Complexity: 237 | 238 | - Time Complexity: O(n2) 239 | - Space Complexity: O(n2) 240 | 241 | ### 🧐 Explanation: 242 | 243 | - 244 | 245 | --- 246 | 247 | 248 | -------------------------------------------------------------------------------- /Divide-and-Conquer/README.md: -------------------------------------------------------------------------------- 1 | # Divide and Conquer based problems 2 | --- 3 | ## 🌟 Problem 1: Number of zeroes in a given array 4 | ### ❓ Question: 5 | 6 | Given an array of 1s and 0s this has all 1s first followed by all 0s. Aim is to find the number of 0s. Write a program using Divide and Conquer to Count the number of zeroes in the given array. 7 | Input Format 8 | - First Line Contains Integer m – Size of array 9 | - Next m lines Contains m numbers – Elements of an array 10 | 11 | Output Format 12 | - First Line Contains Integer – Number of zeroes present in the given array. 13 | ### 💻 Code: 14 | 15 | #include 16 | 17 | void find(int *arr, int *c, int low, int high, int mid) 18 | { 19 | if (arr[mid] == 0 && low <= high) 20 | { 21 | (*c)++; 22 | find(arr, c, low, mid - 1, (low + mid - 1) / 2); 23 | } 24 | if (low <= high) 25 | { 26 | find(arr, c, mid + 1, high, (mid + 1 + high) / 2); 27 | } 28 | } 29 | int main() 30 | { 31 | int n; 32 | scanf("%d", &n); 33 | int arr[n]; 34 | for (int i = 0; i < n; i++) 35 | { 36 | scanf("%d", &arr[i]); 37 | } 38 | int c = 0; 39 | find(arr, &c, 0, n - 1, n / 2); 40 | printf("%d", c); 41 | return 0; 42 | } 43 | 44 | ### 🧐 Explanation: 45 | 46 | - 47 | 48 | --- 49 | ## 🌟 Problem 2: Majority Element 50 | ### ❓ Question: 51 | 52 | Given an array nums of size n, return the majority element. 53 | The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. 54 | 55 | Constraints: 56 | - n == nums.length 57 | - 1 <= n <= 5 * 104 58 | - -2^31 <= nums[i] <= 2^31 - 1 59 | 60 | ### 💻 Code: 61 | 62 | #include 63 | 64 | int main() 65 | { 66 | int n; 67 | scanf("%d", &n); 68 | int arr[n]; 69 | for (int i = 0; i < n; i++) 70 | { 71 | scanf("%d", &arr[i]); 72 | } 73 | for (int i = 0; i < n - 1; i++) 74 | { 75 | int c = 0; 76 | for (int j = i + 1; j < n; j++) 77 | { 78 | if (arr[i] == arr[j]) 79 | { 80 | c++; 81 | } 82 | } 83 | if (c >= n / 2) 84 | { 85 | printf("%d", arr[i]); 86 | return 0; 87 | } 88 | } 89 | } 90 | ### 🧐 Explanation: 91 | 92 | - 93 | 94 | --- 95 | ## 🌟 Problem 3: Finding Floor Value 96 | ### ❓ Question: 97 | 98 | Given a sorted array and a value x, the floor of x is the largest element in array smaller than or equal to x. Write divide and conquer algorithm to find floor of x. 99 | 100 | Input Format 101 | - First Line Contains Integer n – Size of array 102 | - Next n lines Contains n numbers – Elements of an array 103 | - Last Line Contains Integer x – Value for x 104 | 105 | Output Format 106 | - First Line Contains Integer – Floor value for x 107 | 108 | ### 💻 Code: 109 | 110 | #include 111 | 112 | void help(int *arr, int n, int x, int *max, int low, int high, int mid) 113 | { 114 | if (arr[mid] <= x && arr[mid] > (*max)) 115 | { 116 | *max = arr[mid]; 117 | } 118 | if (low > high) 119 | return; 120 | if (x < arr[mid]) 121 | help(arr, n, x, max, low, mid - 1, (low + mid - 1) / 2); 122 | if (x > arr[mid]) 123 | help(arr, n, x, max, mid + 1, high, (high + mid + 1) / 2); 124 | } 125 | int main() 126 | { 127 | int n; 128 | scanf("%d", &n); 129 | int arr[n]; 130 | for (int i = 0; i < n; i++) 131 | { 132 | scanf("%d", &arr[i]); 133 | } 134 | int x; 135 | scanf("%d", &x); 136 | int max = 0; 137 | help(arr, n, x, &max, 0, n - 1, n / 2); 138 | printf("%d", max); 139 | return 0; 140 | } 141 | 142 | ### 🧐 Explanation: 143 | 144 | - 145 | 146 | --- 147 | ## 🌟 Problem 4: Find an element in sorted matrix 148 | ### ❓ Question: 149 | 150 | Given a sorted matrix mat[m][m] and an element ‘x’. Find whether the element x is present in the matrix. Matrix is sorted in a way such that all elements in a row are sorted in increasing order and for row ‘i’, where 1 <= i <= m-1, the first element of row ‘i’ is greater than or equal to the last element of row ‘i-1’. 151 | 152 | Input Format 153 | - First Line Contains Integer m – Size of array 154 | - Next m*m lines Contains m*m numbers – Elements of an array 155 | - An Integer x – Element to check present in matrix or not 156 | 157 | Output Format 158 | - First Line Contains FOUND or NOT FOUND - If x is the present print “FOUND” otherwise print “NOT FOUND” 159 | 160 | ### 💻 Code: 161 | 162 | #include 163 | 164 | int binary_search(int *arr, int x, int low, int high, int mid) 165 | { 166 | if (low > high) 167 | return -1; 168 | if (arr[mid] == x) 169 | return 1; 170 | else if (arr[mid] > x) 171 | return binary_search(arr, x, low, mid - 1, (low + mid - 1) / 2); 172 | else 173 | return binary_search(arr, x, mid + 1, high, (high + mid + 1) / 2); 174 | } 175 | int main() 176 | { 177 | int n; 178 | scanf("%d", &n); 179 | int arr[n * n]; 180 | for (int i = 0; i < n * n; i++) 181 | { 182 | scanf("%d", &arr[i]); 183 | } 184 | int x; 185 | scanf("%d", &x); 186 | if (binary_search(arr, x, 0, n * n - 1, n * n / 2)==1) 187 | { 188 | printf("FOUND"); 189 | } 190 | else 191 | { 192 | printf("NOT FOUND"); 193 | } 194 | return 0; 195 | } 196 | 197 | ### 🧐 Explanation: 198 | 199 | - 200 | 201 | --- 202 | ## 🌟 Problem 5: Two elements sum to x 203 | ### ❓ Question: 204 | 205 | Given a sorted array of integers say arr[] and a number x. Write a recursive program using divide and conquer strategy to check if there exist two elements in the array whose sum = x. If there exist such two elements then return the numbers, otherwise print as “No”. 206 | Note: Write a Divide and Conquer Solution 207 | 208 | Input Format 209 | - First Line Contains Integer n – Size of array 210 | - Next n lines Contains n numbers – Elements of an array 211 | - Last Line Contains Integer x – Sum Value 212 | 213 | Output Format 214 | - First Line Contains Integer – Element1 215 | - Second Line Contains Integer – Element2 (Element 1 and Elements 2 together sums to value “x”) 216 | 217 | ### 💻 Code: 218 | 219 | #include 220 | #include 221 | #include 222 | int help(int *arr, int n, int *map, int j, int x) 223 | { 224 | if (j == 2) 225 | { 226 | if (x == 0) 227 | { 228 | return 1; 229 | } 230 | return 0; 231 | } 232 | for (int i = 0; i < n; i++) 233 | { 234 | if (map[i] == 0) 235 | { 236 | map[i] = 1; 237 | x -= arr[i]; 238 | if (help(arr, n, map, j + 1, x)) 239 | return 1; 240 | ; 241 | map[i] = 0; 242 | x += arr[i]; 243 | } 244 | } 245 | return 0; 246 | } 247 | int main() 248 | { 249 | int n; 250 | scanf("%d", &n); 251 | int arr[n]; 252 | for (int i = 0; i < n; i++) 253 | { 254 | scanf("%d", &arr[i]); 255 | } 256 | int x; 257 | scanf("%d", &x); 258 | int map[n]; 259 | memset(map, 0, sizeof(map)); 260 | if (help(arr, n, map, 0, x)) 261 | { 262 | for (int i = 0; i < n; i++) 263 | { 264 | if (map[i] == 1) 265 | { 266 | printf("%d\n", arr[i]); 267 | } 268 | } 269 | } 270 | else 271 | { 272 | printf("No"); 273 | } 274 | return 0; 275 | } 276 | 277 | ### 🧐 Explanation: 278 | 279 | - 280 | 281 | --- 282 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # REC Design and Analysis of Algorithms (DAA) Digital Cafe 2 |

3 | 4 |

5 | 6 | Contains solutions and explanations for REC's DAA Digital Cafe 7 | 8 | **Disclaimer:** Students are requested to try the questions on their own and look into explanations only when not able to understand the question and then the code if and only if not able to understand the explanation. 9 | 10 | **Note:** If bugs or typo found, please feel free to inform about the correction. 11 | 12 | --- 13 | ## 🌟 [Basic Programs](Basic-Programs/) 14 | 15 | - [Basic](Basic-Programs/Basic/) 16 | 17 | - [Swap Two Numbers](Basic-Programs/Basic/Swap-Two-Numbers.c) 18 | - [Remainder and Quotient of given integers](Basic-Programs/Basic/Rem-Quo.c) 19 | - [Greatest of three integers](Basic-Programs/Basic/Greatest-of-three.c) 20 | - [Odd or Even](Basic-Programs/Basic/Odd-Even.c) 21 | - [Factorial of a Number](Basic-Programs/Basic/Factorial.c) 22 | - [Sum of first N natural numbers](Basic-Programs/Basic/Sum-of-First-N.c) 23 | - [Fibonacci Nth term](Basic-Programs/Basic/Fibo.c) 24 | - [Power of Integer](Basic-Programs/Basic/Pow-of-Int.c) 25 | - [Prime Number](Basic-Programs/Basic/Prime-Num.c) 26 | - [Reverse an Integer](Basic-Programs/Basic/Rev-Int.c) 27 | 28 | - [Basic Programs-1](Basic-Programs/Basic-Program1/) 29 | 30 | - [nth term of Fibonacci series](Basic-Programs/Basic-Program1/fibo.c) 31 | - [Find quotient and remainder](Basic-Programs/Basic-Program1/quo-and-rem.c) 32 | - [Odd or Even](Basic-Programs/Basic-Program1/odd-or-even.c) 33 | - [Swap two numbers](Basic-Programs/Basic-Program1/swap-two-nos.c) 34 | - [Power of integers](Basic-Programs/Basic-Program1/pow-of.c) 35 | 36 | - [Basic Programs-2](Basic-Programs/Basic-Program2/) 37 | 38 | - [Range of Numbers not divisible by N](Basic-Programs/Basic-Program2/nos-not-div-by-n.c) 39 | - [Admission Eligibility](Basic-Programs/Basic-Program2/admission-eligibility.c) 40 | - [Pencils Sold](Basic-Programs/Basic-Program2/pencil-count.c) 41 | - [Print Social Media by letter](Basic-Programs/Basic-Program2/letter-social-media.c) 42 | - [Print colour by character](Basic-Programs/Basic-Program2/letter-colour.c) 43 | 44 | - [Basic Programs-3](Basic-Programs/Basic-Program3/) 45 | 46 | - [Keystrokes Problem](Basic-Programs/Basic-Program3/keystrokes.c) 47 | - [Locker open/close Problem](Basic-Programs/Basic-Program3/locker.c) 48 | - [Power of Two](Basic-Programs/Basic-Program3/pow-of-two.c) 49 | - [Sorted Stock](Basic-Programs/Basic-Program3/sort-stock.c) 50 | - [Odd appearing character](Basic-Programs/Basic-Program3/single-char.c) 51 | - [Nums and Maxes](Basic-Programs/Basic-Program3/nums_maxes.c) 52 | - [Caterpillar and Leaves](Basic-Programs/Basic-Program3/caterpillar_leaves.c) 53 | - [Missing Integer in Sequence](Basic-Programs/Basic-Program3/missing_integer.c) 54 | - [Duplicate Products](Basic-Programs/Basic-Program3/duplicate_prod.c) 55 | - [Power of Two- isPower function](Basic-Programs/Basic-Program3/isPow.c) 56 | --- 57 | ## 🌟 [Finding Time Complexity of Algorithms](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/tree/main/Finding-Time-Complexity-of-Algorithms/README.md) 58 | - [**COMPLETE SOLUTION WITH EXPLANATION**](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Finding-Time-Complexity-of-Algorithms/README.md) 59 | - [Problem 1-Comparing Complexity Quiz](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Finding-Time-Complexity-of-Algorithms/Problem-1-Comparing-Complexity-Quiz.c) 60 | - [Problem 2-Finding Complexity using Counter Method](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Finding-Time-Complexity-of-Algorithms/Problem-2-Finding-Complexity-using-Counter-Method.c) 61 | - [Problem 3-Finding Complexity using Counter Method](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Finding-Time-Complexity-of-Algorithms/Problem-3-Finding-Complexity-using-Counter-Method.c) 62 | - [Problem 4-Finding Complexity using Counter Method](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Finding-Time-Complexity-of-Algorithms/Problem-4-Finding-Complexity-using-Counter-Method.c) 63 | - [Problem 5-Finding Complexity using Counter Method](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Finding-Time-Complexity-of-Algorithms/Problem-5-Finding-Complexity-using-Counter-Method.c) 64 | - [Problem 6-Finding Complexity using Counter Method](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Finding-Time-Complexity-of-Algorithms/Problem-6-Finding-Complexity-using-Counter-Method.c) 65 | 66 | --- 67 | 68 | ## 🌟 [Brute Force Strategy- Simple Programs](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Brute-Force-Strategy-Simple-Programs/README.md) 69 | - [**COMPLETE SOLUTION WITH EXPLANATION**](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Brute-Force-Strategy-Simple-Programs/README.md) 70 | - [Problem 1-Finding Duplicates](Brute-Force-Strategy-Simple-Programs/Finding-Duplicates.c) 71 | - [Problem 2-Bubble Sort](Brute-Force-Strategy-Simple-Programs/Bubble-Sort.c) 72 | - [Problem 3-Pair with Difference](Brute-Force-Strategy-Simple-Programs/Pair-with-Difference.c) 73 | - [Problem 4- String Matching](Brute-Force-Strategy-Simple-Programs/String-Matching.c) 74 | - [Problem 5- Print Intersection of two sorted arrays](Brute-Force-Strategy-Simple-Programs/Print-intersection-of-2-sorted-arrays.c) 75 | - [Problem 6- Add to array](Brute-Force-Strategy-Simple-Programs/Add-Array.c) 76 | - [Problem 7- Find Words](Brute-Force-Strategy-Simple-Programs/Find-Words.c) 77 | - [Problem 8- Subset Sum](Brute-Force-Strategy-Simple-Programs/Subset-Sum.c) 78 | - [Problem 9- Assignment Problem](Brute-Force-Strategy-Simple-Programs/Assignment-Prob.c) 79 | 80 | --- 81 | 82 | ## 🌟 [Divide and Conquer Problems](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Divide-and-Conquer/README.md) 83 | 84 | - [**COMPLETE SOLUTION WITH EXPLANATION**](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Divide-and-Conquer/README.md) 85 | - [Number of Zeroes](Divide-and-Conquer/Number-of-zeroes.c) 86 | - [Majority Element](Divide-and-Conquer/Majority-Element.c) 87 | - [Finding Floor Value](Divide-and-Conquer/Finding-Floor-Value.c) 88 | - [Find an element in a sorted matrix](Divide-and-Conquer/Finding-element-in-sorted-matrix.c) 89 | - [Two elements sum to x](Divide-and-Conquer/Two-elements-sum-to-x.c) 90 | 91 | --- 92 | 93 | ## 🌟 [Greedy Algorithm Problems](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Greedy-Algorithms/README.md) 94 | 95 | - [**COMPLETE SOLUTION WITH EXPLANATION**](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Greedy-Algorithms/README.md) 96 | - [Problem 1](Greedy-Algorithms/Problem-1.c) 97 | - [Problem 2](Greedy-Algorithms/Problem-2.c) 98 | - [Problem 3](Greedy-Algorithms/Problem-3.c) 99 | - [Problem 4](Greedy-Algorithms/Problem-4.c) 100 | - [Problem 5](Greedy-Algorithms/Problem-5.c) 101 | - [Problem 6](Greedy-Algorithms/Problem-6.c) 102 | 103 | --- 104 | ## 🌟 [Dynamic Programming Problems](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Dynamic-Programming/README.md) 105 | 106 | - [**COMPLETE SOLUTION WITH EXPLANATION**](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Dynamic-Programming/README.md) 107 | - [Playing With Numbers](Dynamic-Programming/Playing-With-Numbers.c) 108 | - [Longest Common Subsequence](Dynamic-Programming/Longest-Common-Subsequence.c) 109 | - [Longest non-decreasing subsequence](Dynamic-Programming/Longest-non-Decreasing-Subsquence.c) 110 | - [Playing wth chessboard](Dynamic-Programming/Playing-with-Chessboard.c) 111 | 112 | --- 113 | ## 🌟 [Backtracking Problems](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Backtracking/README.md) 114 | 115 | - [**COMPLETE SOLUTION WITH EXPLANATION**](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/Backtracking/README.md) 116 | - [PP1- Hamiltonian Cycle](Backtracking/PP1-Hamiltonian-Cycle.c) 117 | - [PP2- Graph Colouring](Backtracking/PP2-Graph-Colouring.c) 118 | - [PP3- Subset Sum](Backtracking/PP3-Subset-Sum.c) 119 | 120 | --- 121 | ## 🌟 [String Matching Problems](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/String-Matching/README.md) 122 | 123 | - [**COMPLETE SOLUTION WITH EXPLANATION**](https://github.com/CrypticFate5/REC-Design-and-Analysis-of-Algorithm-DAA-Digital-Cafe/blob/main/String-Matching/README.md) 124 | - [Rabin Karp Algorithm](String-Matching/Rabin-Karp.c) 125 | - [KMP Algorithm](String-Matching/KMP-Algo.c) 126 | 127 | --- 128 |

129 | 130 | 131 | 132 |

133 | -------------------------------------------------------------------------------- /Greedy-Algorithms/README.md: -------------------------------------------------------------------------------- 1 | # Greedy Algorithms 2 | --- 3 | ## 🌟 Problem 1: 4 | ### ❓ Question: 5 | 6 | Write a program to take value V and we want to make change for V Rs, and we have infinite supply of each of the denominations in Indian currency, i.e., we have infinite supply of { 1, 2, 5, 10, 20, 50, 100, 500, 1000} valued coins/notes, what is the minimum number of coins and/or notes needed to make the change. 7 | 8 | Input Format: 9 | - Take an integer from stdin. 10 | 11 | Output Format: 12 | - Print the integer which is change of the number. 13 | 14 | 15 | ### 💻 Code: 16 | 17 | #include 18 | 19 | int main() 20 | { 21 | int n, ans = 0; 22 | scanf("%d", &n); 23 | int arr[] = {1000, 500, 100, 50, 20, 10, 5, 2, 1}; 24 | for (int i = 0; i < 9; i++) 25 | { 26 | if ((arr[i] % n) == arr[i]) 27 | { 28 | n -= arr[i]; 29 | i = 0; 30 | ans++; 31 | } 32 | if (n == 0) 33 | { 34 | break; 35 | } 36 | } 37 | printf("%d", ans); 38 | return 0; 39 | } 40 | 41 | ### 🧐 Explanation: 42 | 43 | - 44 | 45 | --- 46 | ## 🌟 Problem 2: 47 | ### ❓ Question: 48 | 49 | Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. 50 | 51 | Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number. 52 | 53 | ### 💻 Code: 54 | 55 | #include 56 | 57 | void sort(int *arr, int n) 58 | { 59 | for (int i = 0; i < n - 1; i++) 60 | { 61 | for (int j = 0; j < n - i - 1; j++) 62 | { 63 | if (arr[j] > arr[j + 1]) 64 | { 65 | int temp = arr[j]; 66 | arr[j] = arr[j + 1]; 67 | arr[j + 1] = temp; 68 | } 69 | } 70 | } 71 | } 72 | int main() 73 | { 74 | int x, y, ans = 0, k = 0; 75 | scanf("%d", &x); 76 | int child[x]; 77 | for (int i = 0; i < x; i++) 78 | { 79 | scanf("%d", &child[i]); 80 | } 81 | scanf("%d", &y); 82 | int cookies[y]; 83 | for (int i = 0; i < y; i++) 84 | { 85 | scanf("%d", &cookies[i]); 86 | } 87 | sort(child, x); 88 | sort(cookies, y); 89 | for (int i = 0; i < x; i++) 90 | { 91 | for (int j = k; j < y; j++) 92 | { 93 | if (child[i] <= cookies[j]) 94 | { 95 | ans++; 96 | k = j + 1; 97 | break; 98 | } 99 | } 100 | } 101 | printf("%d", ans); 102 | return 0; 103 | } 104 | 105 | ### 🧐 Explanation: 106 | 107 | - 108 | 109 | --- 110 | ## 🌟 Problem 3: 111 | ### ❓ Question: 112 | 113 | At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and order one at a time (in the order specified by bills). Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. You must provide the correct change to each customer so that the net transaction is that the customer pays $5. 114 | 115 | Note that you don't have any change in hand at first. 116 | 117 | Given an integer array bills where bills[i] is the bill the ith customer pays, return true if you can provide every customer with correct change, or false otherwise. 118 | 119 | ### 💻 Code: 120 | 121 | #include 122 | 123 | int main() 124 | { 125 | int n, count5 = 0, count10 = 0; 126 | scanf("%d", &n); 127 | int bills[n]; 128 | for (int i = 0; i < n; i++) 129 | { 130 | scanf("%d", &bills[i]); 131 | } 132 | for (int i = 0; i < n; i++) 133 | { 134 | if (bills[i] == 5) 135 | { 136 | count5++; 137 | } 138 | else if (bills[i] == 10) 139 | { 140 | count10++; 141 | if (count5 > 0) 142 | { 143 | count5--; 144 | } 145 | else 146 | { 147 | printf("false"); 148 | return 0; 149 | } 150 | } 151 | else if (bills[i] == 20) 152 | { 153 | if (count10 > 0 && count5 > 0) 154 | { 155 | count10--; 156 | count5--; 157 | } 158 | else if (count5 >= 3) 159 | { 160 | count5 -= 3; 161 | } 162 | else if (count5 < 0) 163 | { 164 | printf("false"); 165 | return 0; 166 | } 167 | } 168 | } 169 | printf("true"); 170 | return 0; 171 | } 172 | 173 | ### 🧐 Explanation: 174 | 175 | - 176 | 177 | --- 178 | ## 🌟 Problem 4: 179 | ### ❓ Question: 180 | 181 | A person needs to eat burgers. Each burger contains a count of calorie. After eating the burger, the person needs to run a distance to burn out his calories. 182 | If he has eaten i burgers with c calories each, then he has to run at least 3i * c kilometers to burn out the calories. For example, if he ate 3 183 | burgers with the count of calorie in the order: [1, 3, 2], the kilometers he needs to run are (30 * 1) + (31 * 3) + (32 * 2) = 1 + 9 + 18 = 28. 184 | But this is not the minimum, so need to try out other orders of consumption and choose the minimum value. Determine the minimum distance he needs to run. Note: He can eat burger in any order and use an efficient sorting algorithm. 185 | 186 | ### 💻 Code: 187 | 188 | #include 189 | 190 | void sort(int *arr, int n) 191 | { 192 | for (int i = 0; i < n - 1; i++) 193 | { 194 | for (int j = 0; j < n - i - 1; j++) 195 | { 196 | if (arr[j] < arr[j + 1]) 197 | { 198 | int temp = arr[j]; 199 | arr[j] = arr[j + 1]; 200 | arr[j + 1] = temp; 201 | } 202 | } 203 | } 204 | } 205 | int power(int a) 206 | { 207 | if (a == 0) 208 | return 1; 209 | return 3 * power(a - 1); 210 | } 211 | int main() 212 | { 213 | int n; 214 | scanf("%d", &n); 215 | int arr[n]; 216 | for (int i = 0; i < n; i++) 217 | { 218 | scanf("%d", &arr[i]); 219 | } 220 | sort(arr, n); 221 | int ans = 0; 222 | for (int i = 0; i < n; i++) 223 | { 224 | ans += power(i) * arr[i]; 225 | } 226 | printf("%d", ans); 227 | } 228 | 229 | ### 🧐 Explanation: 230 | 231 | - 232 | 233 | --- 234 | ## 🌟 Problem 5: 235 | ### ❓ Question: 236 | 237 | In a gift shop there are N different types of gifts available and the prices for all N different types of gifts are given. It is a discount sale where you can buy a single gift and get at-most M other gifts for free. 238 | - Find the minimum amount of money that is needed to buy all the N different gifts. 239 | - Find the maximum amount of money that is needed to buy all the N different gifts. 240 | 241 | In both these cases utilize the discount and get maximum possible gifts back. If M or more gifts are available, for every purchase take M gifts. In case less than M gifts are in stock, then take all gifts for purchasing a gift. 242 | For example, if there are four gifts in shop with prices 3,2,1,4 respectively and M=2. Since M=2, if we purchase one gift we can take at most two more for free. So in the first case we purchase the gift whose price is 1 and take gifts worth 3 and 4 for free, also you can purchase gift worth 2 as well. Therefore, minimum cost = 1+2 = 3. In the second case we purchase the gift whose price is 4 and take gifts worth 1 and 2 for free, also you can buy gift worth 3 as well. Therefore, maximum cost = 3+4 = 7. 243 | 244 | ### 💻 Code: 245 | 246 | #include 247 | #include 248 | void sort(int *arr, int n) 249 | { 250 | for (int i = 0; i < n - 1; i++) 251 | { 252 | for (int j = 0; j < n - i - 1; j++) 253 | { 254 | if (arr[j] > arr[j + 1]) 255 | { 256 | int temp = arr[j]; 257 | arr[j] = arr[j + 1]; 258 | arr[j + 1] = temp; 259 | } 260 | } 261 | } 262 | } 263 | int main() 264 | { 265 | int n, m, min = 0, max = 0; 266 | scanf("%d", &n); 267 | int arr[n]; 268 | for (int i = 0; i < n; i++) 269 | { 270 | scanf("%d", &arr[i]); 271 | } 272 | scanf("%d", &m); 273 | sort(arr, n); 274 | int k = n; 275 | for (int i = 0; i < k; i++) 276 | { 277 | k -= m; 278 | min += arr[i]; 279 | } 280 | k = 0; 281 | for (int i = n - 1; i >= k; i--) 282 | { 283 | k += m; 284 | max += arr[i]; 285 | } 286 | printf("%d\n%d", min, max); 287 | return 0; 288 | } 289 | 290 | ### 🧐 Explanation: 291 | 292 | - 293 | 294 | --- 295 | ## 🌟 Problem 6: 296 | ### ❓ Question: 297 | 298 | Given lengths of n pipes and a value m. You can either increase or decrease the length of every pipe by m (only once) where m > 0. The task is to minimize the difference between the lengths of the longest and the shortest pipe after modifications, and output this difference. 299 | 300 | ### 💻 Code: 301 | 302 | #include 303 | 304 | void sort(int n, int *arr) 305 | { 306 | for (int i = 0; i < n - 1; i++) 307 | { 308 | for (int j = 0; j < n - i - 1; j++) 309 | { 310 | if (arr[j] > arr[j + 1]) 311 | { 312 | int temp = arr[j]; 313 | arr[j] = arr[j + 1]; 314 | arr[j + 1] = temp; 315 | } 316 | } 317 | } 318 | } 319 | int min(int a, int b) 320 | { 321 | if (a < b) 322 | return a; 323 | return b; 324 | } 325 | int max(int a, int b) 326 | { 327 | if (a > b) 328 | return a; 329 | return b; 330 | } 331 | int main() 332 | { 333 | int n, m; 334 | scanf("%d", &n); 335 | int arr[n]; 336 | for (int i = 0; i < n; i++) 337 | scanf("%d", &arr[i]); 338 | scanf("%d", &m); 339 | sort(n, arr); 340 | int ans = arr[n - 1] - arr[0]; 341 | for (int i = 1; i < n; i++) 342 | { 343 | int x = max(arr[n - 1] - m, arr[i - 1] + m); 344 | int y = min(arr[0] + m, arr[i] - m); 345 | int d = x - y; 346 | if (d < 0) 347 | continue; 348 | ans = min(ans, d); 349 | } 350 | printf("%d", ans); 351 | return 0; 352 | } 353 | 354 | ### 🧐 Explanation: 355 | 356 | - 357 | 358 | --- 359 | -------------------------------------------------------------------------------- /Finding-Time-Complexity-of-Algorithms/README.md: -------------------------------------------------------------------------------- 1 | # Finding Time Complexity of Algorithms 2 | --- 3 | ## 🌟 Problem 1: Comparing Complexity 4 | ### ❓ Question: 5 | Given two positive integers, determine the GCD of the numbers. 6 | Solve the above Problem Statement using two algorithms, hence write two functions, 7 | 8 | 1.Iterative Function 1 (Consecutive Integer Checking): pass the 2 integers to the function, and print the GCD and return the no of times the loop gets executed in the function. 9 | 2.Iterative Function 2 (Euclid’s Algorithm): pass the 2 integers to the function, and print the GCD and return the no of times the loop gets executed in the function. 10 | 11 | Compare the return values and print which function is best for a specific problem instance. 12 | ### 💻 Code: 13 | 14 | #include 15 | void gcdIter(int a, int b, int *gcd, int *c) 16 | { 17 | for (int i = b; i > 1; i--) 18 | { 19 | if (a % i == 0 && b % i == 0) 20 | { 21 | (*gcd) = i; 22 | break; 23 | } 24 | (*c)++; 25 | } 26 | } 27 | int gcdEucl(int a, int b, int *c) 28 | { 29 | if (b == 0) 30 | { 31 | return a; 32 | } 33 | (*c)++; 34 | return gcdEucl(b, a % b, c); 35 | } 36 | int main() 37 | { 38 | int a, b; 39 | scanf("%d %d", &a, &b); 40 | int gcd = 0, c = 1; 41 | gcdIter(a, b, &gcd, &c); 42 | printf("%d\n", gcd); 43 | int c1 = 0; 44 | printf("%d\n", gcdEucl(a, b, &c1)); 45 | printf("%d\n", c); 46 | printf("%d\n", c1); 47 | if (c1 < c) 48 | printf("Function 2\n"); 49 | else 50 | { 51 | printf("Function 1\n"); 52 | } 53 | return 0; 54 | } 55 | 56 | ### 🧐 Explanation: 57 | 58 | - Here we write a simple for loop(iterative based) and recursive(Euclid's Algorithm) solutions for finding GCD of given two numbers as given in the question. 59 | - Find the GCD of two numbers concept: [Click here!](https://www.geeksforgeeks.org/c-program-find-gcd-hcf-two-numbers/) 60 | - In these functions we pass a integer pointer 'c' (c1 and c2 here) such that it keeps a track of the count of iterations done by the respective functions. 61 | - We have passed a pointer variable (a prime example for call by reference too!) so as to save the value of count in every iteration/recursion. 62 | - The value of c1 and c2 is incremented in each iteration/recursion call, which is denoted as (*c)++. 63 | - We used a * while incrementing because pointer is being passed to the funtion, and to convert the address(pointer basically) we have used * to revert back to the value of c1 or c2 at their respective address. 64 | - And finally in the main function, we print the c1 and c2 as shown in the code. 65 | --- 66 | ## 🌟 Problem 2: Finding Complexity using Counter Method 67 | ### ❓ Question: 68 | Convert the following algorithm into a program and find its time complexity using the counter method. 69 | 70 | void function (int n) 71 | { 72 | int i= 1, s =1; 73 | while(s <= n) 74 | { 75 | i++; 76 | s += i; 77 | } 78 | } 79 | Note: No need of counter increment for declarations and scanf() and count variable printf() statements. 80 | Manually find the complexity using counter method and write the same in observation 81 | ### 💻 Code: 82 | #include 83 | void function(int n, int *c) 84 | { 85 | int i = 1, s = 1; 86 | (*c) += 2; 87 | while (s <= n) 88 | { 89 | (*c)++; 90 | i++; 91 | (*c)++; 92 | s += i; 93 | (*c)++; 94 | } 95 | (*c)++; 96 | } 97 | int main() 98 | { 99 | int n; 100 | scanf("%d", &n); 101 | int c = 0; 102 | function(n, &c); 103 | printf("%d", c); 104 | } 105 | ### 🧐 Explanation: 106 | - First we increment the count by 2 as we have declared i and s as 1. 107 | - Then inside the while loop, for condition checking(s<=n), we increment the counter by 1. 108 | - Then again incrementing c by 1 for i++ statement. 109 | - And inside while loop, finally incrementing the counter by one for s+=i condition. 110 | - Now the termination condition is checked for the while loop, and we increment the counter by 1 for the while loop termination check. 111 | - And finally print the count in the main function. 112 | 113 | --- 114 | ## 🌟 Problem 3: Finding Complexity using Counter Method 115 | ### ❓ Question: 116 | Convert the following algorithm into a program and find its time complexity using the counter method. 117 | 118 | void func(int n) 119 | { 120 | if (n == 1) 121 | { 122 | printf(""); 123 | } 124 | else 125 | { 126 | for (int i = 1; i <= n; i++) 127 | { 128 | for (int j = 1; j <= n; j++) 129 | { 130 | printf(""); 131 | printf(""); 132 | break; 133 | } 134 | } 135 | } 136 | } 137 | 138 | Note: No need of counter increment for declarations and scanf() and count variable printf() statements. 139 | ### 💻 Code: 140 | 141 | #include 142 | void func(int n, int *c) 143 | { 144 | if (n == 1) 145 | { 146 | (*c)++; 147 | // printf(" "); 148 | (*c)++; 149 | } 150 | else 151 | { 152 | (*c)++; 153 | for (int i = 1; i <= n; i++) 154 | { 155 | (*c)++; 156 | for (int j = 1; j <= n; j++) 157 | { 158 | (*c)++; 159 | // printf(" "); 160 | (*c)++; 161 | // printf(" "); 162 | (*c)++; 163 | break; 164 | } 165 | (*c)++; 166 | } 167 | (*c)++; 168 | } 169 | } 170 | int main() 171 | { 172 | int n; 173 | scanf("%d", &n); 174 | int c = 0; 175 | func(n, &c); 176 | printf("%d", c); 177 | } 178 | 179 | ### 🧐 Explanation: 180 | 181 | - Here, the question clearly states that count the printf() statement also. 182 | - In the function, first it checks for n==1, so c incremented by 1 and for printf, again incremented by 1. 183 | - For the else part execution, it's incremented by 1. 184 | - Inside the else part, the first for loop,for condtion checking, it's incremented by 1 (every check till before the termination). 185 | - Inside the first for loop, we increment the counter again for the nested for loop and then for the two print statements respectively. Note that break statement here has no significance as statements such as break, continue are used only inside if-else statements for their correct execution. 186 | - And then for each for loop, the termination is done and counter is incremented by 1 for both the loops. 187 | - Finally the count is printed inside the main function. 188 | 189 | --- 190 | ## 🌟 Problem 4: Finding Complexity using Counter Method 191 | ### ❓ Question: 192 | Convert the following algorithm into a program and find its time complexity using counter method. 193 | 194 | Factor(n) 195 | { 196 | { 197 | for (i = 1; i <= num; ++i) 198 | { 199 | if (num % i == 0) 200 | { 201 | printf("%d ", i); 202 | } 203 | } 204 | return 0; 205 | } 206 | 207 | Note: No need of counter increment for declarations and scanf() and printf() statements. 208 | ### 💻 Code: 209 | 210 | #include 211 | void factor(int n, int *c) 212 | { 213 | for (int i = 1; i <= n; i++) 214 | { 215 | (*c)++; 216 | (*c)++; 217 | if (n % i == 0) 218 | { 219 | // printf("%d ", i); 220 | } 221 | } 222 | (*c)++; 223 | (*c)++; 224 | // return ; 225 | } 226 | int main() 227 | { 228 | int n, c = 0; 229 | scanf("%d", &n); 230 | factor(n, &c); 231 | printf("%d", c); 232 | } 233 | 234 | ### 🧐 Explanation: 235 | 236 | - In the factor function, the counter is incremented inside the for loop for condition checking. 237 | - Again the counter is incremented for the if statement and also no increment inside the if statement as the given question has stated that no need for incrementing the counter for printf() statements. 238 | - Again outside the for loop, counter is incremented for the termination process. 239 | - And then incremented again for the return statement. 240 | - Finally inside the main function, the counter is printed. 241 | 242 | --- 243 | ## 🌟 Problem 5: Finding Complexity using Counter Method 244 | ### ❓ Question: 245 | Convert the following algorithm into a program and find its time complexity using counter method. 246 | 247 | void function(int n) 248 | { 249 | int c= 0; 250 | for(int i=n/2; i 260 | void function(int n,int *c1){ 261 | int c= 0; 262 | (*c1)++; 263 | for(int i=n/2; i 314 | void reverse(int n,int*c){ 315 | int rev = 0, remainder; 316 | (*c)++; 317 | while (n != 0) { 318 | (*c)++; 319 | remainder = n % 10; 320 | (*c)++; 321 | rev = rev * 10 + remainder; 322 | (*c)++; 323 | n/= 10; 324 | (*c)++; 325 | } 326 | (*c)++; 327 | // printf("%d",rev); 328 | (*c)++; 329 | } 330 | int main(){ 331 | int n,c=0; 332 | scanf("%d",&n); 333 | reverse(n,&c); 334 | printf("%d",c); 335 | } 336 | 337 | ### 🧐 Explanation 338 | 339 | - Increment the counter by 1 for the initialization of rev=0. 340 | - In the while loop, increment the counter for condtion check. 341 | - Then for each statements inside the while loop, we have incremented the counter by 1. 342 | - And outside the while loop, increment counter for the termination check and also the printf() as the question clearly states that count the printf() statements. 343 | - Finally in the main function, print the counter. 344 | --- 345 | -------------------------------------------------------------------------------- /Brute-Force-Strategy-Simple-Programs/README.md: -------------------------------------------------------------------------------- 1 | # Brute Force Strategy- Simple Programs 2 | --- 3 | ## 🌟 Problem 1: Finding Duplicates 4 | ### ❓ Question: 5 | 6 | Given a read only array of n + 1 integers between 1 and n, find one number that repeats. 7 | ### 💻 Code: 8 | include 9 | int main() 10 | { 11 | int n; 12 | scanf("%d", &n); 13 | int arr[n]; 14 | for (int i = 0; i < n; i++) 15 | scanf("%d", &arr[i]); 16 | for (int i = 0; i < n; i++) 17 | { 18 | for (int j = i + 1; j < n; j++) 19 | { 20 | if (arr[i] == arr[j]) 21 | { 22 | printf("%d", arr[i]); 23 | return 0; 24 | } 25 | } 26 | } 27 | return 0; 28 | } 29 | 30 | ### 🧐 Explanation: 31 | 32 | - First we take the size of array and declare the array. 33 | - Then we iterate over the array by taking each element(index i) and by comparing it throughout the array(index j). 34 | - If the choosen element is equal to the iterating element, then we print that element and terminate the program. 35 | 36 | --- 37 | ## 🌟 Problem 2: Bubble Sort 38 | ### ❓ Question: 39 | 40 | Bubble sort is one of the easiest and brute force sorting algorithms. It is used to sort elements in either ascending or descending order. 41 | 42 | Write a program to implement the bubble sort Algorithm. Get the number of elements as in input in the first line and then get the numbers as input. Display the numbers in ascending order as output. 43 | 44 | ### 💻 Code: 45 | 46 | #include 47 | void bubbleSort(int *arr, int n) 48 | { 49 | for (int i = 0; i < n - 1; i++) 50 | { 51 | for (int j = 0; j < n - i - 1; j++) 52 | { 53 | if (arr[j] > arr[j + 1]) 54 | { 55 | int temp = arr[j]; 56 | arr[j] = arr[j + 1]; 57 | arr[j + 1] = temp; 58 | } 59 | } 60 | } 61 | } 62 | int main() 63 | { 64 | int n; 65 | scanf("%d", &n); 66 | int arr[n]; 67 | for (int i = 0; i < n; i++) 68 | { 69 | scanf("%d", &arr[i]); 70 | } 71 | bubbleSort(arr, n); 72 | for (int i = 0; i < n; i++) 73 | { 74 | printf("%d ", arr[i]); 75 | } 76 | return 0; 77 | } 78 | ### 🧐 Explanation: 79 | 80 | - The function bubbleSort, takes an array and an integer which is the size of the array. 81 | - We iterate from 0 to n-1 in the first for loop. The reason to choose to go upto n-1 is because when n-1 passes are done, the array would be sorted and there is no need of sorting the array again to set the last element in last position of array. 82 | - Then we iterate from 0 to n-i-1 in the second for loop. The reason here to choose n-i-1 is after every i'th pass, the greatest element would be set at n-i th position, so we go from 0 to n-i-1 th position. 83 | - Then we check the corresponding elements (arr[j] and arr[j+1]) and if the next element is smaller than it's previous one, we swap it. 84 | - And finally we call the funtion in main by taking appropriate inputs from the user. 85 | 86 | --- 87 | ## 🌟 Problem 3: Pair with Difference 88 | ### ❓ Question: 89 | 90 | Given an array A of sorted integers and another non negative integer k, find if there exists 2 indices i and j such that A[j] - A[i] = k, i != j. 91 | 92 | ### 💻 Code: 93 | #include 94 | int main() 95 | { 96 | int n; 97 | scanf("%d", &n); 98 | int arr[n]; 99 | for (int i = 0; i < n; i++) 100 | { 101 | scanf("%d", &arr[i]); 102 | } 103 | int k; 104 | scanf("%d", &k); 105 | for (int i = 0; i < n; i++) 106 | { 107 | for (int j = 0; j < n; j++) 108 | { 109 | if (i != j && (arr[i] - arr[j] == k)) 110 | { 111 | printf("1"); 112 | return 0; 113 | } 114 | } 115 | } 116 | printf("0"); 117 | return 0; 118 | } 119 | ### 🧐 Explanation: 120 | 121 | - First we take the size of array and then declare the array and fill the array with elements and then take the the input integer k. 122 | - Then by exactly implementing the condtion given in the question, that is , we iterate over the array to key a value and then again iterate(the nested for loop) over it to find a pair of value such that the difference of the elements is k. 123 | - The nested for loops are made and the condition i!=j is given to ensure that the same elements are not forming a pair and the other condition is same as the question. 124 | - If the condition satisfies, we print 1 and terminate the program by return 0. 125 | - Else if no pairs are found, the program will print 0 and terminate. 126 | 127 | --- 128 | ## 🌟 Problem 4: String Matching 129 | ### ❓ Question: 130 | Given a string and a pattern identify whether the pattern occurs in the given string or not. If the pattern occurs in the given string then print 1 otherwise print 0. 131 | 132 | 133 | ### 💻 Code: 134 | 135 | #include 136 | #include 137 | int main() 138 | { 139 | char st[50], sub[50]; 140 | scanf("%s", st); 141 | scanf("%s", sub); 142 | for (int i = 0; i < strlen(st); i++) 143 | { 144 | char s[strlen(sub)]; 145 | memcpy(s, &st[i], strlen(sub)); 146 | s[strlen(sub)] = '\0'; 147 | if (strcmp(s, sub) == 0) 148 | { 149 | printf("1"); 150 | return 0; 151 | } 152 | } 153 | printf("0"); 154 | return 0; 155 | } 156 | 157 | ### 🧐 Explanation: 158 | 159 | - 160 | - 161 | --- 162 | ## 🌟 Problem 5: Print intersection of 2 sorted arrays 163 | ### ❓ Question: 164 | 165 | Find the intersection of two sorted arrays. 166 | 167 | OR in other words, 168 | 169 | Given 2 sorted arrays, find all the elements which occur in both the arrays. 170 | 171 | Input Format 172 | The first line contains T, the number of test cases. Following T lines contain: 173 | 1. Line 1 contains N1, followed by N1 integers of the first array 174 | 2. Line 2 contains N2, followed by N2 integers of the second array 175 | 176 | Output Format 177 | The intersection of the arrays in a single line 178 | 179 | ### 💻 Code: 180 | 181 | #include 182 | int main() 183 | { 184 | int t; 185 | scanf("%d", &t); 186 | while (t--) 187 | { 188 | int x, y; 189 | scanf("%d", &x); 190 | int arr1[x]; 191 | for (int i = 0; i < x; i++) 192 | { 193 | scanf("%d", &arr1[i]); 194 | } 195 | scanf("%d", &y); 196 | int arr2[y]; 197 | for (int i = 0; i < y; i++) 198 | { 199 | scanf("%d", &arr2[i]); 200 | } 201 | for (int i = 0; i < x; i++) 202 | { 203 | for (int j = 0; j < y; j++) 204 | { 205 | if (arr1[i] == arr2[j]) 206 | { 207 | printf("%d ", arr1[i]); 208 | break; 209 | } 210 | } 211 | } 212 | } 213 | return 0; 214 | } 215 | ### 🧐 Explanation: 216 | 217 | - 218 | 219 | --- 220 | ## 🌟 Problem 6: Add Array 221 | ### ❓ Question: 222 | 223 | The array-form of an integer num is an array representing its digits in left to right order. 224 | For example, for num = 1321, the array form is [1,3,2,1]. 225 | Given num, the array-form of an integer, and an integer k, return the array-form of the integer num + k. 226 | 227 | Constraints: 228 | 229 | - 1 <= num.length <= 104 230 | - 0 <= num[i] <= 9 231 | - num does not contain any leading zeros except for the zero itself. 232 | - 1 <= k <= 104 233 | ### 💻 Code: 234 | 235 | #include 236 | int main() 237 | { 238 | long int sub = 0, n, k, ans; 239 | scanf("%ld", &n); 240 | long int y = n; 241 | while (n--) 242 | { 243 | long int x, pos = 1; 244 | scanf("%ld", &x); 245 | for (int i = 0; i < n; i++) 246 | { 247 | pos *= 10; 248 | } 249 | sub += (pos * x); 250 | } 251 | scanf("%ld", &k); 252 | ans = k + sub; 253 | long int arrans[y + 10]; 254 | long q = 0; 255 | while (ans > 0) 256 | { 257 | arrans[q] = ans % 10; 258 | ans /= 10; 259 | q++; 260 | } 261 | for (long int i = q - 1; i >= 0; i--) 262 | { 263 | printf("%ld ", arrans[i]); 264 | } 265 | return 0; 266 | } 267 | 268 | ### 🧐 Explanation: 269 | 270 | - 271 | 272 | --- 273 | ## 🌟 Problem 7: Find Words 274 | ### ❓ Question: 275 | 276 | You are given an array of strings words and a string chars. A string is good if it can be formed by characters from chars (each character can only be used once). 277 | Return the sum of lengths of all good strings in words. 278 | 279 | Example 1: 280 | Input: words = ["cat","bt","hat","tree"], chars = "atach" 281 | Output: 6 282 | Explanation: The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6. 283 | 284 | Example 2: 285 | Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr" 286 | Output: 10 287 | Explanation: The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10. 288 | 289 | Constraints: 290 | 1 <= words.length <= 1000 291 | 1 <= words[i].length, chars.length <= 100 292 | words[i] and chars consist of lowercase English letters. 293 | 294 | 295 | ### 💻 Code: 296 | 297 | #include 298 | #include 299 | #include 300 | int main() 301 | { 302 | int n; 303 | scanf("%d", &n); 304 | char words[n][100]; 305 | int i = 0; 306 | while (i < n) 307 | { 308 | scanf("%s", words[i]); 309 | i++; 310 | } 311 | char chars[100]; 312 | scanf("%s", chars); 313 | int ans = 0; 314 | for (int i = 0; i < n; i++) 315 | { 316 | int mpp[26] = {0}; 317 | for (int i = 0; i < strlen(chars); i++) 318 | { 319 | mpp[chars[i] - 'a'] += 1; 320 | } 321 | int c = 0; 322 | for (int j = 0; j < strlen(words[i]); j++) 323 | { 324 | for (int k = 0; k < strlen(chars); k++) 325 | { 326 | if (words[i][j] == chars[k] && mpp[chars[k] - 'a'] > 0) 327 | { 328 | c++; 329 | mpp[chars[k] - 'a']--; 330 | break; 331 | } 332 | } 333 | } 334 | if (c == strlen(words[i])) 335 | { 336 | ans += c; 337 | } 338 | } 339 | printf("%d", ans); 340 | return 0; 341 | } 342 | 343 | ### 🧐 Explanation: 344 | 345 | - 346 | 347 | --- 348 | ## 🌟 Problem 8: Subset Sum 349 | ### ❓ Question: 350 | 351 | Given a non-empty array nums containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. 352 | 353 | Constraints: 354 | 1 <= n <= 200 355 | 1 <= nums[i] <= 100 356 | 357 | ### 💻 Code: 358 | 359 | #include 360 | int help(int *arr, int n, int j, int sum,int asum) 361 | { 362 | if (j == n) 363 | { 364 | if (sum == asum/2) 365 | return 1; 366 | if (j == n) 367 | return 0; 368 | } 369 | if (help(arr, n, j + 1, sum - arr[j],asum)) 370 | return 1; 371 | if (help(arr, n, j + 1, sum,asum)) 372 | return 1; 373 | return 0; 374 | } 375 | int main() 376 | { 377 | int n, sum = 0; 378 | scanf("%d", &n); 379 | int arr[n]; 380 | for (int i = 0; i < n; i++) 381 | { 382 | scanf("%d", &arr[i]); 383 | sum += arr[i]; 384 | } 385 | if (sum % 2 == 1) 386 | { 387 | printf("false"); 388 | return 0; 389 | } 390 | if (help(arr, n, 0, sum,sum)) 391 | { 392 | printf("true"); 393 | } 394 | else 395 | { 396 | printf("false"); 397 | } 398 | return 0; 399 | } 400 | 401 | ### 🧐 Explanation: 402 | 403 | - 404 | 405 | --- 406 | ## 🌟 Problem 9: Assignment Problem 407 | ### ❓ Question: 408 | 409 | The assignment problem consists of finding, in a weighted bipartite graph, a matching of a given size, in which the sum of weights of the edges is minimum. If the numbers of agents and tasks are equal, then the problem is called balanced assignment. Otherwise, it is called unbalanced assignment. 410 | 411 | For example, suppose an accounts officer has 4 subordinates and 4 tasks. The subordinates differ in efficiency and take different time to perform each task. If one task is to be assigned to one person in such a way that the total person hours are minimised, the problem is called an assignment problem. 412 | 413 | Write C program to implement the above problem. 414 | 415 | ### 💻 Code: 416 | 417 | #include 418 | 419 | void assignment(int *initial, int row, int n, int j, int arr[][n], int sub, int *ans, int map[]) 420 | { 421 | if (j == n) 422 | { 423 | if ((*initial) == 1) 424 | { 425 | *initial = 0; 426 | (*ans) = sub; 427 | } 428 | else 429 | { 430 | if (sub < (*ans)) 431 | { 432 | (*ans) = sub; 433 | } 434 | } 435 | return; 436 | } 437 | for (int i = 0; i < n; i++) 438 | { 439 | if (map[i] == 1) 440 | { 441 | map[i] = 0; 442 | sub += arr[row][i]; 443 | assignment(initial, row + 1, n, j + 1, arr, sub, ans, map); 444 | map[i] = 1; 445 | sub -= arr[row][i]; 446 | } 447 | } 448 | } 449 | int main() 450 | { 451 | int n; 452 | scanf("%d", &n); 453 | int arr[n][n]; 454 | for (int i = 0; i < n; i++) 455 | { 456 | for (int j = 0; j < n; j++) 457 | { 458 | scanf("%d", &arr[i][j]); 459 | } 460 | } 461 | int map[n]; 462 | for (int i = 0; i < n; i++) 463 | { 464 | map[i] = 1; 465 | } 466 | int ans = 0, initial = 1; 467 | assignment(&initial, 0, n, 0, arr, 0, &ans, map); 468 | printf("%d", ans); 469 | return 0; 470 | } 471 | 472 | ### 🧐 Explanation: 473 | 474 | - 475 | 476 | --- --------------------------------------------------------------------------------