├── .gitignore ├── binaryfile.dat ├── bst.cpp ├── templates.cpp ├── exceptions.cpp ├── pointertomember.cpp ├── program.cpp ├── insertionsort.cpp ├── 30082024.cpp ├── 210824.cpp ├── bubblesort.cpp ├── gfg ├── findDuplicates.cpp ├── minimizeHeights.cpp ├── segregateElements.cpp ├── floydwarshall.cpp └── isHeap.cpp ├── towerofhanoi.cpp ├── narutopattern.cpp ├── opoverloading.cpp ├── topsort.cpp ├── selectionsort.cpp ├── quicksort.cpp ├── graphs.cpp ├── fileops.cpp ├── inheritance.cpp ├── basicfileops.cpp ├── validateTriangle.c ├── shortestpathbfs.cpp ├── dll.cpp ├── djisktraset.cpp ├── floydalgo.cpp ├── OOPS.cpp ├── sumofk-level.cpp ├── mergesort.cpp ├── shortestpath.cpp ├── test.cpp ├── floydwarshallpath.cpp ├── linkedlist.cpp ├── singlycircular.cpp ├── playground.cpp ├── main.cpp ├── graphBFS.cpp ├── graphDFS.cpp ├── .vscode └── settings.json ├── rpg.cpp ├── hw.cpp ├── mythicalkingdom.cpp └── heaps.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe -------------------------------------------------------------------------------- /binaryfile.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cmd-anurag/cse202andcse205/main/binaryfile.dat -------------------------------------------------------------------------------- /bst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node* left; 8 | Node* right; 9 | }; 10 | 11 | 12 | 13 | Node* findMax(Node* root) { 14 | if(!root) return nullptr; 15 | Node* temp = root; 16 | while(temp->right != nullptr) { 17 | temp = temp->right; 18 | } 19 | return temp; 20 | } 21 | 22 | int main() 23 | { 24 | 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /templates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template 5 | class Base { 6 | public: 7 | T value; 8 | 9 | }; 10 | 11 | template 12 | class Derived : public Base { 13 | public: 14 | T derivedValue; 15 | }; 16 | 17 | 18 | int main() 19 | { 20 | Derived obj; 21 | obj.value = 12; 22 | obj.derivedValue = 13.45; 23 | cout << obj.value; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /exceptions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // EXCEPTION HANDLING 5 | 6 | int main() 7 | { 8 | int kills = 4; 9 | int deaths = 0; 10 | 11 | try { 12 | if(deaths == 0) { 13 | throw out_of_range(""); 14 | } 15 | float kdRatio = kills/deaths; 16 | cout << "KD Ratio is : " << kdRatio << '\n'; 17 | } 18 | catch(out_of_range e) { 19 | cout << e.what() << " "; 20 | } 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /pointertomember.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Test { 5 | public: 6 | int x; 7 | 8 | }; 9 | 10 | int main() 11 | { 12 | Test t; 13 | t.x = 4; 14 | 15 | // creating a pointer to a member of a class 16 | int Test::*ptr = &Test::x; 17 | cout << t.*ptr; 18 | 19 | // creating a pointer to object 20 | Test* ptrobj = &t; 21 | 22 | // accessing the member using pointer to object 23 | 24 | cout << ptrobj->*ptr; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /program.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Bulb { 8 | bool turnedON; 9 | friend class Phone; 10 | public: 11 | Bulb() : turnedON(false) {}; 12 | }; 13 | 14 | class Phone { 15 | string name; 16 | 17 | public: 18 | void turnOnBulb(Bulb& bulb) { 19 | bulb.turnedON = true; 20 | cout << "Bulb turned on successfully\n"; 21 | } 22 | }; 23 | 24 | 25 | 26 | int main() 27 | { 28 | Bulb b1; 29 | Phone p1; 30 | 31 | p1.turnOnBulb(b1); 32 | 33 | } -------------------------------------------------------------------------------- /insertionsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void insertionSort(int array[], int n) { 5 | for(int i = 1; i < n; ++i) { 6 | int element = array[i]; 7 | int j = i; 8 | while(j > 0 && element < array[j-1]) { 9 | array[j] = array[j-1]; 10 | j--; 11 | } 12 | array[j] = element; 13 | } 14 | } 15 | 16 | int main() 17 | { 18 | int array[7] = {7, 6, 1, 9, 2, 4, 3}; 19 | insertionSort(array, 7); 20 | for(int i : array) { 21 | cout << i <<" "; 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /30082024.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | enum PIECE { 5 | PAWN = 1, 6 | BISHOP = 3, 7 | KNIGHT = 3, 8 | ROOK = 5, 9 | QUEEN = 9 10 | }; 11 | 12 | enum class PieceValue { 13 | PAWN = 1, 14 | BISHOP = 3, 15 | KNIGHT = 3, 16 | ROOK = 5, 17 | QUEEN = 9 18 | }; 19 | 20 | 21 | int main() 22 | { 23 | // PIECE currentPiece = PAWN; 24 | // cout << "The current piece's value is " << currentPiece; 25 | 26 | PieceValue value = PieceValue::QUEEN; 27 | cout << "The value of queen is " << static_cast(value); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /210824.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Test { 5 | 6 | public: 7 | int x; 8 | static int staticvar; 9 | 10 | static int accessStatic(); 11 | int incrementStatic() { 12 | ++staticvar; 13 | } 14 | 15 | }; 16 | int Test::staticvar = 0; 17 | 18 | int Test::accessStatic() { 19 | cout << staticvar << " "; 20 | } 21 | 22 | 23 | 24 | 25 | int main() 26 | { 27 | Test o1; 28 | Test o2; 29 | 30 | o1.incrementStatic(); 31 | o2.incrementStatic(); 32 | 33 | o1.accessStatic(); 34 | 35 | Test::accessStatic(); 36 | cout << Test::staticvar; 37 | cout << o1.staticvar; 38 | 39 | } -------------------------------------------------------------------------------- /bubblesort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void bubbleSort(int array[], int n) { 5 | bool swapped = false; 6 | for(int i = 0; i < n; ++i) { 7 | swapped = false; 8 | for(int j = 0; j < n - 1; ++j) { 9 | if(array[j] > array[j+1]) { 10 | int temp = array[j]; 11 | array[j] = array[j+1]; 12 | array[j+1] = temp; 13 | swapped = true; 14 | } 15 | } 16 | if(!swapped) break; 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | int arr[7] = {3, 6, 1, 4, 7, 12, 2}; 23 | bubbleSort(arr, 7); 24 | for(int i : arr) { 25 | cout << i << " "; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /gfg/findDuplicates.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | 6 | class Solution { 7 | public: 8 | vector findDuplicates(vector& arr) { 9 | // code here 10 | unordered_map freq; 11 | for(int i : arr) { 12 | freq[i]++; 13 | } 14 | unordered_map::iterator it = freq.begin(); 15 | 16 | vector result; 17 | 18 | 19 | while(it != freq.end()) { 20 | if(it->second > 1) { 21 | result.push_back(it->first); 22 | } 23 | ++it; 24 | } 25 | sort(result.begin(), result.end()); 26 | return result; 27 | } 28 | }; -------------------------------------------------------------------------------- /gfg/minimizeHeights.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int getMinDiff(vector &arr, int k) { 7 | // code here 8 | int n = arr.size(); 9 | sort(arr.begin(), arr.end()); 10 | 11 | int output = arr[n-1] - arr[0]; 12 | 13 | int maxHeight; 14 | int minHeight; 15 | 16 | for(int i = 1; i < n; ++i) { 17 | if(arr[i] - k < 0) { 18 | continue; 19 | } 20 | 21 | maxHeight = max(arr[n-1]-k, arr[i-1]+k); 22 | minHeight = min(arr[0]+k, arr[i]-k); 23 | 24 | output = min(output, maxHeight - minHeight); 25 | } 26 | return output; 27 | } 28 | }; -------------------------------------------------------------------------------- /towerofhanoi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void moveDisk(int n, char source, char auxillary, char destination) { 5 | if(n == 1) { 6 | cout << "Moving disk " << n << " from " << source << " to " << destination << "\n"; 7 | return; 8 | } 9 | 10 | // move n-1 disks from source to auxillary rod 11 | moveDisk(n-1, source, destination, auxillary); 12 | 13 | // moving the nth disc directly from source to destination 14 | cout << "Moving disk " << n << " from " << source << " to " << destination << "\n"; 15 | 16 | // move n-1 disks kept at auxillary to dsetination 17 | moveDisk(n-1, auxillary, source, destination); 18 | 19 | } 20 | 21 | 22 | int main() 23 | { 24 | moveDisk(4, 'A', 'B', 'C'); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /narutopattern.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int array[4][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; 8 | 9 | int TR = 0; 10 | int BR = 3; 11 | int LC = 0; 12 | int RC = 3; 13 | 14 | while (TR < BR && LC=LC; i--) { 25 | cout << array[BR][i] << " "; 26 | } 27 | BR--; 28 | for(int i = BR; i>=TR; --i) { 29 | cout << array[i][LC] << " "; 30 | } 31 | LC++; 32 | } 33 | 34 | 35 | } -------------------------------------------------------------------------------- /gfg/segregateElements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | void segregateElements(vector& arr) { 9 | // Your code goes here 10 | vector positives; 11 | vector negatives; 12 | 13 | for(int i : arr) 14 | { 15 | if(i < 0) { 16 | negatives.push_back(i); 17 | } 18 | else { 19 | positives.push_back(i); 20 | } 21 | } 22 | 23 | int index = 0; 24 | for(; index < positives.size(); ++index) { 25 | arr[index] = positives[index]; 26 | } 27 | 28 | for(int i = 0; i < negatives.size(); ++i) { 29 | arr[index] = negatives[i]; 30 | ++index; 31 | } 32 | 33 | } 34 | }; -------------------------------------------------------------------------------- /opoverloading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | class Test { 6 | public: 7 | int value = 10; 8 | 9 | // basic to class 10 | Test() {}; 11 | Test(int v) { 12 | value = v; 13 | } 14 | 15 | void operator=(int x) { 16 | value = x; 17 | } 18 | 19 | // class to basic 20 | operator int() { 21 | return value; 22 | } 23 | 24 | operator string() { 25 | return "bruh"; 26 | } 27 | 28 | void operator+(int a) { 29 | value += a; 30 | } 31 | 32 | Test operator++(int) { 33 | Test temp = *this; 34 | ++value; 35 | return temp; 36 | } 37 | }; 38 | 39 | 40 | int main() 41 | { 42 | Test t; 43 | t++; 44 | 45 | cout << t.value; 46 | 47 | // basic to class type 48 | t = 13; 49 | 50 | cout << '\n' << t.value << '\n'; 51 | 52 | string s = t; 53 | int x = t; 54 | 55 | cout << s << " " << t; 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /topsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector topologicalSort(vector> &edges, int v, int e) { 5 | // Write your code here 6 | unordered_map> adj; 7 | 8 | for(int i = 0; i < e; ++i) { 9 | int u = edges[i][0]; 10 | int v = edges[i][1]; 11 | adj[u].push_back(v); 12 | } 13 | 14 | vector indegree(v, 0); 15 | for(auto i : adj) { 16 | for(auto j : i.second) { 17 | indegree[j]++; 18 | } 19 | } 20 | 21 | queue q; 22 | for(int i = 0; i < v; ++i) { 23 | if(indegree[i] == 0) q.push(i); 24 | } 25 | 26 | vector result; 27 | 28 | while(!q.empty()) { 29 | int front = q.front(); 30 | q.pop(); 31 | result.push_back(front); 32 | for(auto n : adj[front]) { 33 | indegree[n]--; 34 | if(indegree[n] == 0) { 35 | q.push(n); 36 | } 37 | } 38 | } 39 | 40 | return result; 41 | } 42 | 43 | int main() { 44 | 45 | } -------------------------------------------------------------------------------- /selectionsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int findMinIndex(vector array, int start) { 7 | int minIndex = start; 8 | for(int i = start; i < array.size(); ++i) { 9 | if(array.at(i) < array.at(minIndex)) { 10 | minIndex = i; 11 | } 12 | } 13 | return minIndex; 14 | } 15 | 16 | void swap(int& a, int& b) { 17 | int temp = a; 18 | a = b; 19 | b = temp; 20 | } 21 | 22 | void selectionSort(vector& array) { 23 | for(int i = 0; i < array.size() - 1; ++i) { 24 | int minIndex = findMinIndex(array, i+1); 25 | if(array.at(minIndex) < array.at(i)) { 26 | swap(array.at(minIndex), array.at(i)); 27 | } 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | vector array = {95, 34, 76, 23, 88, 12, 57, 83, 44, 61, 29, 100, 5, 17, 63, 91, 38, 74, 19, 86, 54, 3, 72, 45, 89, 22, 67, 30, 8, 81, 13, 47, 92, 40, 27, 59, 14, 53, 99, 7}; 34 | selectionSort(array); 35 | for(int i : array) { 36 | cout << i <<" "; 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /gfg/floydwarshall.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | void shortestDistance(vector>& mat) { 8 | // Code here 9 | int n = mat.size(); 10 | 11 | for(int i = 0; i < n; ++i) { 12 | for(int j = 0; j < n; ++j) { 13 | if(mat[i][j] == -1) { 14 | mat[i][j] = INT_MAX; 15 | } 16 | } 17 | } 18 | 19 | for(int k = 0; k < n; ++k) { 20 | for(int i = 0; i < n; ++i) { 21 | for(int j = 0; j < n; ++j) { 22 | if(mat[i][k] == INT_MAX || mat[k][j] == INT_MAX) { 23 | continue; 24 | } 25 | mat[i][j] = min(mat[i][j], mat[i][k]+mat[k][j]); 26 | } 27 | } 28 | } 29 | 30 | for(int i = 0; i < n; ++i) { 31 | for(int j = 0; j < n; ++j) { 32 | if(mat[i][j] == INT_MAX) { 33 | mat[i][j] = -1; 34 | } 35 | } 36 | } 37 | 38 | } 39 | }; -------------------------------------------------------------------------------- /quicksort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void swap(int& a, int& b) { 7 | int temp = a; 8 | a = b; 9 | b = temp; 10 | } 11 | 12 | int findPivotIndex(vector &array, int low, int high) { 13 | int start = low; 14 | int end = high; 15 | int pivotElement = array[low]; 16 | while(start <= end) { 17 | while(start <= end && array[start] <= pivotElement) ++start; 18 | while(start <= end && array[end] > pivotElement) --end; 19 | if(start < end) { 20 | swap(array[start], array[end]); 21 | } 22 | } 23 | swap(array[end], array[low]); 24 | return end; 25 | } 26 | 27 | 28 | void QuickSort(vector &array, int low, int high) { 29 | if(high > low) { 30 | int pivotIndex = findPivotIndex(array, low, high); 31 | QuickSort(array, low, pivotIndex-1); 32 | QuickSort(array, pivotIndex+1, high); 33 | } 34 | } 35 | 36 | 37 | 38 | int main() 39 | { 40 | vector array = {7,2,24,6,2,122,46,3}; 41 | QuickSort(array, 0, array.size()-1); 42 | for(int i : array) { 43 | cout << i << " "; 44 | } 45 | return 0; 46 | } -------------------------------------------------------------------------------- /graphs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | 8 | class Graph { 9 | unordered_map> adj; 10 | 11 | // direction == 0 --> undirected 12 | // direction == 1 --> directed fron node 1 -> node 2 13 | public: 14 | 15 | void addNode(int node) { 16 | adj[node] = {}; 17 | } 18 | 19 | void addEdge(int node1, int node2, bool direction) { 20 | adj[node1].push_back(node2); 21 | if(!direction) { 22 | adj[node2].push_back(node1); 23 | } 24 | } 25 | 26 | void printGraph() { 27 | for(auto i : adj) { 28 | cout << i.first << ": "; 29 | for(auto j : i.second) { 30 | cout << j << " "; 31 | } 32 | cout << '\n'; 33 | } 34 | } 35 | 36 | }; 37 | 38 | int main() 39 | { 40 | Graph g; 41 | int edges; 42 | 43 | cout << "Enter the number of edges: "; 44 | cin >> edges; 45 | 46 | int n1, n2; 47 | for(int i = 0; i < edges; ++i) { 48 | cin >> n1 >> n2; 49 | g.addEdge(n1, n2, false); 50 | } 51 | g.printGraph(); 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /fileops.cpp: -------------------------------------------------------------------------------- 1 | // FILE OPERATIONS 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | using namespace std; 9 | 10 | class Person { 11 | public: 12 | 13 | char name[50]; 14 | int age; 15 | float weight; 16 | }; 17 | 18 | void WriteToBinaryFile(Person &p) { 19 | fstream myFile("binaryfile.dat", ios::app | ios::binary); 20 | if(!myFile) {cout << "Error writing to file"; return;} 21 | 22 | myFile.write(reinterpret_cast(&p), sizeof(p)); 23 | cout << "Person details written to file.\n"; 24 | myFile.close(); 25 | } 26 | 27 | void ReadFromBinaryFile() { 28 | Person person; 29 | fstream myfile("binaryfile.dat", ios::in | ios::binary); 30 | 31 | if(!myfile){ 32 | cout << "error"; 33 | return; 34 | } 35 | 36 | while(myfile.read(reinterpret_cast(&person), sizeof(person))) { 37 | cout << person.name << " " << person.age << " " << person.weight << endl; 38 | } 39 | 40 | myfile.close(); 41 | } 42 | 43 | int main() { 44 | Person p1; 45 | strcpy(p1.name, "test"); 46 | p1.age = 23; 47 | p1.weight = 34.45; 48 | WriteToBinaryFile(p1); 49 | ReadFromBinaryFile(); 50 | } -------------------------------------------------------------------------------- /inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Base { 5 | private: 6 | int privateVar; 7 | protected: 8 | int protectedVar; 9 | public: 10 | int publicVar; 11 | }; 12 | 13 | class PublicDerived : public Base { 14 | // private remains private 15 | // protected remains protected 16 | // public remains public 17 | }; 18 | 19 | class ProtectedDerived : protected Base { 20 | // private remains private 21 | // protected remains protected 22 | // public becomes protected 23 | }; 24 | 25 | class PrivateDerived : private Base { 26 | // private remains private 27 | // protected becomes private 28 | // public becomes private 29 | void print() { 30 | 31 | cout << protectedVar; 32 | cout << publicVar; 33 | } 34 | }; 35 | 36 | 37 | class A { 38 | public: 39 | int numA=10; 40 | void print() { 41 | cout << numA; 42 | } 43 | }; 44 | class B { 45 | public: 46 | int numB = 11; 47 | void print(int x) { 48 | cout << numB; 49 | } 50 | }; 51 | 52 | class AB : public A, public B { 53 | 54 | } 55 | 56 | int main() 57 | { 58 | 59 | AB o; 60 | o.print(5); 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /basicfileops.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | // 1. Writing to a file 7 | std::ofstream outfile("example.txt"); // Open file for writing 8 | if (outfile.is_open()) { 9 | outfile << "Hello, World!" << std::endl; 10 | outfile.close(); 11 | } else { 12 | std::cerr << "Failed to open the file for writing!" << std::endl; 13 | return 1; 14 | } 15 | 16 | // 2. Appending to the same file 17 | std::ofstream appendfile("example.txt", std::ios::app); // Open file in append mode 18 | if (appendfile.is_open()) { 19 | appendfile << "Appending some text." << std::endl; 20 | appendfile.close(); 21 | } else { 22 | std::cerr << "Failed to open the file for appending!" << std::endl; 23 | return 1; 24 | } 25 | 26 | // 3. Reading from the file 27 | std::ifstream infile("example.txt"); // Open file for reading 28 | if (infile.is_open()) { 29 | std::string line; 30 | while (std::getline(infile, line)) { // Read line by line 31 | std::cout << line << std::endl; // Print each line 32 | } 33 | infile.close(); 34 | } else { 35 | std::cerr << "Failed to open the file for reading!" << std::endl; 36 | return 1; 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /validateTriangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const char* isValidTriangle(long long int a, long long int b, long long int c) { 5 | 6 | if((a < b+c) && (b < a+c) && (c < a+b)) { 7 | return "Valid"; 8 | } 9 | else { 10 | return "Not Valid"; 11 | } 12 | } 13 | 14 | int main() { 15 | 16 | struct TestCase { 17 | long long int a, b, c; 18 | const char* expected; 19 | } testCases[] = { 20 | {0, 0, 0, "Not Valid"}, 21 | {1, 2, 3, "Not Valid"}, 22 | {3, 4, 5, "Valid"}, 23 | {10, 1, 1, "Not Valid"}, 24 | {1, 1, 1, "Valid"}, 25 | {5, 5, 9, "Valid"}, 26 | {7, 10, 5, "Valid"}, 27 | {1000000000, 1000000000, 1000000000, "Valid"}, 28 | {1000000000, 1, 1, "Not Valid"} 29 | }; 30 | 31 | 32 | int totalTests = sizeof(testCases) / sizeof(testCases[0]); 33 | 34 | 35 | for (int i = 0; i < totalTests; i++) { 36 | const char* result = isValidTriangle(testCases[i].a, testCases[i].b, testCases[i].c); 37 | printf("Test Case %d\n", i+1); 38 | printf("Expected Output: %s\nActual Output: %s\n", testCases[i].expected, result); 39 | assert(result == testCases[i].expected); 40 | printf("Passed\n\n", i + 1); 41 | } 42 | 43 | printf("All test cases passed successfully.\n"); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /shortestpathbfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void add_edge(int adj[][100], int src, int dest) 5 | { 6 | //Type your code 7 | adj[src][dest] = 1; 8 | adj[dest][src] = 1; 9 | } 10 | 11 | bool BFS(int adj[][100], int src, int dest, int v, int pred[], int dist[]) 12 | { 13 | //Type your code 14 | unordered_map visited; 15 | queue q; 16 | 17 | q.push(src); 18 | dist[src] = 0; 19 | pred[src] = -1; 20 | 21 | while(!q.empty()) { 22 | int front = q.front(); 23 | q.pop(); 24 | 25 | for(int i = 0; i < v; ++i) { 26 | if(adj[front][i] && visited.find(i) == visited.end()) { 27 | dist[i] = dist[front] + 1; 28 | pred[i] = front; 29 | visited[i] = true; 30 | q.push(i); 31 | 32 | if(i == dest) return true; 33 | } 34 | } 35 | } 36 | } 37 | 38 | void printShortestDistance(int adj[][100], int s, int dest, int v, int pred[], int dist[]) 39 | { 40 | //Type your code 41 | vector path; 42 | path.push_back(dest); 43 | int crawl = dest; 44 | 45 | while(pred[crawl] != -1) { 46 | path.push_back(pred[crawl]); 47 | crawl = pred[crawl]; 48 | } 49 | 50 | for(vector::iterator it = path.end(); it != path.begin(); --it) { 51 | cout << *it << " "; 52 | } 53 | } -------------------------------------------------------------------------------- /dll.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Node { 6 | public: 7 | int data; 8 | Node *prev; 9 | Node *next; 10 | Node(int val) : data(val), prev(NULL), next(NULL) {}; 11 | }; 12 | 13 | void insertAtTail(Node *&head, int value) { 14 | Node *createdNode = new Node(value); 15 | if(head == NULL) { 16 | head = createdNode; 17 | } 18 | else { 19 | Node *temp = head; 20 | while(temp->next!=NULL) { 21 | temp = temp->next; 22 | } 23 | createdNode->prev = temp; 24 | temp->next = createdNode; 25 | } 26 | } 27 | 28 | void insertAtHead(Node *&head, int value) { 29 | Node *createdNode = new Node(value); 30 | if(head == NULL) { 31 | head = createdNode; 32 | } 33 | else { 34 | createdNode->next = head; 35 | head->prev = createdNode; 36 | head = createdNode; 37 | } 38 | } 39 | 40 | void displayLL(Node *head) { 41 | Node *temp = head; 42 | while (temp->next != NULL) 43 | { 44 | cout << temp->data << "->"; 45 | temp = temp->next; 46 | } 47 | cout << temp->data << endl; 48 | } 49 | 50 | int main() { 51 | Node *head = NULL; 52 | insertAtHead(head, 5); 53 | insertAtHead(head, 9); 54 | // insertAtHead(head, 10); 55 | insertAtTail(head, 4); 56 | insertAtTail(head, 7); 57 | displayLL(head); 58 | } -------------------------------------------------------------------------------- /djisktraset.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | vector dijkstra(vector> &vec, int vertices, int edges, int source) { 6 | // Write your code here. 7 | unordered_map>> adj; 8 | 9 | for(int i = 0; i < edges; ++i) { 10 | int u = vec[i][0]; 11 | int v = vec[i][1]; 12 | 13 | int w = vec[i][2]; 14 | 15 | adj[u].push_back(make_pair(v, w)); 16 | adj[v].push_back(make_pair(u, w)); 17 | } 18 | 19 | vector distance(vertices, INT_MAX); 20 | 21 | set> st; 22 | 23 | distance[source] = 0; 24 | 25 | st.insert(make_pair(0, source)); 26 | 27 | while(!st.empty()) { 28 | auto top = *st.begin(); 29 | st.erase(st.begin()); 30 | 31 | int nodeDistance = top.first; 32 | int topNode = top.second; 33 | 34 | for(auto neighbor : adj[topNode]) { 35 | if(nodeDistance + neighbor.second < distance[neighbor.first]) { 36 | auto record = st.find(make_pair(distance[neighbor.first], neighbor.first)); 37 | if(record != st.end()) { 38 | st.erase(record); 39 | } 40 | distance[neighbor.first] = nodeDistance+neighbor.second; 41 | st.insert(make_pair(distance[neighbor.first], neighbor.first)); 42 | } 43 | } 44 | } 45 | 46 | return distance; 47 | 48 | 49 | } 50 | -------------------------------------------------------------------------------- /floydalgo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class ListNode { 6 | public: 7 | int data; 8 | ListNode *next; 9 | ListNode(int value) : data(value), next(NULL) {}; 10 | }; 11 | 12 | ListNode* createLinkedList() { 13 | int n; 14 | cout << "Enter the length of linked list: "; 15 | cin >> n; 16 | ListNode *head = NULL; 17 | ListNode *temp = NULL; 18 | int data; 19 | for(int i = 0; i < n; ++i) { 20 | cout << "Enter the data for node " << i+1 << endl; 21 | cin >> data; 22 | 23 | ListNode *newNode = new ListNode(data); 24 | 25 | if(head == NULL) { 26 | head = newNode; 27 | } 28 | else { 29 | temp->next = newNode; 30 | } 31 | temp = newNode; 32 | } 33 | return head; 34 | } 35 | 36 | void displayLL(ListNode *head) { 37 | ListNode *temp = head; 38 | while (temp->next != NULL) 39 | { 40 | cout << temp->data << "->"; 41 | temp = temp->next; 42 | } 43 | cout << temp->data << endl; 44 | } 45 | 46 | ListNode* detectLoop(ListNode* head) { 47 | if(head == nullptr) { 48 | return nullptr; 49 | } 50 | ListNode* fast = head; 51 | ListNode* slow = head; 52 | 53 | while (fast == nullptr || fast->next == nullptr) 54 | { 55 | slow = slow->next; 56 | fast = fast->next->next; 57 | if(slow == fast) { 58 | return fast; 59 | } 60 | 61 | } 62 | 63 | return nullptr; 64 | } 65 | 66 | int main() { 67 | ListNode *head = createLinkedList(); 68 | displayLL(head); 69 | cout << detectLoop(head); 70 | } -------------------------------------------------------------------------------- /OOPS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Rectangle { 5 | int length; 6 | int breadth; 7 | public: 8 | Rectangle(int l, int b) : length(l), breadth(b) {} 9 | 10 | int getArea() { 11 | return length*breadth; 12 | } 13 | int getLength() { 14 | return length; 15 | } 16 | void setLength(int l) { 17 | length = l; 18 | } 19 | int getBreadth() { 20 | return breadth; 21 | } 22 | void setBreadth(int b) { 23 | breadth = b; 24 | } 25 | }; 26 | 27 | class BankAccount { 28 | int balance; 29 | public: 30 | BankAccount() : balance(0) {}; 31 | void deposit(int amount) { 32 | if(amount < 0) { 33 | cout << "Invalid Amount"; 34 | return; 35 | } 36 | cout << amount << " deposited in the account.\n"; 37 | balance += amount; 38 | } 39 | void withdraw(int amount) { 40 | if(amount < 0) { 41 | cout << "Invalid Amount\n"; 42 | return; 43 | } 44 | if(amount > balance) { 45 | cout << "Insufficient balance\n"; 46 | return; 47 | } 48 | balance -= amount; 49 | } 50 | int getBalance() { 51 | return balance; 52 | } 53 | }; 54 | 55 | int main() 56 | { 57 | BankAccount account1; 58 | account1.deposit(200); 59 | account1.withdraw(50); 60 | account1.withdraw(350); 61 | cout << account1.getBalance(); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /sumofk-level.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /********************************************************************** 4 | 5 | Following is the Binary Tree node structure already written: 6 | 7 | template 8 | class BinaryTreeNode { 9 | public: 10 | T data; 11 | BinaryTreeNode* left; 12 | BinaryTreeNode* right; 13 | 14 | BinaryTreeNode(T data) { 15 | this->data = data; 16 | left = NULL; 17 | right = NULL; 18 | } 19 | }; 20 | 21 | **********************************************************************/ 22 | 23 | template 24 | class BinaryTreeNode { 25 | public: 26 | T data; 27 | BinaryTreeNode* left; 28 | BinaryTreeNode* right; 29 | 30 | BinaryTreeNode(T data) { 31 | this->data = data; 32 | left = NULL; 33 | right = NULL; 34 | } 35 | }; 36 | 37 | int sumAtKthLevel(BinaryTreeNode* root, int k) 38 | { 39 | // Write your code here. 40 | queue*> q; 41 | q.push(root); 42 | q.push(nullptr); 43 | 44 | int sum = 0; 45 | int count = 1; 46 | 47 | while(!q.empty()) { 48 | BinaryTreeNode* front = q.front(); 49 | q.pop(); 50 | 51 | if(front == nullptr && q.empty()) break; 52 | 53 | if(front == nullptr && !q.empty()) { 54 | ++count; 55 | q.push(nullptr); 56 | } 57 | else { 58 | if(count == k) { 59 | sum += front->data; 60 | } 61 | if(front->left) q.push(front->left); 62 | if(front->right) q.push(front->right); 63 | } 64 | } 65 | return sum; 66 | } 67 | 68 | int main() { 69 | 70 | }; -------------------------------------------------------------------------------- /mergesort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | using namespace std; 6 | 7 | void mergeArray(vector &array, int start, int middle, int end) { 8 | vector result(end - start + 1); 9 | 10 | int i = start; 11 | int j = middle+1; 12 | 13 | // index for merged result array 14 | int k = 0; 15 | 16 | while(i <= middle && j <= end) { 17 | if(array[i] < array[j]) { 18 | result[k] = array[i]; 19 | ++k; 20 | ++i; 21 | } 22 | else if(array[i] > array[j]) { 23 | result[k] = array[j]; 24 | ++k; 25 | ++j; 26 | } 27 | else { 28 | result[k] = array[i]; 29 | ++k; 30 | ++i; 31 | result[k] = array[j]; 32 | ++j; 33 | } 34 | } 35 | 36 | while(i <= middle) { 37 | result[k] = array[i]; 38 | ++k; 39 | ++i; 40 | } 41 | 42 | while(j <= end) { 43 | result[k] = array[j]; 44 | ++k; 45 | ++j; 46 | } 47 | 48 | // overwrite the orginal subarray with sorted subarray 49 | k = 0; 50 | while(start <= end) { 51 | array[start] = result[k]; 52 | ++start; 53 | ++k; 54 | } 55 | 56 | } 57 | 58 | 59 | void MergeSort(vector &array, int start, int end) { 60 | if(start < end) { 61 | int middle = (start+end)/2; 62 | MergeSort(array, start, middle); 63 | MergeSort(array, middle+1, end); 64 | mergeArray(array, start, middle, end); 65 | } 66 | } 67 | 68 | 69 | int main() 70 | { 71 | vector array = {5,1,3,7,2,12,11,4,9}; 72 | MergeSort(array, 0, array.size()-1); 73 | for(int i : array) { 74 | cout << i << " "; 75 | } 76 | return 0; 77 | } -------------------------------------------------------------------------------- /gfg/isHeap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | // User Function template for C++ 6 | 7 | // Structure of node 8 | struct Node { 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int val) { 14 | data = val; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | class Solution { 20 | public: 21 | int countNodes(struct Node* root) { 22 | if(!root) { 23 | return 0; 24 | } 25 | return 1 + countNodes(root->left) + countNodes(root->right); 26 | } 27 | 28 | bool isComplete(vector array, int n) { 29 | for(int i = 0; i < n; ++i) { 30 | if(array[i] == -1) return false; 31 | } 32 | return true; 33 | } 34 | 35 | bool isHeapOrder(struct Node* root) { 36 | 37 | if(!root) return true; 38 | if(!root->left && !root->right) return true; 39 | 40 | if(root->left && root->left->data > root->data) { 41 | return false; 42 | } 43 | 44 | if(root->right && root->right->data > root->data) { 45 | return false; 46 | } 47 | 48 | 49 | return isHeapOrder(root->left) && isHeapOrder(root->right); 50 | 51 | 52 | } 53 | 54 | void treeToArray(struct Node* root, int i, vector& result) { 55 | if(!root || i >= result.size()) { 56 | return; 57 | } 58 | result[i] = root->data; 59 | treeToArray(root->left, 2*i+1, result); 60 | treeToArray(root->right, 2*i+2, result); 61 | } 62 | 63 | bool isHeap(struct Node* tree) { 64 | // code here 65 | vector array(100,-1); 66 | treeToArray(tree, 0, array); 67 | 68 | int n = countNodes(tree); 69 | 70 | return isComplete(array, n) && isHeapOrder(tree); 71 | 72 | 73 | 74 | } 75 | }; -------------------------------------------------------------------------------- /shortestpath.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MAXN 100 5 | int INF = 1e7; 6 | 7 | int dis[MAXN][MAXN]; 8 | int Next[MAXN][MAXN]; 9 | // You are using GCC 10 | void initialise(int n, int arr[MAXN][MAXN]) { 11 | //Type your code here 12 | for(int i = 0; i < n; ++i) { 13 | for(int j = 0; j < n; ++j) { 14 | dis[i][j] = arr[i][j]; 15 | Next[i][j] = -1; 16 | } 17 | } 18 | } 19 | 20 | void floydWarshall(int V) { 21 | 22 | //Type your code here 23 | for(int k = 0; k < V; ++k) { 24 | for(int i = 0; i < V; ++i) { 25 | for(int j = 0; j> V; 53 | 54 | int graph[MAXN][MAXN]; 55 | for (int i = 0; i < V; i++) { 56 | for (int j = 0; j < V; j++) { 57 | cin >> graph[i][j]; 58 | } 59 | } 60 | 61 | initialise(V, graph); 62 | floydWarshall(V); 63 | 64 | int u, v; 65 | cin >> u; 66 | cin >> v; 67 | 68 | int path[MAXN]; 69 | path[0] = u; 70 | int index = 1; 71 | while (u != v) { 72 | u = Next[u][v]; 73 | path[index++] = u; 74 | } 75 | 76 | cout << "Shortest path from " << path[0] << " to " << path[index - 1] << ": "; 77 | printPath(path, index); 78 | 79 | return 0; 80 | } -------------------------------------------------------------------------------- /test.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | #include 3 | using namespace std; 4 | 5 | #define MAXN 100 6 | int INF = 1e7; 7 | 8 | int dis[MAXN][MAXN]; 9 | int Next[MAXN][MAXN]; 10 | 11 | void initialise(int V, int graph[MAXN][MAXN]) { 12 | //Type your code here 13 | for(int i = 0; i < V; ++i) { 14 | for(int j = 0; j < V; ++j) { 15 | dis[i][j] = graph[i][j]; 16 | if(graph[i][j] != INF) { 17 | Next[i][j] = j; 18 | } 19 | else { 20 | Next[i][j] = -1; 21 | } 22 | } 23 | } 24 | } 25 | 26 | void floydWarshall(int V) { 27 | //Type your code here 28 | for(int k = 0; k < V; ++k) { 29 | for(int i = 0; i < V; ++i) { 30 | for(int j = 0; j < V; ++j) { 31 | if(dis[i][k] == INF || dis[k][j] == INF) { 32 | continue; 33 | } 34 | int oldDistance = dis[i][j]; 35 | int newDistance = dis[i][k]+dis[k][j]; 36 | 37 | if(newDistance < oldDistance) { 38 | dis[i][j] = newDistance; 39 | Next[i][j] = Next[i][k]; 40 | } 41 | 42 | } 43 | } 44 | } 45 | } 46 | 47 | void printPath(int path[], int n) { 48 | //Type your code here 49 | for(int i = 0; i < n; ++i) { 50 | cout << path[i] << " "; 51 | } 52 | } 53 | 54 | 55 | int main() { 56 | int V; 57 | cin >> V; 58 | 59 | int graph[MAXN][MAXN]; 60 | for (int i = 0; i < V; i++) { 61 | for (int j = 0; j < V; j++) { 62 | cin >> graph[i][j]; 63 | } 64 | } 65 | 66 | initialise(V, graph); 67 | floydWarshall(V); 68 | 69 | int u, v; 70 | cin >> u; 71 | cin >> v; 72 | 73 | int path[MAXN]; 74 | path[0] = u; 75 | int index = 1; 76 | while (u != v) { 77 | u = Next[u][v]; 78 | path[index++] = u; 79 | } 80 | 81 | cout << "Shortest path from " << path[0] << " to " << path[index - 1] << ": "; 82 | printPath(path, index); 83 | 84 | return 0; 85 | } -------------------------------------------------------------------------------- /floydwarshallpath.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MAXN 100 5 | int INF = 1e7; 6 | 7 | int dis[MAXN][MAXN]; 8 | int Next[MAXN][MAXN]; 9 | 10 | void initialise(int V, int graph[MAXN][MAXN]) { 11 | //Type your code here 12 | for(int i = 0; i < V; ++i) { 13 | for(int j = 0; j < V; ++j) { 14 | 15 | 16 | if(graph[i][j] != 0) { 17 | dis[i][j] = graph[i][j]; 18 | Next[i][j] = j; 19 | } 20 | else { 21 | dis[i][j] = INF; 22 | Next[i][j] = -1; 23 | } 24 | } 25 | } 26 | } 27 | 28 | void floydWarshall(int V) { 29 | //Type your code here 30 | for(int k = 0; k < V; ++k) { 31 | for(int i = 0; i < V; ++i) { 32 | for(int j = 0; j < V; ++j) { 33 | if(dis[i][k] == INF || dis[k][j] == INF) { 34 | continue; 35 | } 36 | 37 | if(dis[i][k]+dis[k][j] < dis[i][j]) { 38 | dis[i][j] = dis[i][k] + dis[k][j]; 39 | Next[i][j] = Next[i][k]; 40 | } 41 | } 42 | } 43 | } 44 | 45 | } 46 | 47 | void printPath(int path[], int n) { 48 | //Type your code here 49 | for(int i = 0; i < n; ++i) { 50 | 51 | cout << path[i] << " "; 52 | if(i != n-1) { 53 | cout << "->"; 54 | } 55 | } 56 | } 57 | 58 | int main() { 59 | int V; 60 | cin >> V; 61 | 62 | int graph[MAXN][MAXN]; 63 | for (int i = 0; i < V; i++) { 64 | for (int j = 0; j < V; j++) { 65 | cin >> graph[i][j]; 66 | } 67 | } 68 | 69 | initialise(V, graph); 70 | floydWarshall(V); 71 | 72 | int u, v; 73 | cin >> u; 74 | cin >> v; 75 | 76 | int path[MAXN]; 77 | path[0] = u; 78 | int index = 1; 79 | while (u != v) { 80 | u = Next[u][v]; 81 | path[index++] = u; 82 | } 83 | 84 | cout << "Shortest path from " << path[0] << " to " << path[index - 1] << ": "; 85 | printPath(path, index); 86 | 87 | return 0; 88 | } -------------------------------------------------------------------------------- /linkedlist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class ListNode { 6 | public: 7 | int data; 8 | ListNode *next; 9 | ListNode(int value) : data(value), next(NULL) {}; 10 | }; 11 | 12 | ListNode* createLinkedList() { 13 | int n; 14 | cout << "Enter the length of linked list: "; 15 | cin >> n; 16 | ListNode *head = NULL; 17 | ListNode *temp = NULL; 18 | int data; 19 | for(int i = 0; i < n; ++i) { 20 | cout << "Enter the data for node " << i+1 << endl; 21 | cin >> data; 22 | 23 | ListNode *newNode = new ListNode(data); 24 | 25 | if(head == NULL) { 26 | head = newNode; 27 | } 28 | else { 29 | temp->next = newNode; 30 | } 31 | temp = newNode; 32 | } 33 | return head; 34 | } 35 | 36 | void displayLL(ListNode *head) { 37 | ListNode *temp = head; 38 | while (temp->next != NULL) 39 | { 40 | cout << temp->data << "->"; 41 | temp = temp->next; 42 | } 43 | cout << temp->data << endl; 44 | } 45 | 46 | void insertAtTail(ListNode* &head, int data) { 47 | ListNode *createdNode = new ListNode(data); 48 | if(head == NULL) { 49 | head = createdNode; 50 | return; 51 | } 52 | 53 | ListNode *currentNode = head; 54 | 55 | while(currentNode->next != NULL) { 56 | currentNode = currentNode->next; 57 | } 58 | currentNode->next = createdNode; 59 | } 60 | 61 | void insertAtStart(ListNode * &head, int value) { 62 | ListNode *createdNode = new ListNode(value); 63 | 64 | if(head == NULL) { 65 | head = createdNode; 66 | return; 67 | } 68 | createdNode->next = head; 69 | head = createdNode; 70 | } 71 | 72 | bool searchLL(ListNode *head, int key) { 73 | ListNode* temp = head; 74 | while(temp != NULL) { 75 | if(temp->data == key) { 76 | return true; 77 | } 78 | temp = temp->next; 79 | } 80 | return false; 81 | } 82 | 83 | int main() { 84 | ListNode *head = NULL; 85 | // head = createLinkedList(); 86 | // displayLL(head); 87 | 88 | insertAtStart(head, 12); 89 | insertAtStart(head, 13); 90 | insertAtStart(head, 14); 91 | displayLL(head); 92 | cout << searchLL(head, 1); 93 | } -------------------------------------------------------------------------------- /singlycircular.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class ListNode { 6 | public: 7 | int data; 8 | ListNode *next; 9 | ListNode(int value) : data(value), next(NULL) {}; 10 | }; 11 | 12 | void insertAtTail(ListNode *&head, int value) { 13 | ListNode* createdNode = new ListNode(value); 14 | if(head == NULL) { 15 | head = createdNode; 16 | createdNode->next = head; 17 | } 18 | else { 19 | ListNode *currentNode = head; 20 | while (currentNode->next != head) { 21 | currentNode = currentNode->next; 22 | } 23 | currentNode->next = createdNode; 24 | createdNode->next = head; 25 | } 26 | } 27 | 28 | void deleteAtPos(ListNode* &head, int pos) { 29 | if(pos == 1) { 30 | ListNode* del = head; 31 | head = head->next; 32 | ListNode* current = head; 33 | while (head->next != nullptr) 34 | { 35 | current=current->next; 36 | } 37 | current->next = head; 38 | delete del; 39 | } 40 | else { 41 | ListNode* current = head; 42 | ListNode* prev = nullptr; 43 | for(int i = 1; i < pos; ++i) { 44 | prev = current; 45 | current = current->next; 46 | } 47 | prev->next = current->next; 48 | delete current; 49 | } 50 | } 51 | 52 | void displayLL(ListNode *head) { 53 | ListNode *temp = head; 54 | while (temp->next != head) 55 | { 56 | cout << temp->data << "->"; 57 | temp = temp->next; 58 | } 59 | cout << temp->data << endl; 60 | } 61 | 62 | ListNode* detectLoop(ListNode* head) { 63 | if(head == nullptr) { 64 | return nullptr; 65 | } 66 | ListNode* fast = head; 67 | ListNode* slow = head; 68 | 69 | while (fast == nullptr || fast->next == nullptr) 70 | { 71 | slow = slow->next; 72 | fast = fast->next->next; 73 | if(slow == fast) { 74 | return fast; 75 | } 76 | } 77 | 78 | return nullptr; 79 | } 80 | 81 | int main() { 82 | ListNode *head = NULL; 83 | insertAtTail(head, 5); 84 | insertAtTail(head, 8); 85 | insertAtTail(head, 7); 86 | insertAtTail(head, 9); 87 | displayLL(head); 88 | cout << detectLoop(head); 89 | 90 | } -------------------------------------------------------------------------------- /playground.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | using namespace std; 6 | 7 | // merge sort 8 | 9 | void merge(vector &array, int start, int middle, int end) { 10 | vector result(end - start + 1); 11 | 12 | int i = start; 13 | int j = middle+1; 14 | 15 | int k = 0; 16 | 17 | while(i <= middle && j <= end) { 18 | if(array[i] < array[j]) { 19 | result[k++] = array[i++]; 20 | } 21 | else if(array[j] < array[i]) { 22 | result[k++] = array[j++]; 23 | } 24 | else { 25 | result[k++] = array[i++]; 26 | result[k++] = array[j++]; 27 | } 28 | } 29 | 30 | while(i <= middle) { 31 | result[k++] = array[i++]; 32 | } 33 | while(j <= end) { 34 | result[k++] = array[j++]; 35 | } 36 | 37 | k = 0; 38 | for(int i = start; i <= end; ++i) { 39 | array[i] = result[k++]; 40 | } 41 | } 42 | 43 | void mergeSort(vector &array, int start, int end) { 44 | if(start < end) { 45 | int middle = (start+end)/2; 46 | mergeSort(array, start, middle); 47 | mergeSort(array, middle+1, end); 48 | 49 | merge(array, start, middle, end); 50 | } 51 | } 52 | 53 | 54 | 55 | // quick sort 56 | 57 | void swap(int &a, int &b) { 58 | int t = a; 59 | a = b; 60 | b = t; 61 | } 62 | 63 | int findPivotIndex(vector& array, int start, int end) { 64 | int i = start; 65 | int j = end; 66 | int pivotElement = array[start]; 67 | 68 | while(i <= j) { 69 | while(i <= j && pivotElement <= array[i]) i++; 70 | while(i <= j && pivotElement > array[j]) j--; 71 | 72 | if(i < j) { 73 | swap(array[i], array[j]); 74 | } 75 | } 76 | swap(array[j], array[start]); 77 | return j; 78 | } 79 | 80 | void quickSort(vector &array, int start, int end) { 81 | if(start < end) { 82 | int pivotIndex = findPivotIndex(array, start, end); 83 | quickSort(array, start, pivotIndex-1); 84 | quickSort(array, pivotIndex+1, end); 85 | } 86 | } 87 | 88 | 89 | 90 | 91 | int main() 92 | { 93 | vector array = {3,2,7,5,9,11,3,46,12,22,23,56,12,10,25,14,18,10,26}; 94 | // mergeSort(array, 0, array.size() -1); 95 | quickSort(array, 0, array.size()-1); 96 | for(int i : array) { 97 | cout << i << " "; 98 | } 99 | return 0; 100 | } -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | class Node { 8 | public: 9 | int data; 10 | Node* left; 11 | Node* right; 12 | 13 | Node(int x) : data(x), left(NULL), right(NULL) {} 14 | }; 15 | 16 | Node* arrayToBinaryTree(int arr[], int n, int i = 0) { 17 | if (i >= n || arr[i] == -1) return NULL; 18 | 19 | Node* root = new Node(arr[i]); 20 | root->left = arrayToBinaryTree(arr, n, 2 * i + 1); 21 | root->right = arrayToBinaryTree(arr, n, 2 * i + 2); 22 | 23 | return root; 24 | } 25 | 26 | void printInOrder(Node* root) { 27 | if (root == NULL) return; 28 | printInOrder(root->left); 29 | cout << root->data << " "; 30 | printInOrder(root->right); 31 | } 32 | 33 | int findHeight(Node* root) { 34 | if(root == nullptr) { 35 | return 0; 36 | } 37 | return 1 + max(findHeight(root->left), findHeight(root->right)); 38 | } 39 | 40 | queue q; 41 | 42 | 43 | void printRightView(Node* root) { 44 | if(!root) return; 45 | q.push(root); 46 | 47 | while(!q.empty()) { 48 | int size = q.size(); 49 | for(int i = 0; i < size; ++i) { 50 | Node* current = q.front(); 51 | q.pop(); 52 | if(i == size -1) { 53 | cout << current->data << " "; 54 | } 55 | if(current->left) { 56 | q.push(current->left); 57 | } 58 | if(current->right) { 59 | q.push(current->right); 60 | } 61 | } 62 | } 63 | } 64 | 65 | bool getPath(Node *root, int key, vector &path) { 66 | 67 | if(!root) { 68 | return false; 69 | } 70 | 71 | path.push_back(root->data); 72 | if(root->data == key) return true; 73 | 74 | if(getPath(root->left, key, path) || getPath(root->left, key, path)) { 75 | return true; 76 | } 77 | path.pop_back(); 78 | return false; 79 | } 80 | 81 | 82 | int lowestCommonAncestor(Node* root, int key1, int key2) { 83 | vector key1path; 84 | vector key2path; 85 | 86 | if(!getPath(root, key1, key1path) || !getPath(root, key2, key2path)) { 87 | return -1; 88 | } 89 | 90 | } 91 | 92 | int main() { 93 | int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; 94 | int n = sizeof(arr) / sizeof(arr[0]); 95 | 96 | Node* root = arrayToBinaryTree(arr, n); 97 | 98 | cout << findHeight(root); 99 | printRightView(root); 100 | 101 | return 0; 102 | } -------------------------------------------------------------------------------- /graphBFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | vector bfsTraversal(int n, vector> &adj){ 6 | // Write your code here. 7 | vector result; 8 | 9 | queue q; 10 | 11 | unordered_map visited; 12 | 13 | q.push(0); 14 | visited[0] = true; 15 | 16 | while(!q.empty()) { 17 | int current = q.front(); 18 | q.pop(); 19 | result.push_back(current); 20 | 21 | for(int i : adj[current]) { 22 | if(!visited[i]) { 23 | q.push(i); 24 | visited[i] = true; 25 | } 26 | } 27 | } 28 | 29 | return result; 30 | 31 | 32 | } 33 | 34 | 35 | // neocolab shortest path bfs 36 | // You are using GCC 37 | #include 38 | #include 39 | #include 40 | 41 | 42 | void add_edge(int adj[][100], int src, int dest) 43 | { 44 | //Type your code 45 | adj[src][dest] = 1; 46 | adj[dest][src] = 1; 47 | } 48 | 49 | bool BFS(int adj[][100], int src, int dest, int v, int pred[], int dist[]) 50 | { 51 | //Type your code 52 | queue q; 53 | unordered_map visited; 54 | 55 | for(int i = 0; i < v; ++i) { 56 | dist[i] = INT_MAX; 57 | pred[i] = -1; 58 | } 59 | 60 | q.push(src); 61 | dist[src] = 0; 62 | visited[src] = true; 63 | 64 | while(!q.empty()) { 65 | int front = q.front(); 66 | q.pop(); 67 | 68 | for(int i = 0; i < v; ++i) { 69 | if(adj[front][i] && !visited[i]) { 70 | q.push(i); 71 | dist[i] = dist[front] + 1; 72 | pred[i] = front; 73 | visited[i] = true; 74 | 75 | if(i == dest) { 76 | return true; 77 | } 78 | } 79 | } 80 | } 81 | return false; 82 | } 83 | 84 | void printShortestDistance(int adj[][100], int s, int dest, int v, int pred[], int dist[]) 85 | { 86 | //Type your code 87 | vector path; 88 | int crawl = dest; 89 | 90 | path.push_back(crawl); 91 | 92 | while(pred[crawl] != -1) { 93 | path.push_back(pred[crawl]); 94 | crawl = pred[crawl]; 95 | } 96 | 97 | cout << "Shortest path length is: " << dist[dest] << '\n'; 98 | cout << "Path is: "; 99 | for(int i = path.size()-1; i >= 0; --i) { 100 | cout << path[i] << " "; 101 | } 102 | } -------------------------------------------------------------------------------- /graphDFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | class Solution { 7 | public: 8 | // Function to return a list containing the DFS traversal of the graph. 9 | vector dfsOfGraph(vector>& adj) { 10 | // Code here 11 | unordered_map> adjlist; 12 | 13 | for(int i = 0; i < adj.size(); ++i) { 14 | 15 | for(int j = 0; j < adj[i].size(); ++j) { 16 | adjlist[i].push_back(adj[i][j]); 17 | } 18 | } 19 | 20 | unordered_map visited; 21 | stack st; 22 | 23 | vector result; 24 | 25 | st.push(0); 26 | 27 | while(!st.empty()) { 28 | int top = st.top(); 29 | st.pop(); 30 | 31 | if(!visited[top]) { 32 | result.push_back(top); 33 | } 34 | 35 | 36 | visited[top] = true; 37 | 38 | for(int i = adjlist[top].size()-1; i >= 0; --i) { 39 | if(!visited[adjlist[top].at(i)]) { 40 | st.push(adjlist[top].at(i)); 41 | } 42 | } 43 | 44 | } 45 | 46 | return result; 47 | } 48 | }; 49 | 50 | // Disconnected Graph 51 | 52 | vector> depthFirstSearch(int V, int E, vector> &edges) 53 | { 54 | // Write your code here 55 | unordered_map> adjlist; 56 | 57 | for(int i = 0; i < edges.size(); ++i) { 58 | 59 | for(int j = 0; j < edges[i].size(); ++j) { 60 | adjlist[i].push_back(edges[i][j]); 61 | } 62 | } 63 | 64 | unordered_map visited; 65 | vector> result; 66 | 67 | stack st; 68 | 69 | for(int i = 0; i < V; ++i) { 70 | if(visited.find(i) == visited.end() || visited[i] == false) { 71 | vector component; 72 | // clear the stack or make it local 73 | st.push(i); 74 | while(!st.empty()) { 75 | int top = st.top(); 76 | st.pop(); 77 | if(!visited[top]) { 78 | component.push_back(top); 79 | } 80 | visited[top] = true; 81 | for(int i = adjlist[top].size()-1; i >= 0; --i) { 82 | if(!visited[adjlist[top][i]]) { 83 | st.push(adjlist[top][i]); 84 | } 85 | } 86 | } 87 | 88 | result.push_back(component); 89 | } 90 | } 91 | 92 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp", 4 | "iosfwd": "cpp", 5 | "any": "cpp", 6 | "array": "cpp", 7 | "atomic": "cpp", 8 | "barrier": "cpp", 9 | "bit": "cpp", 10 | "*.tcc": "cpp", 11 | "bitset": "cpp", 12 | "cctype": "cpp", 13 | "cfenv": "cpp", 14 | "charconv": "cpp", 15 | "chrono": "cpp", 16 | "cinttypes": "cpp", 17 | "clocale": "cpp", 18 | "cmath": "cpp", 19 | "codecvt": "cpp", 20 | "compare": "cpp", 21 | "complex": "cpp", 22 | "concepts": "cpp", 23 | "condition_variable": "cpp", 24 | "coroutine": "cpp", 25 | "csetjmp": "cpp", 26 | "csignal": "cpp", 27 | "cstdarg": "cpp", 28 | "cstddef": "cpp", 29 | "cstdint": "cpp", 30 | "cstdio": "cpp", 31 | "cstdlib": "cpp", 32 | "cstring": "cpp", 33 | "ctime": "cpp", 34 | "cuchar": "cpp", 35 | "cwchar": "cpp", 36 | "cwctype": "cpp", 37 | "deque": "cpp", 38 | "forward_list": "cpp", 39 | "list": "cpp", 40 | "map": "cpp", 41 | "set": "cpp", 42 | "string": "cpp", 43 | "unordered_map": "cpp", 44 | "unordered_set": "cpp", 45 | "vector": "cpp", 46 | "exception": "cpp", 47 | "expected": "cpp", 48 | "algorithm": "cpp", 49 | "functional": "cpp", 50 | "iterator": "cpp", 51 | "memory": "cpp", 52 | "memory_resource": "cpp", 53 | "numeric": "cpp", 54 | "optional": "cpp", 55 | "random": "cpp", 56 | "ratio": "cpp", 57 | "regex": "cpp", 58 | "source_location": "cpp", 59 | "string_view": "cpp", 60 | "system_error": "cpp", 61 | "tuple": "cpp", 62 | "type_traits": "cpp", 63 | "utility": "cpp", 64 | "format": "cpp", 65 | "fstream": "cpp", 66 | "future": "cpp", 67 | "generator": "cpp", 68 | "initializer_list": "cpp", 69 | "iomanip": "cpp", 70 | "istream": "cpp", 71 | "latch": "cpp", 72 | "limits": "cpp", 73 | "mutex": "cpp", 74 | "new": "cpp", 75 | "numbers": "cpp", 76 | "ostream": "cpp", 77 | "print": "cpp", 78 | "ranges": "cpp", 79 | "scoped_allocator": "cpp", 80 | "semaphore": "cpp", 81 | "shared_mutex": "cpp", 82 | "span": "cpp", 83 | "spanstream": "cpp", 84 | "sstream": "cpp", 85 | "stacktrace": "cpp", 86 | "stdexcept": "cpp", 87 | "stdfloat": "cpp", 88 | "stop_token": "cpp", 89 | "streambuf": "cpp", 90 | "syncstream": "cpp", 91 | "text_encoding": "cpp", 92 | "thread": "cpp", 93 | "typeindex": "cpp", 94 | "typeinfo": "cpp", 95 | "valarray": "cpp", 96 | "variant": "cpp" 97 | } 98 | } -------------------------------------------------------------------------------- /rpg.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Character { 5 | string name; 6 | string type; 7 | int health = 100; 8 | int attackPower; 9 | int defensePower; 10 | int magicPower; 11 | 12 | public: 13 | Character() : name("Unknown"), type("Unknown"), attackPower(1), defensePower(1), magicPower(0) {}; 14 | Character(string n, string t, int h, int aP, int dP, int mP) : name(n), type(t), health(h), attackPower(aP), defensePower(dP), magicPower(mP) {}; 15 | Character(string n, string t, int h, int aP, int dP) : name(n), type(t), health(h), attackPower(aP), defensePower(dP), magicPower(0) {}; 16 | 17 | void setName(string s) { 18 | name = s; 19 | } 20 | string getName() { 21 | return name; 22 | } 23 | 24 | void setType(string t) { 25 | type = t; 26 | } 27 | string getType() { 28 | return type; 29 | } 30 | void setHealth(int h) { 31 | if(h > 100) { 32 | cout << "Health value cannot exceed 100"; 33 | return; 34 | } 35 | if(h < 0) { 36 | cout << "Health cannot be negative"; 37 | return; 38 | } 39 | health = h; 40 | } 41 | int getHealth() { 42 | return health; 43 | } 44 | void setAttackPower(int ap) { 45 | attackPower = ap; 46 | } 47 | int getAttackPower() { 48 | return attackPower; 49 | } 50 | void setDefensePower(int dp) { 51 | defensePower = dp; 52 | } 53 | int getDefensePower() { 54 | return defensePower; 55 | } 56 | void setMagicPower(int mp) { 57 | if(type != "Mage") { 58 | cout << "Not a magic character\n"; 59 | return; 60 | } 61 | magicPower = mp; 62 | } 63 | int getMagicPower() { 64 | if(type != "Mage") { 65 | cout << "Not a magic character\n"; 66 | return 0; 67 | } 68 | return magicPower; 69 | } 70 | 71 | void attack(Character& target) { 72 | cout << this->name << " attacks " << target.name << endl; 73 | } 74 | void defend(Character& target) { 75 | cout << this->name << " defends against " << target.name << endl; 76 | } 77 | void useMagic() { 78 | if(type != "Mage") { 79 | cout << "Not a magic character\n"; 80 | return; 81 | } 82 | cout << this->name <<" uses magic\n"; 83 | } 84 | }; 85 | 86 | 87 | 88 | 89 | 90 | int main() 91 | { 92 | Character wizard("wizard1", "Mage", 100, 20, 10, 25); 93 | Character archer("archer1", "Archer", 100, 20, 10); 94 | 95 | wizard.attack(archer); 96 | archer.defend(wizard); 97 | wizard.useMagic(); 98 | archer.useMagic(); 99 | return 0; 100 | } -------------------------------------------------------------------------------- /hw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Animal { 8 | protected: 9 | string species; 10 | int age; 11 | float weight; 12 | 13 | public: 14 | Animal() : species("unknown"), age(0), weight(0) {}; 15 | Animal(string s, int a, float w) : species(s), age(a), weight(w) {}; 16 | 17 | void displayInfo() { 18 | cout << "Species: " << species << '\n'; 19 | cout << "Age: " << age << " years\n"; 20 | cout << "Weight: " << fixed << setprecision(3) << weight << " kilograms\n"; 21 | } 22 | 23 | }; 24 | 25 | class Mammal : public Animal { 26 | protected: 27 | string furColor; 28 | 29 | public: 30 | Mammal() : furColor("unknown") {}; 31 | Mammal(string s, int a, float w, string f) : Animal(s, a, w), furColor(f) {}; 32 | 33 | void makeSound() { 34 | cout << species << " makes a sound\n"; 35 | } 36 | 37 | }; 38 | 39 | class Bird : virtual public Animal { 40 | protected: 41 | float wingSpan; 42 | 43 | public: 44 | Bird() : wingSpan(0) {}; 45 | Bird(string s, int a, float w, float ws) : Animal(s, a, w), wingSpan(ws) {}; 46 | 47 | void fly() { 48 | cout << species << " is flying with a wingspan of " << fixed << setprecision(1) << wingSpan << " meters\n"; 49 | } 50 | 51 | }; 52 | 53 | class Reptile : virtual public Animal { 54 | protected: 55 | bool isVenomous; 56 | public: 57 | Reptile() : isVenomous(true) {}; 58 | Reptile(string s, int a, float w, bool isVenom) : Animal(s, a, w), isVenomous(isVenom) {}; 59 | 60 | void crawl() { 61 | cout << species << " is crawling\n"; 62 | } 63 | 64 | }; 65 | 66 | class Lion : public Mammal { 67 | protected: 68 | int prideSize; 69 | public: 70 | Lion() : prideSize(0) {}; 71 | Lion(string s, int a, float w, string f, int ps) : Mammal(s, a, w, f), prideSize(ps) {}; 72 | 73 | void hunt() { 74 | cout << species << " hunts with its pride of " << prideSize << " members\n"; 75 | } 76 | 77 | void makeSound() { 78 | cout << species << " roars\n"; 79 | } 80 | 81 | }; 82 | 83 | class FlyingReptile : public Bird, public Reptile { 84 | public: 85 | FlyingReptile() {}; 86 | FlyingReptile(string s, int a, float w, float ws, bool isVenom) : Animal(s, a, w), Bird(s, a, w, ws), Reptile(s, a, w, isVenom) {}; 87 | 88 | void glide() { 89 | cout << species << " is gliding with a wingspan of " << fixed << setprecision(1) << wingSpan << " meters\n"; 90 | } 91 | 92 | }; 93 | 94 | class Elephant : public Mammal { 95 | protected: 96 | float trunkLength; 97 | public: 98 | Elephant() : trunkLength(0) {}; 99 | Elephant(string s, int a, float w, string f, float tl) : Mammal(s, a, w, f), trunkLength(tl) {}; 100 | 101 | void useTrunk() { 102 | cout << species << " uses its trunk to drink water\n"; 103 | } 104 | 105 | }; 106 | 107 | int main() { 108 | 109 | Lion lionObject("Lion", 5, 190, "Golden", 6); 110 | lionObject.displayInfo(); 111 | lionObject.makeSound(); 112 | lionObject.hunt(); 113 | 114 | Bird Parrot("Parrot", 2, 1.5, 0.8); 115 | Parrot.displayInfo(); 116 | Parrot.fly(); 117 | 118 | Reptile Crocodile("Crocodile", 12, 400, false); 119 | Crocodile.displayInfo(); 120 | Crocodile.crawl(); 121 | 122 | Elephant Elephantobject("Elephant", 10, 5000, "Grey", 2); 123 | Elephantobject.displayInfo(); 124 | Elephantobject.makeSound(); 125 | Elephantobject.useTrunk(); 126 | 127 | FlyingReptile flyingReptile("Pterodactyl", 3, 50, 3, true); 128 | flyingReptile.displayInfo(); 129 | flyingReptile.fly(); 130 | flyingReptile.crawl(); 131 | flyingReptile.glide(); 132 | 133 | return 0; 134 | } -------------------------------------------------------------------------------- /mythicalkingdom.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Hero { 6 | public: 7 | int heroID; 8 | string name; 9 | int level; 10 | string classType; 11 | int health; 12 | int mana; 13 | int strength; 14 | int magic; 15 | int defense; 16 | bool available; 17 | string heroRank; 18 | 19 | 20 | 21 | void displayHeroStats() { 22 | cout << "----------------------------------\n"; 23 | cout << "ID : " << heroID << "\n"; 24 | cout << "Name of the hero : " << name << "\n"; 25 | cout << "Level : " << level << "\n"; 26 | cout << "Class Type : " << classType << "\n"; 27 | cout << "Current Health : " << health << "\n"; 28 | cout << "Mana points : " << mana << "\n"; 29 | cout << "Strength : " << strength << "\n"; 30 | cout << "Magic Attack Power : " << magic << "\n"; 31 | cout << "Defense : " << defense << "\n"; 32 | cout << "Is Hero Available? : " << available << "\n"; 33 | cout << "----------------------------------\n"; 34 | } 35 | 36 | void levelUp() { 37 | strength += 1; 38 | level += 1; 39 | magic += 1; 40 | defense += 1; 41 | } 42 | 43 | void takeDamage(int damage) { 44 | health -= damage; 45 | if(health <= 0) { 46 | cout << "Hero is dead\n"; 47 | } 48 | } 49 | void restoreHealth(int h) { 50 | 51 | health += h; 52 | if(health > 100) { 53 | health = 100; 54 | } 55 | } 56 | 57 | void assignToFortress(Fortress &fortress) { 58 | available = false; 59 | } 60 | 61 | void fightEnemey(Enemy &enemy) { 62 | cout << name << " is fighting an enemy.\n"; 63 | } 64 | 65 | 66 | }; 67 | 68 | class Fortress { 69 | public: 70 | int fortressID; 71 | string location; 72 | int defenseRating; 73 | int capacity; 74 | int currentHeroes; 75 | bool isgettingattacked; 76 | 77 | 78 | 79 | void addHero(Hero &hero) { 80 | cout << hero.name << " is assigned to the fortress at " << location << "\n"; 81 | ++currentHeroes; 82 | } 83 | void removeHero(Hero &hero) { 84 | cout << hero.name << " is removed from the fortress at " << location << "\n"; 85 | --currentHeroes; 86 | } 87 | 88 | bool isUnderAttack() { 89 | return isgettingattacked; 90 | } 91 | 92 | void defend(Enemy &enemy) { 93 | cout << "The fortess is being defended against " << enemy.enemyType << "\n"; 94 | isgettingattacked = true; 95 | } 96 | }; 97 | 98 | 99 | class Enemy { 100 | public: 101 | int enemyId; 102 | string enemyType; 103 | int strength; 104 | int health; 105 | float speed; 106 | string specialAbilities; 107 | 108 | void attack(Fortress &fortress) { 109 | cout << "Attacking the fortress\n"; 110 | fortress.defenseRating--; 111 | fortress.isgettingattacked = true; 112 | } 113 | 114 | void takeDamage(int damage) { 115 | health -= damage; 116 | if(health <= 0) { 117 | cout << "Enemy is dead\n"; 118 | } 119 | } 120 | 121 | bool isAlive() { 122 | return health > 0; 123 | } 124 | 125 | }; 126 | 127 | class Kingdom { 128 | public: 129 | 130 | string kingdomName; 131 | int resources; 132 | vector heroList; 133 | vector fortressList; 134 | vector enemyList; 135 | 136 | void recruitHero(Hero &hero) { 137 | heroList.push_back(&hero); 138 | } 139 | 140 | void buildFortress(Fortress &fortess) { 141 | fortressList.push_back(&fortess); 142 | } 143 | 144 | void defendKingdom() { 145 | cout << "Defending the kingdom\n"; 146 | } 147 | 148 | void assignHeroToFortress(int fortressId, int heroId) { 149 | Hero* targetHero = nullptr; 150 | Fortress* targetFortress = nullptr; 151 | 152 | for(Hero *hero : heroList) { 153 | if(hero->heroID == heroId) { 154 | targetHero = hero; 155 | break; 156 | } 157 | } 158 | for(Fortress *fortress : fortressList) { 159 | if(fortress->fortressID == fortressId) { 160 | targetFortress = fortress; 161 | break; 162 | } 163 | } 164 | 165 | targetFortress->addHero(*targetHero); 166 | targetHero->assignToFortress(*targetFortress); 167 | } 168 | 169 | void launchCounterAttack(Enemy &enemy) { 170 | cout << "Launching counterattack against " << enemy.enemyType << "\n"; 171 | } 172 | 173 | void manageResources(int allocation) { 174 | resources += allocation; 175 | } 176 | 177 | void viewKingdomStatus() { 178 | cout << "Name of the Kingdom : " << kingdomName << '\n'; 179 | cout << "Available resources: " << resources << '\n'; 180 | cout << "Heroes recruited: \n"; 181 | 182 | for(Hero* h : heroList) { 183 | cout << h->name << " "; 184 | } 185 | 186 | cout << "Location of fortresses of the kingdom: " << '\n'; 187 | for(Fortress* f : fortressList) { 188 | cout << f->location << " "; 189 | } 190 | cout << '\n'; 191 | cout << "All known enemy types: " << '\n'; 192 | for(Enemy* e : enemyList) { 193 | cout << e->enemyType << " "; 194 | } 195 | cout << '\n'; 196 | 197 | } 198 | 199 | }; 200 | 201 | 202 | class Battle { 203 | public: 204 | int battleID; 205 | vector heroGroup; 206 | vector enemygroup; 207 | string battleOutcome; 208 | int damageDealt; 209 | 210 | void initiateBattle(Hero &hero, Enemy &enemy) { 211 | cout << "Heroes and enemies have begun the battle\n"; 212 | } 213 | void calculateOutcome() { 214 | if(heroGroup.size() > enemygroup.size()) { 215 | cout << "Heroes win\n"; 216 | battleOutcome = "Victory"; 217 | } 218 | else if(heroGroup.size() < enemygroup.size()) { 219 | cout << "Enemies win\n"; 220 | battleOutcome = "Defeat"; 221 | } 222 | else { 223 | cout << "Stalemate\n"; 224 | battleOutcome = "Stalemate"; 225 | } 226 | } 227 | void recordBattle() { 228 | cout << "The battle has ended.\n"; 229 | cout << "Status of Battle: " << battleOutcome << " for the heroes.\n"; 230 | } 231 | }; 232 | 233 | int main() 234 | { 235 | 236 | return 0; 237 | } -------------------------------------------------------------------------------- /heaps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class MyHeap { 10 | vector array; 11 | 12 | public: 13 | void push(int data) { 14 | array.push_back(data); 15 | int elementIndex = array.size() -1; 16 | 17 | while(elementIndex > 0) { 18 | int parentIndex = (elementIndex - 1) / 2; 19 | 20 | if(array[parentIndex] < array[elementIndex]) { 21 | swap(array[parentIndex], array[elementIndex]); 22 | } 23 | else { 24 | break; 25 | } 26 | elementIndex = parentIndex; 27 | } 28 | } 29 | 30 | void pop() { 31 | if(array.empty()) { 32 | cout << "Heap is empty\n"; 33 | return; 34 | } 35 | 36 | array[0] = array.back(); 37 | array.pop_back(); 38 | 39 | int elementIndex = 0; 40 | 41 | while(elementIndex < array.size()) { 42 | 43 | int leftChildIndex = 2*elementIndex + 1; 44 | int rightChildIndex = 2*elementIndex + 2; 45 | 46 | int maxIndex = elementIndex; 47 | 48 | if(leftChildIndex < array.size() && array[leftChildIndex] > array[maxIndex]) { 49 | maxIndex = leftChildIndex; 50 | } 51 | 52 | if(rightChildIndex < array.size() && array[rightChildIndex] > array[maxIndex]) { 53 | maxIndex = rightChildIndex; 54 | } 55 | if(elementIndex == maxIndex) { 56 | break; 57 | } 58 | else { 59 | swap(array[elementIndex], array[maxIndex]); 60 | elementIndex = maxIndex; 61 | } 62 | } 63 | } 64 | 65 | int top() { 66 | if(array.empty()) { 67 | cout << "Heap is Empty\n"; 68 | return -1; 69 | } 70 | return array[0]; 71 | } 72 | 73 | bool empty() { 74 | return array.empty(); 75 | } 76 | 77 | }; 78 | 79 | // You are using GCC 80 | 81 | class MinHeap { 82 | vector heap; 83 | 84 | void minHeapify(vector &array, int i) { 85 | 86 | int smallest = i; 87 | int left = 2*i + 1; 88 | int right = 2*i + 2; 89 | 90 | if(left < array.size() && array[left] < array[smallest]) { 91 | smallest = left; 92 | } 93 | if(right < array.size() && array[right] < array[smallest]) { 94 | smallest = right; 95 | } 96 | 97 | if(smallest != i) { 98 | swap(array[smallest], array[i]); 99 | minHeapify(array, smallest); 100 | } 101 | } 102 | 103 | public: 104 | MinHeap() {}; 105 | 106 | MinHeap(vector array) { 107 | int n = array.size(); 108 | for(int i = n/2-1; i >= 0; --i) { 109 | minHeapify(array, i); 110 | } 111 | heap = array; 112 | } 113 | 114 | void push(int value) { 115 | heap.push_back(value); 116 | int elementIndex = heap.size() - 1; 117 | 118 | while(elementIndex > 0) { 119 | int parentIndex = (elementIndex - 1)/2; 120 | if(heap[parentIndex] > heap[elementIndex]) { 121 | swap(heap[parentIndex], heap[elementIndex]); 122 | elementIndex = parentIndex; 123 | } 124 | else { 125 | break; 126 | } 127 | } 128 | } 129 | 130 | void pop() { 131 | if(heap.empty()) { 132 | return; 133 | } 134 | heap[0] = heap.back(); 135 | heap.pop_back(); 136 | 137 | int i = 0; 138 | 139 | while(i < heap.size()) { 140 | int smallest = i; 141 | int left = 2*i + 1; 142 | int right = 2*i + 2; 143 | 144 | if(left < heap.size() && heap[left] < heap[smallest]) { 145 | smallest = left; 146 | } 147 | 148 | if(right < heap.size() && heap[right] < heap[smallest]) { 149 | smallest = right; 150 | } 151 | if(i == smallest) { 152 | break; 153 | } 154 | swap(heap[smallest], heap[i]); 155 | i = smallest; 156 | } 157 | 158 | } 159 | void printHeap() { 160 | for(int i : heap) { 161 | cout << i << " "; 162 | } 163 | cout << '\n'; 164 | } 165 | int top() { 166 | return heap[0]; 167 | } 168 | int findMax() { 169 | int maxval = INT_MIN; 170 | for(int i : heap) { 171 | maxval = max(maxval, i); 172 | } 173 | return maxval; 174 | } 175 | int sumNodes() { 176 | int sum = 0; 177 | for(int i : heap) { 178 | sum += i; 179 | } 180 | return sum; 181 | } 182 | int size() { 183 | return heap.size(); 184 | } 185 | }; 186 | 187 | int main() { 188 | int n; 189 | cin >> n; 190 | vector array(n); 191 | for(int i = 0; i < n; ++i) { 192 | cin >> array[i]; 193 | } 194 | MinHeap h; 195 | for(int i : array) { 196 | h.push(i); 197 | } 198 | cout << "Min Heap: "; 199 | h.printHeap(); 200 | cout << "Root Node: " << h.top() << '\n'; 201 | h.pop(); 202 | cout << "Maximum Value: " << h.findMax() << '\n'; 203 | int sum = h.sumNodes(); 204 | cout << "Sum of Nodes: " << sum << '\n'; 205 | cout << fixed << setprecision(2) << "Average of Nodes: " << ((double)sum / (double)h.size()); 206 | 207 | 208 | } 209 | 210 | 211 | 212 | // int main() 213 | // { 214 | // MyHeap maxHeap; 215 | // maxHeap.push(21); 216 | // maxHeap.push(33); 217 | // maxHeap.push(3); 218 | // maxHeap.push(34); 219 | // maxHeap.push(8); 220 | // maxHeap.push(3); 221 | // maxHeap.push(29); 222 | // cout << maxHeap.top() << "\n"; 223 | // maxHeap.pop(); 224 | // cout << maxHeap.top() << "\n"; 225 | // maxHeap.pop(); 226 | // cout << maxHeap.top() << "\n"; 227 | // maxHeap.pop(); 228 | // cout << maxHeap.top() << "\n"; 229 | // maxHeap.pop(); 230 | // cout << maxHeap.top() << "\n"; 231 | // maxHeap.pop(); 232 | // cout << maxHeap.top() << "\n"; 233 | // maxHeap.pop(); 234 | // cout << maxHeap.top() << "\n"; 235 | 236 | 237 | // // empty heap 238 | // maxHeap.pop(); 239 | // cout << maxHeap.top() << "\n"; 240 | // return 0; 241 | // } --------------------------------------------------------------------------------