├── Week 1 ├── Module 1 │ ├── 1-1 Introduction │ │ ├── Main.c │ │ └── main.cpp │ ├── 1-2 Basic IO │ │ ├── 0.txt │ │ ├── 1.txt │ │ ├── 2.txt │ │ ├── input.txt │ │ ├── main.cpp │ │ └── output.txt │ ├── 1-3 Namespace │ │ └── main.cpp │ ├── 1-4 if-else │ │ └── main.cpp │ └── 1-5 loop │ │ └── main.cpp ├── Module 2 │ ├── 2-1-1 Array │ │ └── main.cpp │ ├── 2-1-2 String │ │ └── main.cpp │ ├── 2-2 Function │ │ └── main.cpp │ ├── 2-3 Pointer │ │ └── main.cpp │ ├── 2-4 Dynamic allocation │ │ └── main.cpp │ ├── 2-5 Vector │ │ └── main.cpp │ └── 2-6 C++ functions │ │ └── main.cpp └── Module 3 │ ├── 3-1 Class and Object │ └── main.cpp │ ├── 3-2 Access modifiers │ └── main.cpp │ ├── 3-3 Constructor and Destructor │ └── main.cpp │ ├── 3-4 Dynamic Object in C++ │ └── main.cpp │ ├── 3-5 Array of Class │ └── main.cpp │ └── 3-6 Sort Array of Class │ └── main.cpp ├── Week 2 ├── Module 4 │ ├── Linear Search │ │ └── 1.cpp │ └── Time Complexity │ │ ├── 1.cpp │ │ ├── 2.cpp │ │ ├── 3.cpp │ │ ├── 4.cpp │ │ └── 5.cpp ├── Module 5 │ ├── Binary Search │ │ └── 1.cpp │ └── Dynamic Array │ │ └── 1.cpp └── Module 6 │ ├── Bubble Sort │ └── 1.cpp │ ├── Insertion Sort │ └── 1.cpp │ └── Sieve Complexity │ └── 1.cpp ├── Week 3 ├── Module 10 │ └── 1.cpp ├── Module 8 │ └── Merge Sort │ │ └── 1.cpp └── Module 9 │ ├── Array Memory │ └── 1.cpp │ └── Quick Sort │ └── 1.cpp ├── Week 4 ├── Module 12 │ └── 1.cpp ├── Module 13 │ └── 1.cpp └── Module 14 │ └── 1.cpp ├── Week 5 ├── Module 16 │ ├── 1.cpp │ ├── 10.cpp │ ├── 11.cpp │ ├── 2.cpp │ ├── 3.cpp │ ├── 4.cpp │ ├── 5.cpp │ ├── 6.cpp │ ├── 7.cpp │ ├── 8.cpp │ └── 9.cpp ├── Module 17 │ ├── 1.cpp │ ├── 2.cpp │ └── 3.cpp └── Module 18 │ ├── 1.cpp │ └── 2.cpp ├── Week 6 ├── Module 20 │ ├── 1.cpp │ ├── 2.cpp │ └── 3.cpp ├── Module 21 │ ├── 1.cpp │ ├── 2.cpp │ ├── 3.cpp │ └── 4.cpp └── Module 22 │ ├── 1.cpp │ ├── 2.cpp │ ├── 3.cpp │ └── 4.cpp ├── Week 7 ├── Module 24 │ ├── 1.cpp │ ├── 2.cpp │ └── 3.cpp ├── Module 25 │ ├── 1.cpp │ ├── 2.cpp │ └── 3.cpp └── Module 26 │ ├── 1.cpp │ ├── 2.cpp │ └── 3.cpp ├── Week 8 ├── Module 28 │ └── 1.cpp ├── Module 29 │ └── 1.cpp └── Module 30 │ └── 1.cpp └── Week 9 ├── Module 32 └── 1.cpp ├── Module 33 ├── 1.cpp ├── 2.cpp ├── 3.cpp └── 4.cpp └── Module 34 ├── 1.cpp ├── 2.cpp ├── 3.cpp └── 4.cpp /Week 1/Module 1/1-1 Introduction/Main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("Hello World\n"); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /Week 1/Module 1/1-1 Introduction/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | cout<<"Hello world"< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // double x = 3.446444; 7 | // printf("%.2f",x); 8 | // cout<> x >> y >> z; 21 | 22 | cout< 2 | 3 | using namespace std; 4 | 5 | int x=5,y,z; 6 | 7 | int func(int a, int b) 8 | { 9 | return a+b; 10 | } 11 | 12 | //namespace std{ 13 | // //decleare cout 14 | // cout 15 | // 16 | //} 17 | 18 | namespace Info{ 19 | int x=10; 20 | int y = 5; 21 | 22 | int func(int a, int b) 23 | { 24 | return a*b; 25 | } 26 | } 27 | 28 | //using namespace Info; 29 | 30 | int main() 31 | { 32 | int a = 4; 33 | int b = 5; 34 | 35 | int x = func(a,b); 36 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // bool x= true; 8 | // bool y = false; 9 | // 10 | // int z = 5; 11 | // if ( z < 10) 12 | // { 13 | // //code 14 | // cout<<"True\n"; 15 | // if(x) 16 | // { 17 | // cout<<"I am nested if\n"; 18 | // } 19 | // else{ 20 | // cout<<"I am nested else\n"; 21 | // } 22 | // } 23 | // else{ 24 | // //code 25 | // cout<<"False\n"; 26 | // } 27 | 28 | int day = 4; 29 | if(day <= 2) 30 | { 31 | cout<<"I am if\n"; 32 | } 33 | else if(day <= 5) 34 | { 35 | cout<<"I am else if\n"; 36 | } 37 | else{ 38 | cout<<"I am else\n"; 39 | } 40 | 41 | int day; 42 | cin>>day; 43 | 44 | switch(day) //expression 45 | { 46 | case 1: 47 | cout<<"Saturday\n"; 48 | break; 49 | case 2: 50 | cout<<"Subday\n"; 51 | break; 52 | case 3: 53 | cout<<"Monday\n"; 54 | break; 55 | default: 56 | cout<<"Invalid day\n"; 57 | break; 58 | } 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Week 1/Module 1/1-5 loop/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // int i; 8 | // for(i=0;i<4;i++) 9 | // cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n; 8 | cin>>n; 9 | 10 | int a[n]; 11 | 12 | for(int i=0;i>a[i]; 14 | 15 | int sum = 0; 16 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // string s = "I am eating rice."; 8 | // string s2 = "I am also eating dal."; 9 | 10 | // cout<>s; 31 | 32 | s.erase(s.begin()); 33 | s.erase(s.begin() + s.size()-1); 34 | // s.pop_back(); 35 | cout< B 40 | B --> C 41 | 42 | abcd xyz 43 | */ 44 | -------------------------------------------------------------------------------- /Week 1/Module 2/2-2 Function/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /* 6 | b = 3 7 | a = b a=3 8 | b = 5 9 | 10 | a = 3 11 | b = 5 12 | */ 13 | string erase_first_last(string s) 14 | { 15 | s.erase(s.begin()); 16 | s.pop_back(); 17 | 18 | return s; 19 | } 20 | 21 | void erase_first_last2(string &s) 22 | { 23 | s.erase(s.begin()); 24 | s.pop_back(); 25 | cout<<"In function s="<>s; 46 | //// string ans = erase_first_last(s); 47 | //// 48 | //// cout<>a>>b; 56 | Swap(a,b); 57 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | int main() 7 | { 8 | int x = 10; 9 | int *y = &x; 10 | 11 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | int main() 7 | { 8 | int *x = new int; 9 | *x = 10; 10 | cout<>n; 15 | // int *x = new int[n]; 16 | // for(int i=0;i>x[i]; 18 | // // int *x --> *x = 10 , x 19 | // // x[i] --> x -- > x[i] 20 | // 21 | // cout< 2 | 3 | using namespace std; 4 | 5 | 6 | int main() 7 | { 8 | // vectora; 9 | 10 | // for(int i=0;i<10;i++) 11 | // { 12 | //// cout<<"Before push "<a = {1,2,3,4,5}; 32 | a.resize(3); 33 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | 6 | int main() 7 | { 8 | // int a = 4, b= 5; 9 | // swap(a,b); 10 | // cout<a = {3,2,1,4,5}; 28 | 29 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | class Student{ 6 | public: 7 | string name; 8 | int std_id; 9 | int age; 10 | string fathers_name; 11 | string mothers_name; 12 | 13 | void print_information() 14 | { 15 | cout<name = "B"; 57 | p.mother->name = "C"; 58 | p.print_info(); 59 | // Rectangle r1,r2; 60 | // r1.height = 4; 61 | // r1.width = 8; 62 | // cout< 2 | 3 | using namespace std; 4 | 5 | class Student 6 | { 7 | private: 8 | string name; 9 | int std_id; 10 | int age; 11 | string fathers_name; 12 | string mothers_name; 13 | public: 14 | void print_information() 15 | { 16 | cout< 2 | 3 | using namespace std; 4 | 5 | class Student 6 | { 7 | public: 8 | string name; 9 | int std_id; 10 | int age; 11 | string fathers_name; 12 | string mothers_name; 13 | 14 | Student() 15 | { 16 | 17 | } 18 | Student(string name, int id, int age, string f_name, string m_name) 19 | { 20 | this->name = name; 21 | std_id = id; 22 | this->age = age; 23 | fathers_name = f_name; 24 | mothers_name = m_name; 25 | } 26 | 27 | Student(string s, int id, int ag) 28 | { 29 | name = s; 30 | std_id = id; 31 | age = ag; 32 | } 33 | 34 | void print_information() 35 | { 36 | cout<name = name; 77 | father = new Person; 78 | father->name = f_name; 79 | mother = new Person; 80 | mother->name = m_name; 81 | } 82 | void print_info() 83 | { 84 | cout<<"Name = "< 2 | 3 | using namespace std; 4 | 5 | class Student{ 6 | public: 7 | string name; 8 | int std_id; 9 | int age; 10 | string fathers_name; 11 | string mothers_name; 12 | 13 | Student(string name, int std_id , int age) 14 | { 15 | this->name = name; 16 | this->std_id = std_id; 17 | this->age = age; 18 | } 19 | 20 | void print_information() 21 | { 22 | cout<print_information(); 62 | (*s).print_information(); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Week 1/Module 3/3-5 Array of Class/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Student 6 | { 7 | public: 8 | string name; 9 | int std_id; 10 | int age; 11 | string fathers_name; 12 | string mothers_name; 13 | 14 | Student() 15 | { 16 | 17 | } 18 | Student(string name, int id, int age, string f_name, string m_name) 19 | { 20 | this->name = name; 21 | std_id = id; 22 | this->age = age; 23 | fathers_name = f_name; 24 | mothers_name = m_name; 25 | } 26 | 27 | Student(string s, int id, int ag) 28 | { 29 | name = s; 30 | std_id = id; 31 | age = ag; 32 | } 33 | 34 | void print_information() 35 | { 36 | cout<name = name; 77 | father = new Person; 78 | father->name = f_name; 79 | mother = new Person; 80 | mother->name = m_name; 81 | } 82 | void print_info() 83 | { 84 | cout<<"Name = "< 2 | 3 | using namespace std; 4 | 5 | class Student 6 | { 7 | public: 8 | string name; 9 | int std_id; 10 | int age; 11 | string fathers_name; 12 | string mothers_name; 13 | 14 | Student() 15 | { 16 | 17 | } 18 | Student(string name, int id, int age, string f_name, string m_name) 19 | { 20 | this->name = name; 21 | std_id = id; 22 | this->age = age; 23 | fathers_name = f_name; 24 | mothers_name = m_name; 25 | } 26 | 27 | Student(string s, int id, int ag) 28 | { 29 | name = s; 30 | std_id = id; 31 | age = ag; 32 | } 33 | 34 | void print_information() 35 | { 36 | cout<a; 56 | for(int i=0;i<10;i++) 57 | { 58 | a.push_back(Student("A" , 20-i , 20)); 59 | } 60 | 61 | for(int i=0;i<10;i++) 62 | { 63 | a[i].print_information(); 64 | } 65 | 66 | cout<<"After sorting\n"; 67 | sort(a.begin(),a.end()); 68 | for(int i=0;i<10;i++) 69 | { 70 | a[i].print_information(); 71 | } 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /Week 2/Module 4/Linear Search/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Time complexity O(n) 6 | Memory complexity O(n) 7 | */ 8 | 9 | int main() 10 | { 11 | int n,k; 12 | cin>>n>>k; 13 | vectora(n); 14 | for(int i=0;i>a[i]; 17 | } 18 | 19 | for(int i=0;i 2 | using namespace std; 3 | 4 | /* 5 | Time complexity O(1) 6 | Memory complexity O(1) 7 | */ 8 | int main() 9 | { 10 | int a,b,c; 11 | cin>>a>>b>>c; 12 | int maxi = max({a,b,c}); 13 | int mini = min({a,b,c}); 14 | int sum = a+b+c; 15 | int mul = a*b*c; 16 | 17 | cout< 2 | using namespace std; 3 | 4 | /* 5 | Time complexity = O(n + 1 + n + 1) = O(2*n + 1) = O(2*n) =O(c*n) = O(n) 6 | Memory complexity = O(n) 7 | 8 | O(c*n) = O(n) 9 | O(1*3) = O(1) 10 | */ 11 | int main() 12 | { 13 | int n; //O(1) 14 | cin>>n; 15 | vectora(n); //O(n) 16 | for(int i=0;i>a[i]; 19 | } 20 | int maxi = a[0]; 21 | int mini = a[0]; 22 | int sum = 0; 23 | for(int i=0;i 2 | using namespace std; 3 | 4 | // 1000 O(1000 + 1000000) 5 | /* 6 | Time complexity = O( n + n^2 ) = O(n^2) 7 | Memory complexity = O(n) 8 | */ 9 | int main() 10 | { 11 | int n; 12 | cin>>n; 13 | vectora(n); 14 | for(int i=0; i>a[i]; 17 | } 18 | // i =0 --> n 19 | // i =1 --> n 20 | // i =2 --> n 21 | // i =n-1 --> n 22 | // n * n = n^2 23 | 24 | // 25 | for(int i=0; i 2 | using namespace std; 3 | 4 | /* 5 | Time complexity = O( (n^2/2) ) = O(n^2) 6 | Memory complexity = O(n) 7 | */ 8 | int main() 9 | { 10 | int n; 11 | cin>>n; 12 | vectora(n); 13 | for(int i=0; i>a[i]; 16 | } 17 | // i =0 --> 1 -- n-1 -> n-1 18 | // i =1 --> 2 -- n-1 -> n-2 19 | // i =2 --> 3 -- n-1 -> n-3 20 | // i =n-1 --> 0 -> 0 21 | // n * n = n^2 22 | 23 | // 24 | for(int i=0; i i and a[i] == a[j] 52 | 53 | 54 | (n-1) + (n-2) + (n-3) + .. + 2 + 1 + 0 55 | 56 | ((n-1)*n)/2 57 | (n^2 - n)/2 58 | (n^2/2) - (n/2) 59 | */ 60 | -------------------------------------------------------------------------------- /Week 2/Module 4/Time Complexity/5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Time complexity = O(2^n) 6 | Memory complexity = O(n) 7 | */ 8 | /* 9 | If ans saved 10 | Time complexity = O(n) 11 | Memory complexity = O(n) 12 | */ 13 | int called = 0; 14 | int save[100]; 15 | 16 | int fib(int n) 17 | { 18 | if(n==0) 19 | return 0; 20 | if(n==1) 21 | return 1; 22 | if(save[n]!=0) 23 | return save[n]; 24 | //fib(n-1) + fib(n-2) 25 | int x = fib(n-1); 26 | called++; 27 | int y = fib(n-2); 28 | called++; 29 | save[n] = x+y; 30 | return x+y; 31 | } 32 | 33 | int main() 34 | { 35 | int n; 36 | cin>>n; 37 | cout<<"Fib = "< 2 | using namespace std; 3 | 4 | 5 | 6 | int main() 7 | { 8 | int n,k; 9 | cin>>n>>k; 10 | vectora(n); 11 | for(int i=0;i>a[i]; 14 | } 15 | int low = 0 , high = n-1; 16 | bool flag = 0; 17 | while(low<=high) 18 | { 19 | int mid = (low+high)/2; 20 | cout<<"Low = "< k) 28 | { 29 | high = mid - 1; 30 | } 31 | else{ 32 | //a[mid] < k 33 | low = mid+1; 34 | } 35 | } 36 | if(flag==0){ 37 | cout<<"NO\n"; 38 | } 39 | 40 | return 0; 41 | } 42 | /* 43 | n 44 | sorted array 45 | 46 | k 47 | 48 | 10 10 49 | 2 4 6 7 8 9 10 11 14 18 50 | 51 | 52 | n - length array 53 | 54 | q - query 55 | k - YES/NO and index 56 | 57 | */ 58 | -------------------------------------------------------------------------------- /Week 2/Module 5/Dynamic Array/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Memory Complexity O(n) 5 | // v.insert(v.begin()+i) 6 | class Array{ 7 | private: 8 | int *arr; 9 | int cap; 10 | int sz; 11 | 12 | void Increase_size() 13 | { 14 | cap = cap*2; 15 | int *tmp = new int[cap]; 16 | for(int i=0;i=pos;i--) 48 | { 49 | arr[i+1] = arr[i]; 50 | } 51 | arr[pos] = x; 52 | sz++; 53 | } 54 | // O(sz) 55 | void Print() 56 | { 57 | for(int i=0;i=sz) 72 | { 73 | cout<<"Error "<=sz) 93 | { 94 | cout<<"Position doesn't exist.\n"; 95 | return; 96 | } 97 | for(int i=pos+1;i 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | int n; 8 | cin>>n; 9 | vectora(n); 10 | for(int i=0; i>a[i]; 13 | } 14 | // n ta pass 15 | // Time Complexity : O(n^2) 16 | // Memory Complexity : O(n) 17 | for(int pass = 0; pass a[j+1]) 24 | { 25 | swap(a[j],a[j+1]); 26 | sorted = false; 27 | } 28 | } 29 | if(sorted) 30 | break; 31 | cout<<"After pass "< 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | int n; 8 | cin>>n; 9 | vectora(n); 10 | for(int i=0; i>a[i]; 13 | } 14 | //Memory complexity O(n) 15 | //Time complexity O(n^2) 16 | for(int i=1; i= 1) 20 | { 21 | if(a[idx-1] > a[idx]) 22 | { 23 | swap(a[idx-1], a[idx]); 24 | idx--; 25 | } 26 | else{ 27 | break; 28 | } 29 | } 30 | cout<<"Considering "< 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | int n; 8 | cin>>n; 9 | vectorprimes; 10 | vectorvisited(n+1); 11 | // O(n * n/2) O(n^2) 12 | // i = 2 , n/2 13 | // i = 3 , n/3 14 | // i = 4 , 0 15 | // i = 5 , n/5 16 | // i = 6 , 0 17 | // .... 18 | // n/2 + n/3 + n/4 + n/5 + n/6 + ... 19 | // O(nlogn) 20 | for(int i=2;i<=n;i++) 21 | { 22 | if(visited[i]==false) 23 | { 24 | primes.push_back(i); 25 | for(int x=2;i*x<=n;x++) 26 | { 27 | visited[i*x] = true; 28 | } 29 | } 30 | } 31 | cout< 2 | 3 | using namespace std; 4 | 5 | class node{ 6 | public: 7 | int data; 8 | node * nxt; 9 | }; 10 | 11 | class LinkedList{ 12 | public: 13 | node * head; 14 | 15 | LinkedList() 16 | { 17 | head = NULL; 18 | } 19 | 20 | //Creates a new node with data = value and nxt= NULL 21 | node* CreateNewNode(int value) 22 | { 23 | node *newnode = new node; 24 | newnode->data = value; 25 | newnode->nxt = NULL; 26 | return newnode; 27 | } 28 | 29 | // Insert new value at Head 30 | void InsertAtHead(int value) 31 | { 32 | node *a = CreateNewNode(value); 33 | if(head == NULL) 34 | { 35 | head = a; 36 | return; 37 | } 38 | //If head is not NULL 39 | a->nxt = head; 40 | head = a; 41 | } 42 | 43 | //Prints the linked list 44 | void Traverse() 45 | { 46 | node* a = head; 47 | while(a!= NULL) 48 | { 49 | cout<data<<" "; 50 | a = a->nxt; 51 | } 52 | cout<<"\n"; 53 | } 54 | 55 | //Search for a single value 56 | int SearchDistinctValue(int value) 57 | { 58 | node* a = head; 59 | int index = 0; 60 | while(a!= NULL) 61 | { 62 | if(a->data==value) 63 | { 64 | return index; 65 | } 66 | a = a->nxt; 67 | index++; 68 | } 69 | return -1; 70 | } 71 | 72 | //Search all possible occurrence 73 | void SearchAllValue(int value) 74 | { 75 | node* a = head; 76 | int index = 0; 77 | while(a!= NULL) 78 | { 79 | if(a->data==value) 80 | { 81 | cout<nxt; 84 | index++; 85 | } 86 | } 87 | }; 88 | 89 | int main() 90 | { 91 | LinkedList l; 92 | 93 | l.Traverse(); 94 | 95 | l.InsertAtHead(10); 96 | l.Traverse(); 97 | 98 | l.InsertAtHead(30); 99 | l.Traverse(); 100 | 101 | l.InsertAtHead(20); 102 | l.Traverse(); 103 | 104 | l.InsertAtHead(30); 105 | l.Traverse(); 106 | 107 | cout<<"10 is found at "< 2 | 3 | using namespace std; 4 | 5 | 6 | vector merge_sort(vectora) 7 | { 8 | //base case 9 | if(a.size()<=1) 10 | { 11 | return a; 12 | } 13 | int mid = a.size()/2; 14 | vectorb; 15 | vectorc; 16 | for(int i=0; isorted_b = merge_sort(b); 23 | vectorsorted_c = merge_sort(c); 24 | 25 | vectorsorted_a; 26 | int idx1 = 0; 27 | int idx2 = 0; 28 | for(int i=0; ia = {5,3,7,1,8,9}; 58 | vectorans = merge_sort(a); 59 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | 6 | int main() 7 | { 8 | int a[5] = {1,2,4,5,6}; 9 | cout< 2 | 3 | using namespace std; 4 | 5 | vector quick_sort(vector&a) 6 | { 7 | if(a.size()<=1) 8 | return a; 9 | 10 | int pivot = rand()%(a.size()); 11 | 12 | vectorb,c; 13 | for(int i=0;isorted_b = quick_sort(b); 26 | vectorsorted_c = quick_sort(c); 27 | vectorsorted_a; 28 | for(int i=0;ia = {5,2,3,5,4,1}; 41 | vectorsorted_a = quick_sort(a); 42 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | class node 6 | { 7 | public: 8 | int data; 9 | node * nxt; 10 | }; 11 | 12 | class LinkedList 13 | { 14 | public: 15 | node * head; 16 | int sz; 17 | LinkedList() 18 | { 19 | head = NULL; 20 | sz=0; 21 | } 22 | 23 | //Creates a new node with data = value and nxt= NULL 24 | node* CreateNewNode(int value) 25 | { 26 | node *newnode = new node; 27 | newnode->data = value; 28 | newnode->nxt = NULL; 29 | return newnode; 30 | } 31 | 32 | // Insert new value at Head 33 | void InsertAtHead(int value) 34 | { 35 | sz++; 36 | node *a = CreateNewNode(value); 37 | if(head == NULL) 38 | { 39 | head = a; 40 | return; 41 | } 42 | //If head is not NULL 43 | a->nxt = head; 44 | head = a; 45 | } 46 | 47 | //Prints the linked list 48 | void Traverse() 49 | { 50 | node* a = head; 51 | while(a!= NULL) 52 | { 53 | cout<data<<" "; 54 | a = a->nxt; 55 | } 56 | cout<<"\n"; 57 | } 58 | 59 | //Search for a single value 60 | int SearchDistinctValue(int value) 61 | { 62 | node* a = head; 63 | int index = 0; 64 | while(a!= NULL) 65 | { 66 | if(a->data==value) 67 | { 68 | return index; 69 | } 70 | a = a->nxt; 71 | index++; 72 | } 73 | return -1; 74 | } 75 | 76 | //Search all possible occurrence 77 | void SearchAllValue(int value) 78 | { 79 | node* a = head; 80 | int index = 0; 81 | while(a!= NULL) 82 | { 83 | if(a->data==value) 84 | { 85 | cout<nxt; 88 | index++; 89 | } 90 | } 91 | 92 | //Returns number of elements in the linked list 93 | int getSize() 94 | { 95 | //O(1) 96 | return sz; 97 | 98 | 99 | //O(size of linked list) = O(n) 100 | // int sz = 0; 101 | // node *a = head; 102 | // while(a!=NULL) 103 | // { 104 | // sz++; 105 | // a = a->nxt; 106 | // } 107 | // return sz; 108 | } 109 | 110 | //Insert a value at the given index 111 | void InsertAtAnyIndex(int index, int value) 112 | { 113 | if(index <0 || index > sz) 114 | { 115 | return; 116 | } 117 | if(index==0) 118 | { 119 | InsertAtHead(value); 120 | return; 121 | } 122 | sz++; 123 | node *a = head; 124 | int cur_index = 0; 125 | while(cur_index!=index-1) 126 | { 127 | a = a->nxt; 128 | cur_index++; 129 | } 130 | node *newnode = CreateNewNode(value); 131 | newnode->nxt = a->nxt; 132 | a->nxt = newnode; 133 | } 134 | 135 | //Delete the first element of a linked list 136 | void DeleteAtHead() 137 | { 138 | if(head == NULL) 139 | { 140 | return; 141 | } 142 | sz--; 143 | node *a = head; 144 | head = a->nxt; 145 | delete a; 146 | } 147 | 148 | //Delete the value at the given index 149 | void DeleteAnyIndex(int index) 150 | { 151 | if(index <0 || index > sz-1) 152 | { 153 | return; 154 | } 155 | if(index==0) 156 | { 157 | DeleteAtHead(); 158 | return; 159 | } 160 | sz--; 161 | node *a = head; 162 | int cur_index = 0; 163 | while(cur_index != index-1) 164 | { 165 | a = a->nxt; 166 | cur_index++; 167 | } 168 | node *b = a->nxt; 169 | a->nxt = b->nxt; 170 | delete b; 171 | } 172 | }; 173 | 174 | int main() 175 | { 176 | LinkedList l; 177 | l.InsertAtHead(10); 178 | l.InsertAtHead(5); 179 | l.InsertAtHead(20); 180 | 181 | l.Traverse(); 182 | 183 | l.InsertAtAnyIndex(1,100); 184 | 185 | l.Traverse(); 186 | 187 | l.InsertAtAnyIndex(4,200); 188 | 189 | l.Traverse(); 190 | 191 | cout< 2 | 3 | using namespace std; 4 | 5 | class node 6 | { 7 | public: 8 | int data; 9 | node * nxt; 10 | }; 11 | 12 | class LinkedList 13 | { 14 | public: 15 | node * head; 16 | int sz; 17 | LinkedList() 18 | { 19 | head = NULL; 20 | sz=0; 21 | } 22 | 23 | //Creates a new node with data = value and nxt= NULL 24 | node* CreateNewNode(int value) 25 | { 26 | node *newnode = new node; 27 | newnode->data = value; 28 | newnode->nxt = NULL; 29 | return newnode; 30 | } 31 | 32 | // Insert new value at Head 33 | void InsertAtHead(int value) 34 | { 35 | sz++; 36 | node *a = CreateNewNode(value); 37 | if(head == NULL) 38 | { 39 | head = a; 40 | return; 41 | } 42 | //If head is not NULL 43 | a->nxt = head; 44 | head = a; 45 | } 46 | 47 | //Prints the linked list 48 | void Traverse() 49 | { 50 | node* a = head; 51 | while(a!= NULL) 52 | { 53 | cout<data<<" "; 54 | a = a->nxt; 55 | } 56 | cout<<"\n"; 57 | } 58 | 59 | //Search for a single value 60 | int SearchDistinctValue(int value) 61 | { 62 | node* a = head; 63 | int index = 0; 64 | while(a!= NULL) 65 | { 66 | if(a->data==value) 67 | { 68 | return index; 69 | } 70 | a = a->nxt; 71 | index++; 72 | } 73 | return -1; 74 | } 75 | 76 | //Search all possible occurrence 77 | void SearchAllValue(int value) 78 | { 79 | node* a = head; 80 | int index = 0; 81 | while(a!= NULL) 82 | { 83 | if(a->data==value) 84 | { 85 | cout<nxt; 88 | index++; 89 | } 90 | } 91 | 92 | //Returns number of elements in the linked list 93 | int getSize() 94 | { 95 | //O(1) 96 | return sz; 97 | 98 | 99 | //O(size of linked list) = O(n) 100 | // int sz = 0; 101 | // node *a = head; 102 | // while(a!=NULL) 103 | // { 104 | // sz++; 105 | // a = a->nxt; 106 | // } 107 | // return sz; 108 | } 109 | 110 | //Insert a value at the given index 111 | void InsertAtAnyIndex(int index, int value) 112 | { 113 | if(index <0 || index > sz) 114 | { 115 | return; 116 | } 117 | if(index==0) 118 | { 119 | InsertAtHead(value); 120 | return; 121 | } 122 | sz++; 123 | node *a = head; 124 | int cur_index = 0; 125 | while(cur_index!=index-1) 126 | { 127 | a = a->nxt; 128 | cur_index++; 129 | } 130 | node *newnode = CreateNewNode(value); 131 | newnode->nxt = a->nxt; 132 | a->nxt = newnode; 133 | } 134 | 135 | //Delete the first element of a linked list 136 | void DeleteAtHead() 137 | { 138 | if(head == NULL) 139 | { 140 | return; 141 | } 142 | sz--; 143 | node *a = head; 144 | head = a->nxt; 145 | delete a; 146 | } 147 | 148 | //Delete the value at the given index 149 | void DeleteAnyIndex(int index) 150 | { 151 | if(index <0 || index > sz-1) 152 | { 153 | return; 154 | } 155 | if(index==0) 156 | { 157 | DeleteAtHead(); 158 | return; 159 | } 160 | sz--; 161 | node *a = head; 162 | int cur_index = 0; 163 | while(cur_index != index-1) 164 | { 165 | a = a->nxt; 166 | cur_index++; 167 | } 168 | node *b = a->nxt; 169 | a->nxt = b->nxt; 170 | delete b; 171 | } 172 | 173 | void InsertAfterValue(int value , int data) 174 | { 175 | node *a = head; 176 | while(a != NULL) 177 | { 178 | if(a->data == value) 179 | { 180 | break; 181 | } 182 | a = a->nxt; 183 | } 184 | if(a== NULL) 185 | { 186 | cout<nxt = a->nxt; 192 | a->nxt = newnode; 193 | } 194 | 195 | //Print the Reverse Order from node a to last 196 | void ReversePrint2(node *a) 197 | { 198 | if(a==NULL) 199 | { 200 | return; 201 | } 202 | ReversePrint2(a->nxt); 203 | cout<data<<" "; 204 | } 205 | void ReversePrint() 206 | { 207 | ReversePrint2(head); 208 | cout<<"\n"; 209 | } 210 | 211 | }; 212 | 213 | int main() 214 | { 215 | LinkedList l; 216 | l.InsertAtHead(30); 217 | l.InsertAtHead(10); 218 | l.InsertAtHead(5); 219 | l.InsertAtHead(1); 220 | l.Traverse(); 221 | 222 | l.ReversePrint(); 223 | l.Traverse(); 224 | return 0; 225 | } 226 | -------------------------------------------------------------------------------- /Week 4/Module 14/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | 7 | class node 8 | { 9 | public: 10 | int data; 11 | node * nxt; 12 | node * prv; 13 | }; 14 | 15 | class DoublyLinkedList 16 | { 17 | public: 18 | node *head; 19 | int sz; 20 | DoublyLinkedList() 21 | { 22 | head = NULL; 23 | sz = 0; 24 | } 25 | 26 | //Creates a new node with the given data and returns it O(1) 27 | node * CreateNewNode(int data) 28 | { 29 | node *newnode = new node; 30 | newnode->data = data; 31 | newnode->nxt = NULL; 32 | newnode->prv = NULL; 33 | return newnode; 34 | } 35 | 36 | //Inserts a node with given data at head O(1) 37 | void InsertAtHead(int data) 38 | { 39 | sz++; 40 | node *newnode = CreateNewNode(data); 41 | if(head == NULL) 42 | { 43 | head = newnode; 44 | return; 45 | } 46 | node *a = head; 47 | newnode->nxt = a; 48 | a->prv = newnode; 49 | head = newnode; 50 | } 51 | 52 | //Inserts the given data at the given index O(n) 53 | void Insert(int index, int data) 54 | { 55 | if(index > sz) 56 | { 57 | return; 58 | } 59 | if(index==0) 60 | { 61 | InsertAtHead(data); 62 | return; 63 | } 64 | node *a = head; 65 | int cur_index = 0; 66 | while(cur_index!= index-1) 67 | { 68 | a = a->nxt; 69 | cur_index++; 70 | } 71 | // a = cur_index - 1 72 | node *newnode = CreateNewNode(data); 73 | newnode->nxt = a->nxt; 74 | newnode->prv = a; 75 | node *b = a->nxt; 76 | b->prv = newnode; 77 | a->nxt = newnode; 78 | sz++; 79 | } 80 | 81 | //Deletes the given index O(n) 82 | void Delete(int index) 83 | { 84 | if(index >= sz) 85 | { 86 | cout<nxt; 94 | cur_index++; 95 | } 96 | node *b = a->prv; 97 | node *c = a->nxt; 98 | if(b!=NULL) 99 | { 100 | b->nxt = c; 101 | } 102 | if(c!= NULL) 103 | { 104 | c->prv = b; 105 | } 106 | delete a; 107 | if(index==0) 108 | { 109 | head = c; 110 | } 111 | sz--; 112 | } 113 | 114 | //Prints the linked list O(n) 115 | void Traverse() 116 | { 117 | node *a = head; 118 | while(a!=NULL) 119 | { 120 | cout<data<<" "; 121 | a = a->nxt; 122 | } 123 | cout<<"\n"; 124 | } 125 | 126 | // Returns the size of linked list O(1) 127 | int getSize() 128 | { 129 | return sz; 130 | } 131 | 132 | //Reverse the doubly linked list O(n) 133 | void Reverse() 134 | { 135 | if(head==NULL) 136 | { 137 | return; 138 | } 139 | node *a = head; 140 | int cur_index = 0; 141 | while(cur_index != sz-1) 142 | { 143 | a = a->nxt; 144 | cur_index++; 145 | } 146 | // last index is in a 147 | 148 | node *b = head; 149 | while(b!= NULL) 150 | { 151 | swap(b->nxt, b->prv); 152 | b = b->prv; 153 | } 154 | head = a; 155 | } 156 | }; 157 | 158 | 159 | int main() 160 | { 161 | DoublyLinkedList dl; 162 | dl.InsertAtHead(10); 163 | dl.InsertAtHead(5); 164 | dl.InsertAtHead(1); 165 | 166 | dl.Traverse(); 167 | dl.Insert(2,100); 168 | 169 | dl.Traverse(); 170 | 171 | dl.Reverse(); 172 | dl.Traverse(); 173 | 174 | 175 | return 0; 176 | } 177 | -------------------------------------------------------------------------------- /Week 5/Module 16/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | //Time O(n) 8 | //Space O(n) 9 | int n; 10 | cin>>n; 11 | 12 | vectora(n); 13 | for(int i=0;i>a[i]; 16 | } 17 | 18 | int sum = 0; 19 | for(int i=0;i 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | //O(n*log(n)) 8 | for(int i=1;i<=n;i++) 9 | { 10 | for(int j=1;j<=n;j=j+i) 11 | { 12 | cout< 2 | using namespace std; 3 | 4 | // O(2^n) 5 | int fib(int n) 6 | { 7 | if(n<=1) 8 | return n; 9 | return fib(n-1) + fib(n-2); 10 | } 11 | int main() 12 | { 13 | int n; 14 | cin>>n; 15 | cout< 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | //Time O(n^2) 8 | //Space O(n^2) 9 | int n; 10 | cin>>n; 11 | 12 | vectora(n*n); 13 | for(int i=0;i>a[i]; 16 | } 17 | 18 | int sum = 0; 19 | for(int i=0;i 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | //Time O(sqrt(n)) 8 | //Space O(1) 9 | int n; 10 | cin>>n; 11 | for(int i=1;i*i<=n;i++) 12 | { 13 | if(n%i==0) 14 | { 15 | cout< 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | //Time O(n^2) 8 | for(int i=0;i 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | // Time O(n + q) 8 | int n,q; 9 | cin>>n>>q; 10 | for(int i=0;i 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | // Time O(n^3) 8 | for(int i=0;i 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | for(int i=0;i 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | // O(logn) 8 | for(int i=1;i 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | // O(n*log(n)) 8 | for(int i=1;i<=n;i++) 9 | { 10 | if(__builtin_popcount(i)==1) 11 | { 12 | for(int j=1;j<=n;j++) 13 | { 14 | cout< 2 | using namespace std; 3 | 4 | int factorial(int n) 5 | { 6 | if(n==0) 7 | return 1; 8 | 9 | return factorial(n-1)*n; 10 | } 11 | int main() 12 | { 13 | int n; 14 | cin>>n; 15 | cout< 2 | using namespace std; 3 | 4 | vectorcoins = {1,3,4}; 5 | 6 | int coin_change(int n) 7 | { 8 | if(n==0) 9 | return 0; 10 | 11 | int ans = 100000000; 12 | for(int i=0; i=coins[i]) 15 | { 16 | ans = min(ans, 1+coin_change(n-coins[i]) ); 17 | } 18 | } 19 | return ans; 20 | } 21 | 22 | int main() 23 | { 24 | int n; 25 | cin>>n; 26 | cout< 2 | using namespace std; 3 | 4 | vector merge_sort(vectora) 5 | { 6 | if(a.size() <= 1) 7 | { 8 | return a; 9 | } 10 | 11 | vectorLeft; 12 | vectorRight; 13 | int mid = a.size()/2; 14 | 15 | for(int i=0;isorted_Left = merge_sort(Left); 22 | vectorsorted_Right = merge_sort(Right); 23 | 24 | vectorans; 25 | int p1 = 0; 26 | int p2 = 0; 27 | for(int i=0;ia = {4,5,2,1,3}; 55 | 56 | vectorans = merge_sort(a); 57 | 58 | for(int i=0;i 2 | using namespace std; 3 | 4 | class node 5 | { 6 | public: 7 | int data; 8 | node* nxt; 9 | }; 10 | 11 | class LinkedList 12 | { 13 | public: 14 | node *head; 15 | 16 | LinkedList() 17 | { 18 | head = NULL; 19 | } 20 | 21 | node* CreateNewNode(int data) 22 | { 23 | node *newnode = new node; 24 | newnode->data = data; 25 | newnode->nxt = NULL; 26 | return newnode; 27 | } 28 | 29 | void InsertAtHead(int data) 30 | { 31 | node *newnode = CreateNewNode(data); 32 | newnode->nxt = head; 33 | head = newnode; 34 | } 35 | 36 | void Traverse() 37 | { 38 | node *a = head; 39 | while(a!=NULL) 40 | { 41 | cout<data<<" "; 42 | a = a->nxt; 43 | } 44 | cout<<"\n"; 45 | } 46 | 47 | void Insert(int index, int data) 48 | { 49 | if(index==0) 50 | { 51 | InsertAtHead(data); 52 | return; 53 | } 54 | node *a = head; 55 | int cur_index = 0; 56 | while(cur_index!= index-1) 57 | { 58 | if(a==NULL) 59 | { 60 | break; 61 | } 62 | a = a->nxt; 63 | cur_index++; 64 | } 65 | if(a==NULL) 66 | { 67 | cout<<"Can't insert\n"; 68 | return; 69 | } 70 | node *newnode = CreateNewNode(data); 71 | newnode->nxt = a->nxt; 72 | a->nxt = newnode; 73 | } 74 | }; 75 | 76 | int main() 77 | { 78 | LinkedList l; 79 | l.InsertAtHead(30); 80 | l.InsertAtHead(20); 81 | l.InsertAtHead(10); 82 | l.Traverse(); 83 | 84 | l.Insert(1,5); 85 | l.Traverse(); 86 | l.Insert(3,8); 87 | l.Traverse(); 88 | return 0; 89 | } 90 | 91 | -------------------------------------------------------------------------------- /Week 5/Module 18/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node 5 | { 6 | public: 7 | int data; 8 | node* nxt; 9 | node* prv; 10 | }; 11 | 12 | class DoublyLinkedList 13 | { 14 | public: 15 | node *head; 16 | 17 | DoublyLinkedList() 18 | { 19 | head = NULL; 20 | } 21 | 22 | node* CreateNewNode(int data) 23 | { 24 | node *newnode = new node; 25 | newnode->data = data; 26 | newnode->nxt = NULL; 27 | newnode->prv = NULL; 28 | return newnode; 29 | } 30 | 31 | void InsertAtHead(int data) 32 | { 33 | node *newnode = CreateNewNode(data); 34 | if(head == NULL) 35 | { 36 | head = newnode; 37 | return; 38 | } 39 | newnode->nxt = head; 40 | head->prv = newnode; 41 | head = newnode; 42 | } 43 | 44 | void Traverse() 45 | { 46 | node *a = head; 47 | while(a!=NULL) 48 | { 49 | cout<data<<" "; 50 | a = a->nxt; 51 | } 52 | cout<<"\n"; 53 | } 54 | 55 | void Insert(int index, int data) 56 | { 57 | if(index==0) 58 | { 59 | InsertAtHead(data); 60 | return; 61 | } 62 | node *a = head; 63 | int cur_index = 0; 64 | while(cur_index!= index-1) 65 | { 66 | if(a==NULL) 67 | { 68 | break; 69 | } 70 | a = a->nxt; 71 | cur_index++; 72 | } 73 | if(a==NULL) 74 | { 75 | cout<<"Can't insert\n"; 76 | return; 77 | } 78 | 79 | node* newnode = CreateNewNode(data); 80 | newnode->nxt = a->nxt; 81 | node* b = a->nxt; 82 | if(b!= NULL) 83 | { 84 | b->prv = newnode; 85 | } 86 | a->nxt = newnode; 87 | newnode->prv = a; 88 | } 89 | }; 90 | 91 | int main() 92 | { 93 | DoublyLinkedList dl; 94 | dl.InsertAtHead(30); 95 | dl.InsertAtHead(20); 96 | dl.InsertAtHead(10); 97 | 98 | dl.Traverse(); 99 | 100 | dl.Insert(2,40); 101 | dl.Traverse(); 102 | 103 | dl.Insert(4,100); 104 | dl.Traverse(); 105 | 106 | dl.Insert(6,90); 107 | 108 | return 0; 109 | } 110 | 111 | -------------------------------------------------------------------------------- /Week 6/Module 20/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int MAX_SIZE = 500; 4 | 5 | 6 | //Stack using static array 7 | class Stack{ 8 | public: 9 | int a[MAX_SIZE]; 10 | int stack_size; 11 | Stack() 12 | { 13 | stack_size = 0; 14 | } 15 | 16 | // Add an element in the stack. O(1) 17 | void push(int val) 18 | { 19 | if(stack_size+1 > MAX_SIZE) 20 | { 21 | cout<<"Stack is full!\n"; 22 | return; 23 | } 24 | stack_size = stack_size + 1; 25 | a[stack_size-1] = val; 26 | } 27 | 28 | //Delete the topmost element from the stack. O(1) 29 | void pop() 30 | { 31 | if(stack_size==0) 32 | { 33 | cout<<"Stack is empty!\n"; 34 | return; 35 | } 36 | a[stack_size -1] = 0; 37 | stack_size = stack_size - 1; 38 | } 39 | 40 | //Returns the top element from the stack. O(1) 41 | int top() 42 | { 43 | if(stack_size == 0) 44 | { 45 | cout<<"Stack is empty!\n"; 46 | return -1; 47 | } 48 | return a[stack_size - 1]; 49 | } 50 | }; 51 | 52 | 53 | int main() 54 | { 55 | Stack st; 56 | st.push(3); 57 | cout< 2 | using namespace std; 3 | 4 | //Stack using dynamic array 5 | class Stack{ 6 | public: 7 | int *a; 8 | int stack_size; 9 | int array_cap; 10 | 11 | Stack() 12 | { 13 | a = new int[1]; 14 | array_cap = 1; 15 | stack_size = 0; 16 | } 17 | 18 | //Makes the array cap multiplied by 2. 19 | void increase_size() 20 | { 21 | int *tmp; 22 | tmp = new int[array_cap*2]; 23 | for(int i=0;i array_cap) 33 | { 34 | increase_size(); 35 | } 36 | stack_size = stack_size + 1; 37 | a[stack_size-1] = val; 38 | } 39 | 40 | //Delete the topmost element from the stack. O(1) 41 | void pop() 42 | { 43 | if(stack_size==0) 44 | { 45 | cout<<"Stack is empty!\n"; 46 | return; 47 | } 48 | a[stack_size -1] = 0; 49 | stack_size = stack_size - 1; 50 | } 51 | 52 | //Returns the top element from the stack. O(1) 53 | int top() 54 | { 55 | if(stack_size == 0) 56 | { 57 | cout<<"Stack is empty!\n"; 58 | return -1; 59 | } 60 | return a[stack_size - 1]; 61 | } 62 | }; 63 | 64 | int main() 65 | { 66 | Stack st; 67 | st.push(3); 68 | cout< 2 | using namespace std; 3 | 4 | 5 | class node 6 | { 7 | public: 8 | int data; 9 | node * nxt; 10 | node * prv; 11 | }; 12 | 13 | class DoublyLinkedList 14 | { 15 | public: 16 | node *head; 17 | int sz; 18 | DoublyLinkedList() 19 | { 20 | head = NULL; 21 | sz = 0; 22 | } 23 | 24 | //Creates a new node with the given data and returns it O(1) 25 | node * CreateNewNode(int data) 26 | { 27 | node *newnode = new node; 28 | newnode->data = data; 29 | newnode->nxt = NULL; 30 | newnode->prv = NULL; 31 | return newnode; 32 | } 33 | 34 | //Inserts a node with given data at head O(1) 35 | void InsertAtHead(int data) 36 | { 37 | sz++; 38 | node *newnode = CreateNewNode(data); 39 | if(head == NULL) 40 | { 41 | head = newnode; 42 | return; 43 | } 44 | node *a = head; 45 | newnode->nxt = a; 46 | a->prv = newnode; 47 | head = newnode; 48 | } 49 | 50 | //Inserts the given data at the given index O(n) 51 | void Insert(int index, int data) 52 | { 53 | if(index > sz) 54 | { 55 | return; 56 | } 57 | if(index==0) 58 | { 59 | InsertAtHead(data); 60 | return; 61 | } 62 | node *a = head; 63 | int cur_index = 0; 64 | while(cur_index!= index-1) 65 | { 66 | a = a->nxt; 67 | cur_index++; 68 | } 69 | // a = cur_index - 1 70 | node *newnode = CreateNewNode(data); 71 | newnode->nxt = a->nxt; 72 | newnode->prv = a; 73 | node *b = a->nxt; 74 | b->prv = newnode; 75 | a->nxt = newnode; 76 | sz++; 77 | } 78 | //Deletes the value at head. O(1) 79 | void DeleteAtHead() 80 | { 81 | if(head== NULL) 82 | { 83 | return; 84 | } 85 | node *a = head; 86 | node *b = head->nxt; 87 | delete a; 88 | if(b!= NULL) 89 | { 90 | b->prv = NULL; 91 | } 92 | head= b; 93 | sz--; 94 | } 95 | //Deletes the given index O(n) 96 | void Delete(int index) 97 | { 98 | if(index >= sz) 99 | { 100 | cout<nxt; 108 | cur_index++; 109 | } 110 | node *b = a->prv; 111 | node *c = a->nxt; 112 | if(b!=NULL) 113 | { 114 | b->nxt = c; 115 | } 116 | if(c!= NULL) 117 | { 118 | c->prv = b; 119 | } 120 | delete a; 121 | if(index==0) 122 | { 123 | head = c; 124 | } 125 | sz--; 126 | } 127 | 128 | //Prints the linked list O(n) 129 | void Traverse() 130 | { 131 | node *a = head; 132 | while(a!=NULL) 133 | { 134 | cout<data<<" "; 135 | a = a->nxt; 136 | } 137 | cout<<"\n"; 138 | } 139 | 140 | // Returns the size of linked list O(1) 141 | int getSize() 142 | { 143 | return sz; 144 | } 145 | 146 | //Reverse the doubly linked list O(n) 147 | void Reverse() 148 | { 149 | if(head==NULL) 150 | { 151 | return; 152 | } 153 | node *a = head; 154 | int cur_index = 0; 155 | while(cur_index != sz-1) 156 | { 157 | a = a->nxt; 158 | cur_index++; 159 | } 160 | // last index is in a 161 | 162 | node *b = head; 163 | while(b!= NULL) 164 | { 165 | swap(b->nxt, b->prv); 166 | b = b->prv; 167 | } 168 | head = a; 169 | } 170 | }; 171 | 172 | //Stack using doubly linked list 173 | class Stack 174 | { 175 | public: 176 | DoublyLinkedList dl; 177 | 178 | Stack() 179 | { 180 | 181 | } 182 | 183 | int top() 184 | { 185 | if(dl.getSize()==0) 186 | { 187 | cout<<"Stack is empty!\n"; 188 | return -1; 189 | } 190 | return dl.head->data; 191 | } 192 | 193 | void push(int val) 194 | { 195 | dl.InsertAtHead(val); 196 | } 197 | 198 | void pop() 199 | { 200 | if(dl.getSize()==0) 201 | { 202 | cout<<"Stack is empty!\n"; 203 | return; 204 | } 205 | dl.DeleteAtHead(); 206 | } 207 | }; 208 | 209 | int main() 210 | { 211 | Stack st; 212 | st.push(3); 213 | cout< 2 | using namespace std; 3 | 4 | //Template based stack using dynamic array 5 | template 6 | class Stack{ 7 | public: 8 | T *a; 9 | int stack_size; 10 | int array_cap; 11 | 12 | Stack() 13 | { 14 | a = new T[1]; 15 | array_cap = 1; 16 | stack_size = 0; 17 | } 18 | 19 | //Makes the array cap multiplied by 2. 20 | void increase_size() 21 | { 22 | T *tmp; 23 | tmp = new T[array_cap*2]; 24 | for(int i=0;i array_cap) 34 | { 35 | increase_size(); 36 | } 37 | stack_size = stack_size + 1; 38 | a[stack_size-1] = val; 39 | } 40 | 41 | //Delete the topmost element from the stack. O(1) 42 | void pop() 43 | { 44 | if(stack_size==0) 45 | { 46 | cout<<"Stack is empty!\n"; 47 | return; 48 | } 49 | stack_size = stack_size - 1; 50 | } 51 | 52 | //Returns the top element from the stack. O(1) 53 | T top() 54 | { 55 | if(stack_size == 0) 56 | { 57 | cout<<"Stack is empty!\n"; 58 | T a; 59 | return a; 60 | } 61 | return a[stack_size - 1]; 62 | } 63 | }; 64 | 65 | int main() 66 | { 67 | Stack st; 68 | st.push(3.5); 69 | st.push(3.6); 70 | st.push(7.8); 71 | cout< st2; 80 | st2.push('a'); 81 | st2.push('g'); 82 | st2.push('u'); 83 | cout< 2 | using namespace std; 3 | 4 | template 5 | class node 6 | { 7 | public: 8 | T data; 9 | node * nxt; 10 | node * prv; 11 | }; 12 | 13 | template 14 | class DoublyLinkedList 15 | { 16 | public: 17 | node *head; 18 | int sz; 19 | DoublyLinkedList() 20 | { 21 | head = NULL; 22 | sz = 0; 23 | } 24 | 25 | //Creates a new node with the given data and returns it O(1) 26 | node * CreateNewNode(T data) 27 | { 28 | node *newnode = new node; 29 | newnode->data = data; 30 | newnode->nxt = NULL; 31 | newnode->prv = NULL; 32 | return newnode; 33 | } 34 | 35 | //Inserts a node with given data at head O(1) 36 | void InsertAtHead(T data) 37 | { 38 | sz++; 39 | node *newnode = CreateNewNode(data); 40 | if(head == NULL) 41 | { 42 | head = newnode; 43 | return; 44 | } 45 | node *a = head; 46 | newnode->nxt = a; 47 | a->prv = newnode; 48 | head = newnode; 49 | } 50 | //Deletes the value at head. O(1) 51 | void DeleteAtHead() 52 | { 53 | if(head== NULL) 54 | { 55 | return; 56 | } 57 | node *a = head; 58 | node *b = head->nxt; 59 | delete a; 60 | if(b!= NULL) 61 | { 62 | b->prv = NULL; 63 | } 64 | head= b; 65 | sz--; 66 | } 67 | // Returns the size of linked list O(1) 68 | int getSize() 69 | { 70 | return sz; 71 | } 72 | }; 73 | 74 | //Stack using doubly linked list 75 | template 76 | class Stack 77 | { 78 | public: 79 | DoublyLinkedList dl; 80 | 81 | Stack() 82 | { 83 | 84 | } 85 | 86 | T top() 87 | { 88 | if(dl.getSize()==0) 89 | { 90 | cout<<"Stack is empty!\n"; 91 | T a; 92 | return a; 93 | } 94 | return dl.head->data; 95 | } 96 | 97 | void push(T val) 98 | { 99 | dl.InsertAtHead(val); 100 | } 101 | 102 | void pop() 103 | { 104 | if(dl.getSize()==0) 105 | { 106 | cout<<"Stack is empty!\n"; 107 | return; 108 | } 109 | dl.DeleteAtHead(); 110 | } 111 | }; 112 | 113 | int main() 114 | { 115 | Stack st; 116 | st.push(3.5); 117 | st.push(3.6); 118 | st.push(7.8); 119 | cout< st2; 128 | st2.push('a'); 129 | st2.push('g'); 130 | st2.push('u'); 131 | cout< 2 | using namespace std; 3 | 4 | template 5 | class node 6 | { 7 | public: 8 | T data; 9 | node * nxt; 10 | node * prv; 11 | }; 12 | 13 | template 14 | class DoublyLinkedList 15 | { 16 | public: 17 | node *head; 18 | int sz; 19 | DoublyLinkedList() 20 | { 21 | head = NULL; 22 | sz = 0; 23 | } 24 | 25 | //Creates a new node with the given data and returns it O(1) 26 | node * CreateNewNode(T data) 27 | { 28 | node *newnode = new node; 29 | newnode->data = data; 30 | newnode->nxt = NULL; 31 | newnode->prv = NULL; 32 | return newnode; 33 | } 34 | 35 | //Inserts a node with given data at head O(1) 36 | void InsertAtHead(T data) 37 | { 38 | sz++; 39 | node *newnode = CreateNewNode(data); 40 | if(head == NULL) 41 | { 42 | head = newnode; 43 | return; 44 | } 45 | node *a = head; 46 | newnode->nxt = a; 47 | a->prv = newnode; 48 | head = newnode; 49 | } 50 | //Deletes the value at head. O(1) 51 | void DeleteAtHead() 52 | { 53 | if(head== NULL) 54 | { 55 | return; 56 | } 57 | node *a = head; 58 | node *b = head->nxt; 59 | delete a; 60 | if(b!= NULL) 61 | { 62 | b->prv = NULL; 63 | } 64 | head= b; 65 | sz--; 66 | } 67 | // Returns the size of linked list O(1) 68 | int getSize() 69 | { 70 | return sz; 71 | } 72 | }; 73 | 74 | //Stack using doubly linked list 75 | template 76 | class Stack 77 | { 78 | public: 79 | DoublyLinkedList dl; 80 | 81 | Stack() 82 | { 83 | 84 | } 85 | 86 | T top() 87 | { 88 | if(dl.getSize()==0) 89 | { 90 | cout<<"Stack is empty!\n"; 91 | T a; 92 | return a; 93 | } 94 | return dl.head->data; 95 | } 96 | 97 | void push(T val) 98 | { 99 | dl.InsertAtHead(val); 100 | } 101 | 102 | void pop() 103 | { 104 | if(dl.getSize()==0) 105 | { 106 | cout<<"Stack is empty!\n"; 107 | return; 108 | } 109 | dl.DeleteAtHead(); 110 | } 111 | 112 | int getSize() 113 | { 114 | return dl.getSize(); 115 | } 116 | }; 117 | 118 | int main() 119 | { 120 | //Reverse a stack using another stack 121 | Stacka; 122 | a.push(4); 123 | a.push(6); 124 | a.push(3); 125 | a.push(7); 126 | 127 | Stacktmp; 128 | while(a.getSize()>0) 129 | { 130 | tmp.push(a.top()); 131 | a.pop(); 132 | } 133 | swap(a,tmp); 134 | 135 | while(a.getSize() > 0) 136 | { 137 | cout< 2 | using namespace std; 3 | 4 | template 5 | class node 6 | { 7 | public: 8 | T data; 9 | node * nxt; 10 | node * prv; 11 | }; 12 | 13 | template 14 | class DoublyLinkedList 15 | { 16 | public: 17 | node *head; 18 | int sz; 19 | DoublyLinkedList() 20 | { 21 | head = NULL; 22 | sz = 0; 23 | } 24 | 25 | //Creates a new node with the given data and returns it O(1) 26 | node * CreateNewNode(T data) 27 | { 28 | node *newnode = new node; 29 | newnode->data = data; 30 | newnode->nxt = NULL; 31 | newnode->prv = NULL; 32 | return newnode; 33 | } 34 | 35 | //Inserts a node with given data at head O(1) 36 | void InsertAtHead(T data) 37 | { 38 | sz++; 39 | node *newnode = CreateNewNode(data); 40 | if(head == NULL) 41 | { 42 | head = newnode; 43 | return; 44 | } 45 | node *a = head; 46 | newnode->nxt = a; 47 | a->prv = newnode; 48 | head = newnode; 49 | } 50 | //Deletes the value at head. O(1) 51 | void DeleteAtHead() 52 | { 53 | if(head== NULL) 54 | { 55 | return; 56 | } 57 | node *a = head; 58 | node *b = head->nxt; 59 | delete a; 60 | if(b!= NULL) 61 | { 62 | b->prv = NULL; 63 | } 64 | head= b; 65 | sz--; 66 | } 67 | // Returns the size of linked list O(1) 68 | int getSize() 69 | { 70 | return sz; 71 | } 72 | }; 73 | 74 | //Stack using doubly linked list 75 | template 76 | class Stack 77 | { 78 | public: 79 | DoublyLinkedList dl; 80 | 81 | Stack() 82 | { 83 | 84 | } 85 | 86 | T top() 87 | { 88 | if(dl.getSize()==0) 89 | { 90 | cout<<"Stack is empty!\n"; 91 | T a; 92 | return a; 93 | } 94 | return dl.head->data; 95 | } 96 | 97 | void push(T val) 98 | { 99 | dl.InsertAtHead(val); 100 | } 101 | 102 | void pop() 103 | { 104 | if(dl.getSize()==0) 105 | { 106 | cout<<"Stack is empty!\n"; 107 | return; 108 | } 109 | dl.DeleteAtHead(); 110 | } 111 | 112 | int getSize() 113 | { 114 | return dl.getSize(); 115 | } 116 | }; 117 | 118 | int main() 119 | { 120 | //O(n^2) 121 | Stack st; 122 | st.push(5); 123 | st.push(2); 124 | st.push(7); 125 | st.push(8); 126 | st.push(3); 127 | 128 | 129 | Stacktmp; 130 | while(st.getSize() > 0) 131 | { 132 | int t = st.top(); 133 | st.pop(); 134 | while(tmp.getSize()>0) 135 | { 136 | if(tmp.top() < t) 137 | { 138 | break; 139 | } 140 | st.push(tmp.top()); 141 | tmp.pop(); 142 | } 143 | tmp.push(t); 144 | } 145 | swap(st,tmp); 146 | 147 | while(st.getSize() > 0) 148 | { 149 | cout< 2 | using namespace std; 3 | 4 | void print(listl) 5 | { 6 | //O(n) 7 | // list::iterator a = l.begin(); 8 | auto a = l.begin(); 9 | while(a!=l.end()) 10 | { 11 | cout<<*a<<" "; 12 | a++; 13 | } 14 | cout<<"\n"; 15 | } 16 | 17 | //O(n) 18 | void Insert(list&l, int index , int value) 19 | { 20 | auto it = l.begin(); //O(1) 21 | advance(it , index); //O(index) 22 | l.insert(it , value); //O(1) 23 | } 24 | 25 | //O(n) 26 | void Delete(list&l , int index) 27 | { 28 | auto it = l.begin(); //O(1) 29 | advance(it , index); //O(index) 30 | l.erase(it); //O(1) 31 | } 32 | int main() 33 | { 34 | list l; 35 | //push_front O(1) 36 | l.push_front(10); 37 | l.push_front(20); 38 | l.push_front(30); 39 | 40 | print(l); 41 | 42 | //push_back O(1) 43 | l.push_back(40); 44 | l.push_back(50); 45 | print(l); 46 | 47 | //pop_back O(1) 48 | l.pop_back(); 49 | print(l); 50 | 51 | //pop_front O(1) 52 | l.pop_front(); 53 | print(l); 54 | 55 | //insert at any position 56 | Insert(l,1,100); 57 | print(l); 58 | 59 | //Delete at any position 60 | Delete(l,1); 61 | print(l); 62 | 63 | //size 64 | cout< 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | //Stack 8 | stack st; 9 | 10 | //push O(1) 11 | st.push(10); 12 | st.push(20); 13 | st.push(30); 14 | 15 | //size O(1) 16 | cout<<"Stack size : "< 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | string s; 8 | cin>>s; 9 | 10 | stackst; 11 | for(int i=0; i 2 | using namespace std; 3 | 4 | int prec(char ch) 5 | { 6 | if(ch=='+' || ch=='-') 7 | { 8 | return 0; 9 | } 10 | // * or / 11 | return 1; 12 | } 13 | int main() 14 | { 15 | string s; 16 | cin>>s; 17 | stackst; 18 | string ans=""; 19 | 20 | for(int i=0;i='a' && now <= 'z') 24 | { 25 | ans += now; 26 | } 27 | else{ 28 | while(st.size() && prec(st.top()) >= prec(now) ) 29 | { 30 | ans += st.top(); 31 | st.pop(); 32 | } 33 | st.push(now); 34 | } 35 | } 36 | while(st.size()) 37 | { 38 | ans += st.top(); 39 | st.pop(); 40 | } 41 | 42 | cout< 2 | using namespace std; 3 | 4 | const int MAX_SIZE = 500; 5 | 6 | //Queue using Array 7 | class Queue 8 | { 9 | public: 10 | int a[MAX_SIZE]; 11 | int l,r; 12 | 13 | Queue() 14 | { 15 | l = 0; 16 | r = -1; 17 | } 18 | // O(1) 19 | void enqueue(int value) 20 | { 21 | if(r+1 >= MAX_SIZE) 22 | { 23 | cout<<"Queue is full\n"; 24 | return; 25 | } 26 | r++; 27 | a[r] = value; 28 | } 29 | 30 | // O(1) 31 | void dequeue() 32 | { 33 | if(l > r) 34 | { 35 | cout<<"Queue is empty\n"; 36 | return; 37 | } 38 | l++; 39 | } 40 | //O(1) 41 | int front() 42 | { 43 | if(l>r) 44 | { 45 | cout<<"Queue is empty\n"; 46 | return -1; 47 | } 48 | return a[l]; 49 | } 50 | //O(1) 51 | int size() 52 | { 53 | return r-l+1; 54 | } 55 | }; 56 | 57 | int main() 58 | { 59 | Queue q; 60 | q.enqueue(5); 61 | q.enqueue(6); 62 | q.enqueue(7); 63 | 64 | cout<<"Q size : "< 2 | using namespace std; 3 | 4 | const int MAX_SIZE = 500; 5 | 6 | //Queue using Circular array 7 | class Queue 8 | { 9 | public: 10 | int a[MAX_SIZE]; 11 | int l,r; 12 | int sz; 13 | 14 | Queue() 15 | { 16 | l = 0; 17 | r = -1; 18 | sz = 0; 19 | } 20 | // O(1) 21 | void enqueue(int value) 22 | { 23 | if(sz == MAX_SIZE) 24 | { 25 | cout<<"Queue is full\n"; 26 | return; 27 | } 28 | r++; 29 | if(r == MAX_SIZE) 30 | { 31 | r = 0; 32 | } 33 | a[r] = value; 34 | sz++; 35 | } 36 | 37 | // O(1) 38 | void dequeue() 39 | { 40 | if(sz == 0) 41 | { 42 | cout<<"Queue is empty\n"; 43 | return; 44 | } 45 | l++; 46 | if(l==MAX_SIZE) 47 | { 48 | l = 0; 49 | } 50 | sz--; 51 | } 52 | //O(1) 53 | int front() 54 | { 55 | if(sz == 0) 56 | { 57 | cout<<"Queue is empty\n"; 58 | return -1; 59 | } 60 | return a[l]; 61 | } 62 | //O(1) 63 | int size() 64 | { 65 | return sz; 66 | } 67 | }; 68 | 69 | int main() 70 | { 71 | Queue q; 72 | q.enqueue(5); 73 | q.enqueue(6); 74 | q.enqueue(7); 75 | 76 | cout<<"Q size : "< 2 | using namespace std; 3 | 4 | 5 | //Queue using Dynamic Circular array 6 | class Queue 7 | { 8 | public: 9 | int *a; 10 | int array_cap; 11 | int l,r; 12 | int sz; 13 | 14 | Queue() 15 | { 16 | a = new int[1]; 17 | array_cap = 1; 18 | l = 0; 19 | r = -1; 20 | sz = 0; 21 | } 22 | void remove_circular() 23 | { 24 | if(l>r) 25 | { 26 | int *tmp = new int[array_cap]; 27 | int idx = 0; 28 | for(int i=l; i 2 | using namespace std; 3 | 4 | // Queue using Linked List 5 | 6 | class node{ 7 | public: 8 | int data; 9 | node* nxt; 10 | }; 11 | 12 | class Queue{ 13 | public: 14 | node* head; 15 | node* tail; 16 | int sz; 17 | 18 | Queue() 19 | { 20 | head = NULL; 21 | tail = NULL; 22 | sz = 0; 23 | } 24 | node * CreateNode(int value) 25 | { 26 | node* newnode = new node; 27 | newnode->data = value; 28 | newnode->nxt = NULL; 29 | return newnode; 30 | } 31 | void enqueue(int value) 32 | { 33 | sz++; 34 | node* newnode = CreateNode(value); 35 | if(head == NULL) 36 | { 37 | head = newnode; 38 | tail = newnode; 39 | return; 40 | } 41 | tail->nxt = newnode; 42 | tail = newnode; 43 | } 44 | 45 | void dequeue() 46 | { 47 | if(sz==0) 48 | { 49 | cout<<"Queue is empty\n"; 50 | return; 51 | } 52 | if(sz==1) 53 | { 54 | delete head; 55 | head = NULL; 56 | tail = NULL; 57 | sz--; 58 | return; 59 | } 60 | node* a = head; 61 | head = head->nxt; 62 | sz--; 63 | delete a; 64 | } 65 | 66 | int front() 67 | { 68 | if(sz==0) 69 | { 70 | cout<<"Queue is empty\n"; 71 | return -1; 72 | } 73 | return head->data; 74 | } 75 | 76 | int size() 77 | { 78 | return sz; 79 | } 80 | }; 81 | 82 | int main() 83 | { 84 | Queue q; 85 | q.enqueue(5); 86 | q.enqueue(10); 87 | q.enqueue(15); 88 | 89 | cout<<"Q size : "< 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | queue q; 8 | //enqueue O(1) 9 | q.push(5); 10 | q.push(10); 11 | q.push(15); 12 | 13 | //size O(1) 14 | cout<<"Queue size : "<q2; 33 | q2.push('a'); 34 | q2.push('b'); 35 | q2.push('c'); 36 | 37 | cout< 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | int n; 8 | cin>>n; 9 | queueq; 10 | q.push("1"); 11 | for(int i=1;i<=n;i++) 12 | { 13 | cout<<"Binary representation of "< 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node* prv; 8 | node* nxt; 9 | }; 10 | 11 | class Deque{ 12 | public: 13 | node* head; 14 | node* tail; 15 | int sz; 16 | 17 | Deque() 18 | { 19 | head = NULL; 20 | tail = NULL; 21 | sz = 0; 22 | } 23 | 24 | node* CreateNewNode(int value) 25 | { 26 | node* newnode = new node; 27 | newnode->data = value; 28 | newnode->nxt = NULL; 29 | newnode->prv = NULL; 30 | return newnode; 31 | } 32 | 33 | //pushback O(1) 34 | void push_back(int value) 35 | { 36 | node* newnode = CreateNewNode(value); 37 | if(sz==0) 38 | { 39 | head = newnode; 40 | tail = newnode; 41 | sz++; 42 | return; 43 | } 44 | tail->nxt = newnode; 45 | newnode->prv = tail; 46 | tail = newnode; 47 | sz++; 48 | } 49 | 50 | //pushfront O(1) 51 | void push_front(int value) 52 | { 53 | node* newnode = CreateNewNode(value); 54 | if(sz==0) 55 | { 56 | head = newnode; 57 | tail = newnode; 58 | sz++; 59 | return; 60 | } 61 | head->prv = newnode; 62 | newnode->nxt = head; 63 | head = newnode; 64 | sz++; 65 | } 66 | 67 | //popback O(1) 68 | void pop_back() 69 | { 70 | if(sz==0) 71 | { 72 | cout<<"Deque is empty\n"; 73 | return; 74 | } 75 | if(sz==1) 76 | { 77 | delete tail; 78 | head = NULL; 79 | tail = NULL; 80 | sz--; 81 | return; 82 | } 83 | node *a = tail; 84 | tail= tail->prv; 85 | delete a; 86 | tail->nxt = NULL; 87 | sz--; 88 | } 89 | //popfront O(1) 90 | void pop_front() 91 | { 92 | if(sz==0) 93 | { 94 | cout<<"Deque is empty\n"; 95 | return; 96 | } 97 | if(sz==1) 98 | { 99 | delete tail; 100 | head = NULL; 101 | tail = NULL; 102 | sz--; 103 | return; 104 | } 105 | 106 | node *a = head; 107 | head = head->nxt; 108 | delete a; 109 | head->prv = NULL; 110 | sz--; 111 | } 112 | 113 | //front O(1) 114 | int front() 115 | { 116 | if(sz==0) 117 | { 118 | cout<<"Deque is empty\n"; 119 | return -1; 120 | } 121 | return head->data; 122 | } 123 | 124 | //back O(1) 125 | int back() 126 | { 127 | if(sz==0) 128 | { 129 | cout<<"Deque is empty\n"; 130 | return -1; 131 | } 132 | return tail->data; 133 | } 134 | }; 135 | 136 | 137 | int main() 138 | { 139 | Deque d; 140 | d.push_back(5); 141 | d.push_back(10); 142 | d.push_back(15); 143 | 144 | cout<<"Back : "< 2 | using namespace std; 3 | 4 | //Deque with reverse operation 5 | class node 6 | { 7 | public: 8 | int data; 9 | node* prv; 10 | node* nxt; 11 | }; 12 | 13 | class Deque 14 | { 15 | public: 16 | node* head; 17 | node* tail; 18 | int sz; 19 | int rev; 20 | 21 | Deque() 22 | { 23 | head = NULL; 24 | tail = NULL; 25 | sz = 0; 26 | rev = 0; 27 | } 28 | 29 | node* CreateNewNode(int value) 30 | { 31 | node* newnode = new node; 32 | newnode->data = value; 33 | newnode->nxt = NULL; 34 | newnode->prv = NULL; 35 | return newnode; 36 | } 37 | 38 | void InsertAtTail(int value) 39 | { 40 | node* newnode = CreateNewNode(value); 41 | if(sz==0) 42 | { 43 | head = newnode; 44 | tail = newnode; 45 | sz++; 46 | return; 47 | } 48 | tail->nxt = newnode; 49 | newnode->prv = tail; 50 | tail = newnode; 51 | sz++; 52 | } 53 | 54 | void InsertAtHead(int value) 55 | { 56 | node* newnode = CreateNewNode(value); 57 | if(sz==0) 58 | { 59 | head = newnode; 60 | tail = newnode; 61 | sz++; 62 | return; 63 | } 64 | head->prv = newnode; 65 | newnode->nxt = head; 66 | head = newnode; 67 | sz++; 68 | } 69 | //pushback O(1) 70 | void push_back(int value) 71 | { 72 | if(rev == 0) 73 | { 74 | InsertAtTail(value); 75 | } 76 | else 77 | { 78 | InsertAtHead(value); 79 | } 80 | } 81 | 82 | //pushfront O(1) 83 | void push_front(int value) 84 | { 85 | if(rev == 0) 86 | { 87 | InsertAtHead(value); 88 | } 89 | else 90 | { 91 | InsertAtTail(value); 92 | } 93 | } 94 | 95 | void DeleteLast() 96 | { 97 | if(sz==0) 98 | { 99 | cout<<"Deque is empty\n"; 100 | return; 101 | } 102 | if(sz==1) 103 | { 104 | delete tail; 105 | head = NULL; 106 | tail = NULL; 107 | sz--; 108 | return; 109 | } 110 | node *a = tail; 111 | tail= tail->prv; 112 | delete a; 113 | tail->nxt = NULL; 114 | sz--; 115 | } 116 | 117 | void DeleteFirst() 118 | { 119 | if(sz==0) 120 | { 121 | cout<<"Deque is empty\n"; 122 | return; 123 | } 124 | if(sz==1) 125 | { 126 | delete tail; 127 | head = NULL; 128 | tail = NULL; 129 | sz--; 130 | return; 131 | } 132 | 133 | node *a = head; 134 | head = head->nxt; 135 | delete a; 136 | head->prv = NULL; 137 | sz--; 138 | } 139 | 140 | //popback O(1) 141 | void pop_back() 142 | { 143 | if(rev==0) 144 | { 145 | DeleteLast(); 146 | } 147 | else 148 | { 149 | DeleteFirst(); 150 | } 151 | } 152 | 153 | //popfront O(1) 154 | void pop_front() 155 | { 156 | if(rev==0) 157 | { 158 | DeleteFirst(); 159 | } 160 | else 161 | { 162 | DeleteLast(); 163 | } 164 | } 165 | 166 | //front O(1) 167 | int front() 168 | { 169 | if(sz==0) 170 | { 171 | cout<<"Deque is empty\n"; 172 | return -1; 173 | } 174 | if(rev==0) 175 | { 176 | return head->data; 177 | } 178 | else 179 | { 180 | return tail->data; 181 | } 182 | } 183 | 184 | //back O(1) 185 | int back() 186 | { 187 | if(sz==0) 188 | { 189 | cout<<"Deque is empty\n"; 190 | return -1; 191 | } 192 | if(rev==0) 193 | { 194 | return tail->data; 195 | } 196 | else 197 | { 198 | return head->data; 199 | } 200 | } 201 | 202 | //Reverse operation O(1) 203 | void reverse_deque() 204 | { 205 | if(rev == 0) 206 | { 207 | rev = 1; 208 | } 209 | else 210 | { 211 | rev = 0; 212 | } 213 | } 214 | }; 215 | 216 | 217 | int main() 218 | { 219 | Deque d; 220 | d.push_back(5); 221 | d.push_back(10); 222 | d.push_back(15); 223 | d.push_front(20); 224 | d.reverse_deque(); 225 | cout<<"Back : "< 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | dequedq; 8 | dq.push_back(5); 9 | dq.push_back(10); 10 | dq.push_front(20); 11 | dq.pop_back(); 12 | dq.pop_front(); 13 | 14 | cout< 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int id; 7 | node *Left; 8 | node *Right; 9 | node *parent; 10 | }; 11 | // Manually insert nodes in a binary tree and print information of the tree 12 | class BinaryTree{ 13 | public: 14 | node* root; 15 | node* allnode[6]; 16 | 17 | BinaryTree() 18 | { 19 | root = NULL; 20 | } 21 | 22 | node* CreateNewNode(int id) 23 | { 24 | node* newnode = new node; 25 | newnode->id = id; 26 | newnode->Left = NULL; 27 | newnode->Right = NULL; 28 | newnode->parent = NULL; 29 | return newnode; 30 | } 31 | 32 | void build_binary_tree() 33 | { 34 | 35 | for(int i=0;i<6;i++) 36 | allnode[i] = CreateNewNode(i); 37 | 38 | allnode[0]->Left = allnode[1]; 39 | allnode[0]->Right = allnode[2]; 40 | 41 | allnode[1]->Left = allnode[5]; 42 | allnode[1]->parent = allnode[0]; 43 | 44 | allnode[2]->Left = allnode[3]; 45 | allnode[2]->Right = allnode[4]; 46 | allnode[2]->parent = allnode[0]; 47 | 48 | allnode[5]->parent = allnode[1]; 49 | 50 | allnode[3]->parent = allnode[2]; 51 | allnode[4]->parent = allnode[2]; 52 | root = allnode[0]; 53 | } 54 | 55 | void print_tree_info() 56 | { 57 | 58 | for(int i=0;i<6;i++) 59 | { 60 | int p = -1; 61 | int l = -1; 62 | int r = -1; 63 | if(allnode[i]->parent!= NULL) 64 | p = allnode[i]->parent->id; 65 | if(allnode[i]->Left!= NULL) 66 | l = allnode[i]->Left->id; 67 | if(allnode[i]->Right!= NULL) 68 | r = allnode[i]->Right->id; 69 | cout<<"Node "< 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int id; 7 | int value; 8 | node *Left; 9 | node *Right; 10 | node *parent; 11 | }; 12 | 13 | 14 | class BinaryTree{ 15 | public: 16 | node* root; 17 | 18 | 19 | BinaryTree() 20 | { 21 | root = NULL; 22 | } 23 | 24 | node* CreateNewNode(int id, int value) 25 | { 26 | node* newnode = new node; 27 | newnode->id = id; 28 | newnode->value = value; 29 | newnode->Left = NULL; 30 | newnode->Right = NULL; 31 | newnode->parent = NULL; 32 | return newnode; 33 | } 34 | 35 | void Insertion(int id, int value) 36 | { 37 | node* newnode = CreateNewNode(id, value); 38 | if(root==NULL) 39 | { 40 | root = newnode; 41 | return; 42 | } 43 | queueq; 44 | q.push(root); 45 | 46 | while(!q.empty()) 47 | { 48 | node* a = q.front(); 49 | q.pop(); 50 | if(a->Left != NULL){ 51 | q.push(a->Left); 52 | } 53 | else{ 54 | //Insert in left child of node a 55 | a->Left = newnode; 56 | newnode->parent = a; 57 | return; 58 | } 59 | if(a->Right != NULL){ 60 | q.push(a->Right); 61 | } 62 | else{ 63 | //Insert in right child of node a 64 | a->Right = newnode; 65 | newnode->parent = a; 66 | return; 67 | } 68 | 69 | } 70 | } 71 | 72 | void BFS() 73 | { 74 | if(root == NULL) 75 | return; 76 | queueq; 77 | q.push(root); 78 | 79 | while(!q.empty()) 80 | { 81 | node* a = q.front(); 82 | q.pop(); 83 | int p = -1, l = -1 , r= - 1; 84 | if(a->Left != NULL){ 85 | l = a->Left->id; 86 | q.push(a->Left); 87 | } 88 | if(a->Right != NULL){ 89 | r = a->Right->id; 90 | q.push(a->Right); 91 | } 92 | if(a->parent != NULL) 93 | p = a->parent->id; 94 | cout<<"Node id = "<id<<" Left Child = "<id<<" "; 107 | DFS(a->Left); 108 | DFS(a->Right); 109 | } 110 | 111 | void Inorder(node *a) 112 | { 113 | if(a==NULL) 114 | { 115 | return; 116 | } 117 | Inorder(a->Left); 118 | cout<id<<" "; 119 | Inorder(a->Right); 120 | } 121 | 122 | void Preorder(node *a) 123 | { 124 | if(a==NULL) 125 | { 126 | return; 127 | } 128 | cout<id<<" "; 129 | Preorder(a->Left); 130 | Preorder(a->Right); 131 | } 132 | 133 | void Postorder(node *a) 134 | { 135 | if(a==NULL) 136 | { 137 | return; 138 | } 139 | 140 | Postorder(a->Left); 141 | Postorder(a->Right); 142 | cout<id<<" "; 143 | } 144 | 145 | void Search(node* a, int value) 146 | { 147 | if(a==NULL) 148 | return; 149 | if(a->value == value) 150 | { 151 | cout<id<<" "; 152 | } 153 | Search(a->Left , value); 154 | Search(a->Right , value); 155 | } 156 | }; 157 | 158 | 159 | int main() 160 | { 161 | BinaryTree bt; 162 | bt.Insertion(0,5); 163 | bt.Insertion(1,10); 164 | bt.Insertion(2,10); 165 | bt.Insertion(3,9); 166 | bt.Insertion(4,8); 167 | bt.Insertion(5,5); 168 | bt.Insertion(6,7); 169 | 170 | bt.BFS(); 171 | cout<<"\n"; 172 | bt.Search(bt.root , 5); 173 | cout<<"\n"; 174 | bt.Search(bt.root , 10); 175 | return 0; 176 | } 177 | 178 | -------------------------------------------------------------------------------- /Week 8/Module 30/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | class node{ 6 | public: 7 | int value; 8 | node* Left; 9 | node* Right; 10 | }; 11 | 12 | class BST{ 13 | public: 14 | node *root; 15 | 16 | BST() 17 | { 18 | root = NULL; 19 | } 20 | 21 | node* CreateNewNode(int value) 22 | { 23 | node *newnode = new node; 24 | newnode->value = value; 25 | newnode->Left = NULL; 26 | newnode->Right = NULL; 27 | return newnode; 28 | } 29 | 30 | 31 | void BFS() 32 | { 33 | if(root == NULL) 34 | return; 35 | queueq; 36 | q.push(root); 37 | 38 | while(!q.empty()) 39 | { 40 | node* a = q.front(); 41 | q.pop(); 42 | int p = -1, l = -1 , r= - 1; 43 | if(a->Left != NULL){ 44 | l = a->Left->value; 45 | q.push(a->Left); 46 | } 47 | if(a->Right != NULL){ 48 | r = a->Right->value; 49 | q.push(a->Right); 50 | } 51 | cout<<"Node value = "<value<<" Left Child = "<value > cur->value) 72 | { 73 | prv = cur; 74 | cur = cur->Right; 75 | } 76 | else{ 77 | prv = cur; 78 | cur = cur->Left; 79 | } 80 | } 81 | if(newnode->value > prv->value) 82 | { 83 | prv->Right = newnode; 84 | } 85 | else{ 86 | prv->Left = newnode; 87 | } 88 | 89 | } 90 | 91 | bool Search(int value) 92 | { 93 | node* cur = root; 94 | while(cur != NULL) 95 | { 96 | if(value > cur->value) 97 | { 98 | cur = cur->Right; 99 | } 100 | else if(value < cur->value) 101 | { 102 | cur = cur->Left; 103 | } 104 | else{ 105 | return true; 106 | } 107 | } 108 | return false; 109 | } 110 | 111 | void Delete(int value) 112 | { 113 | node* cur = root; 114 | node* prv = NULL; 115 | 116 | while(cur != NULL) 117 | { 118 | if(value > cur->value) 119 | { 120 | prv = cur; 121 | cur = cur->Right; 122 | } 123 | else if(value < cur->value) 124 | { 125 | prv = cur; 126 | cur = cur->Left; 127 | } 128 | else{ 129 | break; 130 | } 131 | } 132 | if(cur== NULL) 133 | { 134 | cout<<"Value is not present in BST\n"; 135 | return; 136 | } 137 | //Case 1: both child is NULL 138 | if(cur->Left == NULL &&cur->Right==NULL) 139 | { 140 | if(prv->Left!=NULL && prv->Left->value== cur->value) 141 | { 142 | prv->Left = NULL; 143 | } 144 | else{ 145 | prv->Right = NULL; 146 | } 147 | delete cur; 148 | return; 149 | } 150 | //Case 2: node has only one child 151 | if(cur->Left==NULL && cur->Right != NULL) 152 | { 153 | if(prv->Left!=NULL &&prv->Left->value== cur->value) 154 | { 155 | prv->Left = cur->Right; 156 | } 157 | else{ 158 | prv->Right = cur->Right;; 159 | } 160 | delete cur; 161 | return; 162 | } 163 | if(cur->Left!=NULL && cur->Right == NULL) 164 | { 165 | if(prv->Left!=NULL &&prv->Left->value== cur->value) 166 | { 167 | prv->Left = cur->Left; 168 | } 169 | else{ 170 | prv->Right = cur->Left; 171 | } 172 | delete cur; 173 | return; 174 | } 175 | //Case 3: node has two child 176 | node *tmp = cur->Right; 177 | while(tmp->Left!=NULL) 178 | { 179 | tmp = tmp->Left; 180 | } 181 | int saved = tmp->value; 182 | Delete(saved); 183 | cur->value = saved; 184 | 185 | } 186 | }; 187 | 188 | 189 | int main() 190 | { 191 | BST bst; 192 | bst.Insert(6); 193 | bst.Insert(4); 194 | bst.Insert(3); 195 | bst.Insert(5); 196 | bst.Insert(7); 197 | bst.Insert(8); 198 | 199 | //Case 1 200 | // bst.Delete(8); 201 | // bst.BFS(); 202 | 203 | //Case 2 204 | // bst.Delete(7); 205 | // bst.BFS(); 206 | 207 | //Case 3 208 | bst.Delete(6); 209 | bst.BFS(); 210 | 211 | return 0; 212 | } 213 | 214 | -------------------------------------------------------------------------------- /Week 9/Module 32/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class MaxHeap{ 5 | public: 6 | vectornodes; 7 | 8 | MaxHeap() 9 | { 10 | 11 | } 12 | 13 | //O(logn) 14 | void up_heapify(int idx) 15 | { 16 | while(idx > 0 && nodes[idx] > nodes[(idx-1)/2]) 17 | { 18 | swap(nodes[idx] , nodes[(idx-1)/2]); 19 | idx = (idx-1)/2; 20 | } 21 | } 22 | //O(logn) 23 | void insert(int x) 24 | { 25 | nodes.push_back(x); 26 | up_heapify(nodes.size()-1); 27 | } 28 | 29 | //O(n) 30 | void PrintHeap() 31 | { 32 | for(int i=0;i= nodes.size()) 67 | return; 68 | swap(nodes[idx] , nodes[nodes.size()-1]); 69 | nodes.pop_back(); 70 | down_heapify(idx); 71 | } 72 | 73 | //O(1) 74 | int getMax() 75 | { 76 | if(nodes.empty()) 77 | { 78 | cout<<"Heap is empty!\n"; 79 | return -1; 80 | } 81 | return nodes[0]; 82 | } 83 | 84 | //O(logn) 85 | int ExtractMax() 86 | { 87 | if(nodes.empty()) 88 | { 89 | cout<<"Heap is empty!\n"; 90 | return -1; 91 | } 92 | int ret = nodes[0]; 93 | Delete(0); 94 | return ret; 95 | } 96 | }; 97 | 98 | int main() 99 | { 100 | MaxHeap heap; 101 | heap.insert(4); 102 | heap.insert(7); 103 | heap.insert(9); 104 | heap.insert(1); 105 | heap.insert(10); 106 | heap.insert(20); 107 | heap.insert(30); 108 | cout<<"Max element = "< 2 | using namespace std; 3 | 4 | class MaxHeap{ 5 | public: 6 | vectornodes; 7 | 8 | MaxHeap() 9 | { 10 | 11 | } 12 | 13 | //O(logn) 14 | void up_heapify(int idx) 15 | { 16 | while(idx > 0 && nodes[idx] > nodes[(idx-1)/2]) 17 | { 18 | swap(nodes[idx] , nodes[(idx-1)/2]); 19 | idx = (idx-1)/2; 20 | } 21 | } 22 | //O(logn) 23 | void insert(int x) 24 | { 25 | nodes.push_back(x); 26 | up_heapify(nodes.size()-1); 27 | } 28 | 29 | //O(n) 30 | void PrintHeap() 31 | { 32 | for(int i=0;i= nodes.size()) 67 | return; 68 | swap(nodes[idx] , nodes[nodes.size()-1]); 69 | nodes.pop_back(); 70 | down_heapify(idx); 71 | } 72 | 73 | //O(1) 74 | int getMax() 75 | { 76 | if(nodes.empty()) 77 | { 78 | cout<<"Heap is empty!\n"; 79 | return -1; 80 | } 81 | return nodes[0]; 82 | } 83 | 84 | //O(logn) 85 | int ExtractMax() 86 | { 87 | if(nodes.empty()) 88 | { 89 | cout<<"Heap is empty!\n"; 90 | return -1; 91 | } 92 | int ret = nodes[0]; 93 | Delete(0); 94 | return ret; 95 | } 96 | 97 | //O(n) 98 | void build_from_array(vector&a) 99 | { 100 | nodes = a; 101 | int n = nodes.size(); 102 | int last_non_leaf = n/2 - 1; 103 | for(int i=last_non_leaf; i>=0; i--) 104 | { 105 | down_heapify(i); 106 | } 107 | } 108 | }; 109 | 110 | //O(nlogn) 111 | vector heap_sort(vectora) 112 | { 113 | MaxHeap h; 114 | h.build_from_array(a); 115 | vectorans; 116 | for(int i=0;ia = {1,2,3,4,10,9,8,7}; 127 | // h.build_from_array(a); 128 | // h.PrintHeap(); 129 | 130 | vectora = {1,2,3,4,10,9,5,7}; 131 | vectorsorted_a = heap_sort(a); 132 | for(int i=0;i 2 | using namespace std; 3 | 4 | class MaxHeap{ 5 | public: 6 | vectornodes; 7 | 8 | MaxHeap() 9 | { 10 | 11 | } 12 | 13 | //O(logn) 14 | void up_heapify(int idx) 15 | { 16 | while(idx > 0 && nodes[idx] > nodes[(idx-1)/2]) 17 | { 18 | swap(nodes[idx] , nodes[(idx-1)/2]); 19 | idx = (idx-1)/2; 20 | } 21 | } 22 | //O(logn) 23 | void insert(int x) 24 | { 25 | nodes.push_back(x); 26 | up_heapify(nodes.size()-1); 27 | } 28 | 29 | //O(n) 30 | void PrintHeap() 31 | { 32 | for(int i=0;i= nodes.size()) 67 | return; 68 | swap(nodes[idx] , nodes[nodes.size()-1]); 69 | nodes.pop_back(); 70 | down_heapify(idx); 71 | } 72 | 73 | //O(1) 74 | int getMax() 75 | { 76 | if(nodes.empty()) 77 | { 78 | cout<<"Heap is empty!\n"; 79 | return -1; 80 | } 81 | return nodes[0]; 82 | } 83 | 84 | //O(logn) 85 | int ExtractMax() 86 | { 87 | if(nodes.empty()) 88 | { 89 | cout<<"Heap is empty!\n"; 90 | return -1; 91 | } 92 | int ret = nodes[0]; 93 | Delete(0); 94 | return ret; 95 | } 96 | 97 | //O(n) 98 | void build_from_array(vector&a) 99 | { 100 | nodes = a; 101 | int n = nodes.size(); 102 | int last_non_leaf = n/2 - 1; 103 | for(int i=last_non_leaf; i>=0; i--) 104 | { 105 | down_heapify(i); 106 | } 107 | } 108 | 109 | int size() 110 | { 111 | return nodes.size(); 112 | } 113 | }; 114 | 115 | class PriorityQueue{ 116 | public: 117 | MaxHeap h; 118 | PriorityQueue() 119 | { 120 | 121 | } 122 | 123 | void push(int x) 124 | { 125 | h.insert(x); 126 | } 127 | 128 | void pop() 129 | { 130 | h.Delete(0); 131 | } 132 | 133 | int top() 134 | { 135 | return h.getMax(); 136 | } 137 | 138 | int size() 139 | { 140 | return h.size(); 141 | } 142 | }; 143 | int main() 144 | { 145 | PriorityQueue pq; 146 | pq.push(5); 147 | pq.push(7); 148 | pq.push(10); 149 | pq.push(1); 150 | pq.push(2); 151 | 152 | while(pq.size() != 0) 153 | { 154 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | priority_queue pq; 7 | //push O(logn) 8 | pq.push(10); 9 | pq.push(5); 10 | pq.push(3); 11 | pq.push(8); 12 | pq.push(9); 13 | 14 | while(pq.size() != 0) 15 | { 16 | //top O(1) 17 | cout< 2 | using namespace std; 3 | 4 | 5 | int digit(int x) 6 | { 7 | int cnt = 0; 8 | while(x != 0) 9 | { 10 | x = x/10; 11 | cnt++; 12 | } 13 | return cnt; 14 | } 15 | 16 | int main() 17 | { 18 | int n; 19 | cin>>n; 20 | vectora(n); 21 | vectorb(n); 22 | for(int i=0;i>a[i]; 25 | } 26 | for(int i=0;i>b[i]; 29 | } 30 | priority_queue p1; 31 | priority_queuep2; 32 | for(int i=0;i y) 50 | { 51 | p1.pop(); 52 | p1.push(digit(x)); 53 | } 54 | else{ 55 | p2.pop(); 56 | p2.push(digit(y)); 57 | } 58 | } 59 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | set st; 7 | 8 | // n = number of elements currently in set 9 | //Insert O(log(n)) 10 | st.insert(5); 11 | st.insert(3); 12 | st.insert(4); 13 | st.insert(7); 14 | st.insert(3); 15 | st.insert(4); 16 | 17 | //Print O(n) 18 | cout<<"Printing set\n"; 19 | for(auto i: st) 20 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | multiset a; 7 | 8 | //Insert O(logn) 9 | a.insert(5); 10 | a.insert(7); 11 | a.insert(4); 12 | a.insert(3); 13 | a.insert(4); 14 | a.insert(7); 15 | 16 | //Print O(n) 17 | cout<<"Printing the multiset\n"; 18 | for(auto it:a) 19 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | pair a; 7 | a.first = "abcd"; 8 | a.second = 6; 9 | 10 | cout<<"Pair a = "< b; 13 | b.first = 100; 14 | b.second = 200; 15 | 16 | cout<<"Pair b = "< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | map mp; 7 | //insert O(logn) 8 | mp["goru"] = 1; 9 | mp["chagol"] = 2; 10 | mp["vera"] = 3; 11 | 12 | //value of a key O(logn) 13 | cout<<"Value of a key\n"; 14 | cout<mp2; 33 | 34 | mp2[69] = 1; 35 | mp2[57] = 4; 36 | mp2[89] = 4; 37 | 38 | mp2[1000000] = 10; 39 | cout<<"Printing map 2\n"; 40 | for(auto it:mp2) 41 | { 42 | cout<<"Key : "<