├── Lecture - 04 ├── a.exe ├── bin_decimal.cpp ├── bitwise.cpp ├── cin_get.cpp ├── direction.cpp ├── directions.cpp ├── function_basic.cpp ├── square_root.cpp ├── ternary.cpp └── typecasting.cpp ├── Lecture - 05 ├── 2n_2.cpp ├── a.exe ├── array_basic.cpp ├── binary_search.cpp ├── char_array.cpp ├── linear_search.cpp ├── merge_2_array.cpp ├── sq.cpp ├── square_root.cpp ├── subarray_print.cpp ├── sum_print.cpp └── unique_no_1.cpp ├── Lecture - 06 ├── a.exe ├── char_arrays.cpp ├── character_arrays.cpp ├── largest_string.cpp ├── pass_by_value_ref.cpp ├── pointer_test.cpp ├── pointer_vs_array.cpp ├── pointers.cpp ├── reference_variable.cpp ├── string_class.cpp └── string_palindrome.cpp ├── Lecture - 07 ├── AnagramSubstringSearch.cpp ├── DMA.cpp ├── DNF-012sort.cpp ├── FirstNonRepeatingCharacterInAString.cpp ├── a.out ├── bubblesort.cpp ├── rotateImage.cpp ├── run └── staircase.cpp ├── Lecture - 08 ├── a.exe ├── bubble_sort_recursive.cpp ├── factorial.cpp ├── fibonacci.cpp ├── power_fn.cpp ├── printDecreasing.cpp ├── printIncreasing.cpp ├── printIncreasingDecreasing.cpp ├── printSent.cpp ├── run └── sorted_array.cpp ├── Lecture - 09 ├── Kjumps.cpp ├── ReplacePi.cpp ├── Subsequence.cpp ├── SubsequenceWithAscii.cpp ├── Tiling.cpp ├── run └── substring.cpp ├── Lecture - 10 ├── a.exe ├── n_queen.cpp ├── n_queen_bitset.cpp ├── permutation.cpp ├── rat_in_maze.cpp └── sudoku.cpp ├── Lecture - 11 OOPs ├── a.exe ├── complex.cpp ├── oops.cpp ├── student.cpp ├── vector_implementation.cpp └── vector_stl.cpp ├── Lecture - 12 ├── a.exe ├── linked_list.cpp ├── paranethis_check.cpp ├── redudant_paranthesis.cpp ├── stack.h ├── stack_stl.cpp └── stack_test.cpp ├── Lecture-14 ├── a.exe ├── binary_tree.cpp ├── bst.cpp ├── input.txt ├── queue.cpp ├── queue.h ├── queue.h.gch ├── queue_stl.cpp └── stack_using_2_queues.cpp ├── Lecture-15 Heaps ├── a.exe ├── fruit_heap.cpp ├── heap.cpp ├── heap.h ├── heap_stl.cpp ├── input_txt.cpp └── running_stream_k_largest.cpp ├── Lecture-16 hashing ├── a.exe ├── hashmain.cpp ├── hashtable.h ├── hashtable_stl.cpp ├── phonebook.cpp ├── trie.cpp └── union_interesection.cpp ├── Lecture-17 Greedy ├── a.exe ├── activity_selection.cpp ├── binary_search.cpp ├── cows.cpp └── upper_bound.cpp ├── Lecture-19 DP ├── a.exe ├── knapsack.cpp └── wines_prob.cpp └── Lecture-20 Graph ├── a.exe ├── dfs_moon.cpp ├── graph_basic.cpp └── graph_holi.cpp /Lecture - 04/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coding-blocks-archives/launchpad-jan-2019/cb57a19c33e14aa77319a78bdc2f3afe1c3b7dc2/Lecture - 04/a.exe -------------------------------------------------------------------------------- /Lecture - 04/bin_decimal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int convertBinaryToDecimal(int n){ 6 | 7 | int ans = 0; 8 | int p = 1; 9 | 10 | for( ;n>0;n=n/10){ 11 | int last_digit = n%10; 12 | ans = ans + last_digit*p; 13 | p = p*2; 14 | } 15 | return ans; 16 | } 17 | 18 | int convertDecimalToBinary(int n){ 19 | 20 | int ans = 0; 21 | int p = 1; 22 | 23 | for( ;n>0;n=n/2){ 24 | int last_digit = n%2; 25 | ans = ans + last_digit*p; 26 | p = p*10; 27 | } 28 | return ans; 29 | } 30 | 31 | 32 | 33 | 34 | int main(){ 35 | 36 | int n; 37 | cin>>n; 38 | //cout< 2 | using namespace std; 3 | 4 | 5 | int countBits(int n){ 6 | //set bits count 7 | int cnt = 0; 8 | while(n>0){ 9 | cnt += n&1; 10 | n = n>>1; 11 | } 12 | return cnt; 13 | } 14 | 15 | int main(){ 16 | 17 | // &&,||,! -->Logical Operators 18 | 19 | // &,|,~,^,>>,<< ---> Bitwise Operators 20 | 21 | int n; 22 | cin>>n; 23 | 24 | //Single line --> Odd or Even without using % 25 | //(n&1)?cout<<"ODD" : cout<<"Even"; 26 | 27 | cout< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | char a,b; 8 | 9 | //Read a sentence till new line character. 10 | char ch; 11 | ch = cin.get(); //read the first character 12 | while(ch!='.'){ 13 | cout< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | char a,b; 8 | 9 | //Read a sentence till new line character. 10 | char ch; 11 | int x,y; 12 | x= y = 0; 13 | 14 | ch = cin.get(); //read the first character 15 | while(ch!='\n'){ 16 | 17 | switch(ch){ 18 | case 'N': y++;break; 19 | case 'W': x--;break; 20 | case 'S': y--;break; 21 | case 'E': x++;break; 22 | } 23 | ch = cin.get(); //read the next character 24 | } 25 | cout<<"X: "< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | char a,b; 8 | 9 | cin>>a>>b; 10 | 11 | cout< 2 | using namespace std; 3 | 4 | int square(int n){ //Function Declaration 5 | //cout<>n; 54 | 55 | PrintAllPrime(n); 56 | /* 57 | bool primeHai = isPrime(n); 58 | if(primeHai){ 59 | cout<<"Prime No "; 60 | } 61 | else{ 62 | cout<<"Not Prime"; 63 | }*/ 64 | 65 | } -------------------------------------------------------------------------------- /Lecture - 04/square_root.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | int n,p; 8 | cin>>n>>p; 9 | 10 | 11 | float ans = 0.0; 12 | float inc = 1.0; 13 | 14 | for(int times=1;times<=p+1;times++){ 15 | 16 | while(ans*ans <=n){ 17 | ans = ans + inc; 18 | } 19 | ans = ans-inc; 20 | inc = inc/10; 21 | } 22 | cout< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | //Ternary Operator 8 | int n; 9 | cin>>n; 10 | 11 | n%7==0?cout<<"Lucky!":cout<<"Unlucky!"; 12 | 13 | int x = n%7==0?1:0; 14 | 15 | 16 | 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Lecture - 04/typecasting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | int main(){ 7 | 8 | char a = 'A'; 9 | 10 | char b = a + 1; 11 | int c = a + 1; 12 | 13 | cout< 2 | using namespace std; 3 | 4 | int findLastBit(int n){ 5 | 6 | int pos=0; 7 | while(n>0){ 8 | int last_bit = (n&1); 9 | if(last_bit==1){ 10 | return pos; 11 | } 12 | pos++; 13 | n = n>>1; 14 | } 15 | } 16 | 17 | void findUnique(int a[],int n){ 18 | 19 | //XOR all the numbers 20 | int ans = 0; 21 | for(int i=0;i0){ 35 | unique_no_1 = unique_no_1^a[i]; 36 | } 37 | } 38 | int unique_no_2 = ans^unique_no_1; 39 | 40 | cout< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | //Declare + Init 8 | int a[100] = {1}; 9 | 10 | //Input 11 | int n; 12 | cin>>n; 13 | 14 | for(int i=0;i>a[i]; 16 | } 17 | 18 | //Print (Traverse/Iterate) --> Reverse Order 19 | for(int i=0;i 2 | using namespace std; 3 | 4 | //Binary Search 5 | //--> Sorted Array element mei search element 6 | int binary_search(int a[],int key,int n){ 7 | //write the algorithm 8 | 9 | int s = 0; 10 | int e = n-1; 11 | 12 | while(s<=e){ 13 | int mid = (s+e)/2; 14 | 15 | if(a[mid]==key){ 16 | return mid; 17 | } 18 | else if(a[mid]>key; 36 | //Binary Search 37 | cout< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | 8 | 9 | return 0; 10 | } -------------------------------------------------------------------------------- /Lecture - 05/linear_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int linear_search(int a[],int n,int key){ 5 | //Function accepts an array, an integer n, and key to search for and returns the index of the key 6 | 7 | for(int i=0;i>n; 23 | 24 | for(int i=0;i>a[i]; 26 | } 27 | 28 | //Print (Traverse/Iterate) --> Reverse Order 29 | for(int i=0;i>key; 35 | //Find the index of the key 36 | 37 | int keyMila = linear_search(a,n,key); 38 | if(keyMila!=-1){ 39 | cout<<"Found at Index "< 2 | using namespace std; 3 | 4 | void merge(int a[],int n,int b[],int m,int c[]){ 5 | 6 | int i=0,j=0,k=0; 7 | 8 | while(i 2 | using namespace std; 3 | 4 | //Binary Search 5 | //--> Sorted Array element mei search element 6 | int binary_search(int a[],int key,int n){ 7 | //write the algorithm 8 | 9 | int s = 0; 10 | int e = n-1; 11 | 12 | while(s<=e){ 13 | int mid = (s+e)/2; 14 | 15 | if(a[mid]==key){ 16 | return mid; 17 | } 18 | else if(a[mid]>key; 62 | //Binary Search 63 | //cout< 2 | using namespace std; 3 | 4 | //Binary Search 5 | //--> Sorted Array element mei search element 6 | int binary_search(int a[],int key,int n){ 7 | //write the algorithm 8 | 9 | int s = 0; 10 | int e = n-1; 11 | 12 | while(s<=e){ 13 | int mid = (s+e)/2; 14 | 15 | if(a[mid]==key){ 16 | return mid; 17 | } 18 | else if(a[mid]>key; 62 | //Binary Search 63 | //cout< 2 | using namespace std; 3 | 4 | void printAllSubarray(int a[],int n){ 5 | 6 | for(int i=0;ims){ 34 | ms = cs; 35 | } 36 | } 37 | return ms; 38 | } 39 | 40 | int main(){ 41 | 42 | 43 | int a[] = {-1,2,-4,6,3,-2,-1,6,-1}; 44 | int n = sizeof(a)/sizeof(int); 45 | 46 | int i = 0; 47 | int j = n-1; 48 | //printAllSubarray(a,n); 49 | 50 | cout< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | 8 | int a[] = {-2,-1,1,2,3,5,8,9,10}; 9 | int n = sizeof(a)/sizeof(int); 10 | 11 | int i = 0; 12 | int j = n-1; 13 | 14 | while(i 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | int n; 8 | cin>>n; 9 | 10 | int ans = 0; 11 | for(int i=0;i>no; 14 | ans = ans^no; 15 | 16 | } 17 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | //Character Array 8 | //terminate a char array with null character 9 | char a[100] = {'a','b','c','d','e','f','\0'}; 10 | 11 | 12 | //another way 13 | char b[] = "Hello_World!"; 14 | cout<>c; 20 | cin.getline(c,100); 21 | cout< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | 7 | int a[10][10] = {0}; 8 | 9 | int r,c; 10 | cin>>r>>c; 11 | //Input 12 | for(int i=0;i>a[i][j]; 15 | } 16 | } 17 | 18 | //Output 19 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | void readline(char a[],int max_size,char delim){ 6 | //Try to complete 7 | //cin or cin.get()? 8 | 9 | for(int i=0;i>n; 24 | 25 | readline(a,100,'\n'); 26 | cout<largest_len){ 39 | largest_len = l; 40 | strcpy(largest_string,a); 41 | } 42 | } 43 | cout< 2 | using namespace std; 3 | 4 | /* 5 | void inc(int no){ 6 | no = no + 1; 7 | }*/ 8 | void inc(int *nptr){ 9 | *nptr = *nptr + 1; 10 | } 11 | 12 | void swap(int *aptr,int *bptr){ 13 | 14 | int temp = *aptr; 15 | *aptr = *bptr; 16 | *bptr = temp; 17 | } 18 | 19 | int main(){ 20 | 21 | int no=10; 22 | inc(&no); 23 | cout< 2 | using namespace std; 3 | 4 | 5 | 6 | 7 | int main(){ 8 | 9 | int x = 10; 10 | int *xptr = &x; 11 | int *yptr = xptr; 12 | 13 | cout<< x < 2 | using namespace std; 3 | 4 | void print(int a[],int n){ 5 | cout< *(a + i) 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Lecture - 06/pointers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | 7 | int main(){ 8 | 9 | int x = 10; 10 | 11 | // & --> Address Of Operator 12 | cout<< x < Value pointer to by operator 48 | // 5*3 = 15, int *ptr, third use case 49 | 50 | // &Bucket --> Address 51 | // *Address -->Bucket 52 | 53 | cout<< *yptr < 2 | using namespace std; 3 | 4 | void inc(int &z){ 5 | z = z + 1; 6 | } 7 | /* 8 | void swap(int &a,int &b){ 9 | int temp = a; 10 | a = b; 11 | b = temp; 12 | }*/ 13 | 14 | int main(){ 15 | 16 | // & --> Bitwise AND, Address Of, Reference Variable 17 | int x = 10; 18 | int &y = x; //Init must 19 | 20 | y++; 21 | inc(x); 22 | 23 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | 9 | string s; 10 | getline(cin,s); 11 | cout< 2 | #include 3 | using namespace std; 4 | 5 | void readline(char a[],int max_size,char delim){ 6 | //Try to complete 7 | //cin or cin.get()? 8 | 9 | for(int i=0;i 2 | using namespace std; 3 | 4 | bool isAnagram(int arr1[], int arr2[]) { 5 | for(int i=0;i<256;i++) { 6 | if(arr1[i]!=arr2[i]){ 7 | return false; 8 | } 9 | } 10 | return true; 11 | } 12 | 13 | void printAnagramSubstring(string text, string pat) { 14 | int T[256] = {0}; 15 | int P[256] = {0}; 16 | int n = text.length(), m = pat.length(); 17 | for(int i=0;i 2 | using namespace std; 3 | 4 | 5 | 6 | int main(int argc, char const *argv[]) 7 | { 8 | // 1-d array in c++ using DMA; 9 | // int *arr = new int[100]; 10 | // for(int i=0;i<100;i++) { 11 | // arr[i] = i; 12 | // } 13 | // for(int i=0;i<100;i++) { 14 | // cout< 2 | #include 3 | using namespace std; 4 | void swap(int *a, int *b) 5 | { 6 | int temp = *a; 7 | *a = *b; 8 | *b = temp; 9 | 10 | } 11 | void sort012(int a[], int arr_size) 12 | { 13 | int lo = 0; 14 | int hi = arr_size - 1; 15 | int mid = 0; 16 | 17 | while (mid <= hi) 18 | { 19 | switch (a[mid]) 20 | { 21 | case 0: 22 | swap(&a[lo++], &a[mid++]); 23 | break; 24 | case 1: 25 | mid++; 26 | break; 27 | case 2: 28 | swap(&a[mid], &a[hi--]); 29 | break; 30 | } 31 | } 32 | } 33 | int main(int argc, char const *argv[]) 34 | { 35 | /* code */ 36 | int n; 37 | cin>>n; 38 | int arr[n]; 39 | for(int i=0;i>arr[i]; 41 | } 42 | sort012(arr, n); 43 | for(int i=0;i 2 | using namespace std; 3 | 4 | char firstNonRepeatingCharacter(string str) { 5 | //Homework 6 | 7 | 8 | return 'a' ; 9 | } 10 | void filterChar(int no,char a[]){ 11 | //Extract the characters corresponding to set bits of that number 12 | int i = 0; 13 | while(no>0){ 14 | int last_bit = (no&1); 15 | if(last_bit){ 16 | cout<>1; 20 | } 21 | 22 | } 23 | 24 | void subsequence(char *str){ 25 | 26 | int n = strlen(str); 27 | for(int i=0;i<(1<>a; 39 | 40 | subsequence(a); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Lecture - 07/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coding-blocks-archives/launchpad-jan-2019/cb57a19c33e14aa77319a78bdc2f3afe1c3b7dc2/Lecture - 07/a.out -------------------------------------------------------------------------------- /Lecture - 07/bubblesort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void bubbleSort(int *arr, int n) { 5 | for(int i=0;i arr[j+1]) { 8 | swap(arr[j], arr[j+1]); 9 | } 10 | } 11 | } 12 | return; 13 | } 14 | 15 | int main(int argc, char const *argv[]) 16 | { 17 | /* code */ 18 | int arr[5] = {5,4,3,2,1}; 19 | bubbleSort(arr, 5); 20 | for(int i=0;i<5;i++) { 21 | cout< 2 | using namespace std; 3 | 4 | void rotateA(int a[][10],int n){ 5 | 6 | 7 | //Transpose! 8 | for(int i=0;i>n; 40 | 41 | //Input 42 | for(int i=0;i>a[i][j]; 45 | } 46 | } 47 | 48 | //Output 49 | for(int i=0;i 2 | using namespace std; 3 | 4 | bool staircaseSearch(int mat[][4], int key) { 5 | int row = 0, col = 3; 6 | while(row<4 and col>=0) { 7 | if(mat[row][col] == key) { 8 | cout< 2 | using namespace std; 3 | 4 | void bubble_sort(int *a,int n){ 5 | //base 6 | if(n==1 or n==0){ 7 | return; 8 | } 9 | 10 | //rec case 11 | for(int i=0;ia[i+1]){ 13 | swap(a[i],a[i+1]); 14 | } 15 | } 16 | bubble_sort(a,n-1); 17 | 18 | } 19 | void bubble_sort2(int *a,int i,int n){ 20 | //base 21 | if(n==1 or n==0){ 22 | return; 23 | } 24 | 25 | if(i==n-1){ 26 | bubble_sort2(a,0,n-1); 27 | return; 28 | } 29 | 30 | //rec case 31 | if(a[i]>a[i+1]){ 32 | swap(a[i],a[i+1]); 33 | } 34 | bubble_sort2(a,i+1,n); 35 | } 36 | 37 | int main(){ 38 | int a[] = {5,4,3,2,1}; 39 | 40 | //cout<<"Not Sorted!"; 41 | bubble_sort2(a,0,5); 42 | for(int i=0;i<5;i++){ 43 | cout< 2 | using namespace std; 3 | 4 | int factorial(int n) { 5 | // Base Case 6 | if(n==0) { 7 | return 1; 8 | } 9 | // Assumption; 10 | int x = factorial(n-1); 11 | // self work 12 | int result = n * x; 13 | return result; 14 | } 15 | 16 | int main(int argc, char const *argv[]) 17 | { 18 | /* code */ 19 | cout< 2 | using namespace std; 3 | 4 | int fib(int n) { 5 | // Base case 6 | if(n==0 or n==1) { 7 | return n; 8 | } 9 | // Assumption 10 | int retval1 = fib(n-1); 11 | int retval2 = fib(n-2); 12 | // Self work 13 | int result = retval1 + retval2; 14 | return result; 15 | } 16 | 17 | int main(int argc, char const *argv[]) 18 | { 19 | /* code */ 20 | cout< 2 | using namespace std; 3 | 4 | 5 | int power(int a,int n){ 6 | //Base Case 7 | if(n==0){ 8 | return 1; 9 | } 10 | 11 | //Rec Case 12 | int smallPower= power(a,n-1); 13 | return a*smallPower; 14 | } 15 | 16 | int fastpower(int a,int n){ 17 | //base case 18 | if(n==0){ 19 | return 1; 20 | } 21 | //rec case 22 | int smallPower = fastpower(a,n/2); 23 | smallPower *= smallPower; 24 | if(n&1){ 25 | return a*smallPower; 26 | } 27 | return smallPower; 28 | } 29 | int fastpowerbitmasking(int a,int n){ 30 | 31 | int ans = 1; 32 | while(n>0){ 33 | if(n&1){ 34 | ans = ans*a; 35 | } 36 | n = n>>1; 37 | a = a*a; 38 | } 39 | return ans; 40 | } 41 | 42 | int main(){ 43 | int a,n; 44 | cin>>a>>n; 45 | 46 | cout< 2 | using namespace std; 3 | 4 | void printDecreasing(int n) { 5 | if(n==1) { // Base case 6 | cout<<1< 2 | using namespace std; 3 | 4 | void printIncreasing(int n) { 5 | if(n==0) { // Base case 6 | return; 7 | } 8 | // Assumption 9 | printIncreasing(n-1); 10 | // Self work 11 | cout< 2 | using namespace std; 3 | 4 | void printDecInc(int n) { 5 | if(n==0) { // Base case 6 | return; 7 | } 8 | // Self work 9 | if(n%2!=0) 10 | cout< 2 | using namespace std; 3 | 4 | char spelling[][10] = {"zero","one","two","three","four","five","six" 5 | "seven","eight","nine"}; 6 | 7 | void printSent(int no){ 8 | //Base Case no>0 9 | if(no==0){ 10 | return; 11 | } 12 | 13 | //Rec Case 14 | int last_digit = no%10; 15 | printSent(no/10); 16 | cout<>n; 23 | printSent(n); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Lecture - 08/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coding-blocks-archives/launchpad-jan-2019/cb57a19c33e14aa77319a78bdc2f3afe1c3b7dc2/Lecture - 08/run -------------------------------------------------------------------------------- /Lecture - 08/sorted_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isSorted(int *a,int n){ 5 | //Base Case 6 | if(n==1){ 7 | return true; 8 | } 9 | 10 | //Rec Case 11 | if(a[0] 2 | using namespace std; 3 | 4 | int kjumps(int n, int k) { 5 | if(n<0){ 6 | return 0; 7 | } 8 | if(n==0) { 9 | return 1; 10 | } 11 | int ans = 0; 12 | for(int i=1;i<=k;i++) { 13 | ans+=kjumps(n-i, k); 14 | } 15 | return ans; 16 | } 17 | 18 | int main(int argc, char const *argv[]) 19 | { 20 | /* code */ 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Lecture - 09/ReplacePi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void replacePi(string str, string osf) { 5 | // BAse case 6 | if(str.length()==0) { 7 | cout< 2 | using namespace std; 3 | 4 | void subsequence(string str, string osf) { 5 | if(str.length()==0) { 6 | cout< 2 | #include 3 | using namespace std; 4 | 5 | string itos(int i) // convert int to string 6 | { 7 | stringstream s; 8 | s << i; 9 | return s.str(); 10 | } 11 | 12 | void subsequenceWithAscii(string str, string osf) { 13 | if(str.length()==0) { 14 | cout< 2 | using namespace std; 3 | 4 | int tiling(int n) { 5 | if(n==1 or n==2 or n==3) { 6 | return 1; 7 | } 8 | if(n==4) { 9 | return 2; 10 | } 11 | return tiling(n-1)+tiling(n-4); 12 | } 13 | 14 | int main(int argc, char const *argv[]) 15 | { 16 | /* code */ 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Lecture - 09/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coding-blocks-archives/launchpad-jan-2019/cb57a19c33e14aa77319a78bdc2f3afe1c3b7dc2/Lecture - 09/run -------------------------------------------------------------------------------- /Lecture - 09/substring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(int argc, char const *argv[]) 5 | { 6 | string str = "CodingBlocks"; 7 | cout< 2 | using namespace std; 3 | 4 | bool isSafe(int board[][10],int n,int i,int j){ 5 | //same col 6 | for(int x=0;x=0 and y>=0){ 15 | if(board[x][y]==1){ 16 | return false; 17 | } 18 | x--; 19 | y--; 20 | } 21 | //right diag 22 | x=i,y=j; 23 | while(x>=0 and y>n; 80 | 81 | int board[10][10] = {0}; 82 | 83 | //placeQueen(board,n,0); 84 | cout< 2 | using namespace std; 3 | 4 | 5 | int ld[30]={0},rd[30]={0},col[30]={0}; 6 | int total_ways=0; 7 | 8 | void nqueen(int i,int n){ 9 | if(i==n){total_ways++; return;} 10 | 11 | //rec case 12 | for(int j=0;j>n; 26 | nqueen(0,n); 27 | cout< 2 | using namespace std; 3 | 4 | void permute(char *a,int i){ 5 | //Base Case 6 | if(a[i]=='\0'){ 7 | cout<>a; 23 | permute(a,0); 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Lecture - 10/rat_in_maze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void findPath(char maze[][10],int i,int j,int m,int n){ 5 | //goal reached 6 | if(i==m and j==n){ 7 | 8 | maze[m][n] = '*'; 9 | for(int x=0;x<=m;x++){ 10 | for(int y=0;y<=n;y++){ 11 | cout<m or j>n){ 19 | return; 20 | } 21 | //X case 22 | else if(maze[i][j]=='X'){ 23 | return; 24 | } 25 | //Rec Case 26 | 27 | maze[i][j] = '*'; 28 | findPath(maze,i+1,j,m,n); 29 | findPath(maze,i,j+1,m,n); 30 | //Backtracking! 31 | maze[i][j] = '0'; 32 | return; 33 | 34 | } 35 | 36 | int main(){ 37 | 38 | char maze[10][10] = { 39 | "00XX0", 40 | "0X00X", 41 | "00000", 42 | "000X0", 43 | "0000X", 44 | "00X00" 45 | }; 46 | findPath(maze,0,0,5,4); 47 | 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Lecture - 10/sudoku.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isSafe(int mat[][9],int i,int j,int n,int no){ 5 | 6 | //same row or col 7 | for(int x=0;x<9;x++){ 8 | if(mat[i][x]==no or mat[x][j]==no){ 9 | return false; 10 | } 11 | } 12 | //subgrid 13 | int sx,sy; 14 | sx = (i/3)*3; 15 | sy = (j/3)*3; 16 | 17 | //iterate over subgrid 18 | for(int x=sx;x 2 | using namespace std; 3 | 4 | class Complex{ 5 | public: 6 | int r; 7 | int i; 8 | 9 | Complex(){ 10 | r = i = 0; 11 | } 12 | Complex(int r,int i){ 13 | this->r = r; 14 | this->i = i; 15 | } 16 | Complex operator+(Complex b){ 17 | Complex c; 18 | c.r = r + b.r; 19 | c.i = i + b.i; 20 | return c; 21 | } 22 | int operator[](char c){ 23 | if(c=='r'){ 24 | return r; 25 | } 26 | else{ 27 | return i; 28 | } 29 | } 30 | }; 31 | 32 | ostream& operator<<(ostream &a,Complex &c){ 33 | cout<>(istream &a,Complex &c){ 37 | cin>>c.r>>c.i; 38 | return a; 39 | } 40 | 41 | int main(){ 42 | 43 | Complex c,c1,c2; 44 | cin>>c1>>c2; 45 | Complex c3; 46 | c3 = c1 +c2; 47 | cout<<"Comple No is "< 2 | #include 3 | using namespace std; 4 | 5 | //blueprint 6 | class Car{ 7 | int price; 8 | public: 9 | //Data Members 10 | char name[13]; 11 | //Functions 12 | //1. Constructor (by default call) 13 | Car(){ 14 | //override 15 | name[0] = '\0'; 16 | price = 0; 17 | } 18 | Car(char *n,int p){ 19 | //constructor overloading 20 | strcpy(name,n); 21 | price = p; 22 | } 23 | //Getters and Setters 24 | void setPrice(int p){ 25 | if(p>0){ 26 | price = p; 27 | } 28 | } 29 | //2. Copy Constructor 30 | Car(Car &X){ 31 | //<<"In copy constructor"; 32 | price = X.price; 33 | strcpy(name,X.name); 34 | } 35 | //3. Copy Assignment Operator 36 | void operator=(Car &X){ 37 | price = X.price; 38 | strcpy(name,X.name); 39 | } 40 | 41 | int getPrice(){ 42 | return price; 43 | } 44 | void print() const{ 45 | cout<<"Name :"<>(istream&a, Car&b){ 75 | char name[13]; 76 | int p; 77 | cin>>name>>p; 78 | 79 | strcpy(b.name,name); 80 | b.setPrice(p); 81 | return a; 82 | } 83 | 84 | 85 | int main(){ 86 | 87 | Car x; 88 | Car y("BMW",200); 89 | 90 | Car z(y); //copy constructor 91 | Car z2 = y; //copy constructor 92 | 93 | Car z3; //default constructor 94 | 95 | y.setPrice(150); 96 | strcpy(y.name,"Audi"); 97 | z3 = y; // copy assignment operator 98 | z3.print(); 99 | 100 | z3+20; 101 | cout< object -->behaving like a function --> functor 111 | 112 | cin>>fun>>z3; //cascading 113 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | class Student{ 7 | 8 | public: 9 | char *name; 10 | int marks; 11 | 12 | Student(char *n){ 13 | //Dynamic Allocation of Array 14 | name = new char[strlen(name)+1]; 15 | strcpy(name,n); 16 | marks = 10; 17 | } 18 | //Default Copy Constructor makes Shallow Copy! 19 | //Deep Copy 20 | Student(Student &s){ 21 | name = new char[strlen(s.name)+1]; 22 | strcpy(name,s.name); 23 | 24 | marks = s.marks; 25 | } 26 | //copy assignment operator for deep copy 27 | void operator=(Student &s){ 28 | name = new char[strlen(s.name)+1]; 29 | strcpy(name,s.name); 30 | 31 | marks = s.marks; 32 | } 33 | void print(){ 34 | cout< 2 | using namespace std; 3 | 4 | template 5 | class Vector{ 6 | 7 | int cs; 8 | int ms; 9 | T *arr; 10 | 11 | 12 | public: 13 | Vector(int ds=10){ 14 | cs = 0; 15 | ms = ds; 16 | arr = new T[ms]{0}; 17 | } 18 | void push_back(int d){ 19 | if(cs==ms){ 20 | //double the space 21 | T *oldArr = arr; 22 | arr = new T[2*ms]; 23 | ms = ms*2; 24 | 25 | for(int i=0;i0){ 36 | cs--; 37 | } 38 | } 39 | 40 | T operator[](int i){ 41 | return arr[i]; 42 | } 43 | int size(){ 44 | return cs; 45 | } 46 | int capacity(){ 47 | return ms; 48 | } 49 | void operator+(Vector&v2){ 50 | for(int i=0;i &v){ 56 | for(int i=0;i v1,v2; 66 | v1.push_back(71); 67 | v1.push_back(72); 68 | v1.push_back(73); 69 | 70 | 71 | v2.push_back(74); 72 | v2.push_back(75); 73 | 74 | v1+v2; 75 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | //Vector -> ResiZable Array(it can grow or shrink in size automatically) 7 | int main(){ 8 | 9 | vector v; 10 | v.reserve(100); 11 | 12 | for(int i=1;i<=10;i++){ 13 | v.push_back(100-i*i); 14 | } 15 | v.pop_back(); 16 | 17 | cout<<"Capcity "< 2 | using namespace std; 3 | 4 | class node{ 5 | 6 | public: 7 | int data; 8 | node* next; 9 | 10 | public: 11 | //Node Constructor 12 | node(int d){ 13 | data = d; 14 | next = NULL; 15 | } 16 | }; 17 | 18 | /* 19 | class LinkedList{ 20 | node*head; 21 | }; 22 | */ 23 | void insertAtHead(node*&head,int data){ 24 | if(head==NULL){ 25 | head = new node(data); 26 | return; 27 | } 28 | 29 | //otherwise! 30 | node* n = new node(data); 31 | n->next = head; 32 | head = n; 33 | } 34 | //Insert at tail 35 | void insertAtTail(node*&head,int data){ 36 | 37 | if(head==NULL){ 38 | head = new node(data); 39 | return; 40 | } 41 | node*t = head; 42 | //bring t pointer to the last node 43 | while(t->next!=NULL){ 44 | t = t->next; 45 | } 46 | //last node ->next attach new node 47 | t->next = new node(data); 48 | return; 49 | } 50 | int length(node*head){ 51 | int l=0; 52 | while(head!=NULL){ 53 | l++; 54 | head = head->next; 55 | } 56 | return l; 57 | } 58 | 59 | void insertInMiddle(node*&head,int data,int pos){ 60 | if(pos==0){ 61 | insertAtHead(head,data); 62 | return; 63 | } 64 | if(pos>length(head)){ 65 | insertAtTail(head,data); 66 | return; 67 | } 68 | //Middle 69 | node*t = head; 70 | for(int jump=1;jump<=pos-1;jump++){ 71 | t = t->next; 72 | } 73 | node* n = new node(data); 74 | n->next = t->next; 75 | t->next = n; 76 | } 77 | 78 | int search1(node*head,int key){ 79 | //Iterative Search 80 | node*t = head; 81 | int idx = 0; 82 | while(t!=NULL){ 83 | if(t->data==key){ 84 | return idx; 85 | } 86 | idx++; 87 | t = t->next; 88 | } 89 | return -1; 90 | } 91 | 92 | int search2(node*head,int key){ 93 | //Recursive Search 94 | //base case 95 | if(head==NULL){ 96 | return -1; 97 | } 98 | 99 | //rec case 100 | if(head->data==key){ 101 | return 0; 102 | } 103 | else{ 104 | int l = search2(head->next,key); 105 | if(l!=-1){ 106 | return 1 + l; 107 | } 108 | return l; 109 | } 110 | } 111 | void reverse(node*&head){ 112 | //iterative way 113 | node* p = NULL; 114 | node* c = head; 115 | node* n = c->next; 116 | 117 | while(c!=NULL){ 118 | //save the next 119 | n = c->next; 120 | //make the current point to prev 121 | c->next = p; 122 | //update c and p 123 | p = c; 124 | c = n; 125 | } 126 | head = p; 127 | } 128 | node* reverse_rec(node*head){ 129 | //recursive way 130 | //base case 131 | if(head->next==NULL){ 132 | return head; 133 | } 134 | //rec case 135 | node* cHead = reverse_rec(head->next); 136 | node*t = cHead; 137 | while(t->next!=NULL){ 138 | t = t->next; 139 | } 140 | t->next = head; 141 | head->next = NULL; 142 | return cHead; 143 | } 144 | node* reverse_rec_fast(node*head){ 145 | //recursive way 146 | //base case 147 | if(head->next==NULL){ 148 | return head; 149 | } 150 | //rec case 151 | node* cHead = reverse_rec_fast(head->next); 152 | 153 | head->next->next = head; 154 | head->next = NULL; 155 | return cHead; 156 | } 157 | 158 | node* midPoint(node*head){ 159 | node*slow = head; 160 | node*fast = head->next; 161 | 162 | while(fast!=NULL and fast->next!=NULL){ 163 | fast = fast->next->next; 164 | slow = slow->next; 165 | } 166 | return slow; 167 | 168 | } 169 | 170 | node* k_th_from_last(node*head,int k){ 171 | // Homework! 172 | node*f = head; 173 | for(int jump=1;jump<=k;jump++){ 174 | f = f->next; 175 | } 176 | node*s = head; 177 | while(f!=NULL){ 178 | f = f->next; 179 | s = s->next; 180 | } 181 | return s; 182 | } 183 | 184 | //Let us print our linked list 185 | void print(node *head){ 186 | while(head!=NULL){ 187 | cout<data<<"-->"; 188 | head = head->next; 189 | } 190 | cout<next!=NULL){ 200 | 201 | fast = fast->next->next; 202 | slow = slow->next; 203 | 204 | if(fast==slow){ 205 | 206 | cout<<"First meeting "<data<next; 214 | fast = fast->next; 215 | } 216 | 217 | cout<<"Meeting point is "<data<<" "; 218 | 219 | //3. Break the cycle 220 | // meeting point prev ->next = NULL 221 | 222 | 223 | return false; 224 | } 225 | 226 | node* merge(node *a,node *b){ 227 | //base case 228 | if(a==NULL){ 229 | return b; 230 | } 231 | if(b==NULL){ 232 | return a; 233 | } 234 | //rec case 235 | node*c; 236 | if(a->datadata){ 237 | c = a; 238 | c->next = merge(a->next,b); 239 | } 240 | else{ 241 | c = b; 242 | c->next = merge(a,b->next); 243 | } 244 | return c; 245 | 246 | } 247 | 248 | istream& operator>>(istream& a,node*&head){ 249 | 250 | int d; 251 | cin>>d; 252 | while(d!=-1){ 253 | insertAtTail(head,d); 254 | a>>d; 255 | } 256 | 257 | return a; 258 | } 259 | ostream& operator<<(ostream&a,node*head){ 260 | print(head); 261 | return a; 262 | } 263 | 264 | node* merge_sort(node*l){ 265 | //Base Case 266 | if(l==NULL or l->next==NULL){ 267 | return l; 268 | } 269 | 270 | //Recursive Case 271 | node* mid = midPoint(l); 272 | node* a = l; 273 | node* b = mid->next; 274 | mid->next = NULL;//break the list 275 | a = merge_sort(a); 276 | b = merge_sort(b); 277 | 278 | l = merge(a,b); 279 | return l; 280 | } 281 | 282 | 283 | 284 | int main(){ 285 | 286 | //Linked List 287 | node*a = NULL; 288 | cin>>a; 289 | cout<next!=NULL){ 311 | t = t->next; 312 | } 313 | t->next = head->next->next; 314 | floyd_cycle(head); 315 | 316 | 317 | 318 | insertInMiddle(head,4,4); 319 | 320 | print(head); 321 | reverse(head); 322 | print(head); 323 | head = reverse_rec_fast(head); 324 | print(head); 325 | */ 326 | 327 | return 0; 328 | } -------------------------------------------------------------------------------- /Lecture - 12/paranethis_check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool isBalanced(char *input){ 6 | 7 | stack s; 8 | 9 | for(int i=0;input[i]!='\0';i++){ 10 | char ch = input[i]; 11 | if(ch=='(' or ch=='['){ 12 | s.push(ch); 13 | } 14 | else if(ch==')'){ 15 | if(!s.empty() and s.top()=='('){ 16 | s.pop(); 17 | continue; 18 | } 19 | else{ 20 | return false; 21 | } 22 | 23 | } 24 | else if(ch==']'){ 25 | if(!s.empty() and s.top()=='['){ 26 | s.pop(); 27 | continue; 28 | } 29 | else{ 30 | return false; 31 | } 32 | 33 | } 34 | } 35 | if(s.empty()){ 36 | return true; 37 | } 38 | return false; 39 | } 40 | 41 | int main(){ 42 | 43 | char input[100]; 44 | cin>>input; 45 | 46 | if(isBalanced(input)){ 47 | cout<<"Yes!"; 48 | } 49 | else{ 50 | cout<<"No!"; 51 | } 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /Lecture - 12/redudant_paranthesis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool isBalanced(char *input){ 6 | 7 | stack s; 8 | //cout<>input; 64 | 65 | if(isBalanced(input)){ 66 | cout<<"Redundant"; 67 | } 68 | else{ 69 | cout<<"Not Redundant!"; 70 | } 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /Lecture - 12/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_H 2 | #define STACK_H 3 | 4 | template 5 | class node{ 6 | public: 7 | T data; 8 | node*next; 9 | 10 | node(T d){ 11 | data = d; 12 | next = NULL; 13 | } 14 | }; 15 | 16 | template 17 | class Stack{ 18 | node *head; 19 | 20 | public: 21 | Stack(){ 22 | head = NULL; 23 | } 24 | void push(T d){ 25 | if(head==NULL){ 26 | head = new node(d); 27 | return; 28 | } 29 | node *n = new node(d); 30 | n->next = head; 31 | head = n; 32 | } 33 | void pop(){ 34 | if(head!=NULL){ 35 | node*temp = head; 36 | head = head->next; 37 | delete temp; 38 | } 39 | } 40 | T top(){ 41 | return head->data; 42 | } 43 | bool empty(){ 44 | return head==NULL; 45 | } 46 | }; 47 | 48 | #endif -------------------------------------------------------------------------------- /Lecture - 12/stack_stl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Book{ 7 | 8 | public: 9 | string name; 10 | int price; 11 | Book(){ 12 | 13 | } 14 | Book(string n,int p){ 15 | name = n ; 16 | price = p; 17 | } 18 | }; 19 | 20 | ostream& operator<<(ostream&a,Book&b){ 21 | cout< &sb){ 25 | while(!sb.empty()){ 26 | Book temp = sb.top(); 27 | cout< s; 37 | 38 | for(int i=61;i<=65;i++){ 39 | s.push(i); 40 | } 41 | 42 | 43 | //Empty 44 | while(!s.empty()){ 45 | cout< sb; 55 | sb.push(b1); 56 | sb.push(b2); 57 | sb.push(b3); 58 | 59 | cout< 2 | #include "stack.h" 3 | #include "stack.h" 4 | using namespace std; 5 | 6 | class Book{ 7 | 8 | public: 9 | string name; 10 | int price; 11 | Book(){ 12 | 13 | } 14 | Book(string n,int p){ 15 | name = n ; 16 | price = p; 17 | } 18 | }; 19 | 20 | ostream& operator<<(ostream&a,Book&b){ 21 | cout< &sb){ 25 | while(!sb.empty()){ 26 | Book temp = sb.top(); 27 | cout< s; 37 | 38 | for(int i=61;i<=65;i++){ 39 | s.push(i); 40 | } 41 | 42 | 43 | //Empty 44 | while(!s.empty()){ 45 | cout< sb; 55 | sb.push(b1); 56 | sb.push(b2); 57 | sb.push(b3); 58 | 59 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class node{ 8 | public: 9 | int data; 10 | node*left; 11 | node*right; 12 | 13 | node(int d){ 14 | data = d; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | node* buildTree(){ 20 | int d; 21 | cin>>d; 22 | //Base Case 23 | if(d==-1){ 24 | return NULL; 25 | } 26 | 27 | node*root = new node(d); 28 | root->left = buildTree(); 29 | root->right = buildTree(); 30 | return root; 31 | } 32 | void print(node*root){ 33 | if(root==NULL){ 34 | return; 35 | } 36 | cout<data<<" "; 37 | print(root->left); 38 | print(root->right); 39 | } 40 | void printIn(node*root){ 41 | if(root==NULL){ 42 | return; 43 | } 44 | printIn(root->left); 45 | cout<data<<" "; 46 | printIn(root->right); 47 | } 48 | void printPost(node*root){ 49 | if(root==NULL){ 50 | return; 51 | } 52 | 53 | printPost(root->left); 54 | printPost(root->right); 55 | cout<data<<" "; 56 | } 57 | int countNodes(node *root){ 58 | if(root==NULL){ 59 | return 0; 60 | } 61 | return countNodes(root->left) + 1 + countNodes(root->right); 62 | 63 | } 64 | int height(node*root){ 65 | if(root==NULL){ 66 | return 0; 67 | } 68 | int h1 = height(root->left); 69 | int h2 = height(root->right); 70 | return max(h1,h2) +1; 71 | } 72 | 73 | bool search(node*root,int key){ 74 | //base case(kahi nahi mila) 75 | if(root==NULL){ 76 | return false; 77 | } 78 | //yaha pe mila 79 | if(root->data==key){ 80 | return true; 81 | } 82 | //subtree me mila 83 | bool leftMeiMila = search(root->left,key); 84 | if(leftMeiMila){ 85 | return true; 86 | } 87 | return search(root->right,key); 88 | } 89 | 90 | void printAtLevelK(node*root,int k){ 91 | if(root==NULL){ 92 | return; 93 | } 94 | //otherwise 95 | if(k==0){ 96 | cout<data<<" "; 97 | } 98 | printAtLevelK(root->left,k-1); 99 | printAtLevelK(root->right,k-1); 100 | return; 101 | } 102 | void levelOrder1(node*root){ 103 | 104 | int h = height(root); 105 | for(int k=0;kleft,root->right); 116 | mirror(root->left); 117 | mirror(root->right); 118 | } 119 | 120 | int sumOfNodes(node*root){ 121 | if(root==NULL){ 122 | return 0; 123 | } 124 | return root->data + sumOfNodes(root->left) + sumOfNodes(root->right); 125 | } 126 | 127 | int replaceSumOfNodes(node*root){ 128 | if(root==NULL){ 129 | return 0; 130 | } 131 | if(root->left==NULL and root->right==NULL){ 132 | return root->data; 133 | } 134 | //rec case 135 | int leftSum = replaceSumOfNodes(root->left); 136 | int rightSum = replaceSumOfNodes(root->right); 137 | int temp = root->data; 138 | root->data = leftSum + rightSum; 139 | return root->data + temp; 140 | } 141 | 142 | int diameter(node*root){ 143 | //largest distance btw any two nodes! 144 | if(root==NULL){ 145 | return 0; 146 | } 147 | int op1 = height(root->left) + height(root->right); 148 | int op2 = max(diameter(root->left),diameter(root->right)); 149 | return max(op1,op2); 150 | } 151 | 152 | class Pair{ 153 | public: 154 | int height; 155 | int diam; 156 | 157 | Pair(){ 158 | height=diam=0; 159 | } 160 | }; 161 | 162 | Pair diamFast(node*root){ 163 | //Base case 164 | Pair p; 165 | if(root==NULL){ 166 | return p; 167 | } 168 | // rec case(post order - or bottom up approach!) 169 | Pair left = diamFast(root->left); 170 | Pair right = diamFast(root->right); 171 | 172 | int op1 = left.height + right.height; 173 | int op2 = max(left.diam,right.diam); 174 | 175 | p.diam = max(op1,op2); 176 | p.height = max(left.height,right.height)+1; 177 | return p; 178 | } 179 | int i=0; 180 | 181 | node* buildTreeInPre(int *in,int *pre,int s,int e){ 182 | static int i = 0; 183 | if(s>e){ 184 | return NULL; 185 | } 186 | //rec case 187 | node *root = new node(pre[i]); 188 | //linear search 189 | int j=0; 190 | for(j=s;j<=e;j++){ 191 | if(in[j]==pre[i]){ 192 | break; 193 | } 194 | } 195 | i++; 196 | 197 | //j -->mid point 198 | //recursively build left and right subtree 199 | root->left = buildTreeInPre(in,pre,s,j-1); 200 | root->right = buildTreeInPre(in,pre,j+1,e); 201 | return root; 202 | } 203 | 204 | void root2LeafAllPaths(node*root,vector &v){ 205 | 206 | if(root==NULL){ 207 | return; 208 | } 209 | 210 | if(root->left==NULL and root->right==NULL){ 211 | v.push_back(root->data); 212 | for(int i=0;i "; 214 | } 215 | cout<data); 221 | root2LeafAllPaths(root->left,v); 222 | root2LeafAllPaths(root->right,v); 223 | v.pop_back(); 224 | return; 225 | } 226 | 227 | class HB{ 228 | public: 229 | bool balance; 230 | int height; 231 | 232 | }; 233 | 234 | HB isHeightBalanced(node*root){ 235 | 236 | HB p; 237 | if(root==NULL){ 238 | p.balance = true; 239 | p.height = 0; 240 | return p; 241 | } 242 | HB left,right; 243 | left = isHeightBalanced(root->left); 244 | right = isHeightBalanced(root->right); 245 | 246 | if(abs(left.height-right.height)<=1 and left.balance and right.balance){ 247 | p.balance = true; 248 | } 249 | else{ 250 | p.balance = false; 251 | } 252 | p.height = max(left.height,right.height)+1; 253 | return p; 254 | } 255 | 256 | void bfs(node*root){ 257 | 258 | queue q; 259 | q.push(root); 260 | 261 | while(!q.empty()){ 262 | node* t = q.front(); 263 | cout<data<<" "; 264 | q.pop(); 265 | 266 | if(t->left){ 267 | q.push(t->left); 268 | } 269 | if(t->right){ 270 | q.push(t->right); 271 | } 272 | } 273 | return ; 274 | } 275 | void bfs_2(node*root){ 276 | 277 | queue q; 278 | q.push(root); 279 | q.push(NULL); 280 | 281 | while(!q.empty()){ 282 | node* t = q.front(); 283 | 284 | if(t==NULL){ 285 | cout<data<<" "; 294 | q.pop(); 295 | 296 | 297 | 298 | if(t->left){ 299 | q.push(t->left); 300 | } 301 | if(t->right){ 302 | q.push(t->right); 303 | } 304 | } 305 | return ; 306 | } 307 | 308 | node* buildTreeLevelWise(){ 309 | 310 | int d; 311 | cout<<"Root Data "; 312 | cin>>d; 313 | 314 | node*root = new node(d); 315 | queue q; 316 | q.push(root); 317 | 318 | while(!q.empty()){ 319 | 320 | node*f = q.front(); 321 | q.pop(); 322 | 323 | cout<<"Enter children of "<data<<" "; 324 | int c1,c2; 325 | cin>>c1>>c2; 326 | 327 | if(c1!=-1){ 328 | f->left = new node(c1); 329 | q.push(f->left); 330 | } 331 | if(c2!=-1){ 332 | f->right = new node(c2); 333 | q.push(f->right); 334 | } 335 | } 336 | return root; 337 | } 338 | 339 | 340 | int main(){ 341 | /* 342 | node*root = buildTree(); 343 | print(root); 344 | cout< v; 369 | root2LeafAllPaths(root,v); 370 | 371 | */ 372 | node*root = buildTreeLevelWise(); 373 | bfs_2(root); 374 | 375 | 376 | 377 | 378 | return 0; 379 | } -------------------------------------------------------------------------------- /Lecture-14/bst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class node{ 8 | public: 9 | int data; 10 | node*left; 11 | node*right; 12 | 13 | node(int d){ 14 | data = d; 15 | left = right = NULL; 16 | } 17 | }; 18 | void print(node*root){ 19 | if(root==NULL){ 20 | return; 21 | } 22 | cout<data<<" "; 23 | print(root->left); 24 | print(root->right); 25 | } 26 | void printIn(node*root){ 27 | if(root==NULL){ 28 | return; 29 | } 30 | printIn(root->left); 31 | cout<data<<" "; 32 | printIn(root->right); 33 | } 34 | void printPost(node*root){ 35 | if(root==NULL){ 36 | return; 37 | } 38 | 39 | printPost(root->left); 40 | printPost(root->right); 41 | cout<data<<" "; 42 | } 43 | void bfs_2(node*root){ 44 | 45 | queue q; 46 | q.push(root); 47 | q.push(NULL); 48 | 49 | while(!q.empty()){ 50 | node* t = q.front(); 51 | 52 | if(t==NULL){ 53 | cout<data<<" "; 62 | q.pop(); 63 | 64 | 65 | 66 | if(t->left){ 67 | q.push(t->left); 68 | } 69 | if(t->right){ 70 | q.push(t->right); 71 | } 72 | } 73 | return ; 74 | } 75 | 76 | //try to insert a data into a bst 77 | node* insertInBST(node*root,int data){ 78 | if(root==NULL){ 79 | return new node(data); 80 | } 81 | if(data<=root->data){ 82 | root->left = insertInBST(root->left,data); 83 | } 84 | else{ 85 | root->right = insertInBST(root->right,data); 86 | } 87 | return root; 88 | } 89 | 90 | 91 | 92 | node* buildTreeLevelWise(){ 93 | 94 | int d; 95 | cout<<"Root Data "; 96 | cin>>d; 97 | 98 | node*root = new node(d); 99 | queue q; 100 | q.push(root); 101 | 102 | while(!q.empty()){ 103 | 104 | node*f = q.front(); 105 | q.pop(); 106 | 107 | cout<<"Enter children of "<data<<" "; 108 | int c1,c2; 109 | cin>>c1>>c2; 110 | 111 | if(c1!=-1){ 112 | f->left = new node(c1); 113 | q.push(f->left); 114 | } 115 | if(c2!=-1){ 116 | f->right = new node(c2); 117 | q.push(f->right); 118 | } 119 | } 120 | return root; 121 | } 122 | 123 | void takeInput(node*&root){ 124 | int d; 125 | cin>>d; 126 | while(d!=-1){ 127 | root = insertInBST(root,d); 128 | cin>>d; 129 | } 130 | return; 131 | } 132 | 133 | node* arr2bst(int *arr,int s,int e){ 134 | //base case 135 | if(s>e){ 136 | return NULL; 137 | } 138 | 139 | int mid = (s+e)/2; 140 | node*root = new node(arr[mid]); 141 | root->left = arr2bst(arr,s,mid-1); 142 | root->right = arr2bst(arr,mid+1,e); 143 | return root; 144 | 145 | } 146 | 147 | int findNoOfTrees(int n){ 148 | if(n==0){ 149 | return 1; 150 | } 151 | 152 | int ans =0; 153 | for(int i=1;i<=n;i++){ 154 | ans += findNoOfTrees(n-i)*findNoOfTrees(i-1); 155 | } 156 | return ans; 157 | 158 | } 159 | 160 | node* deleteInTree(node*root,int key){ 161 | if(root==NULL){ 162 | return NULL; 163 | } 164 | if(root->data==key){ 165 | //ise delete krna 166 | //1. no child 167 | if(root->left==NULL and root->right==NULL){ 168 | delete root; 169 | return NULL; 170 | } 171 | else if(root->left==NULL and root->right!=NULL){ 172 | node*temp = root->right; 173 | delete root; 174 | return temp; 175 | } 176 | else if(root->right==NULL and root->left!=NULL){ 177 | node*temp = root->left; 178 | delete root; 179 | return temp; 180 | } 181 | else{ 182 | //find the inorder successor 183 | node*temp = root->right; 184 | while(temp->left!=NULL){ 185 | temp = temp->left; 186 | } 187 | root->data = temp->data; 188 | root->right = deleteInTree(root->right,root->data); 189 | return root; 190 | } 191 | } 192 | else if(root->dataright = deleteInTree(root->right,key); 195 | } 196 | else{ 197 | root->left = deleteInTree(root->left,key); 198 | } 199 | return root; 200 | } 201 | 202 | class LinkedList{ 203 | public: 204 | node*head; 205 | node*tail; 206 | }; 207 | 208 | LinkedList convertTree2LL(node*root){ 209 | LinkedList l; 210 | if(root==NULL){ 211 | l.head = l.tail =NULL; 212 | return l; 213 | } 214 | //Leaf Node 215 | if(root->left==NULL and root->right==NULL){ 216 | l.head = l.tail = root; 217 | return l; 218 | } 219 | //Left Child 220 | if(root->left!=NULL and root->right==NULL){ 221 | LinkedList leftLL = convertTree2LL(root->left); 222 | leftLL.tail->right = root; 223 | l.head = leftLL.head; 224 | l.tail = root; 225 | return l; 226 | } 227 | else if(root->left==NULL and root->right!=NULL){ 228 | LinkedList rightLL = convertTree2LL(root->right); 229 | //leftLL.tail->right = root; 230 | root->right = rightLL.head; 231 | l.head = root; 232 | l.tail = rightLL.tail; 233 | return l; 234 | } 235 | else{ 236 | LinkedList leftLL = convertTree2LL(root->left); 237 | LinkedList rightLL = convertTree2LL(root->right); 238 | leftLL.tail->right = root; 239 | root->right = rightLL.head; 240 | l.head = leftLL.head; 241 | l.tail = rightLL.tail; 242 | return l; 243 | } 244 | } 245 | 246 | bool isBST(node*root,int minV,int maxV){ 247 | if(root==NULL){ 248 | return true; 249 | } 250 | 251 | //Rec Case 252 | if(root->data >=minV and root->data<=maxV and isBST(root->left,minV,root->data) and 253 | isBST(root->right,root->data,maxV)){ 254 | return true; 255 | } 256 | return false; 257 | } 258 | 259 | 260 | 261 | int main(){ 262 | /* 263 | node*root = buildTree(); 264 | print(root); 265 | cout< v; 290 | root2LeafAllPaths(root,v); 291 | 292 | */ 293 | 294 | 295 | node*root = NULL; 296 | //takeInput(root); 297 | 298 | int arr[] = {1,3,4,5,7,8,9,10}; 299 | int n = sizeof(arr)/sizeof(int); 300 | 301 | root = arr2bst(arr,0,n-1); 302 | bfs_2(root); 303 | 304 | printIn(root); 305 | 306 | if(isBST(root,INT_MIN,INT_MAX)){ 307 | cout<<"BST "; 308 | } 309 | else{ 310 | cout<<"Not a BST!"; 311 | } 312 | 313 | //cin>>n; 314 | //cout<>key; 319 | cout<data<<"-->"; 329 | temp = temp->right; 330 | } 331 | */ 332 | 333 | //printIn(root); 334 | 335 | 336 | return 0; 337 | } -------------------------------------------------------------------------------- /Lecture-14/input.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 -1 -1 4 9 -1 10 -1 -1 -1 5 6 -1 8 -1 -1 7 -1 -1 -------------------------------------------------------------------------------- /Lecture-14/queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "queue.h" 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | Queue q; 9 | 10 | for(int i=1;i<=7;i++){ 11 | q.push(i); 12 | } 13 | q.pop(); 14 | q.push(8); 15 | 16 | while(!q.isEmpty()){ 17 | cout<0){ 28 | front = (front+1)%ms; 29 | cs--; 30 | } 31 | } 32 | int getfront() const{ 33 | return arr[front]; 34 | } 35 | bool isEmpty(){ 36 | return cs==0; 37 | } 38 | bool isFull(){ 39 | return cs==ms; 40 | } 41 | 42 | }; -------------------------------------------------------------------------------- /Lecture-14/queue.h.gch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coding-blocks-archives/launchpad-jan-2019/cb57a19c33e14aa77319a78bdc2f3afe1c3b7dc2/Lecture-14/queue.h.gch -------------------------------------------------------------------------------- /Lecture-14/queue_stl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | queue q; 9 | 10 | for(int i=1;i<=7;i++){ 11 | q.push(i); 12 | } 13 | q.pop(); 14 | q.push(8); 15 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | class Stack{ 7 | 8 | queue q1,q2; 9 | int cs; 10 | 11 | public: 12 | Stack(){ 13 | cs = 0; 14 | } 15 | void push(int data){ 16 | 17 | if(q1.empty()){ 18 | q2.push(data); 19 | } 20 | else if(q2.empty()){ 21 | q1.push(data); 22 | } 23 | cs++; 24 | 25 | } 26 | void pop(){ 27 | if(!q1.empty()){ 28 | while(q1.size()!=1){ 29 | q2.push(q1.front()); 30 | q1.pop(); 31 | } 32 | q1.pop(); 33 | } 34 | 35 | else if(!q2.empty()){ 36 | while(q2.size()!=1){ 37 | q1.push(q2.front()); 38 | q2.pop(); 39 | } 40 | q2.pop(); 41 | } 42 | cs--; 43 | } 44 | int top(){ 45 | if(!q1.empty()){ 46 | while(q1.size()!=1){ 47 | q2.push(q1.front()); 48 | q1.pop(); 49 | } 50 | int t = q1.front(); 51 | q1.pop(); 52 | q2.push(t); 53 | return t; 54 | } 55 | 56 | else if(!q2.empty()){ 57 | while(q2.size()!=1){ 58 | q1.push(q2.front()); 59 | q2.pop(); 60 | } 61 | int t = q2.front(); 62 | q2.pop(); 63 | q1.push(t); 64 | return t; 65 | } 66 | } 67 | bool empty(){ 68 | return cs==0; 69 | } 70 | }; 71 | 72 | int main(){ 73 | 74 | Stack s; 75 | for(int i=1;i<=5;i++){ 76 | s.push(i); 77 | } 78 | while(!s.empty()){ 79 | cout< 2 | #include 3 | using namespace std; 4 | 5 | class Fruit{ 6 | public: 7 | string name; 8 | int price; 9 | public: 10 | Fruit(){ 11 | 12 | } 13 | Fruit(string n,int p){ 14 | name = n; 15 | price = p; 16 | } 17 | 18 | }; 19 | 20 | class FruitCompare{ 21 | public: 22 | bool operator()(Fruit a,Fruit b){ 23 | //cout<b.price; 25 | } 26 | 27 | }; 28 | int main(){ 29 | 30 | Fruit myFruit; 31 | //myFruit("apple","guava"); //functional_object = functor 32 | 33 | 34 | 35 | priority_queue hmax; //maxheap 36 | priority_queue, FruitCompare > h; //minheap 37 | 38 | 39 | int arr[] = {1,5,12,7,9,0}; 40 | string names[] = {"apple","mango","guava","chiku","strawberry","papaya"}; 41 | int n = sizeof(arr)/sizeof(int); 42 | 43 | for(int i=0;i 2 | #include "heap.h" 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | Heap h(false); 9 | int arr[] = {1,5,12,7,9,0}; 10 | int n = sizeof(arr)/sizeof(int); 11 | 12 | for(int i=0;i 2 | using namespace std; 3 | 4 | template 5 | class Heap{ 6 | 7 | vector v; 8 | bool minH; 9 | 10 | bool cmp(int a,int b){ 11 | if(minH) 12 | return a>b; 13 | else 14 | return b>a; 15 | } 16 | void heapify(int i){ 17 | int left = i<<1; 18 | int right = left+1; 19 | 20 | int ptr = i; 21 | if(left1){ 46 | int parent = idx/2; 47 | if(cmp(v[parent],v[idx])){ 48 | swap(v[parent],v[idx]); 49 | idx = parent; 50 | } 51 | else{ 52 | break; 53 | } 54 | } 55 | } 56 | 57 | void pop(){ 58 | //Swap first and the last element 59 | int last = v.size()-1; 60 | swap(v[1],v[last]); 61 | v.pop_back(); 62 | heapify(1); 63 | } 64 | 65 | bool empty(){ 66 | return v.size()==1; 67 | } 68 | int top(){ 69 | return v[1]; 70 | } 71 | }; -------------------------------------------------------------------------------- /Lecture-15 Heaps/heap_stl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | priority_queue hmax; //maxheap 8 | priority_queue, greater > h; //minheap 9 | 10 | 11 | int arr[] = {1,5,12,7,9,0}; 12 | int n = sizeof(arr)/sizeof(int); 13 | 14 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void printHeap(priority_queue,greater > h){ 7 | 8 | while(!h.empty()){ 9 | cout<,greater > h; 22 | 23 | int k=3; 24 | int cs=0; 25 | 26 | while(scanf("%d",&no)!=EOF){ 27 | //cout<h.top()){ 35 | h.pop(); 36 | h.push(no); 37 | } 38 | } 39 | else{ 40 | cout<<"Query "; 41 | printHeap(h); 42 | } 43 | } 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Lecture-16 hashing/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coding-blocks-archives/launchpad-jan-2019/cb57a19c33e14aa77319a78bdc2f3afe1c3b7dc2/Lecture-16 hashing/a.exe -------------------------------------------------------------------------------- /Lecture-16 hashing/hashmain.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "hashtable.h" 3 | using namespace std; 4 | 5 | 6 | 7 | int main(){ 8 | hashtable h; 9 | 10 | h.insert("Apple",100); 11 | h.insert("Mango",200); 12 | h.insert("Guava",40); 13 | h.insert("Grape",80); 14 | h.insert("Kiwi",190); 15 | 16 | 17 | 18 | 19 | 20 | h["papaya"] = 95; 21 | h["papaya"] += 5; 22 | cout<>f; 28 | 29 | int *price = h.search(f); 30 | if(price!=NULL){ 31 | cout< 2 | using namespace std; 3 | 4 | template 5 | class node{ 6 | public: 7 | string key; 8 | T value; 9 | node *next; 10 | 11 | node(string k,T val){ 12 | key = k; 13 | value = val; 14 | next = NULL; 15 | } 16 | ~node(){ 17 | if(next!=NULL){ 18 | delete next; 19 | } 20 | } 21 | }; 22 | 23 | template 24 | class hashtable{ 25 | //Data Members 26 | node** table; 27 | int cs; 28 | int ms; 29 | 30 | int hashFn(string key){ 31 | 32 | int ans = 0; 33 | int p =1; 34 | 35 | for(int i=0;i**oldTable = table; 50 | table = new node*[ms]; 51 | for(int i=0;i*temp = oldTable[i]; 57 | while(temp!=NULL){ 58 | insert(temp->key,temp->value); 59 | temp = temp->next; 60 | } 61 | //delete the ith linked list 62 | delete oldTable[i]; 63 | } 64 | delete [] oldTable; 65 | } 66 | 67 | public: 68 | hashtable(int ds=7){ 69 | cs = 0; 70 | ms = ds; 71 | table = new node*[ms]; 72 | for(int i=0;i*n = new node(key,val); 80 | n->next = table[idx]; 81 | table[idx] = n; 82 | cs++; 83 | 84 | float load_factor = (cs)/float(ms); 85 | if(load_factor>0.7){ 86 | rehash(); 87 | } 88 | } 89 | 90 | T* search(string key){ 91 | int idx = hashFn(key); 92 | node*temp = table[idx]; 93 | while(temp!=NULL){ 94 | if(temp->key ==key){ 95 | return &temp->value; 96 | } 97 | temp= temp->next; 98 | } 99 | return NULL; 100 | } 101 | 102 | void erase(string key){ 103 | //remve the node from linked list (homework)! 104 | } 105 | 106 | T& operator[](string key){ 107 | //first step 108 | T* val = search(key); 109 | if(val==NULL){ 110 | T obj; 111 | insert(key,obj); 112 | } 113 | //pakka hogi ... 114 | val = search(key); 115 | return *val; 116 | } 117 | 118 | void show(){ 119 | 120 | for(int i=0;i*temp = table[i]; 122 | cout<<"Bucket "<key<<"-->"; 125 | temp = temp->next; 126 | } 127 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | 9 | unordered_map h; 10 | 11 | //insert 1st way 12 | h.insert(make_pair("apple",100)); 13 | //2nd way 14 | h["mango"] = 90; 15 | //3rd way 16 | pair p; 17 | p.first = "guava"; 18 | p.second = 87; 19 | h.insert(p); 20 | 21 | //search 22 | if(h.count("apple")>0){ 23 | cout<second<::iterator it=h.begin();it!=h.end();it++){ 43 | cout<first; 44 | cout<second; 45 | } 46 | 47 | 48 | 49 | 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Lecture-16 hashing/phonebook.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | int main(){ 8 | 9 | map > phonebook; 10 | 11 | phonebook["Arun"].push_back("1001"); 12 | phonebook["Vishesh"].push_back("1002"); 13 | phonebook["Akshat"].push_back("1003"); 14 | phonebook["Kushal"].push_back("1004"); 15 | phonebook["Prateek"].push_back("1005"); 16 | phonebook["Neeraj"].push_back("1006"); 17 | 18 | phonebook["Arun"].push_back("1006"); 19 | 20 | //phone book 21 | for(auto p:phonebook){ 22 | string name = p.first; 23 | 24 | cout<"; 25 | for(auto phoneno:p.second){ 26 | cout< 2 | #include 3 | using namespace std; 4 | 5 | class trie;// 6 | 7 | class node{ 8 | public: 9 | char data; 10 | unordered_map m; 11 | bool isterminal; 12 | 13 | public: 14 | node(char d){ 15 | data = d; 16 | isterminal = false; 17 | } 18 | friend class trie; 19 | }; 20 | class trie{ 21 | 22 | node*root; 23 | public: 24 | trie(){ 25 | root = new node('\0'); 26 | } 27 | void addWord(char word[]){ 28 | 29 | node*temp = root; 30 | for(int i=0;word[i]!='\0';i++){ 31 | char ch = word[i]; 32 | 33 | if(temp->m.count(ch)==0){ 34 | node*n = new node(ch); 35 | temp->m[ch] = n; 36 | } 37 | temp = temp->m[ch]; 38 | } 39 | temp->isterminal= true; 40 | } 41 | bool search(char word[]){ 42 | node*temp = root; 43 | 44 | for(int i=0;word[i]!='\0';i++){ 45 | char ch = word[i]; 46 | 47 | if(temp->m.count(ch)==0){ 48 | return false; 49 | } 50 | temp = temp->m[ch]; 51 | } 52 | return temp->isterminal; 53 | } 54 | }; 55 | 56 | int main(){ 57 | 58 | char words[][10] = {"india","indian","no","news"}; 59 | char w[10]; 60 | cin>>w; 61 | 62 | trie t; 63 | for(int i=0;i<4;i++){ 64 | t.addWord(words[i]); 65 | } 66 | 67 | if(t.search(w)){ 68 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | map m; 9 | //union 10 | int a[] = {1,2,3,5,4,0}; 11 | int b[] = {2,3,5,9,11,12}; 12 | 13 | for(int i=0;i<6;i++){ 14 | m[a[i]] = true; 15 | m[b[i]] = true; 16 | } 17 | 18 | for(auto p:m){ 19 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | bool cmp(pair a,pair b){ 8 | return a.second < b.second; 9 | } 10 | 11 | int main(){ 12 | 13 | vector > v; 14 | int n; 15 | cin>>n; 16 | 17 | for(int i=0;i>s>>e; 20 | v.push_back(make_pair(s,e)); 21 | } 22 | //sort the activities according to the end time 23 | sort(v.begin(),v.end(),cmp); 24 | 25 | //activitiy 26 | int act = 1; 27 | int endTime = v[0].second; 28 | for(int i=1;i current_act = v[i]; 30 | int current_start = v[i].first; 31 | if(current_start>=endTime){ 32 | act++; 33 | endTime = current_act.second; 34 | } 35 | } 36 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | int a[] = {1,2,3,4,5,5,5,6,7,10,15,20}; 9 | int n = sizeof(a)/sizeof(int); 10 | 11 | int key; 12 | cin>>key; 13 | //Bool (0 or 1 ) 14 | int present = binary_search(a,a+n,key); 15 | //Index 16 | if(present){ 17 | int lb = lower_bound(a,a+n,key)-a; 18 | cout<<"Present at index"< 2 | using namespace std; 3 | 4 | bool cowsRakhSkteHai(int stalls[],int n,int c,int d){ 5 | int cows=1; 6 | int last_cow = stalls[0]; 7 | 8 | for(int i=1;i=d){ 10 | cows++; 11 | last_cow = stalls[i]; 12 | if(cows==c){ 13 | return true; 14 | } 15 | } 16 | } 17 | return false; 18 | } 19 | 20 | int main(){ 21 | 22 | int stalls[] = {1,2,4,8,9}; //sort 23 | int n = sizeof(stalls)/sizeof(int); 24 | 25 | int c = 3; 26 | 27 | int s= 0; 28 | int e = stalls[n-1] - stalls[0]; 29 | 30 | int saved = -1; 31 | while(s<=e){ 32 | int mid =(s+e)/2; 33 | if(cowsRakhSkteHai(stalls,n,c,mid)){ 34 | s = mid+1; 35 | saved = mid; 36 | } 37 | else{ 38 | e = mid -1; 39 | } 40 | } 41 | 42 | cout< 2 | using namespace std; 3 | 4 | int upperBound(int *arr,int n,int key){ 5 | int s = 0; 6 | int e = n-1; 7 | int saved = -1; 8 | while(s<=e){ 9 | int m = (s+e)/2; 10 | if(arr[m]==key){ 11 | saved = m; 12 | s = m +1; 13 | } 14 | else if(arr[m]>key; 31 | int idx = upperBound(arr,n,key); 32 | cout< 2 | using namespace std; 3 | 4 | int knapasack_profit(int wts[],int prices[],int n,int C){ 5 | //base case 6 | if(C==0 or n==0){ 7 | return 0; 8 | } 9 | //rec case 10 | int inc=0,exc=0; 11 | 12 | //limited qty case //infinite qty case 13 | if(C-wts[n-1]>=0){ 14 | inc = prices[n-1] + knapasack_profit(wts,prices,n-1,C-wts[n-1]); 15 | } 16 | exc = 0 + knapasack_profit(wts,prices,n-1,C); 17 | return max(inc,exc); 18 | } 19 | 20 | int knapasack_profit_bu(int wts[],int prices[],int n,int C){ 21 | 22 | int dp[100][100] = {0}; 23 | 24 | for(int i=1;i<=n;i++){ 25 | for(int j=1;j<=C;j++){ 26 | int ans = 0; 27 | if(wts[i-1]<=j){ 28 | ans = prices[i-1] + dp[i-1][j-wts[i-1]]; 29 | } 30 | int exc = dp[i-1][j]; 31 | dp[i][j] = max(ans,exc); 32 | } 33 | } 34 | return dp[n][C]; 35 | } 36 | 37 | int knapasack_profit_buo(int wts[],int prices[],int n,int C){ 38 | 39 | int dp[2][100] = {0}; 40 | 41 | for(int i=1;i<=n;i++){ 42 | for(int j=1;j<=C;j++){ 43 | int ans = 0; 44 | 45 | if(wts[i-1]<=j){ 46 | ans = prices[i-1] + dp[(i-1)%2][j-wts[i-1]]; 47 | } 48 | int exc = dp[(i-1)%2][j]; 49 | dp[i%2][j] = max(ans,exc); 50 | } 51 | } 52 | return dp[n%2][C]; 53 | } 54 | 55 | int main(){ 56 | 57 | int item_wt[] = {3,1,2,5}; 58 | int item_price[] = {25,8,10,15}; 59 | cout< 2 | using namespace std; 3 | 4 | int profit(int *wines,int dp[][100],int s,int e,int y){ 5 | 6 | if(s>e){ 7 | return 0; 8 | } 9 | if(dp[s][e]!=0){ 10 | return dp[s][e]; 11 | } 12 | 13 | //Rec Case 14 | int op1 = wines[s]*y + profit(wines,dp,s+1,e,y+1); 15 | int op2 = wines[e]*y + profit(wines,dp,s,e-1,y+1); 16 | return dp[s][e]=max(op1,op2); 17 | } 18 | 19 | int main(){ 20 | 21 | int dp[100][100] ={0}; 22 | 23 | int wines[] = {2,3,5,1,4}; 24 | int n = sizeof(wines)/sizeof(int); 25 | cout< 2 | #include 3 | using namespace std; 4 | 5 | class Graph{ 6 | list *l; 7 | int V; 8 | public: 9 | Graph(int V){ 10 | this->V = V; 11 | l = new list[V]; 12 | } 13 | void addEdge(int u,int v){ 14 | l[u].push_back(v); 15 | l[v].push_back(u); 16 | } 17 | int dfs_helper(int i,bool *visited){ 18 | visited[i] = true; 19 | int sz = 1; 20 | 21 | for(int neighbour:l[i]){ 22 | if(!visited[neighbour]){ 23 | sz += dfs_helper(neighbour,visited); 24 | } 25 | } 26 | return sz; 27 | } 28 | 29 | int dfs(){ 30 | bool *visited = new bool[V]{0}; 31 | int ans = V*(V-1)/2; 32 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Graph{ 7 | map > m; 8 | 9 | public: 10 | void add_edge(string a,string b,bool bidir=true){ 11 | m[a].push_back(b); 12 | if(bidir){ 13 | m[b].push_back(a); 14 | } 15 | } 16 | void print(){ 17 | for(auto p:m){ 18 | cout< "; 19 | for(auto city:p.second){ 20 | cout< &visited){ 27 | cout< visited; 38 | traverse_city(start,visited); 39 | cout< 2 | #include 3 | using namespace std; 4 | 5 | class Graph{ 6 | list > *l; 7 | int V; 8 | public: 9 | Graph(int V){ 10 | this->V = V; 11 | l = new list >[V]; 12 | } 13 | void addEdge(int u,int v,int wt){ 14 | l[u].push_back(make_pair(v,wt)); 15 | l[v].push_back(make_pair(u,wt)); 16 | } 17 | int dfs_helper(int i,bool *visited,int &ans){ 18 | visited[i] = true; 19 | int sz = 1; 20 | 21 | for(auto jpair:l[i]){ 22 | int j = jpair.first; 23 | int wt = jpair.second; 24 | 25 | if(!visited[j]){ 26 | int small_sz = dfs_helper(j,visited,ans); 27 | ans += 2*min(small_sz,V-small_sz)*wt; 28 | sz += small_sz; 29 | } 30 | } 31 | return sz; 32 | } 33 | 34 | int dfs(){ 35 | bool *visited = new bool[V]{0}; 36 | int ans=0; 37 | dfs_helper(0,visited,ans); 38 | return ans; 39 | } 40 | 41 | 42 | }; 43 | 44 | 45 | int main(){ 46 | Graph g(4); 47 | 48 | g.addEdge(0,1,3); 49 | g.addEdge(1,2,2); 50 | g.addEdge(3,2,2); 51 | 52 | cout<