diff --git a/theory/backtracking/findingSubsets.cpp b/theory/backtracking/findingSubsets.cpp new file mode 100644 index 0000000..d7af2f2 --- /dev/null +++ b/theory/backtracking/findingSubsets.cpp @@ -0,0 +1,38 @@ +#include + +void findSubsets(char *input, char *output, int i, int j) { + + //base case + if(input[i] == '\0') { + output[j] = '\0'; + if(output[0]=='\0') + std::cout << "NULL"; + std::cout << output << std::endl; + return; + } + + //rec case + //include the ith letter + output[j] = input[i]; + findSubsets(input, output, i+1, j+1); + + //exclude the ith letter + output[j] = '\0'; + findSubsets(input,output,i+1,j); + +} + + + + +int main(){ + + char s[100], output[100]; + + std::cin >> s; + findSubsets(s,output,0,0); + +} + + + diff --git a/theory/backtracking/permutations.cpp b/theory/backtracking/permutations.cpp new file mode 100644 index 0000000..b746e48 --- /dev/null +++ b/theory/backtracking/permutations.cpp @@ -0,0 +1,44 @@ +#include +#include +#include + +void swap(char *a, char *b) { + char tmp = *a; + *a=*b; + *b=tmp; +} + +/* + + Given a string s, find all permutations of the string + + +*/ + +void permutationsString(char *n1, int l, int r) { + + int i; + if(l == r) + std::cout << n1 << std::endl; + + for(i = l; i <= r; ++i) { + swap((n1+l), (n1+i)); + permutationsString(n1, l+1, r); + swap((n1+l),(n1+i)); + } + +} + +int main(){ + + char str[100]; + std::cin >> str; + int n = strlen(str); + + permutationsString(str, 0 , n-1); + + +} + + + diff --git a/theory/inclusion_and_exclusion/inclusion_exclusion.md b/theory/inclusion_and_exclusion/inclusion_exclusion.md new file mode 100644 index 0000000..1ad81bf --- /dev/null +++ b/theory/inclusion_and_exclusion/inclusion_exclusion.md @@ -0,0 +1,26 @@ +# Principles of Inclusion and Exclusion + +-> Set Theory + +Set is a form to store elements that doesn't repeat +Generally sets're sorted + +''' +{AuB} = A + B - {A^B} -> union of the sets + +-> Generalised function + +''' +Discovering the pattern of even and odd sets +Let's see if we've 3 sets + +|AuBuC| -> A + B + C + - |AB| - |AC| - |BC| + + |ABC| + +Now let's see for 4 sets + +|AuBuCuD| -> A + B + C + D + - |AB| - |AC| - |BC| + + |ABC| + |ABD| + |BCD| + |ACD| + - |ABCD| diff --git a/theory/recursion/factorial.cpp b/theory/recursion/factorial.cpp new file mode 100644 index 0000000..bb42940 --- /dev/null +++ b/theory/recursion/factorial.cpp @@ -0,0 +1,19 @@ +#include + + +int factorial(int n) { + + if (n == 1) + return 1; + + return n*factorial(n-1); + +} + + +int main(){ + + std::cout << factorial(5) << std::endl; + + +} diff --git a/theory/recursion/fibonnaci.cpp b/theory/recursion/fibonnaci.cpp new file mode 100644 index 0000000..027518e --- /dev/null +++ b/theory/recursion/fibonnaci.cpp @@ -0,0 +1,17 @@ +#include + +int fib(int n) { + + if(n == 0 or n == 1) + return n; + + return fib(n-1) + fib(n-2); + +} + + +int main(){ + + std::cout << fib(5) << std::endl; + +} \ No newline at end of file