├── DPP ├── Unit 4 │ ├── Dharshan.cpp │ ├── vinith.cpp │ ├── Alex 2 3 1 0.cpp │ ├── Critical customer.cpp │ ├── Sarah avid programmer.cpp │ ├── merge sort developing leaderboard.cpp │ ├── Imagine various algorithm problems.cpp │ ├── Money Heist.cpp │ ├── numbered disks(1 to n) pegs.cpp │ ├── mystical kingdom.cpp │ ├── Sarah 52 57.cpp │ ├── Developing a software module BST.cpp │ ├── you are working asa programmer.cpp │ ├── Tou've received secrect message.cpp │ ├── working with software developer.cpp │ ├── Arun.cpp │ ├── Alex.cpp │ ├── Ragu.cpp │ ├── Freshmart.cpp │ ├── Lalitha.cpp │ ├── harsh.cpp │ ├── Venugopal.cpp │ ├── technical interview.cpp │ ├── John student.cpp │ ├── Arsha.cpp │ ├── rearrangeNegativeBeforePositive.cpp │ ├── Dictionary.cpp │ ├── Binary Search Tree integer K.cpp │ └── Imagine you librarian.cpp ├── Unit 3 │ ├── ReadMe.md │ ├── a.exe │ ├── Agalya.exe │ ├── Kamali.exe │ ├── Thiru.exe │ ├── hi.cpp │ ├── Alice and Bob JNCAB.cpp │ ├── Dequeue Operation Enqueue Operation responsibility.cpp │ ├── implementing a simulate queues for a supermarket.cpp │ ├── reverse a string using a queue.cpp │ ├── manages a deque 12_95-9_6_0 reversal.cpp │ ├── Michael.cpp │ ├── computing system URGENCY.cpp │ ├── Olivia.cpp │ ├── Nayana.cpp │ ├── double-ended queue 1 2 3 4 5 .cpp │ ├── implement dequeue operation list MAX = 25.cpp │ ├── Jack.cpp │ ├── binary sequence.cpp │ ├── Bob.cpp │ ├── Lala.cpp │ ├── Responsibility Enqueue Task Get Rear task.cpp │ ├── Lara.cpp │ ├── Lisa.cpp │ ├── Julie.cpp │ ├── Student attendence system.cpp │ ├── event reg conference.cpp │ ├── Student registration system for a university achievemenets.cpp │ ├── challenging developing a queue data .cpp │ ├── Mira.cpp │ ├── Restaurant Get Rear.cpp │ ├── Raja.cpp │ ├── The result is Calculator RPN.cpp │ ├── Agalya.cpp │ ├── task scheduling double-ended queue.cpp │ ├── Nandha.cpp │ ├── Laddu.cpp │ ├── Meenu.cpp │ ├── responsibility array based methodology.cpp │ ├── generate and print binary sequence user's input.cpp │ ├── ticket system for a popular music concert.cpp │ ├── Nithish.cpp │ └── Bindu.cpp ├── .vscode │ └── settings.json ├── Unit 1 │ ├── a.exe │ ├── Traverse and find Sum of array.cpp │ ├── Secont smallest element.cpp │ ├── Data Analytics.cpp │ ├── Data_Analysis_Update_Array.cpp │ ├── friend.cpp │ ├── Updated_array_insertion.cpp │ ├── policeman.cpp │ ├── Pooja.cpp │ ├── Large Online Retail Platform.cpp │ ├── Text_Editor.cpp │ ├── Alex.cpp │ ├── stock Trading Application.cpp │ ├── Mountaineer FindPeakElement.cpp │ ├── robotics.cpp │ ├── Element_Frequency.cpp │ ├── Geetha.cpp │ ├── Rotate the array to right.cpp │ ├── Delete an element from array.cpp │ ├── Traverse array findIndex.cpp │ ├── Apple_Mango_Banana.cpp │ ├── Insert_Beignning_shift.cpp │ ├── Mathematical Application.cpp │ ├── Mathematical Application.cpp │ ├── merge them into a single array.cpp │ ├── prices of product.cpp │ ├── Merges two sorted arrays student IDs.cpp │ ├── e-Commerec system.cpp │ ├── Developing a Program For Data Processing.cpp │ ├── Rajesh.cpp │ ├── Merging K Sorted Array.cpp │ ├── Skilled Software Engineer.cpp │ ├── Permutation.cpp │ ├── Sports analytics system.cpp │ ├── Single Array Sorted Alphabetically.cpS │ ├── Dhiya.cpp │ ├── Voting Booth.cpp │ ├── Vishwas.cpp │ ├── Developing a sales analytics.cpp │ ├── Imagine you are developing a program.cpp │ ├── Software Developer Online Retail.cpp │ ├── Insert an element at a specific position in an array.cpp │ ├── Stock trading platform.cpp │ ├── Sarah.cpp │ ├── Developing merges two tudentIDs.cpp │ ├── Delete an element.cpp │ ├── commonElements.cpp │ ├── insert specfic position.cpp │ ├── 2D array binary Search │ ├── Mathematical Research Project.cpp │ ├── management system.cpp │ ├── university.cpp │ ├── Student grades for different subjects.cpp │ ├── Merge the arrays unique combinations.cpp │ ├── Text analysis.cpp │ ├── University Wizard.cpp │ ├── data analysis findMedian.cpp │ ├── Shyam.cpp │ ├── Teacher at a School.cpp │ └── Merge two arr[ ] Corresponding sum.cpp └── Unit 2 │ ├── a.exe │ ├── Dhanush.exe │ ├── text-editing-application_INDEX.exe │ ├── contact management system in front.cpp │ ├── Alice.cpp │ ├── Developing Contact Managaement system front of the contact list.cpp │ ├── Implement operates on a circular.cpp │ ├── Library.cpp │ ├── Lisa.cpp │ ├── Implementing a system.cpp │ ├── Library online application.cpp │ ├── Jannu.cpp │ ├── Task of developing operatoes circulat header.cpp │ ├── Developing Contact Managaement system end of the contact list.cpp │ ├── Developing a text editor INSERTING NEW STRING.cpp │ ├── Scientific research project.cpp │ ├── 5_Milton.cpp │ ├── -5_0_5_Analyzing Temperature.cpp │ ├── Suresh.cpp │ ├── Uma(insert node at end).cpp │ ├── Milton.cpp │ ├── deleting the first student's record.cpp │ ├── Developing a smart device binary data from sensor.cpp │ ├── Dharun.cpp │ ├── Imagine Cashier.cpp │ ├── Developing operates circular header Standard linked list points back.cpp │ ├── Dhanush.cpp │ ├── Student record system.cpp │ ├── text-editing-application_INDEX.cpp │ ├── Text editing application.cpp │ ├── Imagine teacher seating arrangement.cpp │ ├── Web analytics.cpp │ ├── Vijay.cpp │ ├── Imagine teacher preparing seating arrangement.cpp │ ├── Sita.cpp │ └── Program to maintain STUDENT ROSTER.cpp ├── README.md ├── Academics ├── Arrays │ ├── a.exe │ ├── help.exe │ ├── Linear_search.exe │ ├── Array.cpp │ ├── Linear_search.cpp │ ├── help.c++ │ ├── Array_basic.c │ ├── Traversing_Array.c │ └── Binary Seach.cpp ├── Linkedlist │ ├── a.exe │ └── insert_at_start_or_end.exe ├── stack │ └── Stack.cpp ├── Queue │ └── Priority Queue │ │ └── Priority_queue.cpp └── Mergesort.cpp ├── Self Paced ├── Linked List │ ├── a.exe │ └── Display.cpp └── Stack_Data │ ├── Stack_Linkedlist │ ├── a.exe │ ├── Stack_Linkedlist.exe │ ├── Stack_Linkedlist.cpp │ └── Stack_class.cpp │ ├── Paranthesis matching │ ├── a.exe │ └── Paranthesis Match.cpp │ └── Stack_Array │ └── Stack_Array.cpp └── .vscode └── c_cpp_properties.json /DPP/Unit 4/Dharshan.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /DPP/Unit 4/vinith.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /DPP/Unit 4/Alex 2 3 1 0.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /DPP/Unit 4/Critical customer.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /DPP/Unit 3/ReadMe.md: -------------------------------------------------------------------------------- 1 | Updating Soon 2 | -------------------------------------------------------------------------------- /DPP/Unit 4/Sarah avid programmer.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /DPP/Unit 4/merge sort developing leaderboard.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data-Structures-and-Algorithms 2 | The Journey of DSA begins here. 3 | -------------------------------------------------------------------------------- /DPP/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp" 4 | } 5 | } -------------------------------------------------------------------------------- /DPP/Unit 1/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/DPP/Unit 1/a.exe -------------------------------------------------------------------------------- /DPP/Unit 2/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/DPP/Unit 2/a.exe -------------------------------------------------------------------------------- /DPP/Unit 3/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/DPP/Unit 3/a.exe -------------------------------------------------------------------------------- /DPP/Unit 3/Agalya.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/DPP/Unit 3/Agalya.exe -------------------------------------------------------------------------------- /DPP/Unit 3/Kamali.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/DPP/Unit 3/Kamali.exe -------------------------------------------------------------------------------- /DPP/Unit 3/Thiru.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/DPP/Unit 3/Thiru.exe -------------------------------------------------------------------------------- /DPP/Unit 3/hi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | cout << "Sushant" 7 | } -------------------------------------------------------------------------------- /Academics/Arrays/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/Academics/Arrays/a.exe -------------------------------------------------------------------------------- /DPP/Unit 2/Dhanush.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/DPP/Unit 2/Dhanush.exe -------------------------------------------------------------------------------- /Academics/Arrays/help.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/Academics/Arrays/help.exe -------------------------------------------------------------------------------- /Academics/Linkedlist/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/Academics/Linkedlist/a.exe -------------------------------------------------------------------------------- /Self Paced/Linked List/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/Self Paced/Linked List/a.exe -------------------------------------------------------------------------------- /Academics/stack/Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | 6 | } 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Academics/Arrays/Linear_search.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/Academics/Arrays/Linear_search.exe -------------------------------------------------------------------------------- /Academics/Queue/Priority Queue/Priority_queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | 7 | int main() 8 | { 9 | 10 | } -------------------------------------------------------------------------------- /DPP/Unit 2/text-editing-application_INDEX.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/DPP/Unit 2/text-editing-application_INDEX.exe -------------------------------------------------------------------------------- /Self Paced/Stack_Data/Stack_Linkedlist/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/Self Paced/Stack_Data/Stack_Linkedlist/a.exe -------------------------------------------------------------------------------- /Academics/Linkedlist/insert_at_start_or_end.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/Academics/Linkedlist/insert_at_start_or_end.exe -------------------------------------------------------------------------------- /Self Paced/Stack_Data/Paranthesis matching/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/Self Paced/Stack_Data/Paranthesis matching/a.exe -------------------------------------------------------------------------------- /Self Paced/Stack_Data/Stack_Linkedlist/Stack_Linkedlist.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/07Sushant/Data-Structures-and-Algorithms/HEAD/Self Paced/Stack_Data/Stack_Linkedlist/Stack_Linkedlist.exe -------------------------------------------------------------------------------- /Academics/Arrays/Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std ; 3 | int main() 4 | { 5 | int A[5]; 6 | int Z[6] = {2,4,6,8,10}; 7 | A[0] = 12; 8 | A[1] = 15; 9 | A[2] = 25; 10 | 11 | cout << sizeof(A) << endl; 12 | cout < 2 | int main() 3 | { 4 | const int MAX_SIZE = 100; 5 | int n; 6 | std::cin>>n; 7 | 8 | int arr[MAX_SIZE]; 9 | for(int i = 0; i < n; ++i) 10 | { 11 | std::cin>>arr[i]; 12 | } 13 | 14 | int sum = 0; 15 | for(int i = 0; i < n; ++i) 16 | { 17 | sum +=arr[i]; 18 | } 19 | 20 | std::cout < 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | string s; 8 | getline(cin, s); 9 | 10 | stack charStack; 11 | 12 | for (char c : s) { 13 | charStack.push(c); 14 | } 15 | 16 | while (!charStack.empty()) { 17 | cout << charStack.top(); 18 | charStack.pop(); 19 | } 20 | 21 | cout << endl; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Win32", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "defines": [ 9 | "_DEBUG", 10 | "UNICODE", 11 | "_UNICODE" 12 | ], 13 | "compilerPath": "C:\\MinGW\\bin\\gcc.exe", 14 | "cStandard": "c11", 15 | "cppStandard": "gnu++14", 16 | "intelliSenseMode": "windows-gcc-x86" 17 | } 18 | ], 19 | "version": 4 20 | } -------------------------------------------------------------------------------- /DPP/Unit 1/Secont smallest element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | int n; 8 | std::cin >> n; 9 | std::vector arr(n); 10 | 11 | for (int i = 0; i < n; i++) 12 | { 13 | std::cin >> arr[i]; 14 | } 15 | 16 | std::sort(arr.begin(), arr.end()); 17 | 18 | if (n >= 2) 19 | { 20 | std::cout << "Second Smallest Element: " << arr[1] << std::endl; 21 | } 22 | else 23 | { 24 | std::cout << "Array size should be at least 2." << std::endl; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /DPP/Unit 1/Data Analytics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int findSecondSmallest(std::vector arr) 6 | { 7 | std::sort(arr.begin(), arr.end()); 8 | return arr[1]; 9 | } 10 | 11 | int main() 12 | { 13 | int n; 14 | std::cin >> n; 15 | 16 | std::vector arr(n); 17 | for (int i = 0; i>arr[i]; 20 | 21 | } 22 | 23 | int secondSmallest = findSecondSmallest(arr); 24 | std::cout<<"Second Smallest Element: " << 25 | secondSmallest << std::endl; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /DPP/Unit 1/Data_Analysis_Update_Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void reverseArray(char arr[], int size) { 6 | reverse(arr, arr + size); 7 | } 8 | 9 | int main() { 10 | const int MAX_SIZE = 100; 11 | char arr[MAX_SIZE]; 12 | int size; 13 | 14 | cin >> size; 15 | 16 | for (int i = 0; i < size; i++) { 17 | cin >> arr[i]; 18 | } 19 | 20 | reverseArray(arr, size); 21 | 22 | cout << "Updated array: "; 23 | for (int i = 0; i < size; i++) { 24 | cout << arr[i] << " "; 25 | } 26 | cout << endl; 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /DPP/Unit 1/friend.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Box { 4 | private: 5 | double length; 6 | 7 | public: 8 | Box() : length(0) {} 9 | 10 | void get() { 11 | std::cout << "Enter the length of the box: "; 12 | std::cin >> length; 13 | } 14 | 15 | friend void printVolume(Box); 16 | }; 17 | 18 | void printVolume(Box box) { 19 | double volume = box.length * box.length * box.length; 20 | std::cout << "Volume of the box: " << volume << std::endl; 21 | } 22 | 23 | int main() { 24 | Box customBox; 25 | customBox.get(); 26 | printVolume(customBox); 27 | 28 | return 0; 29 | } 30 | //sushant 31 | -------------------------------------------------------------------------------- /DPP/Unit 3/Dequeue Operation Enqueue Operation responsibility.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int front = 0, rear = 0; 5 | 6 | int main() { 7 | int max_size; 8 | cin >> max_size; 9 | 10 | int* queue = new int[max_size]; 11 | 12 | int value; 13 | for (int i = 0; i < max_size; i++) { 14 | cin >> value; 15 | if (rear < max_size) { 16 | queue[rear++] = value; 17 | } else { 18 | break; 19 | } 20 | } 21 | 22 | cout << "Dequeuing elements: "; 23 | while (front < rear) { 24 | cout << queue[front++] << " "; 25 | } 26 | cout << endl; 27 | 28 | delete[] queue; 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /DPP/Unit 1/Updated_array_insertion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int size, position; 6 | char character; 7 | 8 | cin >> size; 9 | char arr[100]; 10 | for (int i = 0; i < size; i++) { 11 | cin >> arr[i]; 12 | } 13 | 14 | cin >> position; 15 | 16 | cin >> character; 17 | 18 | for (int i = size; i > position; i--) { 19 | arr[i] = arr[i - 1]; 20 | } 21 | 22 | arr[position] = character; 23 | 24 | cout << "Updated array after insertion:"; 25 | for (int i = 0; i <= size; i++) { 26 | cout << " " << arr[i]; 27 | } 28 | cout << endl; 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /DPP/Unit 1/policeman.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | 9 | int *arr = new int[n]; 10 | 11 | for (int i = 0; i < n; ++i) { 12 | cin >> arr[i]; 13 | } 14 | 15 | int item; 16 | cin >> item; 17 | 18 | bool found = false; 19 | 20 | for (int i = 0; i < n; ++i) { 21 | if (arr[i] == item) { 22 | found = true; 23 | break; 24 | } 25 | } 26 | 27 | if (found) { 28 | cout << "Item found" << endl; 29 | } else { 30 | cout << "Item not found" << endl; 31 | } 32 | 33 | delete[] arr; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /DPP/Unit 1/Pooja.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | int n, m; 7 | std::cin >> n; 8 | 9 | std::vector arr1(n); 10 | for (int i = 0; i < n; ++i) { 11 | std::cin >> arr1[i]; 12 | } 13 | 14 | std::cin >> m; 15 | std::vector arr2(m); 16 | for (int i = 0; i < m; ++i) { 17 | std::cin >> arr2[i]; 18 | } 19 | 20 | std::vector merged(arr1.begin(), arr1.end()); 21 | merged.insert(merged.end(), arr2.begin(), arr2.end()); 22 | 23 | int maxScore = *std::max_element(merged.begin(), merged.end()); 24 | std::cout << maxScore << std::endl; 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /DPP/Unit 4/Imagine various algorithm problems.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | bool isValidInorder(int arr[], int n) { 7 | int minVal = INT_MIN; 8 | for (int i = 0; i < n; i++) { 9 | if (arr[i] < minVal) { 10 | return false; 11 | } 12 | minVal = arr[i]; 13 | } 14 | return true; 15 | } 16 | 17 | int main() { 18 | int n; 19 | scanf("%d", &n); 20 | int arr[n]; 21 | for (int i = 0; i < n; i++) { 22 | scanf("%d", &arr[i]); 23 | } 24 | if (isValidInorder(arr, n)) { 25 | printf("Yes\n"); 26 | } else { 27 | printf("No\n"); 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /DPP/Unit 1/Large Online Retail Platform.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, target; 5 | scanf("%d", &n); 6 | 7 | int arr[n]; 8 | for (int i = 0; i < n; i++) { 9 | scanf("%d", &arr[i]); 10 | } 11 | 12 | scanf("%d", &target); 13 | 14 | int index = -1; 15 | for (int i = 0; i < n; i++) { 16 | if (arr[i] == target) { 17 | index = i; 18 | break; 19 | } 20 | } 21 | 22 | if (index != -1) { 23 | printf("Element %d is present at index %d\n", target, index); 24 | } else { 25 | printf("Element %d is not present\n", target); 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /DPP/Unit 4/Money Heist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int MAX_N = 20; 4 | 5 | int maximumLoot(int hval[], int n) { 6 | if (n < 0) { 7 | return 0; 8 | } else if (n == 0) { 9 | return hval[0]; 10 | } else { 11 | int robCurrentHouse = hval[n] + maximumLoot(hval, n - 2); 12 | int skipCurrentHouse = maximumLoot(hval, n - 1); 13 | return max(robCurrentHouse, skipCurrentHouse); 14 | } 15 | } 16 | 17 | int main() { 18 | int n; 19 | cin >> n; 20 | int hval[MAX_N]; 21 | for (int i = 0; i < n; i++) { 22 | cin >> hval[i]; 23 | } 24 | cout << maximumLoot(hval, n - 1); 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /DPP/Unit 1/Text_Editor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int size, position; 7 | char character; 8 | 9 | cin >> size; 10 | 11 | char arr[100]; 12 | for (int i =0; i> arr[i]; 15 | } 16 | 17 | cin >> position; 18 | cin >> character; 19 | for (int i = size; i >position; i--) 20 | { 21 | arr[i] = arr[i-1]; 22 | } 23 | 24 | arr[position] = character; 25 | cout<<"Updated array after insertion:"; 26 | for(int i=0; i<=size; i++) 27 | { 28 | cout << " "<< arr[i]; 29 | } 30 | 31 | cout < 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int k, n; 9 | cin >> k >> n; 10 | 11 | vector> queues(k); 12 | 13 | for (int i = 0; i < n; i++) { 14 | int item, qn; 15 | cin >> item >> qn; 16 | queues[qn].push(item); 17 | } 18 | 19 | for (int i = 0; i < k; i++) { 20 | cout << "Queue " << i << ": "; 21 | while (!queues[i].empty()) { 22 | cout << queues[i].front() << " "; 23 | queues[i].pop(); 24 | } 25 | cout << endl; 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /DPP/Unit 3/reverse a string using a queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | string reverseString(const string& input) { 8 | stack charStack; 9 | for (char ch : input) { 10 | charStack.push(ch); 11 | } 12 | 13 | string reversed; 14 | while (!charStack.empty()) { 15 | reversed += charStack.top(); 16 | charStack.pop(); 17 | } 18 | 19 | return reversed; 20 | } 21 | 22 | int main() { 23 | string input; 24 | getline(cin, input); 25 | 26 | string reversed = reverseString(input); 27 | 28 | cout << "Reversed string: " << reversed << endl; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /DPP/Unit 1/Alex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int n, m; 9 | cin >> n; 10 | unordered_map inventory; 11 | 12 | for (int i = 0; i < n; ++i) { 13 | int item; 14 | cin >> item; 15 | inventory[item]++; 16 | } 17 | 18 | cin >> m; 19 | 20 | for (int i = 0; i < m; ++i) { 21 | int item; 22 | cin >> item; 23 | inventory[item]++; 24 | } 25 | 26 | int totalItems = 0; 27 | for (const auto& item : inventory) { 28 | totalItems += item.second; 29 | } 30 | 31 | cout << totalItems; 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /DPP/Unit 1/stock Trading Application.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | int n; 7 | std::cin >> n; 8 | 9 | std::vector prices(n); 10 | std::vector thresholds(n); 11 | 12 | for (int i = 0; i < n; i++) { 13 | std::cin >> prices[i]; 14 | } 15 | 16 | for (int i = 0; i < n; i++) { 17 | std::cin >> thresholds[i]; 18 | } 19 | 20 | int count = 0; 21 | 22 | for (int i = 0; i < n - 1; i++) { 23 | if (std::abs(prices[i + 1] - prices[i]) >= thresholds[i]) { 24 | count++; 25 | } 26 | } 27 | 28 | std::cout << count << std::endl; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /DPP/Unit 1/Mountaineer FindPeakElement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int findPeakElement(int arr[], int n) { 4 | int left = 0, right = n - 1; 5 | while (left < right) { 6 | int mid = left + (right - left) / 2; 7 | if (arr[mid] < arr[mid + 1]) { 8 | left = mid + 1; 9 | } else { 10 | right = mid; 11 | } 12 | } 13 | return arr[left]; 14 | } 15 | 16 | int main() { 17 | int n; 18 | scanf("%d", &n); 19 | 20 | int arr[n]; 21 | for (int i = 0; i < n; i++) { 22 | scanf("%d", &arr[i]); 23 | } 24 | 25 | int peak = findPeakElement(arr, n); 26 | printf("%d\n", peak); 27 | 28 | return 0; 29 | } 30 | 31 | 32 | //sushant 33 | -------------------------------------------------------------------------------- /DPP/Unit 1/robotics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int findMinimum(int array[], int size) { 4 | int left = 0; 5 | int right = size - 1; 6 | 7 | while (left < right) { 8 | int mid = left + (right - left) / 2; 9 | 10 | if (array[mid] > array[right]) { 11 | left = mid + 1; 12 | } else { 13 | right = mid; 14 | } 15 | } 16 | 17 | return array[left]; 18 | } 19 | 20 | int main() { 21 | int n; 22 | scanf("%d", &n); 23 | int array[n]; 24 | 25 | for (int i = 0; i < n; i++) { 26 | scanf("%d", &array[i]); 27 | } 28 | 29 | int minimum = findMinimum(array, n); 30 | printf("%d\n", minimum); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /DPP/Unit 1/Element_Frequency.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int size; 7 | cin >> size; 8 | int elements[size]; 9 | const int MAX_ELEMENTS = 1000; 10 | int frequency[MAX_ELEMENTS] = {0}; 11 | 12 | for(int i = 0; i < size; i++) { 13 | cin >> elements[i]; 14 | frequency[elements[i]]++; 15 | } 16 | 17 | cout << "Element Frequency" << endl; 18 | 19 | for(int i = 0; i < size; i++) { 20 | if(frequency[elements[i]] > 0) { 21 | cout << left << setw(10) << elements[i] << setw(10) << frequency[elements[i]] << endl; 22 | frequency[elements[i]] = 0; 23 | } 24 | } 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /DPP/Unit 3/manages a deque 12_95-9_6_0 reversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void reverseDeque(int* dq, int n) { 5 | int left = 0, right = n - 1; 6 | while (left < right) { 7 | int temp = dq[left]; 8 | dq[left] = dq[right]; 9 | dq[right] = temp; 10 | left++; 11 | right--; 12 | } 13 | } 14 | 15 | int main() { 16 | int n; 17 | cin >> n; 18 | 19 | int* myDeque = new int[n]; 20 | for (int i = 0; i < n; i++) { 21 | cin >> myDeque[i]; 22 | } 23 | 24 | reverseDeque(myDeque, n); 25 | 26 | for (int i = 0; i < n; i++) { 27 | cout << myDeque[i] << " "; 28 | } 29 | // cout << endl; 30 | 31 | delete[] myDeque; 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /DPP/Unit 1/Geetha.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int m, n; 5 | scanf("%d %d", &m, &n); 6 | int array[m][n]; 7 | 8 | for (int i = 0; i < m; i++) { 9 | for (int j = 0; j < n; j++) { 10 | scanf("%d", &array[i][j]); 11 | } 12 | } 13 | 14 | int target; 15 | scanf("%d", &target); 16 | 17 | int found = 0; 18 | for (int i = 0; i < m; i++) { 19 | for (int j = 0; j < n; j++) { 20 | if (array[i][j] == target) { 21 | printf("[%d][%d]\n", i, j); 22 | found = 1; 23 | } 24 | } 25 | } 26 | 27 | if (!found) { 28 | printf("Element %d not found.\n", target); 29 | } 30 | 31 | return 0; 32 | } 33 | //Sushant 34 | -------------------------------------------------------------------------------- /DPP/Unit 3/Michael.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | std::deque stockPrices; 6 | 7 | int price; 8 | while (true) { 9 | std::cin >> price; 10 | if (price == -1) { 11 | break; 12 | } 13 | stockPrices.push_back(price); 14 | } 15 | 16 | if (stockPrices.empty()) { 17 | std::cout << "No stock prices provided." << std::endl; 18 | } else { 19 | int minPrice = stockPrices.front(); 20 | for (int price : stockPrices) { 21 | if (price < minPrice) { 22 | minPrice = price; 23 | } 24 | } 25 | std::cout << "The minimum element in the deque is: " << minPrice << std::endl; 26 | } 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /DPP/Unit 1/Rotate the array to right.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | const int MAX_SIZE = 100; 6 | int size, positions; 7 | 8 | cin >> size; 9 | int arr[MAX_SIZE]; 10 | for (int i = 0; i < size; i++) { 11 | cin >> arr[i]; 12 | } 13 | cin >> positions; 14 | 15 | int rotatedArr[MAX_SIZE]; 16 | for (int i = 0; i < size; i++) { 17 | rotatedArr[(i + positions) % size] = arr[i]; 18 | } 19 | 20 | cout << "Original array:"; 21 | for (int i = 0; i < size; i++) { 22 | cout << " " << arr[i]; 23 | } 24 | cout << endl; 25 | 26 | cout << "Updated array:"; 27 | for (int i = 0; i < size; i++) { 28 | cout << " " << rotatedArr[i]; 29 | } 30 | cout << endl; 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /DPP/Unit 1/Delete an element from array.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | #include 3 | 4 | int main() { 5 | const int MAX_SIZE = 100; 6 | int n; 7 | std::cin >> n; 8 | int arr[MAX_SIZE]; 9 | for (int i = 0; i < n; ++i) { 10 | std::cin >> arr[i];} 11 | int position; 12 | std::cin >> position; 13 | std::cout << "Original array:"; 14 | for(int i = 0; i< n; ++i){ 15 | std::cout<< " "<< arr[i];} 16 | std::cout < n) { 18 | std::cout << "Invalid position!" << std::endl; 19 | } else { 20 | for (int i = position -1; i < n -1; ++i) { 21 | arr[i] = arr[i+1]; 22 | } 23 | n--; 24 | std::cout << "Updated array:"; 25 | for (int i = 0; i < n; ++i) { 26 | std::cout << " " < 2 | #include 3 | int main() 4 | { 5 | const int MAX_SIZE = 100; 6 | int n; 7 | std::cin>>n; 8 | std::string strings[MAX_SIZE]; 9 | for(int i = 0; i < n; ++i) 10 | { 11 | std:: cin>> strings[i]; 12 | } 13 | 14 | std::string searchString; 15 | std::cin>> searchString; 16 | int foundIndex = -1; 17 | for(int i = 0; i < n; ++i) 18 | { 19 | if (strings[i] == searchString) 20 | { 21 | foundIndex = i; 22 | break; 23 | } 24 | } 25 | 26 | if (foundIndex != -1) 27 | { 28 | std::cout< 2 | #include 3 | 4 | const int MAX_SIZE = 100; 5 | 6 | int main() { 7 | int size; 8 | std::string array[MAX_SIZE]; 9 | std::string searchStr; 10 | 11 | 12 | std::cin >> size; 13 | for (int i = 0; i < size; ++i) { 14 | std::cin >> array[i]; 15 | } 16 | std::cin.ignore(); 17 | std::getline(std::cin, searchStr); 18 | 19 | int foundIndex = -1; 20 | for (int i = 0; i < size; ++i) { 21 | if (array[i] == searchStr) { 22 | foundIndex = i; 23 | break; 24 | } 25 | } 26 | 27 | if (foundIndex != -1) { 28 | std::cout << foundIndex << std::endl; 29 | } else { 30 | std::cout << "String not found!" << std::endl; 31 | } 32 | 33 | return 0; 34 | } 35 | //Sushant 36 | -------------------------------------------------------------------------------- /DPP/Unit 1/Insert_Beignning_shift.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | const int MAX_SIZE = 100; 5 | int n; 6 | std::cin >> n; 7 | 8 | int arr[MAX_SIZE]; 9 | for (int i = 0; i < n; ++i) { 10 | std::cin >> arr[i]; 11 | } 12 | 13 | int newValue; 14 | std::cin >> newValue; 15 | 16 | std::cout << "Original array:"; 17 | for (int i = 0; i < n; ++i) { 18 | std::cout << " " << arr[i]; 19 | } 20 | std::cout << std::endl; 21 | 22 | for (int i = n - 1; i >= 0; --i) { 23 | arr[i + 1] = arr[i]; 24 | } 25 | arr[0] = newValue; 26 | 27 | std::cout << "Updated array:"; 28 | for (int i = 0; i <= n; ++i) { 29 | std::cout << " " << arr[i]; 30 | } 31 | std::cout << std::endl; 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /DPP/Unit 1/Mathematical Application.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | int n, m; 7 | std::cin >> n; 8 | 9 | std::vector arr1(n); 10 | for (int i = 0; i < n; ++i) { 11 | std::cin >> arr1[i]; 12 | } 13 | 14 | std::cin >> m; 15 | 16 | std::vector arr2(m); 17 | for (int i = 0; i < m; ++i) { 18 | std::cin >> arr2[i]; 19 | } 20 | 21 | std::vector mergedArray; 22 | mergedArray.reserve(n + m); 23 | mergedArray.insert(mergedArray.end(), arr1.begin(), arr1.end()); 24 | mergedArray.insert(mergedArray.end(), arr2.begin(), arr2.end()); 25 | 26 | int minIngredient = *std::min_element(mergedArray.begin(), mergedArray.end()); 27 | 28 | std::cout << minIngredient << std::endl; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /DPP/Unit 1/Mathematical Application.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | int n, m; 7 | std::cin >> n; 8 | 9 | std::vector arr1(n); 10 | for (int i = 0; i < n; ++i) { 11 | std::cin >> arr1[i]; 12 | } 13 | 14 | std::cin >> m; 15 | 16 | std::vector arr2(m); 17 | for (int i = 0; i < m; ++i) { 18 | std::cin >> arr2[i]; 19 | } 20 | 21 | std::vector mergedArray; 22 | mergedArray.reserve(n + m); 23 | mergedArray.insert(mergedArray.end(), arr1.begin(), arr1.end()); 24 | mergedArray.insert(mergedArray.end(), arr2.begin(), arr2.end()); 25 | 26 | int minIngredient = *std::min_element(mergedArray.begin(), mergedArray.end()); 27 | 28 | std::cout << minIngredient << std::endl; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /DPP/Unit 1/merge them into a single array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | int main() 6 | { int numArrays; 7 | cin >> numArrays; 8 | 9 | int mergedArray[10000]; 10 | int mergedIndex = 0; 11 | 12 | for (int i = 0; i < numArrays; ++i) 13 | { 14 | int arraySize; 15 | cin >> arraySize; 16 | 17 | for (int j = 0; j> element; 21 | mergedArray[mergedIndex] = element; 22 | mergedIndex++; 23 | } 24 | } 25 | 26 | cout <<"Merged array: "; 27 | for(int k = 0; k < mergedIndex; ++k) 28 | { 29 | cout< 2 | #include 3 | 4 | int main() 5 | { 6 | int n; 7 | std::cin >> n; 8 | 9 | std::vector arr(n); 10 | 11 | for (int i = 0; i < n; i++) 12 | { 13 | std::cin >> arr[i]; 14 | } 15 | 16 | std::vector modifiedArr(n); 17 | 18 | for (int i = 0; i < n; i++) 19 | { 20 | if (i == 0) 21 | { 22 | modifiedArr[i] = arr[i] * arr[i + 1]; 23 | } 24 | else if (i == n - 1) 25 | { 26 | modifiedArr[i] = arr[i] * arr[i - 1]; 27 | } 28 | else 29 | { 30 | modifiedArr[i] = arr[i - 1] * arr[i + 1]; 31 | } 32 | } 33 | 34 | for (int i = 0; i < n; i++) 35 | { 36 | std::cout << modifiedArr[i] << " "; 37 | } 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /DPP/Unit 1/Merges two sorted arrays student IDs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int n, m; 8 | cin >> n; 9 | vector arr1(n); 10 | for (int i = 0; i < n; ++i) { 11 | cin >> arr1[i]; 12 | } 13 | 14 | cin >> m; 15 | vector arr2(m); 16 | for (int i = 0; i < m; ++i) { 17 | cin >> arr2[i]; 18 | } 19 | 20 | vector merged; 21 | 22 | for (int i = 0; i < m; ++i) { 23 | merged.push_back(arr2[i]); 24 | } 25 | 26 | for (int i = 0; i < n; ++i) { 27 | merged.push_back(arr1[i]); 28 | } 29 | 30 | for (int k = 0; k < merged.size(); ++k) { 31 | cout << merged[k]; 32 | if (k != merged.size() - 1) { 33 | cout << " "; 34 | } 35 | } 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /DPP/Unit 1/e-Commerec system.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main () 6 | { 7 | int size; 8 | vectorproductIDs; 9 | cin>> size; 10 | for (int i =0; i> productID; 14 | productIDs.push_back(productID); 15 | } 16 | 17 | vector updatedProductIDs; 18 | for (int i = 0; i 2 | #include 3 | 4 | int main() { 5 | const int MAX_SIZE = 100; 6 | int n; 7 | scanf("%d", &n); 8 | char strings[MAX_SIZE][101]; 9 | for (int i = 0; i < n; ++i) { 10 | scanf("%s", strings[i]); 11 | } 12 | for (int i = 0; i < n; ++i) { 13 | if (strings[i][0] != '\0') { 14 | for (int j = i + 1; j < n; ++j) { 15 | if (strcmp(strings[i], strings[j]) == 0) { 16 | strings[j][0] = '\0'; 17 | } 18 | } 19 | } 20 | } 21 | printf("Updated array:"); 22 | for (int i = 0; i < n; ++i) { 23 | if (strings[i][0] != '\0') { 24 | printf(" %s", strings[i]); 25 | } 26 | } 27 | printf("\n"); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /DPP/Unit 1/Rajesh.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, m; 5 | scanf("%d", &n); 6 | int arr1[n]; 7 | for (int i = 0; i < n; ++i) { 8 | scanf("%d", &arr1[i]); 9 | } 10 | scanf("%d", &m); 11 | int arr2[m]; 12 | for (int i = 0; i < m; ++i) { 13 | scanf("%d", &arr2[i]); 14 | } 15 | int max_size = (n > m) ? n : m; 16 | int merged[max_size]; 17 | for (int i = 0; i < max_size; ++i) { 18 | int num1 = (i < n) ? arr1[i] : 0; 19 | int num2 = (i < m) ? arr2[i] : 0; 20 | merged[i] = num1 + num2; 21 | } 22 | for (int i = max_size - 1; i >= 0; --i) { 23 | printf("%d", merged[i]); 24 | if (i > 0) { 25 | printf(" "); 26 | } 27 | } 28 | printf("\n"); 29 | return 0; 30 | } 31 | 32 | //sushant 33 | -------------------------------------------------------------------------------- /Academics/Arrays/Linear_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int linearSearch(int array[], int size, int value) { 4 | for (int i = 0; i < size; i++) { 5 | if (array[i] == value) { 6 | return i; 7 | } 8 | } 9 | return -1; 10 | } 11 | 12 | int main() { 13 | 14 | int x; 15 | 16 | int array[] = {12, 45, 67, 23, 89, 34, 56}; 17 | int arraySize = sizeof(array) / sizeof(array[0]); 18 | 19 | cout<<"searching value: "<>x; 21 | int searchValue = x; 22 | 23 | int result = linearSearch(array, arraySize, searchValue); 24 | 25 | if (result != -1) { 26 | std::cout << "Value " << searchValue << " found at index " << result << std::endl; 27 | } else { 28 | std::cout << "Value " << searchValue << " not found" << std::endl; 29 | } 30 | 31 | return 0; 32 | } 33 | //sushant -------------------------------------------------------------------------------- /DPP/Unit 1/Merging K Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main() 5 | { 6 | int K; 7 | std::cin >> K; 8 | 9 | std::vector>arrays(K); 10 | for(int i = 0; i < K; ++i) 11 | { 12 | int n; 13 | std::cin>>n; 14 | arrays[i].resize(n); 15 | 16 | for (int j = 0; j < n; ++j) 17 | { 18 | std::cin>> arrays[i][j]; 19 | } 20 | } 21 | 22 | std::vector mergedArray; 23 | for(const std::vector& arr : arrays) 24 | { 25 | mergedArray.insert(mergedArray.end(), arr.begin(), arr.end()); 26 | } 27 | 28 | std::sort(mergedArray.begin(), mergedArray.end(), std::greater()); 29 | for(int num : mergedArray) 30 | { 31 | std::cout << num << " "; 32 | } 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /DPP/Unit 4/numbered disks(1 to n) pegs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int countMoves(int n, char source, char auxiliary, char destination) { 4 | if (n == 1) { 5 | printf("Move disk 1 from %c to %c\n", source, destination); 6 | return 1; 7 | } 8 | 9 | int moves = 0; 10 | moves += countMoves(n - 1, source, destination, auxiliary); 11 | printf("Move disk %d from %c to %c\n", n, source, destination); 12 | moves += countMoves(n - 1, auxiliary, source, destination); 13 | 14 | return moves + 1; 15 | } 16 | 17 | int main() { 18 | int n; 19 | scanf("%d", &n); 20 | char source = 'A'; 21 | char auxiliary = 'B'; 22 | char destination = 'C'; 23 | 24 | int totalMoves = countMoves(n, source, auxiliary, destination); 25 | 26 | printf("Total number of moves: %d\n", totalMoves); 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /DPP/Unit 1/Skilled Software Engineer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int MAX_SIZE = 100; 4 | 5 | void filterNegativePrices(double prices[], int size) { 6 | int positiveSize = 0; 7 | double positivePrices[MAX_SIZE]; 8 | 9 | for (int i = 0; i < size; i++) { 10 | if (prices[i] >= 0) { 11 | positivePrices[positiveSize++] = prices[i]; 12 | } 13 | } 14 | 15 | std::cout << "Updated Array with Positive Closing Prices: "; 16 | for (int i = 0; i < positiveSize; i++) { 17 | std::cout << positivePrices[i] << " "; 18 | } 19 | std::cout << std::endl; 20 | } 21 | 22 | int main() { 23 | double prices[MAX_SIZE]; 24 | int size; 25 | 26 | std::cin >> size; 27 | 28 | for (int i = 0; i < size; i++) { 29 | std::cin >> prices[i]; 30 | } 31 | 32 | filterNegativePrices(prices, size); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Academics/Arrays/help.c++: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int size; 7 | cout << "Enter the size of the array: "; 8 | cin >> size; 9 | 10 | int array[size]; 11 | cout << "Enter elements of the array:" << endl; 12 | for (int i = 0; i < size; i++) { 13 | cin >> array[i]; 14 | } 15 | 16 | unordered_map frequencyMap; 17 | for (int i = 0; i < size; i++) { 18 | if (frequencyMap.find(array[i]) != frequencyMap.end()) { 19 | frequencyMap[array[i]]++; 20 | } else { 21 | frequencyMap[array[i]] = 1; 22 | } 23 | } 24 | 25 | cout << "Element : Frequency" << endl; 26 | for (auto it = frequencyMap.begin(); it != frequencyMap.end(); it++) { 27 | cout << it->first << " : " << it->second << endl; 28 | } 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /DPP/Unit 4/mystical kingdom.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int tower_of_hanoi(int n, int source_peg, int destination_peg, int helper_peg) { 6 | if (n == 1) { 7 | return 1; 8 | } 9 | 10 | int moves = tower_of_hanoi(n - 1, source_peg, helper_peg, destination_peg); 11 | 12 | moves += 1; 13 | 14 | moves += tower_of_hanoi(n - 1, helper_peg, destination_peg, source_peg); 15 | 16 | return moves; 17 | } 18 | 19 | int main() { 20 | int n; 21 | cout << "Enter the number of disks: "; 22 | cin >> n; 23 | 24 | if (n < 1 || n > 8) { 25 | cout << "The number of disks must be between 1 and 8." << endl; 26 | return 1; 27 | } 28 | 29 | int minimum_moves = tower_of_hanoi(n, 0, 2, 1); 30 | 31 | cout << "The minimum number of moves required to solve the Tower of Hanoi problem for " << n << " magical disks is " << minimum_moves << endl; 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /DPP/Unit 1/Permutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void nextPermutation(std::vector& nums) { 6 | int n = nums.size(); 7 | int i = n - 2; 8 | while (i >= 0 && nums[i] >= nums[i + 1]) { 9 | i--;} 10 | if (i >= 0) { 11 | int j = n - 1; 12 | while (j >= 0 && nums[j] <= nums[i]) { 13 | j--; 14 | } 15 | std::swap(nums[i], nums[j]); 16 | } 17 | std::reverse(nums.begin() + i + 1, nums.end()); 18 | } 19 | int main() { 20 | int n; 21 | std::cin >> n; 22 | std::vector nums(n); 23 | for (int i = 0; i < n; ++i) { 24 | std::cin >> nums[i]; 25 | } 26 | nextPermutation(nums); 27 | for (int i = 0; i < n; ++i) { 28 | std::cout << nums[i]; 29 | if (i < n - 1) { 30 | std::cout << " "; 31 | } 32 | } 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /DPP/Unit 3/computing system URGENCY.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Task { 7 | int priority; 8 | int deadline; 9 | string taskName; 10 | 11 | bool operator<(const Task& other) const { 12 | if (priority != other.priority) { 13 | return priority > other.priority; 14 | } else { 15 | return deadline < other.deadline; 16 | } 17 | } 18 | }; 19 | 20 | int main() { 21 | int N; 22 | cin >> N; 23 | vector tasks(N); 24 | for (int i = 0; i < N; i++) { 25 | cin >> tasks[i].priority >> tasks[i].deadline >> tasks[i].taskName; 26 | } 27 | 28 | sort(tasks.begin(), tasks.end()); 29 | 30 | cout << "Executed Tasks:" << endl; 31 | for (const auto& task : tasks) { 32 | cout << task.taskName << endl; 33 | } 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Academics/Arrays/Array_basic.c: -------------------------------------------------------------------------------- 1 | // # include 2 | // int main 3 | // () 4 | // { 5 | // int a[20], i, b; 6 | // scanf("%d",&b); 7 | // printf("Enter the element of array: "); 8 | // for(i = 0; i<=b; i++) 9 | // { 10 | // scanf("%d",&a[i]); 11 | // } 12 | // } 13 | 14 | 15 | 16 | 17 | #include 18 | 19 | int main() 20 | { 21 | int a[20], i, b; 22 | printf("Enter the number of elements in the array: "); 23 | scanf("%d", &b); 24 | 25 | if (b > 20) { 26 | printf("Array size exceeds the limit of 20.\n"); 27 | return 1; 28 | } 29 | 30 | printf("Enter the elements of the array: "); 31 | for (i = 0; i < b; i++) 32 | { 33 | scanf("%d", &a[i]); 34 | } 35 | 36 | printf("Array elements:\n"); 37 | for (i = 0; i < b; i++) 38 | { 39 | printf("%d ", a[i]); 40 | } 41 | 42 | return 0; 43 | } 44 | 45 | //Sushant -------------------------------------------------------------------------------- /DPP/Unit 1/Sports analytics system.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void selectionSort(int arr[], int n) { 4 | for (int i = 0; i < n - 1; i++) { 5 | int maxIndex = i; 6 | for (int j = i + 1; j < n; j++) { 7 | if (arr[j] > arr[maxIndex]) { 8 | maxIndex = j; 9 | } 10 | } 11 | 12 | int temp = arr[i]; 13 | arr[i] = arr[maxIndex]; 14 | arr[maxIndex] = temp; 15 | } 16 | } 17 | 18 | int main() { 19 | int n; 20 | scanf("%d", &n); 21 | int heights[n]; 22 | 23 | for (int i = 0; i < n; i++) { 24 | scanf("%d", &heights[i]); 25 | } 26 | 27 | selectionSort(heights, n); 28 | 29 | printf("Sorted array:"); 30 | for (int i = 0; i < n; i++) { 31 | printf(" %d", heights[i]); 32 | } 33 | printf("\n"); 34 | 35 | printf("The second largest element is %d\n", heights[1]); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /DPP/Unit 1/Single Array Sorted Alphabetically.cpS: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int size1, size2; 10 | cin>> size1; 11 | vector arr1(size1); 12 | for (int i = 0; i < size1; ++i) 13 | { 14 | cin >> arr1[i]; 15 | } 16 | 17 | cin >> size2; 18 | vector arr2(size2); 19 | for(int i = 0; i < size2; ++i) 20 | { 21 | cin >> arr2[i]; 22 | } 23 | 24 | vector merged(arr1.begin(), 25 | arr1.end()); 26 | 27 | merged.insert(merged.end(), arr2.begin(),arr2.end()); 28 | 29 | sort(merged.begin(), merged.end()); 30 | 31 | cout<<"Merged and sorted array: "; 32 | for(const string &element : merged) 33 | { 34 | cout < 2 | #include 3 | 4 | int binarySearch(const std::vector& playlist, int target) { 5 | int left = 0; 6 | int right = playlist.size() - 1; 7 | 8 | while (left <= right) { 9 | int mid = left + (right - left) / 2; 10 | 11 | if (playlist[mid] == target) { 12 | return mid; 13 | } else if (playlist[mid] < target) { 14 | left = mid + 1; 15 | } else { 16 | right = mid - 1; 17 | } 18 | } 19 | 20 | return left; 21 | } 22 | 23 | int main() { 24 | int n; 25 | std::cin >> n; 26 | 27 | std::vector playlist(n); 28 | for (int i = 0; i < n; i++) { 29 | std::cin >> playlist[i]; 30 | } 31 | 32 | int target; 33 | std::cin >> target; 34 | 35 | int index = binarySearch(playlist, target); 36 | std::cout << index << std::endl; 37 | 38 | return 0; 39 | } 40 | 41 | //Sushant 42 | -------------------------------------------------------------------------------- /Self Paced/Linked List/Display.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct Node 7 | { 8 | int data; 9 | struct Node *next; 10 | }*first=NULL; 11 | 12 | 13 | void create(int A[], int n) 14 | { 15 | int i; 16 | struct Node *t,*last; 17 | first=(struct Node *)malloc(sizeof(struct Node)); 18 | first->data=A[0]; 19 | first->next=NULL; 20 | last=first; 21 | 22 | for(i=1; idata=A[i]; 26 | t->next=NULL; 27 | last->next=t; 28 | last=t; 29 | } 30 | } 31 | 32 | 33 | 34 | void Display(struct Node *p) 35 | { 36 | while (p!=NULL) 37 | { 38 | printf("%d ",p->data); 39 | p=p->next; 40 | } 41 | } 42 | 43 | 44 | 45 | int main () 46 | { 47 | int A[] = {3,5,7,10,15}; 48 | create (A,5); 49 | Display(first); 50 | 51 | return 0; 52 | 53 | } -------------------------------------------------------------------------------- /DPP/Unit 3/Olivia.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | std::deque userIds; 8 | std::unordered_set uniqueUserIds; 9 | std::vector uniqueUserIdsVector; 10 | 11 | int userId; 12 | while (true) { 13 | std::cin >> userId; 14 | if (userId == -1) { 15 | break; 16 | } 17 | userIds.push_back(userId); 18 | if (uniqueUserIds.insert(userId).second) { 19 | // If it's a new unique user ID, add it to the vector 20 | uniqueUserIdsVector.push_back(userId); 21 | } 22 | } 23 | 24 | if (userIds.empty()) { 25 | std::cout << "" << std::endl; 26 | } else { 27 | std::cout << ""; 28 | for (int userId : uniqueUserIdsVector) { 29 | std::cout << " " << userId; 30 | } 31 | std::cout << std::endl; 32 | } 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /DPP/Unit 3/Nayana.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printNGE(int arr[], int n) 5 | { 6 | int nextGreater[n]; 7 | 8 | for (int i = 0; i < n; i++) 9 | { 10 | nextGreater[i] = -1; 11 | } 12 | 13 | int stack[n]; 14 | int top = -1; 15 | 16 | for (int i = 0; i < n; i++) 17 | { 18 | int next = arr[i]; 19 | 20 | 21 | while (top >= 0 && arr[stack[top]] < next) 22 | { 23 | nextGreater[stack[top]] = next; 24 | top--; 25 | } 26 | 27 | stack[++top] = i; 28 | } 29 | 30 | for (int i = 0; i < n; i++) 31 | { 32 | cout << arr[i] << " " << ((nextGreater[i] != -1) ? nextGreater[i] : -1) << endl; 33 | } 34 | } 35 | 36 | int main() 37 | { 38 | int n; 39 | cin >> n; 40 | int arr[n]; 41 | for (int i = 0; i < n; i++) 42 | { 43 | cin >> arr[i]; 44 | } 45 | printNGE(arr, n); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Academics/Arrays/Traversing_Array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() { 4 | int array[100]; 5 | int i, n, x, pos; 6 | 7 | printf("Enter the number of elements in the array: "); 8 | scanf("%d", &n); 9 | printf("Enter the elements:\n"); 10 | 11 | for (i = 0; i < n; i++) { 12 | scanf("%d", &array[i]); 13 | } 14 | 15 | printf("Input array elements are:\n"); 16 | for (i = 0; i < n; i++) { 17 | printf("%d ", array[i]); 18 | } 19 | 20 | printf("\nEnter the new element to be inserted: "); 21 | scanf("%d", &x); 22 | printf("Enter the position where you want to insert the element: "); 23 | scanf("%d", &pos); 24 | 25 | 26 | for (i = n; i >= pos; i--) { 27 | array[i] = array[i - 1]; 28 | } 29 | 30 | array[pos - 1] = x; 31 | n = n + 1; 32 | 33 | printf("New array after insertion:\n"); 34 | for (i = 0; i < n; i++) { 35 | printf("%d ", array[i]); 36 | } 37 | } 38 | 39 | 40 | //Sushant -------------------------------------------------------------------------------- /DPP/Unit 1/Voting Booth.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | int n; 8 | std::cin >> n; 9 | 10 | std::vector votes(n); 11 | for (int i = 0; i < n; i++) 12 | { 13 | std::cin >> votes[i]; 14 | } 15 | 16 | std::map voteCount; 17 | for (int candidateID : votes) 18 | { 19 | voteCount[candidateID]++; 20 | } 21 | 22 | int majorityThreshold = n / 2 + 1; 23 | int majorityCandidate = -1; 24 | 25 | for (const auto &entry : voteCount) 26 | { 27 | if (entry.second >= majorityThreshold) 28 | { 29 | majorityCandidate = entry.first; 30 | break; 31 | } 32 | } 33 | 34 | if (majorityCandidate != -1) 35 | { 36 | std::cout << "The majority candidate is ID " << majorityCandidate << std::endl; 37 | } 38 | else 39 | { 40 | std::cout << "No majority candidate found" << std::endl; 41 | } 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /DPP/Unit 1/Vishwas.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void selectionSort(int arr[], int n) { 4 | for (int i = 0; i < n - 1; i++) { 5 | int maxIndex = i; 6 | for (int j = i + 1; j < n; j++) { 7 | if (arr[j] > arr[maxIndex]) { 8 | maxIndex = j; 9 | } 10 | } 11 | 12 | int temp = arr[i]; 13 | arr[i] = arr[maxIndex]; 14 | arr[maxIndex] = temp; 15 | 16 | printf("After his step: %d\n", i + 1); 17 | for (int k = 0; k < n; k++) { 18 | printf("%d ", arr[k]); 19 | } 20 | printf("\n"); 21 | } 22 | printf("Finally, he got it\n"); 23 | for (int k = 0; k < n; k++) { 24 | printf("%d ", arr[k]); 25 | } 26 | printf("\n"); 27 | } 28 | 29 | int main() { 30 | int n; 31 | scanf("%d", &n); 32 | int films[n]; 33 | 34 | for (int i = 0; i < n; i++) { 35 | scanf("%d", &films[i]); 36 | } 37 | 38 | selectionSort(films, n); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /DPP/Unit 1/Developing a sales analytics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printArray(int arr[], int n) { 5 | for (int i = 0; i < n; i++) { 6 | cout << arr[i] << " "; 7 | } 8 | cout << endl; 9 | } 10 | 11 | void insertionSort(int arr[], int n) { 12 | for (int i = 1; i < n; i++) { 13 | int key = arr[i]; 14 | int j = i - 1; 15 | 16 | cout << "After Iteration " << i << ": "; 17 | printArray(arr, n); 18 | 19 | while (j >= 0 && arr[j] < key) { 20 | arr[j + 1] = arr[j]; 21 | j--; 22 | } 23 | arr[j + 1] = key; 24 | } 25 | } 26 | 27 | int main() { 28 | int n; 29 | cin >> n; 30 | 31 | int arr[n]; 32 | for (int i = 0; i < n; i++) { 33 | cin >> arr[i]; 34 | } 35 | 36 | cout << "Initial order: "; 37 | printArray(arr, n); 38 | 39 | insertionSort(arr, n); 40 | 41 | cout << "Sorted order: "; 42 | printArray(arr, n); 43 | 44 | return 0; 45 | } 46 | //sushant 47 | -------------------------------------------------------------------------------- /DPP/Unit 1/Imagine you are developing a program.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(float *a, float *b) { 4 | float temp = *a; 5 | *a = *b; 6 | *b = temp; 7 | } 8 | 9 | void bubbleSort(float arr[], int n) { 10 | int maxIterations = 2; 11 | for (int i = 0; i < n - 1 && i < maxIterations; i++) { 12 | for (int j = 0; j < n - i - 1; j++) { 13 | if (arr[j] > arr[j + 1]) { 14 | swap(&arr[j], &arr[j + 1]); 15 | } 16 | } 17 | printf("After iteration %d:", i + 1); 18 | for (int k = 0; k < n; k++) { 19 | printf(" %.2f", arr[k]); 20 | } 21 | printf("\n"); 22 | } 23 | printf("Sorted array:"); 24 | for (int i = 0; i < n; i++) { 25 | printf(" %.2f", arr[i]); 26 | } 27 | printf("\n"); 28 | } 29 | 30 | int main() { 31 | int n; 32 | scanf("%d", &n); 33 | 34 | float arr[n]; 35 | for (int i = 0; i < n; i++) { 36 | scanf("%f", &arr[i]); 37 | } 38 | 39 | bubbleSort(arr, n); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Academics/Arrays/Binary Seach.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int binarySearch(int arr[], int size, int target) { 5 | int left = 0; 6 | int right = size - 1; 7 | 8 | while (left <= right) { 9 | int mid = left + (right - left) / 2; 10 | 11 | if (arr[mid] == target) { 12 | return mid; 13 | } 14 | else if (arr[mid] < target) { 15 | left = mid + 1; 16 | } 17 | else { 18 | right = mid - 1; 19 | } 20 | } 21 | 22 | return -1; 23 | } 24 | 25 | int main() { 26 | int x; 27 | int arr[] = {2, 4, 6, 8, 10, 12, 14, 16, 18}; 28 | int size = sizeof(arr) / sizeof(arr[0]); 29 | cout << "Enter the searching value: "<< endl; 30 | cin >> x; 31 | int target = x; 32 | 33 | int result = binarySearch(arr, size, target); 34 | 35 | if (result != -1) { 36 | cout << "Target found at index " << result << endl; 37 | } else { 38 | cout << "Target not found" << endl; 39 | } 40 | 41 | return 0; 42 | } 43 | 44 | //Sushant -------------------------------------------------------------------------------- /DPP/Unit 1/Software Developer Online Retail.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | int size1, size2; 7 | std::cin >> size1; 8 | std::vector stock(size1); 9 | for (int i = 0; i < size1; ++i) { 10 | std::cin >> stock[i]; 11 | } 12 | std::cin >> size2; 13 | std::vector cart(size2); 14 | for (int i = 0; i < size2; ++i) { 15 | std::cin >> cart[i]; 16 | } 17 | std::vector merged(size1 + size2); 18 | std::merge(stock.begin(), stock.end(), 19 | cart.begin(), cart.end(), merged.begin()); 20 | 21 | merged.erase(std::unique(merged.begin(), 22 | merged.end()), merged.end()); 23 | 24 | for (int i = 0; i < merged.size(); ++i) { 25 | std::cout << merged[i]; 26 | if (i < merged.size() - 1) { 27 | std::cout << " "; 28 | } 29 | 30 | } 31 | std::cout << std::endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /DPP/Unit 3/double-ended queue 1 2 3 4 5 .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int n; 6 | std::cin >> n; 7 | std::deque dq; 8 | 9 | for (int i = 0; i < n; i++) { 10 | int element; 11 | std::cin >> element; 12 | dq.push_front(element); 13 | } 14 | 15 | int num_deletions; 16 | std::cin >> num_deletions; 17 | 18 | 19 | num_deletions = std::min(num_deletions, static_cast(dq.size())); 20 | 21 | if (dq.empty()) { 22 | std::cout << "The front element is -1." << std::endl; 23 | } else { 24 | std::cout << "The front element is " << dq.front() << "." << std::endl; 25 | } 26 | 27 | while (num_deletions > 0 && !dq.empty()) { 28 | dq.pop_front(); 29 | num_deletions--; 30 | } 31 | 32 | if (dq.empty()) { 33 | std::cout << "After deletion, the front element becomes -1." << std::endl; 34 | } else { 35 | std::cout << "After deletion, the front element becomes " << dq.front() << "." << std::endl; 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /DPP/Unit 1/Insert an element at a specific position in an array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int size; 6 | std::cin >> size; 7 | std::vector arr(size); 8 | for (int i = 0; i < size; i++) { 9 | std::cin >> arr[i];} 10 | int position, value; 11 | std::cin >> position >> value; 12 | if (position >= 1 && position <= size + 1) { 13 | std::cout << "Original array:"; 14 | for (int i = 0; i < size; i++) { 15 | std::cout << " " << arr[i];} 16 | std::cout << std::endl; 17 | arr.insert(arr.begin() + position - 1, value); 18 | std::cout << "Updated array:"; 19 | for (int i = 0; i < arr.size(); i++) { 20 | std::cout << " " << arr[i];} 21 | std::cout << std::endl;} 22 | else { 23 | std::cout << "Original array:"; 24 | for (int i = 0; i < size; i++) { 25 | std::cout << " " << arr[i];} 26 | std::cout << std::endl; 27 | std::cout << "Invalid position!" << std::endl;} 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /DPP/Unit 1/Stock trading platform.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void insertionSort(float arr[], int n) { 4 | for (int i = 1; i < n; i++) { 5 | float key = arr[i]; 6 | int j = i - 1; 7 | 8 | while (j >= 0 && arr[j] > key) { 9 | arr[j + 1] = arr[j]; 10 | j--; 11 | } 12 | 13 | arr[j + 1] = key; 14 | 15 | printf("After Iteration %d:", i); 16 | for (int k = 0; k < n; k++) { 17 | printf(" %.2f", arr[k]); 18 | } 19 | printf("\n"); 20 | } 21 | } 22 | 23 | int main() { 24 | int n; 25 | scanf("%d", &n); 26 | float prices[n]; 27 | 28 | for (int i = 0; i < n; i++) { 29 | scanf("%f", &prices[i]); 30 | } 31 | 32 | printf("Initial order:"); 33 | for (int i = 0; i < n; i++) { 34 | printf(" %.2f", prices[i]); 35 | } 36 | printf("\n"); 37 | 38 | insertionSort(prices, n); 39 | 40 | printf("Sorted order:"); 41 | for (int i = 0; i < n; i++) { 42 | printf(" %.2f", prices[i]); 43 | } 44 | printf("\n"); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /DPP/Unit 1/Sarah.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | int n, m; 10 | cin >> n; 11 | unordered_map freqMap; 12 | vector result; 13 | 14 | for (int i = 0; i < n; ++i) { 15 | int num; 16 | cin >> num; 17 | freqMap[num]++; 18 | } 19 | 20 | cin >> m; 21 | for (int i = 0; i < m; ++i) { 22 | int num; 23 | cin >> num; 24 | freqMap[num]++; 25 | } 26 | 27 | int maxFrequency = 0; 28 | for (const auto& entry : freqMap) { 29 | if (entry.second > maxFrequency) { 30 | maxFrequency = entry.second; 31 | result.clear(); 32 | result.push_back(entry.first); 33 | } else if (entry.second == maxFrequency) { 34 | result.push_back(entry.first); 35 | } 36 | } 37 | 38 | sort(result.begin(), result.end()); 39 | 40 | for (int num : result) { 41 | cout << num << " "; 42 | } 43 | 44 | return 0; 45 | } 46 | 47 | 48 | //Sushant 49 | -------------------------------------------------------------------------------- /DPP/Unit 1/Developing merges two tudentIDs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int n, m; 8 | cin >> n; 9 | vector arr1(n); 10 | for (int i = 0; i < n; ++i) { 11 | cin >> arr1[i]; 12 | } 13 | 14 | cin >> m; 15 | vector arr2(m); 16 | for (int i = 0; i < m; ++i) { 17 | cin >> arr2[i]; 18 | } 19 | 20 | vector merged; 21 | int i = 0, j = 0; 22 | 23 | while (i < n && j < m) { 24 | if (arr1[i] < arr2[j]) { 25 | merged.push_back(arr1[i]); 26 | i++; 27 | } else { 28 | merged.push_back(arr2[j]); 29 | j++; 30 | } 31 | } 32 | 33 | while (i < n) { 34 | merged.push_back(arr1[i]); 35 | i++; 36 | } 37 | 38 | while (j < m) { 39 | merged.push_back(arr2[j]); 40 | j++; 41 | } 42 | 43 | for (int k = 0; k < merged.size(); ++k) { 44 | cout << merged[k]; 45 | if (k != merged.size() - 1) { 46 | cout << " "; 47 | } 48 | } 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /DPP/Unit 1/Delete an element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int n; 6 | std::cin >> n; 7 | std::vector arr(n); 8 | 9 | for (int i = 0; i < n; i++) { 10 | std::cin >> arr[i]; 11 | } 12 | 13 | int position; 14 | std::cin >> position; 15 | 16 | if (position >= 1 && position <= n) { 17 | std::cout << "Original array: "; 18 | for (int i = 0; i < n; i++) { 19 | std::cout << arr[i] << " "; 20 | } 21 | std::cout << std::endl; 22 | 23 | arr.erase(arr.begin() + position - 1); 24 | 25 | std::cout << "Updated array: "; 26 | for (int i = 0; i < arr.size(); i++) { 27 | std::cout << arr[i] << " "; 28 | } 29 | std::cout << std::endl; 30 | } else { 31 | std::cout << "Original array: "; 32 | for (int i = 0; i < n; i++) { 33 | std::cout << arr[i] << " "; 34 | } 35 | std::cout << std::endl; 36 | 37 | std::cout << "Invalid position!" << std::endl; 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /DPP/Unit 2/contact management system in front.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | struct Node { 6 | int data; 7 | struct Node* next; 8 | }; 9 | 10 | struct Node* createNode(int data) { 11 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 12 | newNode->data = data; 13 | newNode->next = NULL; 14 | return newNode; 15 | } 16 | 17 | void insertAtBeginning(struct Node* header, int data) { 18 | struct Node* newNode = createNode(data); 19 | newNode->next = header->next; 20 | header->next = newNode; 21 | } 22 | 23 | void displayList(struct Node* header) { 24 | struct Node* current = header->next; 25 | while (current != NULL) { 26 | printf("%d ", current->data); 27 | current = current->next; 28 | } 29 | printf("\n"); 30 | } 31 | 32 | int main() { 33 | struct Node* header = createNode(-1); 34 | int n, data; 35 | 36 | scanf("%d", &n); 37 | while (n != -1) { 38 | insertAtBeginning(header, n); 39 | scanf("%d", &n); 40 | } 41 | 42 | displayList(header); 43 | 44 | return 0; 45 | } 46 | 47 | 48 | //Sushant -------------------------------------------------------------------------------- /DPP/Unit 3/implement dequeue operation list MAX = 25.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 25 3 | 4 | int queue[MAX]; 5 | int rear = -1; 6 | int front = -1; 7 | 8 | void Enqueue(int data) { 9 | if (rear == MAX - 1) 10 | std::cout << "Overflow" << std::endl; 11 | else { 12 | if (front == -1) 13 | front = 0; 14 | rear = rear + 1; 15 | queue[rear] = data; 16 | } 17 | } 18 | 19 | void Dequeue() { 20 | if (front == -1 || front > rear) { 21 | std::cout << "Underflow" << std::endl; 22 | return; 23 | } else { 24 | front = front + 1; 25 | } 26 | } 27 | 28 | void display() { 29 | if (front == -1) 30 | std::cout << "Queue is empty" << std::endl; 31 | else { 32 | for (int i = front; i <= rear; i++) 33 | std::cout << queue[i] << " "; 34 | std::cout << std::endl; 35 | } 36 | } 37 | 38 | int main() { 39 | int n, i, e; 40 | std::cin >> n; 41 | for (i = 0; i < n; i++) { 42 | std::cin >> e; 43 | Enqueue(e); 44 | } 45 | Dequeue(); 46 | display(); 47 | 48 | return 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Self Paced/Stack_Data/Stack_Linkedlist/Stack_Linkedlist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Node 4 | { 5 | int data; 6 | struct Node *next; 7 | } *top = NULL; 8 | void push(int x) 9 | { 10 | struct Node *t; 11 | t = (struct Node *)malloc(sizeof(struct Node)); 12 | 13 | if (t == NULL) 14 | printf("stack is full\n"); 15 | else 16 | { 17 | t->data = x; 18 | t->next = top; 19 | top = t; 20 | } 21 | } 22 | int pop() 23 | { 24 | struct Node *t; 25 | int x = -1; 26 | 27 | if (top == NULL) 28 | printf("Stack is Empty\n"); 29 | else 30 | { 31 | t = top; 32 | top = top->next; 33 | x = t->data; 34 | free(t); 35 | } 36 | return x; 37 | } 38 | void Display() 39 | { 40 | struct Node *p; 41 | p = top; 42 | while (p != NULL) 43 | { 44 | printf("%d ", p->data); 45 | p = p->next; 46 | } 47 | printf("\n"); 48 | } 49 | int main() 50 | { 51 | push(10); 52 | push(20); 53 | push(30); 54 | 55 | Display(); 56 | 57 | printf("%d ", pop()); 58 | 59 | return 0; 60 | } -------------------------------------------------------------------------------- /DPP/Unit 1/commonElements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int size1, size2; 9 | cin >> size1; 10 | 11 | unordered_set set1; 12 | 13 | for (int i = 0; i> num; 17 | set1.insert(num); 18 | 19 | } 20 | cin >> size2; 21 | 22 | vector commonElements; 23 | 24 | for (int i = 0; i> num; 28 | if (set1.find(num) != set1.end()) 29 | { 30 | commonElements.push_back(num); 31 | set1.erase(num); 32 | } 33 | } 34 | 35 | sort(commonElements.begin(), 36 | commonElements.end()); 37 | 38 | 39 | cout << "Common Elements: "; 40 | if (commonElements.empty()){ 41 | cout << "None"; 42 | 43 | }else 44 | { 45 | for (int num : commonElements) 46 | { 47 | cout << num << " "; 48 | } 49 | } 50 | cout << endl; 51 | return 0; 52 | } -------------------------------------------------------------------------------- /DPP/Unit 1/insert specfic position.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int size; 6 | std::cin >> size; 7 | 8 | std::vector arr(size); 9 | 10 | for (int i = 0; i < size; i++) { 11 | std::cin >> arr[i]; 12 | } 13 | 14 | int position, value; 15 | std::cin >> position >> value; 16 | 17 | if (position >= 1 && position <= size + 1) { 18 | arr.insert(arr.begin() + position - 1, value); 19 | 20 | std::cout << "Original array:"; 21 | for (int i = 0; i < size; i++) { 22 | std::cout << " " << arr[i]; 23 | } 24 | std::cout << std::endl; 25 | 26 | std::cout << "Updated array:"; 27 | for (int i = 0; i < arr.size(); i++) { 28 | std::cout << " " << arr[i]; 29 | } 30 | std::cout << std::endl; 31 | } else { 32 | std::cout << "Original array:"; 33 | for (int i = 0; i < size; i++) { 34 | std::cout << " " << arr[i]; 35 | } 36 | std::cout << std::endl; 37 | 38 | std::cout << "Invalid position!" << std::endl; 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /DPP/Unit 1/2D array binary Search: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | bool compare(const std::vector& a, const std::vector& b) { 6 | return a[0] < b[0] || (a[0] == b[0] && a[1] > b[1]); 7 | } 8 | 9 | int maxEnvelopes(std::vector>& envelopes) { 10 | int n = envelopes.size(); 11 | if (n == 0) return 0; 12 | 13 | std::sort(envelopes.begin(), envelopes.end(), compare); 14 | 15 | std::vector dp(n, 1); 16 | 17 | for (int i = 1; i < n; i++) { 18 | for (int j = 0; j < i; j++) { 19 | if (envelopes[i][1] > envelopes[j][1]) { 20 | dp[i] = std::max(dp[i], dp[j] + 1); 21 | } 22 | } 23 | } 24 | 25 | return *std::max_element(dp.begin(), dp.end()); 26 | } 27 | 28 | int main() { 29 | int n; 30 | std::cin >> n; 31 | 32 | std::vector> envelopes(n, std::vector(2)); 33 | for (int i = 0; i < n; i++) { 34 | std::cin >> envelopes[i][0] >> envelopes[i][1]; 35 | } 36 | 37 | int result = maxEnvelopes(envelopes); 38 | std::cout << result << std::endl; 39 | 40 | return 0; 41 | } 42 | //Sushant 43 | -------------------------------------------------------------------------------- /DPP/Unit 2/Alice.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Define a struct for a node in the linked list 5 | struct Node { 6 | int data; 7 | Node* next; 8 | Node(int value) : data(value), next(nullptr) {} 9 | }; 10 | 11 | // Function to insert a new node at the front of the linked list 12 | void insertAtFront(Node*& head, int value) { 13 | Node* newNode = new Node(value); 14 | newNode->next = head; 15 | head = newNode; 16 | } 17 | 18 | // Function to display the linked list 19 | void displayList(Node* head) { 20 | while (head) { 21 | cout << head->data << " "; 22 | head = head->next; 23 | } 24 | } 25 | 26 | int main() { 27 | int n; 28 | cin >> n; 29 | 30 | Node* head = nullptr; 31 | 32 | for (int i = 0; i < n; ++i) { 33 | int value; 34 | cin >> value; 35 | insertAtFront(head, value); 36 | } 37 | 38 | cout << "Created Linked list: "; 39 | displayList(head); 40 | 41 | int new_element; 42 | cin >> new_element; 43 | insertAtFront(head, new_element); 44 | 45 | cout << endl << "Final List: "; 46 | displayList(head); 47 | 48 | return 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /DPP/Unit 2/Developing Contact Managaement system front of the contact list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | }; 8 | 9 | struct Node* createNode(int data) { 10 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 11 | newNode->data = data; 12 | newNode->next = NULL; 13 | return newNode; 14 | } 15 | 16 | void insertAtBeginning(struct Node* header, int data) { 17 | struct Node* newNode = createNode(data); 18 | newNode->next = header->next; 19 | header->next = newNode; 20 | } 21 | 22 | void displayList(struct Node* header) { 23 | struct Node* current = header->next; 24 | while (current != NULL) { 25 | printf("%d ", current->data); 26 | current = current->next; 27 | } 28 | printf("\n"); 29 | } 30 | 31 | int main() { 32 | struct Node* header = createNode(-1); 33 | int n, data; 34 | 35 | scanf("%d", &n); 36 | while (n != -1) { 37 | insertAtBeginning(header, n); 38 | scanf("%d", &n); 39 | } 40 | 41 | 42 | displayList(header); 43 | 44 | return 0; 45 | } 46 | 47 | 48 | 49 | 50 | 51 | 52 | //Sushant -------------------------------------------------------------------------------- /DPP/Unit 3/Jack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void enqueue(int arr[], int& rear, int element) { 5 | arr[rear++] = element; 6 | } 7 | 8 | void dequeue(int arr[], int& front, int rear) { 9 | if (front == rear) { 10 | cout << "Queue underflow" << endl; 11 | return; 12 | } 13 | 14 | int maxIndex = front; 15 | for (int i = front + 1; i < rear; i++) { 16 | if (arr[i] > arr[maxIndex]) { 17 | maxIndex = i; 18 | } 19 | } 20 | 21 | cout << arr[maxIndex] << " "; 22 | arr[maxIndex] = arr[front]; 23 | front++; 24 | } 25 | 26 | void sortArray(int arr[], int size) { 27 | int front = 0, rear = 0; 28 | 29 | for (int i = 0; i < size; i+=2) { 30 | enqueue(arr, rear, arr[i]); 31 | } 32 | 33 | for (int i = 0; i < size; i+=2) { 34 | dequeue(arr, front, rear); 35 | } 36 | } 37 | 38 | int main() { 39 | int size; 40 | cin >> size; 41 | 42 | int arr[size]; 43 | for (int i = 0; i < size; i++) { 44 | cin >> arr[i]; 45 | } 46 | 47 | cout << "Sorted elements in dequeue format: "; 48 | sortArray(arr, size); 49 | cout << endl; 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /DPP/Unit 4/Sarah 52 57.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Definition for a binary tree node. 5 | struct TreeNode { 6 | int val; 7 | TreeNode* left; 8 | TreeNode* right; 9 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | }; 11 | 12 | // Function to insert a node into the binary search tree 13 | TreeNode* insert(TreeNode* root, int val) { 14 | if (root == NULL) { 15 | return new TreeNode(val); 16 | } 17 | if (val < root->val) { 18 | root->left = insert(root->left, val); 19 | } else { 20 | root->right = insert(root->right, val); 21 | } 22 | return root; 23 | } 24 | 25 | // Function to perform in-order traversal and print the values 26 | void inOrderTraversal(TreeNode* root) { 27 | if (root == NULL) { 28 | return; 29 | } 30 | inOrderTraversal(root->left); 31 | cout << root->val << " "; 32 | inOrderTraversal(root->right); 33 | } 34 | 35 | int main() { 36 | int val; 37 | TreeNode* root = NULL; 38 | 39 | while (cin >> val && val >= 1) { 40 | root = insert(root, val); 41 | } 42 | 43 | inOrderTraversal(root); 44 | cout << endl; 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /DPP/Unit 1/Mathematical Research Project.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_SIZE = 100; 5 | 6 | void findCommonElements(int arr1[], int size1, int arr2[], int size2) { 7 | int common[MAX_SIZE]; 8 | int commonSize = 0; 9 | 10 | for (int i = 0; i < size1; i++) { 11 | for (int j = 0; j < size2; j++) { 12 | if (arr1[i] == arr2[j]) { 13 | common[commonSize++] = arr1[i]; 14 | break; 15 | } 16 | } 17 | } 18 | 19 | cout << "Common Elements: "; 20 | if (commonSize == 0) { 21 | cout << "None"; 22 | } else { 23 | for (int i = 0; i < commonSize; i++) { 24 | cout << common[i] << " "; 25 | } 26 | } 27 | cout << endl; 28 | } 29 | 30 | int main() { 31 | int arr1[MAX_SIZE]; 32 | int arr2[MAX_SIZE]; 33 | int size1, size2; 34 | 35 | cin >> size1; 36 | 37 | for (int i = 0; i < size1; i++) { 38 | cin >> arr1[i]; 39 | } 40 | 41 | cin >> size2; 42 | 43 | for (int i = 0; i < size2; i++) { 44 | cin >> arr2[i]; 45 | } 46 | 47 | findCommonElements(arr1, size1, arr2, size2); 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /DPP/Unit 4/Developing a software module BST.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | struct TreeNode { 7 | int val; 8 | struct TreeNode* left; 9 | struct TreeNode* right; 10 | }; 11 | 12 | int isBST(struct TreeNode* root, long long minVal, long long maxVal) { 13 | if (root == NULL) { 14 | return 1; 15 | } 16 | 17 | if (root->val <= minVal || root->val >= maxVal) { 18 | return 0; 19 | } 20 | 21 | return isBST(root->left, minVal, root->val) && isBST(root->right, root->val, maxVal); 22 | } 23 | 24 | struct TreeNode* buildTree() { 25 | int val; 26 | scanf("%d", &val); 27 | if (val == -1) { 28 | return NULL; 29 | } 30 | struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); 31 | root->val = val; 32 | root->left = buildTree(); 33 | root->right = buildTree(); 34 | return root; 35 | } 36 | 37 | int main() { 38 | struct TreeNode* root = buildTree(); 39 | if (isBST(root, LLONG_MIN, LLONG_MAX)) { 40 | printf("The given binary tree is a BST\n"); 41 | } else { 42 | printf("The given binary tree is not a BST\n"); 43 | } 44 | return 0; 45 | } -------------------------------------------------------------------------------- /DPP/Unit 1/management system.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void insertionSort(char contacts[][50], int n) { 5 | for (int i = 1; i < n; i++) { 6 | char key[50]; 7 | strcpy(key, contacts[i]); 8 | int j = i - 1; 9 | 10 | while (j >= 0 && strcmp(contacts[j], key) > 0) { 11 | strcpy(contacts[j + 1], contacts[j]); 12 | j--; 13 | } 14 | 15 | strcpy(contacts[j + 1], key); 16 | 17 | printf("After Iteration %d:", i); 18 | for (int k = 0; k < n; k++) { 19 | printf(" %s", contacts[k]); 20 | } 21 | printf("\n"); 22 | } 23 | } 24 | 25 | int main() { 26 | int n; 27 | scanf("%d", &n); 28 | char contacts[n][50]; 29 | 30 | for (int i = 0; i < n; i++) { 31 | scanf("%s", contacts[i]); 32 | } 33 | 34 | printf("Initial order:"); 35 | for (int i = 0; i < n; i++) { 36 | printf(" %s", contacts[i]); 37 | } 38 | printf("\n"); 39 | 40 | insertionSort(contacts, n); 41 | 42 | printf("Sorted order:"); 43 | for (int i = 0; i < n; i++) { 44 | printf(" %s", contacts[i]); 45 | } 46 | printf("\n"); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /DPP/Unit 4/you are working asa programmer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // Function to partition the array of scores for Quick Sort 7 | int partition(vector& arr, int low, int high) { 8 | int pivot = arr[high]; 9 | int i = low - 1; 10 | 11 | for (int j = low; j <= high - 1; j++) { 12 | if (arr[j] >= pivot) { 13 | i++; 14 | swap(arr[i], arr[j]); 15 | } 16 | } 17 | 18 | swap(arr[i + 1], arr[high]); 19 | return i + 1; 20 | } 21 | 22 | // Function to perform Quick Sort on scores in descending order 23 | void quickSort(vector& arr, int low, int high) { 24 | if (low < high) { 25 | int pi = partition(arr, low, high); 26 | 27 | quickSort(arr, low, pi - 1); 28 | quickSort(arr, pi + 1, high); 29 | } 30 | } 31 | 32 | int main() { 33 | int n; 34 | cin >> n; 35 | 36 | vector scores(n); 37 | 38 | for (int i = 0; i < n; i++) { 39 | cin >> scores[i]; 40 | } 41 | 42 | quickSort(scores, 0, n - 1); 43 | 44 | for (int i = 0; i < n; i++) { 45 | cout << scores[i] << " "; 46 | } 47 | 48 | cout << endl; 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /DPP/Unit 3/binary sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Node { 4 | std::string data; 5 | Node* next; 6 | Node(const std::string& value) : data(value), next(nullptr) {} 7 | }; 8 | 9 | struct Queue { 10 | Node* front; 11 | Node* rear; 12 | Queue() : front(nullptr), rear(nullptr) {} 13 | 14 | bool isEmpty() { 15 | return front == nullptr; 16 | } 17 | 18 | void enqueue(const std::string& item) { 19 | Node* newNode = new Node(item); 20 | 21 | if (rear == nullptr) { 22 | front = rear = newNode; 23 | } else { 24 | rear->next = newNode; 25 | rear = newNode; 26 | } 27 | } 28 | }; 29 | 30 | void generatePrintBinary(int n) { 31 | Queue queue; 32 | queue.enqueue("1"); 33 | 34 | Node* current = queue.front; 35 | 36 | while (n--) { 37 | std::string s1 = current->data; 38 | std::cout << s1 << " "; 39 | 40 | std::string s2 = s1; 41 | 42 | queue.enqueue(s1 + "0"); 43 | queue.enqueue(s2 + "1"); 44 | 45 | current = current->next; 46 | } 47 | } 48 | 49 | int main() { 50 | int n; 51 | std::cin >> n; 52 | generatePrintBinary(n); 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /DPP/Unit 4/Tou've received secrect message.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // Function to partition the array for Quick Sort 7 | int partition(vector& arr, int low, int high) { 8 | char pivot = arr[high]; 9 | int i = low - 1; 10 | 11 | for (int j = low; j <= high - 1; j++) { 12 | if (arr[j] < pivot) { 13 | i++; 14 | swap(arr[i], arr[j]); 15 | } 16 | } 17 | 18 | swap(arr[i + 1], arr[high]); 19 | return i + 1; 20 | } 21 | 22 | // Function to perform Quick Sort on characters in ascending order 23 | void quickSort(vector& arr, int low, int high) { 24 | if (low < high) { 25 | int pi = partition(arr, low, high); 26 | 27 | quickSort(arr, low, pi - 1); 28 | quickSort(arr, pi + 1, high); 29 | } 30 | } 31 | 32 | int main() { 33 | int n; 34 | cin >> n; 35 | 36 | vector characters(n); 37 | 38 | for (int i = 0; i < n; i++) { 39 | cin >> characters[i]; 40 | } 41 | 42 | quickSort(characters, 0, n - 1); 43 | 44 | for (int i = 0; i < n; i++) { 45 | cout << characters[i] << " "; 46 | } 47 | 48 | cout << endl; 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /DPP/Unit 1/university.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int n, m; 8 | cin >> n; 9 | vector arr1(n); 10 | for (int i = 0; i < n; ++i) { 11 | cin >> arr1[i]; 12 | } 13 | 14 | cin >> m; 15 | vector arr2(m); 16 | for (int i = 0; i < m; ++i) { 17 | cin >> arr2[i]; 18 | } 19 | 20 | vector merged; 21 | int i = 0, j = 0; 22 | 23 | while (i < n && j < m) { 24 | if (arr1[i] < arr2[j]) { 25 | merged.push_back(arr1[i]); 26 | i++; 27 | } else if (arr1[i] > arr2[j]) { 28 | merged.push_back(arr2[j]); 29 | j++; 30 | } else { 31 | merged.push_back(arr1[i]); 32 | i++; 33 | j++; 34 | } 35 | } 36 | 37 | while (i < n) { 38 | merged.push_back(arr1[i]); 39 | i++; 40 | } 41 | 42 | while (j < m) { 43 | merged.push_back(arr2[j]); 44 | j++; 45 | } 46 | 47 | for (int k = 0; k < merged.size(); ++k) { 48 | cout << merged[k]; 49 | if (k != merged.size() - 1) { 50 | cout << " "; 51 | } 52 | } 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /DPP/Unit 1/Student grades for different subjects.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | double calculate_average_grade(int numSubjects, int* grades) 6 | { 7 | int total_grade = 0; 8 | for (int i = 0; i(total_grade) / numSubjects; 14 | return round(average_grade * 10) / 10; 15 | } 16 | 17 | int main () 18 | { 19 | int numStudents, numSubjects; 20 | std::cin>> numStudents >> numSubjects; 21 | double* average_grades = new double[numStudents]; 22 | 23 | for(int i = 0; i < numStudents; ++i) 24 | { int* student_grades = new int[numSubjects]; 25 | for(int j = 0; j < numSubjects; ++j){ 26 | std::cin >> student_grades[j]; 27 | } 28 | average_grades[i] = calculate_average_grade(numSubjects, student_grades); 29 | delete[] student_grades; 30 | } 31 | 32 | std::cout<<"Average grades:"< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int size1; 7 | cin >> size1; 8 | 9 | string arr1[size1]; 10 | for (int i = 0; i < size1; i++) { 11 | cin >> arr1[i]; 12 | } 13 | 14 | int size2; 15 | cin >> size2; 16 | 17 | string arr2[size2]; 18 | 19 | for (int i = 0; i < size2; i++) { 20 | cin >> arr2[i]; 21 | } 22 | 23 | string mergedArr[size1 * size2]; 24 | int count = 0; 25 | for (int i = 0; i < size1; i++) { 26 | for (int j = 0; j < size2; j++) { 27 | string combination = arr1[i] + arr2[j]; 28 | bool isUnique = true; 29 | for (int k = 0; k < count; k++) { 30 | if (mergedArr[k] == combination) { 31 | isUnique = false; 32 | break; 33 | } 34 | } 35 | if (isUnique) { 36 | mergedArr[count] = combination; 37 | count++; 38 | } 39 | } 40 | } 41 | 42 | for (int i = 0; i < count; i++) { 43 | cout << mergedArr[i] << " "; 44 | } 45 | cout << endl; 46 | 47 | return 0; 48 | } 49 | 50 | //Sushant 51 | -------------------------------------------------------------------------------- /DPP/Unit 1/Text analysis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | bool isWordPresent(const char* sentence, const char* targetWord) { 6 | int sentenceLength = strlen(sentence); 7 | int targetLength = strlen(targetWord); 8 | 9 | for (int i = 0; i <= sentenceLength - targetLength; i++) { 10 | bool found = true; 11 | for (int j = 0; j < targetLength; j++) { 12 | if (sentence[i + j] != targetWord[j]) { 13 | found = false; 14 | break; 15 | } 16 | } 17 | if (found) { 18 | return true; 19 | } 20 | } 21 | 22 | return false; 23 | } 24 | 25 | int main() { 26 | char sentence[1000]; 27 | char targetWord[100]; 28 | 29 | fgets(sentence, sizeof(sentence), stdin); 30 | fgets(targetWord, sizeof(targetWord), stdin); 31 | 32 | sentence[strcspn(sentence, "\n")] = '\0'; 33 | targetWord[strcspn(targetWord, "\n")] = '\0'; 34 | 35 | if (isWordPresent(sentence, targetWord)) { 36 | printf("The word '%s' is present in the given sentence.\n", targetWord); 37 | } else { 38 | printf("The word '%s' is not present in the given sentence.\n", targetWord); 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /DPP/Unit 2/Implement operates on a circular.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | #include 3 | using namespace std; 4 | 5 | struct Node { 6 | int data; 7 | Node* next; 8 | }; 9 | 10 | void insertEnd(Node*& head, int value) { 11 | Node* newNode = new Node; 12 | newNode->data = value; 13 | newNode->next = head; 14 | 15 | if (head == nullptr) { 16 | newNode->next = newNode; 17 | head = newNode; 18 | } else { 19 | Node* temp = head; 20 | while (temp->next != head) { 21 | temp = temp->next; 22 | } 23 | temp->next = newNode; 24 | } 25 | } 26 | 27 | void displayList(Node* head) { 28 | if (head == nullptr) { 29 | cout << "Linked List is empty." << endl; 30 | return; 31 | } 32 | 33 | Node* current = head; 34 | do { 35 | cout << current->data << " "; 36 | current = current->next; 37 | } while (current != head); 38 | 39 | cout << endl; 40 | } 41 | 42 | int main() { 43 | Node* head = nullptr; 44 | int n, value; 45 | cin >> n; 46 | for (int i = 0; i < n; i++) { 47 | cin >> value; 48 | insertEnd(head, value); 49 | } 50 | 51 | displayList(head); 52 | 53 | return 0; 54 | } 55 | 56 | 57 | //sushant 58 | 59 | -------------------------------------------------------------------------------- /DPP/Unit 2/Library.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | #include 3 | #include 4 | 5 | struct Node { 6 | char data; 7 | struct Node* next; 8 | }; 9 | 10 | struct Node* insertEnd(struct Node* head, char data) { 11 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 12 | newNode->data = data; 13 | newNode->next = newNode; 14 | 15 | if (head == NULL) { 16 | return newNode; 17 | } 18 | 19 | struct Node* temp = head; 20 | while (temp->next != head) { 21 | temp = temp->next; 22 | } 23 | 24 | temp->next = newNode; 25 | newNode->next = head; 26 | 27 | return head; 28 | } 29 | 30 | int main() { 31 | int n; 32 | scanf("%d", &n); 33 | struct Node* head = NULL; 34 | 35 | for (int i = 0; i < n; i++) { 36 | char data; 37 | scanf(" %c", &data); 38 | head = insertEnd(head, data); 39 | } 40 | 41 | if (head == NULL) { 42 | printf("Linked List is empty.\n"); 43 | } else { 44 | struct Node* current = head; 45 | do { 46 | printf("%c ", current->data); 47 | current = current->next; 48 | } while (current != head); 49 | printf("\n"); 50 | } 51 | 52 | return 0; 53 | } 54 | 55 | //Susahnt -------------------------------------------------------------------------------- /DPP/Unit 1/University Wizard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | int n, m; 8 | cin >> n; 9 | vector arr1(n); 10 | for (int i = 0; i < n; ++i) { 11 | cin >> arr1[i]; 12 | } 13 | 14 | cin >> m; 15 | vector arr2(m); 16 | for (int i = 0; i < m; ++i) { 17 | cin >> arr2[i]; 18 | } 19 | 20 | vector merged; 21 | int i = 0, j = 0; 22 | 23 | while (i < n && j < m) { 24 | if (arr1[i] < arr2[j]) { 25 | merged.push_back(arr1[i]); 26 | i++; 27 | } else if (arr1[i] > arr2[j]) { 28 | merged.push_back(arr2[j]); 29 | j++; 30 | } else { 31 | merged.push_back(arr1[i]); 32 | i++; 33 | j++; 34 | } 35 | } 36 | 37 | while (i < n) { 38 | merged.push_back(arr1[i]); 39 | i++; 40 | } 41 | 42 | while (j < m) { 43 | merged.push_back(arr2[j]); 44 | j++; 45 | } 46 | 47 | cout << merged[0]; 48 | 49 | for (int k = 1; k < merged.size(); ++k) { 50 | if (merged[k] != merged[k - 1]) { 51 | cout << " " << merged[k]; 52 | } 53 | } 54 | 55 | return 0; 56 | } 57 | 58 | //Sushant 59 | -------------------------------------------------------------------------------- /Self Paced/Stack_Data/Stack_Linkedlist/Stack_class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node *next; 9 | }; 10 | 11 | 12 | class stack 13 | { 14 | private: 15 | Node *top; 16 | public: 17 | stack(){top=NULL;} 18 | void push(int x); 19 | int pop(); 20 | void Display(); 21 | }; 22 | 23 | void stack::push(int x) 24 | { 25 | Node *t=new Node; 26 | if(t==NULL) 27 | cout<<"Stack is full\n"; 28 | else 29 | { 30 | t->data=x; 31 | t->next=top; 32 | top=t; 33 | } 34 | 35 | } 36 | 37 | int stack::pop() 38 | { 39 | 40 | int x=-1; 41 | if(top==NULL) 42 | cout<<"Stack is Empty \n"; 43 | else 44 | { 45 | x=top->data; 46 | Node *t=top; 47 | top=top->next; 48 | delete t; 49 | } 50 | return x; 51 | } 52 | 53 | void stack::Display() 54 | { 55 | Node *p=top; 56 | while(p!=NULL) 57 | { 58 | cout<data<<" "; 59 | p=p->next; 60 | } 61 | cout< 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | Node* next; 7 | Node* prev; 8 | }; 9 | 10 | struct Deque { 11 | Node* front; 12 | Node* rear; 13 | int size; 14 | }; 15 | 16 | void initDeque(Deque* deque) { 17 | deque->front = NULL; 18 | deque->rear = NULL; 19 | deque->size = 0; 20 | } 21 | 22 | bool isEmpty(Deque* deque) { 23 | return deque->size == 0; 24 | } 25 | 26 | void pushBack(Deque* deque, int data) { 27 | Node* newNode = (Node*)malloc(sizeof(Node)); 28 | newNode->data = data; 29 | newNode->next = NULL; 30 | newNode->prev = deque->rear; 31 | 32 | if (isEmpty(deque)) { 33 | deque->front = newNode; 34 | } else { 35 | deque->rear->next = newNode; 36 | } 37 | 38 | deque->rear = newNode; 39 | deque->size++; 40 | } 41 | 42 | int getSize(Deque* deque) { 43 | return deque->size; 44 | } 45 | 46 | int main() { 47 | Deque myDeque; 48 | initDeque(&myDeque); 49 | int num; 50 | while (1) { 51 | std::cin >> num; 52 | if (num == -1) 53 | break; 54 | pushBack(&myDeque, num); 55 | } 56 | int count = getSize(&myDeque); 57 | std::cout << "Number of elements in the deque: " << count << std::endl; 58 | return 0; 59 | } -------------------------------------------------------------------------------- /DPP/Unit 4/working with software developer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Function to partition the array of words for Quick Sort 8 | int partition(vector& arr, int low, int high) { 9 | string pivot = arr[high]; 10 | int i = low - 1; 11 | 12 | for (int j = low; j <= high - 1; j++) { 13 | if (arr[j] >= pivot) { 14 | i++; 15 | swap(arr[i], arr[j]); 16 | } 17 | } 18 | 19 | swap(arr[i + 1], arr[high]); 20 | return i + 1; 21 | } 22 | 23 | // Function to perform Quick Sort on words in reverse lexicographical order 24 | void quickSort(vector& arr, int low, int high) { 25 | if (low < high) { 26 | int pi = partition(arr, low, high); 27 | 28 | quickSort(arr, low, pi - 1); 29 | quickSort(arr, pi + 1, high); 30 | } 31 | } 32 | 33 | int main() { 34 | int n; 35 | cin >> n; 36 | cin.ignore(); // Ignore the newline character 37 | 38 | vector words(n); 39 | 40 | for (int i = 0; i < n; i++) { 41 | cin >> words[i]; 42 | } 43 | 44 | quickSort(words, 0, n - 1); 45 | 46 | for (int i = 0; i < n; i++) { 47 | cout << words[i] << " "; 48 | } 49 | 50 | cout << endl; 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /DPP/Unit 3/Lala.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 15 3 | using namespace std; 4 | 5 | int top = -1; 6 | int a[MAX]; 7 | 8 | bool push(int x) { 9 | if (top >= (MAX - 1)) { 10 | cout << "Stack Overflow" << endl; 11 | return false; 12 | } else { 13 | a[++top] = x; 14 | return true; 15 | } 16 | } 17 | 18 | bool pop(int &x) { 19 | if (top < 0) { 20 | return false; 21 | } else { 22 | x = a[top--]; 23 | return true; 24 | } 25 | } 26 | 27 | void display() { 28 | for (int i = 0; i <= top; i++) { 29 | cout << a[i] << " "; 30 | } 31 | } 32 | 33 | bool isEmpty() { 34 | return (top < 0); 35 | } 36 | 37 | int main() { 38 | int i, n; 39 | cin >> n; 40 | if (n > MAX) { 41 | cout << "Stack Overflow" << endl; 42 | return 0; 43 | } 44 | for (i = 0; i < n; i++) { 45 | int val; 46 | cin >> val; 47 | if (!push(val)) { 48 | return 1; // Exit with an error code if there is a stack overflow 49 | } 50 | } 51 | display(); 52 | 53 | int topElement; 54 | if (pop(topElement)) { 55 | cout << "\nTop element: " << topElement << endl; 56 | display(); 57 | } else { 58 | cout << "Stack is empty." << endl; 59 | } 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Academics/Mergesort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void merge(int *arr, int start, int end){ 5 | int mid=(start+end)/2; 6 | int len1=mid-start+1; 7 | int len2 = end-mid; 8 | 9 | 10 | int *first = new int[len1]; 11 | int *second = new int [len2]; 12 | 13 | // copy the ememnet from main to sub array 14 | 15 | for (int i = 0; i < len1; i++){ 16 | first[i] = arr[start + i];} 17 | for (int i = 0; i < len2; i++){ 18 | second[i] = arr[mid + 1 + i];} 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | } 27 | 28 | void mergesort(int *arr, int start, int end) 29 | { 30 | if (start>=end) 31 | { 32 | return; // if one lelement is there 33 | 34 | } 35 | 36 | int mid = (start + end) / 2; 37 | mergesort(arr, start, mid); 38 | mergesort(arr, mid+1, end); 39 | 40 | //merger the sorted sub array 41 | 42 | 43 | merge(arr, start, end); 44 | 45 | } 46 | 47 | 48 | int main() 49 | { 50 | int n; 51 | cout<<"Enter the no of element for array:"; 52 | cout <<"Enter"<>arr[i]; 57 | } 58 | } 59 | 60 | 61 | mergesort(arr, 0, n-1); 62 | cout<<"Sorted Array:"; 63 | for (int i = 0; i 2 | 3 | int countLessThanOrEqualToMid(int matrix[], int rows, int cols, int mid) { 4 | int count = 0; 5 | int row = rows - 1; 6 | int col = 0; 7 | 8 | while (row >= 0 && col < cols) { 9 | if (matrix[row * cols + col] <= mid) { 10 | count += (row + 1); 11 | col++; 12 | } else { 13 | row--; 14 | } 15 | } 16 | 17 | return count; 18 | } 19 | 20 | int findMedian(int matrix[], int rows, int cols) { 21 | int low = matrix[0]; 22 | int high = matrix[rows * cols - 1]; 23 | 24 | while (low < high) { 25 | int mid = low + (high - low) / 2; 26 | int count = countLessThanOrEqualToMid(matrix, rows, cols, mid); 27 | 28 | if (count <= (rows * cols) / 2) { 29 | low = mid + 1; 30 | } else { 31 | high = mid; 32 | } 33 | } 34 | 35 | return low; 36 | } 37 | 38 | int main() { 39 | int m, n; 40 | scanf("%d %d", &m, &n); 41 | int matrix[m][n]; 42 | 43 | for (int i = 0; i < m; i++) { 44 | for (int j = 0; j < n; j++) { 45 | scanf("%d", &matrix[i][j]); 46 | } 47 | } 48 | 49 | int median = findMedian((int *)matrix, m, n); 50 | printf("%d\n", median); 51 | 52 | return 0; 53 | } 54 | //07Sushant 55 | -------------------------------------------------------------------------------- /DPP/Unit 2/Lisa.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | 3 | #include 4 | 5 | class Node { 6 | public: 7 | int data; 8 | Node* next; 9 | 10 | Node(int value) { 11 | data = value; 12 | next = nullptr; 13 | } 14 | }; 15 | 16 | Node* insertSorted(Node* head, int value) { 17 | Node* newNode = new Node(value); 18 | 19 | if (!head || value <= head->data) { 20 | newNode->next = head; 21 | return newNode; 22 | } 23 | 24 | Node* current = head; 25 | 26 | while (current->next && current->next->data < value) { 27 | current = current->next; 28 | } 29 | 30 | newNode->next = current->next; 31 | current->next = newNode; 32 | 33 | return head; 34 | } 35 | 36 | void displayList(Node* head) { 37 | Node* current = head; 38 | while (current) { 39 | std::cout << current->data << " "; 40 | current = current->next; 41 | } 42 | } 43 | 44 | int main() { 45 | int n; 46 | std::cin >> n; 47 | 48 | Node* head = nullptr; 49 | 50 | for (int i = 0; i < n; ++i) { 51 | int value; 52 | std::cin >> value; 53 | head = insertSorted(head, value); 54 | } 55 | 56 | int new_element; 57 | std::cin >> new_element; 58 | head = insertSorted(head, new_element); 59 | 60 | displayList(head); 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /DPP/Unit 2/Implementing a system.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct Node { 6 | int data; 7 | Node* prev; 8 | Node* next; 9 | }; 10 | 11 | Node* createNode(int data) { 12 | Node* newNode = new Node; 13 | newNode->data = data; 14 | newNode->prev = nullptr; 15 | newNode->next = nullptr; 16 | return newNode; 17 | } 18 | 19 | void insertScoreAtFront(Node*& head, Node*& tail, int score) { 20 | Node* newNode = createNode(score); 21 | if (head == nullptr) { 22 | head = newNode; 23 | tail = newNode; 24 | } else { 25 | newNode->next = head; 26 | head->prev = newNode; 27 | head = newNode; 28 | } 29 | } 30 | 31 | void printScores(Node* head) { 32 | while (head != nullptr) { 33 | cout << head->data << " "; 34 | head = head->next; 35 | } 36 | cout << endl; 37 | } 38 | 39 | int main() { 40 | Node* head = nullptr; 41 | Node* tail = nullptr; 42 | 43 | int score; 44 | while (cin >> score) { 45 | insertScoreAtFront(head, tail, score); 46 | cout << "Node Inserted" << endl; 47 | printScores(head); 48 | } 49 | 50 | while (head != nullptr) { 51 | Node* temp = head; 52 | head = head->next; 53 | delete temp; 54 | } 55 | 56 | return 0; 57 | } 58 | 59 | 60 | ///SuSHANT -------------------------------------------------------------------------------- /DPP/Unit 2/Library online application.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | #include 3 | #include 4 | 5 | struct Node { 6 | char data; 7 | struct Node* next; 8 | }; 9 | 10 | void insertAtEnd(struct Node** head, char data) { 11 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 12 | newNode->data = data; 13 | 14 | if (*head == NULL) { 15 | newNode->next = newNode; 16 | *head = newNode; 17 | } else { 18 | struct Node* temp = *head; 19 | while (temp->next != *head) { 20 | temp = temp->next; 21 | } 22 | temp->next = newNode; 23 | newNode->next = *head; 24 | } 25 | } 26 | 27 | int main() { 28 | struct Node* head = NULL; 29 | int n; 30 | scanf("%d", &n); 31 | 32 | if (n > 0) { 33 | for (int i = 0; i < n; i++) { 34 | char data; 35 | scanf(" %c", &data); 36 | insertAtEnd(&head, data); 37 | } 38 | } else { 39 | printf("Linked List is empty.\n"); 40 | } 41 | 42 | if (head != NULL) { 43 | struct Node* current = head; 44 | do { 45 | printf("%c ", current->data); 46 | current = current->next; 47 | } while (current != head); 48 | printf("\n"); 49 | } 50 | 51 | return 0; 52 | } 53 | 54 | 55 | 56 | // Sushant -------------------------------------------------------------------------------- /DPP/Unit 2/Jannu.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Node { 7 | string data; 8 | Node* next; 9 | Node(const string& value) : data(value), next(nullptr) {} 10 | }; 11 | 12 | void insertAtEnd(Node*& head, const string& value) { 13 | Node* newNode = new Node(value); 14 | if (!head) { 15 | head = newNode; 16 | } else { 17 | Node* current = head; 18 | while (current->next) { 19 | current = current->next; 20 | } 21 | current->next = newNode; 22 | } 23 | } 24 | 25 | void displayList(Node* head) { 26 | cout << "Linked List Contents:"; 27 | while (head) { 28 | cout << " " << head->data; 29 | head = head->next; 30 | } 31 | cout << endl; 32 | } 33 | 34 | int main() { 35 | int num_of_strings; 36 | cin >> num_of_strings; 37 | 38 | Node* head = nullptr; 39 | 40 | for (int i = 0; i < num_of_strings; ++i) { 41 | string str; 42 | cin >> str; 43 | insertAtEnd(head, str); 44 | } 45 | 46 | string new_string; 47 | cin >> new_string; 48 | insertAtEnd(head, new_string); 49 | 50 | displayList(head); 51 | 52 | while (head) { 53 | Node* temp = head; 54 | head = head->next; 55 | delete temp; 56 | } 57 | 58 | return 0; 59 | } 60 | 61 | //Sushant -------------------------------------------------------------------------------- /DPP/Unit 3/Responsibility Enqueue Task Get Rear task.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct QNode { 6 | string data; 7 | QNode* next; 8 | QNode(const string& d) 9 | { 10 | data = d; 11 | next = NULL; 12 | } 13 | }; 14 | 15 | struct Queue { 16 | QNode *front, *rear; 17 | Queue() { front = rear = NULL; } 18 | 19 | void enQueue(const string& x) 20 | { 21 | // Create a new LL node 22 | QNode* temp = new QNode(x); 23 | 24 | // If queue is empty, then 25 | // new node is front and rear both 26 | if (rear == NULL) { 27 | front = rear = temp; 28 | return; 29 | } 30 | 31 | // Add the new node at 32 | // the end of queue and change rear 33 | rear->next = temp; 34 | rear = temp; 35 | } 36 | }; 37 | 38 | int main() 39 | { 40 | Queue q; 41 | 42 | int num; 43 | cin >> num; 44 | cin.ignore(); // Ignore the newline character after reading the number 45 | 46 | for (int i = 0; i < num; i++) { 47 | string task; 48 | getline(cin, task); 49 | q.enQueue(task); 50 | } 51 | 52 | cout << "Front Task: " << ((q.front != NULL) ? (q.front)->data : "-1") << endl; 53 | cout << "Rear Task: " << ((q.rear != NULL) ? (q.rear)->data : "-1"); 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /DPP/Unit 2/Task of developing operatoes circulat header.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node* next; 8 | 9 | Node(int data) { 10 | this->data = data; 11 | next = nullptr; 12 | } 13 | }; 14 | 15 | void insertEnd(Node*& last, int data) { 16 | Node* newNode = new Node(data); 17 | if (!last) { 18 | last = newNode; 19 | last->next = last; 20 | } else { 21 | newNode->next = last->next; 22 | last->next = newNode; 23 | last = newNode; 24 | } 25 | } 26 | 27 | void printAlternateNodes(Node* last) { 28 | if (!last) { 29 | cout << "Linked List is empty." << endl; 30 | return; 31 | } 32 | 33 | Node* current = last->next; 34 | bool print = true; 35 | 36 | cout << "Alternate Nodes: "; 37 | while (current != last) { 38 | if (print) { 39 | cout << current->data << " "; 40 | } 41 | print = !print; 42 | current = current->next; 43 | } 44 | cout << current->data << endl; 45 | } 46 | 47 | int main() { 48 | int n; 49 | cin >> n; 50 | 51 | Node* last = nullptr; 52 | 53 | for (int i = 0; i < n; i++) { 54 | int data; 55 | cin >> data; 56 | insertEnd(last, data); 57 | } 58 | 59 | printAlternateNodes(last); 60 | 61 | return 0; 62 | } 63 | 64 | 65 | //Sushant -------------------------------------------------------------------------------- /DPP/Unit 2/Developing Contact Managaement system end of the contact list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | struct link { 5 | char info; 6 | struct link* next; 7 | }; 8 | 9 | struct link* start = NULL; 10 | 11 | 12 | struct link* create_header_list(char data) 13 | { 14 | struct link *new_node, *node; 15 | new_node = (struct link*)malloc(sizeof(struct link)); 16 | new_node->info = data; 17 | new_node->next = NULL; 18 | 19 | 20 | if (start == NULL) { 21 | 22 | 23 | start = (struct link*)malloc(sizeof(struct link)); 24 | start->next = new_node; 25 | } 26 | else { 27 | 28 | node = start; 29 | while (node->next != NULL) { 30 | node = node->next; 31 | } 32 | node->next = new_node; 33 | } 34 | return start; 35 | } 36 | 37 | 38 | struct link* display() 39 | { 40 | struct link* node; 41 | node = start; 42 | node = node->next; 43 | 44 | 45 | while (node != NULL) { 46 | 47 | 48 | std::cout << node->info << " "; 49 | node = node->next; 50 | } 51 | std::cout << std::endl; 52 | 53 | 54 | return start; 55 | } 56 | 57 | 58 | int main() 59 | { 60 | int numElements; 61 | std::cin >> numElements; 62 | for (int i = 0; i < numElements; i++) { 63 | char value; 64 | std::cin >> value; 65 | create_header_list(value); 66 | } 67 | display(); 68 | 69 | return 0; 70 | } 71 | 72 | 73 | 74 | 75 | 76 | 77 | //Sushant 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /DPP/Unit 2/Developing a text editor INSERTING NEW STRING.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct Node { 6 | string data; 7 | Node* next; 8 | }; 9 | 10 | void push(Node** head_ref, string new_data) { 11 | Node* new_node = new Node(); 12 | new_node->data = new_data; 13 | new_node->next = (*head_ref); 14 | (*head_ref) = new_node; 15 | } 16 | 17 | void append(Node** head_ref, string new_data) { 18 | Node* new_node = new Node(); 19 | new_node->data = new_data; 20 | Node* last = *head_ref; 21 | new_node->next = NULL; 22 | if (*head_ref == NULL) { 23 | *head_ref = new_node; 24 | return; 25 | } 26 | while (last->next != NULL) { 27 | last = last->next; 28 | } 29 | last->next = new_node; 30 | } 31 | 32 | void printList(Node* node) { 33 | while (node != NULL) { 34 | cout << " " << node->data; 35 | node = node->next; 36 | } 37 | } 38 | 39 | int main() { 40 | Node* head = NULL; 41 | int n; 42 | cin >> n; 43 | for (int i = 0; i < n; i++) { 44 | string str; 45 | cin >> str; 46 | push(&head, str); 47 | } 48 | cout << "Document:"; 49 | printList(head); 50 | string new_string; 51 | cin >> new_string; 52 | append(&head, new_string); 53 | cout << "\nUpdated Document:"; 54 | printList(head); 55 | return 0; 56 | } 57 | 58 | 59 | 60 | //Sushant -------------------------------------------------------------------------------- /DPP/Unit 4/Arun.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class TreeNode { 4 | public: 5 | int val; 6 | TreeNode* left; 7 | TreeNode* right; 8 | 9 | TreeNode(int key) : val(key), left(nullptr), right(nullptr) {} 10 | }; 11 | 12 | TreeNode* insert(TreeNode* root, int key) { 13 | if (root == nullptr) { 14 | return new TreeNode(key); 15 | } else { 16 | if (root->val < key) { 17 | root->right = insert(root->right, key); 18 | } else { 19 | root->left = insert(root->left, key); 20 | } 21 | } 22 | return root; 23 | } 24 | 25 | void search(TreeNode* root, int key) { 26 | if (root == nullptr || root->val == key) { 27 | if (root == nullptr) { 28 | std::cout << key << " is not present in the BST" << std::endl; 29 | } else { 30 | std::cout << key << " is present in the BST" << std::endl; 31 | } 32 | } else if (root->val < key) { 33 | search(root->right, key); 34 | } else { 35 | search(root->left, key); 36 | } 37 | } 38 | 39 | int main() { 40 | TreeNode* root = nullptr; 41 | int value; 42 | 43 | while (true) { 44 | std::cin >> value; 45 | if (value == -1) { 46 | break; 47 | } 48 | root = insert(root, value); 49 | } 50 | 51 | int search_value; 52 | std::cin >> search_value; 53 | search(root, search_value); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /DPP/Unit 4/Alex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void merge(int arr[], int left, int mid, int right) { 4 | int n1 = mid - left + 1; 5 | int n2 = right - mid; 6 | 7 | int L[n1], R[n2]; 8 | 9 | for (int i = 0; i < n1; i++) 10 | L[i] = arr[left + i]; 11 | for (int j = 0; j < n2; j++) 12 | R[j] = arr[mid + 1 + j]; 13 | 14 | int i = 0, j = 0, k = left; 15 | 16 | while (i < n1 && (L[i] % 2 == 0)) { 17 | arr[k] = L[i]; 18 | i++; 19 | k++; 20 | } 21 | 22 | while (j < n2 && (R[j] % 2 == 0)) { 23 | arr[k] = R[j]; 24 | j++; 25 | k++; 26 | } 27 | 28 | while (i < n1) { 29 | arr[k] = L[i]; 30 | i++; 31 | k++; 32 | } 33 | while (j < n2) { 34 | arr[k] = R[j]; 35 | j++; 36 | k++; 37 | } 38 | } 39 | 40 | void mergeSort(int arr[], int left, int right) { 41 | if (left < right) { 42 | int mid = left + (right - left) / 2; 43 | 44 | mergeSort(arr, left, mid); 45 | mergeSort(arr, mid + 1, right); 46 | 47 | merge(arr, left, mid, right); 48 | } 49 | } 50 | 51 | int main() { 52 | int n; 53 | scanf("%d", &n); 54 | 55 | int arr[n]; 56 | for (int i = 0; i < n; i++) { 57 | scanf("%d", &arr[i]); 58 | } 59 | 60 | mergeSort(arr, 0, n - 1); 61 | 62 | for (int i = 0; i < n; i++) { 63 | printf("%d ", arr[i]); 64 | } 65 | printf("\n"); 66 | 67 | return 0; 68 | } -------------------------------------------------------------------------------- /DPP/Unit 4/Ragu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node*left; 7 | struct node*right; 8 | }; 9 | struct node*root; 10 | void append(int d)//1 11 | { 12 | struct node*newnode = (struct node*)malloc(sizeof(struct node)); 13 | struct node*temp = root;//6 14 | newnode->data = d;//4 15 | newnode->left = NULL; 16 | newnode->right = NULL; 17 | if(root == NULL)//6 18 | { 19 | root = newnode;//6 20 | } 21 | else 22 | { 23 | while(true) 24 | { 25 | if(d < temp->data)//4 < 3 26 | { 27 | if(temp->left != NULL)//3 28 | { 29 | temp = temp->left;//3 30 | } 31 | else 32 | { 33 | temp->left = newnode;//1 34 | break; 35 | } 36 | } 37 | else 38 | { 39 | if(temp->right != NULL) 40 | { 41 | temp = temp->right; 42 | }else 43 | { 44 | temp->right = newnode; 45 | break; 46 | } 47 | } 48 | } 49 | } 50 | } 51 | void postorder(struct node*root) 52 | { 53 | if(root != NULL) 54 | { 55 | 56 | postorder(root->left); 57 | postorder(root->right); 58 | printf("%d ",root->data); 59 | } 60 | } 61 | int main() 62 | { 63 | int d; 64 | do 65 | { 66 | scanf("%d",&d);//4 67 | if(d > 0)//4 68 | append(d); 69 | }while(d != -1);//1 70 | printf("Post order Traversal:\n"); 71 | postorder(root); 72 | return 0; 73 | } -------------------------------------------------------------------------------- /DPP/Unit 2/Scientific research project.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node { 5 | float data; 6 | Node* next; 7 | 8 | Node(float val) : data(val), next(nullptr) {} 9 | }; 10 | 11 | void insertAtEnd(Node*& header, float value) { 12 | Node* newNode = new Node(value); 13 | 14 | if (!header) { 15 | newNode->next = newNode; 16 | header = newNode; 17 | } else { 18 | newNode->next = header->next; 19 | header->next = newNode; 20 | header = newNode; 21 | } 22 | } 23 | 24 | void display(Node* header) { 25 | if (!header) { 26 | cout << "Linked List is empty." << endl; 27 | return; 28 | } 29 | 30 | Node* current = header->next; 31 | 32 | do { 33 | cout << current->data << " "; 34 | current = current->next; 35 | } while (current != header->next); 36 | 37 | cout << endl; 38 | } 39 | 40 | int main() { 41 | int n; 42 | cin >> n; 43 | 44 | Node* header = nullptr; 45 | 46 | for (int i = 0; i < n; i++) { 47 | float value; 48 | cin >> value; 49 | insertAtEnd(header, value); 50 | } 51 | 52 | display(header); 53 | 54 | if (header) { 55 | Node* current = header; 56 | do { 57 | Node* temp = current; 58 | current = current->next; 59 | delete temp; 60 | } while (current != header); 61 | } 62 | 63 | return 0; 64 | } 65 | 66 | 67 | 68 | //sUSHANT -------------------------------------------------------------------------------- /DPP/Unit 4/Freshmart.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void Merge(int *a, int low, int high, int mid) { 5 | int i, j, k; 6 | int n1 = mid - low + 1; 7 | int n2 = high - mid; 8 | 9 | int L[n1], R[n2]; 10 | 11 | for (i = 0; i < n1; i++) { 12 | L[i] = a[low + i]; 13 | } 14 | for (j = 0; j < n2; j++) { 15 | R[j] = a[mid + 1 + j]; 16 | } 17 | 18 | i = 0; 19 | j = 0; 20 | k = low; 21 | 22 | while (i < n1 && j < n2) { 23 | if (L[i] >= R[j]) { 24 | a[k] = L[i]; 25 | i++; 26 | } else { 27 | a[k] = R[j]; 28 | j++; 29 | } 30 | k++; 31 | } 32 | 33 | while (i < n1) { 34 | a[k] = L[i]; 35 | i++; 36 | k++; 37 | } 38 | 39 | while (j < n2) { 40 | a[k] = R[j]; 41 | j++; 42 | k++; 43 | } 44 | } 45 | 46 | void MergeSort(int *a, int low, int high) { 47 | if (low < high) { 48 | int mid = low + (high - low) / 2; 49 | MergeSort(a, low, mid); 50 | MergeSort(a, mid + 1, high); 51 | Merge(a, low, high, mid); 52 | } 53 | } 54 | 55 | int main() { 56 | int n; 57 | cin >> n; 58 | 59 | int arr[n]; 60 | for (int i = 0; i < n; i++) { 61 | cin >> arr[i]; 62 | } 63 | 64 | MergeSort(arr, 0, n - 1); 65 | 66 | for (int i = 0; i < n; i++) { 67 | cout << arr[i] << " "; 68 | } 69 | cout << endl; 70 | 71 | return 0; 72 | } -------------------------------------------------------------------------------- /DPP/Unit 3/Lara.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | struct Node { 6 | int data; 7 | struct Node* next; 8 | }; 9 | 10 | void deleteMid(struct Node** head_ref, int n) { 11 | if (*head_ref == NULL || n == 0) { 12 | return; 13 | } 14 | 15 | if (n == 1) { 16 | struct Node* temp = *head_ref; 17 | *head_ref = temp->next; 18 | free(temp); 19 | return; 20 | } 21 | 22 | struct Node* prev = NULL; 23 | struct Node* curr = *head_ref; 24 | int count = 0; 25 | 26 | while (curr != NULL) { 27 | if (count == n / 2) { 28 | prev->next = curr->next; 29 | free(curr); 30 | return; 31 | } 32 | 33 | prev = curr; 34 | curr = curr->next; 35 | count++; 36 | } 37 | } 38 | 39 | void push(struct Node** head_ref, int new_data) { 40 | struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); 41 | new_node->data = new_data; 42 | new_node->next = (*head_ref); 43 | (*head_ref) = new_node; 44 | } 45 | 46 | void printList(struct Node* node) { 47 | while (node != NULL) { 48 | cout<< node->data<<" "; 49 | node = node->next; 50 | } 51 | } 52 | 53 | int main() { 54 | struct Node* head = NULL; 55 | int i, n; 56 | cin>>n; 57 | for (i = n; i > 0; i--) { 58 | push(&head, i); 59 | } 60 | deleteMid(&head, n); 61 | printList(head); 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /DPP/Unit 3/Lisa.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int MAX_SIZE = 100; 4 | 5 | int stackA[MAX_SIZE]; 6 | int topA = -1; 7 | 8 | int stackB[MAX_SIZE]; 9 | int topB = -1; 10 | 11 | void pushA(int value) { 12 | if (topA == MAX_SIZE - 1) { 13 | return; 14 | } 15 | stackA[++topA] = value; 16 | } 17 | 18 | int popA() { 19 | if (topA == -1) { 20 | return -1; 21 | } 22 | return stackA[topA--]; 23 | } 24 | 25 | void pushB(int value) { 26 | if (topB == MAX_SIZE - 1) { 27 | return; 28 | } 29 | stackB[++topB] = value; 30 | } 31 | 32 | int popB() { 33 | if (topB == -1) { 34 | return -1; 35 | } 36 | return stackB[topB--]; 37 | } 38 | 39 | void reverseStack() { 40 | while (topA != -1) { 41 | int element = popA(); 42 | pushB(element); 43 | } 44 | } 45 | 46 | void displayStackB() { 47 | std::cout << "Elements in Stack B (reversed): "; 48 | for (int i = topB; i >= 0; --i) { 49 | std::cout << stackB[i] << " "; 50 | } 51 | std::cout << std::endl; 52 | } 53 | 54 | int main() { 55 | int n, value; 56 | 57 | std::cin >> n; 58 | 59 | for (int i = 0; i < n; ++i) { 60 | std::cin >> value; 61 | pushA(value); 62 | } 63 | 64 | std::cout << "Stack A elements: "; 65 | for (int i = topA; i >= 0; --i) { 66 | std::cout << stackA[i] << " "; 67 | } 68 | std::cout << std::endl; 69 | 70 | reverseStack(); 71 | displayStackB(); 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /DPP/Unit 2/5_Milton.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | }; 8 | 9 | struct Node* insertEnd(struct Node* head, int data) { 10 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 11 | newNode->data = data; 12 | if (head == NULL) { 13 | newNode->next = newNode; 14 | return newNode; 15 | } else { 16 | struct Node* temp = head; 17 | while (temp->next != head) { 18 | temp = temp->next; 19 | } 20 | temp->next = newNode; 21 | newNode->next = head; 22 | return head; 23 | } 24 | } 25 | 26 | 27 | 28 | int isSorted(struct Node* head) { 29 | if (head == NULL) { 30 | return 1; 31 | } 32 | struct Node* current = head; 33 | do { 34 | if (current->data > current->next->data) { 35 | return 0; 36 | } 37 | current = current->next; 38 | } while (current->next != head); 39 | return 1; 40 | } 41 | 42 | int main() { 43 | int n; 44 | scanf("%d", &n); 45 | struct Node* head = NULL; 46 | for (int i = 0; i < n; i++) { 47 | int data; 48 | scanf("%d", &data); 49 | head = insertEnd(head, data); 50 | } 51 | if (head == NULL || isSorted(head)) { 52 | printf("The list is sorted in non-decreasing order.\n"); 53 | } else { 54 | printf("The list is not sorted in non-decreasing order.\n"); 55 | } 56 | return 0; 57 | } 58 | 59 | -------------------------------------------------------------------------------- /DPP/Unit 4/Lalitha.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // Define the structure for a binary tree node 6 | struct Node { 7 | int data; 8 | Node* left; 9 | Node* right; 10 | Node(int value) : data(value), left(nullptr), right(nullptr) {} 11 | }; 12 | 13 | // Function to construct the binary tree from the given array 14 | Node* constructBinaryTree(vector& values, int index, int n) { 15 | if (index >= n) { 16 | return nullptr; 17 | } 18 | Node* root = new Node(values[index]); 19 | root->left = constructBinaryTree(values, 2 * index + 1, n); 20 | root->right = constructBinaryTree(values, 2 * index + 2, n); 21 | return root; 22 | } 23 | 24 | // Function to perform post-order traversal and print the values 25 | void postOrderTraversal(Node* root) { 26 | if (root == nullptr) { 27 | return; 28 | } 29 | postOrderTraversal(root->left); 30 | postOrderTraversal(root->right); 31 | cout << root->data << " "; 32 | } 33 | 34 | int main() { 35 | int n; 36 | cin >> n; 37 | 38 | vector values(n); 39 | 40 | // Read the values representing the binary tree 41 | for (int i = 0; i < n; i++) { 42 | cin >> values[i]; 43 | } 44 | 45 | // Construct the binary tree from the given array 46 | Node* root = constructBinaryTree(values, 0, n); 47 | 48 | // Perform post-order traversal and print the values 49 | postOrderTraversal(root); 50 | cout << endl; 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /DPP/Unit 1/Shyam.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int countOccurrences(std::vector& arr, int target) { 5 | int left = 0, right = arr.size() - 1; 6 | int firstOccurrence = -1; 7 | 8 | while (left <= right) { 9 | int mid = left + (right - left) / 2; 10 | if (arr[mid] == target) { 11 | firstOccurrence = mid; 12 | right = mid - 1; 13 | } else if (arr[mid] < target) { 14 | left = mid + 1; 15 | } else { 16 | right = mid - 1; 17 | } 18 | } 19 | 20 | if (firstOccurrence == -1) { 21 | return 0; 22 | } 23 | 24 | int lastOccurrence = -1; 25 | 26 | left = 0, right = arr.size() - 1; 27 | while (left <= right) { 28 | int mid = left + (right - left) / 2; 29 | if (arr[mid] == target) { 30 | lastOccurrence = mid; 31 | left = mid + 1; 32 | } else if (arr[mid] < target) { 33 | left = mid + 1; 34 | } else { 35 | right = mid - 1; 36 | } 37 | } 38 | 39 | return lastOccurrence - firstOccurrence + 1; 40 | } 41 | 42 | int main() { 43 | int n; 44 | std::cin >> n; 45 | 46 | std::vector arr(n); 47 | for (int i = 0; i < n; i++) { 48 | std::cin >> arr[i]; 49 | } 50 | 51 | int target; 52 | std::cin >> target; 53 | 54 | int frequency = countOccurrences(arr, target); 55 | std::cout << frequency << std::endl; 56 | 57 | return 0; 58 | } 59 | 60 | 61 | /Sushant 62 | -------------------------------------------------------------------------------- /DPP/Unit 2/-5_0_5_Analyzing Temperature.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Node { 4 | int data; 5 | Node* prev; 6 | Node* next; 7 | }; 8 | 9 | void insertAtBeginning(Node** head, int newData) { 10 | Node* newNode = new Node; 11 | newNode->data = newData; 12 | newNode->prev = nullptr; 13 | newNode->next = *head; 14 | 15 | if (*head != nullptr) 16 | (*head)->prev = newNode; 17 | 18 | *head = newNode; 19 | } 20 | 21 | void reverseList(Node** head) { 22 | Node* current = *head; 23 | Node* temp = nullptr; 24 | 25 | while (current != nullptr) { 26 | temp = current->prev; 27 | current->prev = current->next; 28 | current->next = temp; 29 | 30 | current = current->prev; 31 | } 32 | 33 | if (temp != nullptr) 34 | *head = temp->prev; 35 | } 36 | 37 | void printList(Node* head) { 38 | Node* current = head; 39 | while (current != nullptr) { 40 | std::cout << current->data << " "; 41 | current = current->next; 42 | } 43 | std::cout << std::endl; 44 | } 45 | 46 | int main() { 47 | Node* head = nullptr; 48 | int n, data; 49 | 50 | std::cin >> n; 51 | 52 | for (int i = 0; i < n; i++) { 53 | std::cin >> data; 54 | insertAtBeginning(&head, data); 55 | } 56 | 57 | std::cout << "Original List: "; 58 | printList(head); 59 | 60 | reverseList(&head); 61 | 62 | std::cout << "Reversed List: "; 63 | printList(head); 64 | 65 | return 0; 66 | } 67 | 68 | //Sushant -------------------------------------------------------------------------------- /DPP/Unit 2/Suresh.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | }; 8 | 9 | struct Node* createNode(int value) { 10 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 11 | newNode->data = value; 12 | newNode->next = NULL; 13 | return newNode; 14 | } 15 | 16 | void insertNode(struct Node** head, int value) { 17 | struct Node* newNode = createNode(value); 18 | if (*head == NULL) { 19 | *head = newNode; 20 | } else { 21 | struct Node* current = *head; 22 | while (current->next != NULL) { 23 | current = current->next; 24 | } 25 | current->next = newNode; 26 | } 27 | } 28 | 29 | void displayList(struct Node* head) { 30 | if (head == NULL) { 31 | printf("The list is empty.\n"); 32 | return; 33 | } 34 | 35 | while (head != NULL) { 36 | printf("%d ", head->data); 37 | head = head->next; 38 | } 39 | printf("\n"); 40 | } 41 | 42 | int main() { 43 | int numElements; 44 | scanf("%d", &numElements); 45 | 46 | struct Node* head = NULL; 47 | 48 | for (int i = 0; i < numElements; i++) { 49 | int element; 50 | scanf("%d", &element); 51 | insertNode(&head, element); 52 | } 53 | 54 | displayList(head); 55 | 56 | while (head != NULL) { 57 | struct Node* temp = head; 58 | head = head->next; 59 | free(temp); 60 | } 61 | 62 | return 0; 63 | } 64 | //Sushant 65 | -------------------------------------------------------------------------------- /DPP/Unit 3/Julie.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_SIZE = 100; 5 | 6 | int stack[MAX_SIZE]; 7 | int top = -1; 8 | 9 | void push(int value) { 10 | if (top == MAX_SIZE - 1 || value >= MAX_SIZE) { 11 | cout << "Stack Overflow. Cannot push element." << endl; 12 | return; 13 | } 14 | 15 | stack[++top] = value; 16 | cout << "Element " << value << " pushed onto the stack." << endl; 17 | } 18 | 19 | void pop() { 20 | if (top == -1) { 21 | cout << "Stack Underflow. Cannot perform pop operation." << endl; 22 | return; 23 | } 24 | 25 | int element = stack[top--]; 26 | cout << "Element " << element << " popped from the stack." << endl; 27 | } 28 | 29 | void displayStack() { 30 | if (top == -1) { 31 | cout << "Stack is empty." << endl; 32 | return; 33 | } 34 | 35 | cout << "Elements in the stack: "; 36 | for (int i = top; i >= 0; --i) { 37 | cout << stack[i] << " "; 38 | } 39 | cout << endl; 40 | } 41 | 42 | int main() { 43 | int choice, value; 44 | 45 | do { 46 | std::cin >> choice; 47 | 48 | switch (choice) { 49 | case 1: 50 | cin >> value; 51 | push(value); 52 | break; 53 | case 2: 54 | pop(); 55 | break; 56 | case 3: 57 | displayStack(); 58 | break; 59 | case 4: 60 | cout << "Exiting the program." << endl; 61 | break; 62 | default: 63 | cout << "Invalid choice." << endl; 64 | } 65 | } while (choice != 4); 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /DPP/Unit 2/Uma(insert node at end).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Node { 4 | public: 5 | int data; 6 | Node* next; 7 | 8 | Node(int value) { 9 | data = value; 10 | next = nullptr; 11 | } 12 | }; 13 | 14 | class LinkedList { 15 | public: 16 | Node* head; 17 | 18 | LinkedList() { 19 | head = nullptr; 20 | } 21 | 22 | void insert(int value) { 23 | Node* newNode = new Node(value); 24 | if (head == nullptr) { 25 | head = newNode; 26 | } else { 27 | Node* current = head; 28 | while (current->next != nullptr) { 29 | current = current->next; 30 | } 31 | current->next = newNode; 32 | } 33 | } 34 | 35 | void display() { 36 | if (head == nullptr) { 37 | std::cout << "Linked List is empty." << std::endl; 38 | } else { 39 | std::cout << "Linked List: "; 40 | Node* current = head; 41 | while (current != nullptr) { 42 | std::cout << current->data << " "; 43 | current = current->next; 44 | } 45 | std::cout << std::endl; 46 | } 47 | } 48 | }; 49 | 50 | int main() { 51 | LinkedList linkedList; 52 | int value; 53 | 54 | while (true) { 55 | std::cin >> value; 56 | if (value < 0) { 57 | break; 58 | } 59 | linkedList.insert(value); 60 | } 61 | 62 | linkedList.display(); 63 | 64 | return 0; 65 | } 66 | 67 | 68 | //Sushant -------------------------------------------------------------------------------- /DPP/Unit 3/Student attendence system.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int MAX_SIZE = 100; 4 | 5 | struct Queue { 6 | int queue[MAX_SIZE]; 7 | int front; 8 | int rear; 9 | }; 10 | 11 | void initializeQueue(Queue& q) { 12 | q.front = -1; 13 | q.rear = -1; 14 | } 15 | 16 | bool isEmpty(const Queue& q) { 17 | return q.front == -1; 18 | } 19 | 20 | bool isFull(const Queue& q) { 21 | return q.rear == MAX_SIZE - 1; 22 | } 23 | 24 | void enqueue(Queue& q, int data) { 25 | if (isFull(q)) { 26 | return; 27 | } 28 | 29 | if (isEmpty(q)) { 30 | q.front = 0; 31 | } 32 | 33 | q.rear++; 34 | q.queue[q.rear] = data; 35 | } 36 | 37 | int countOccurrences(const Queue& q, int element) { 38 | int count = 0; 39 | 40 | if (isEmpty(q)) { 41 | return count; 42 | } 43 | 44 | for (int i = q.front; i <= q.rear; i++) { 45 | if (q.queue[i] == element) { 46 | count++; 47 | } 48 | } 49 | 50 | return count; 51 | } 52 | 53 | int main() { 54 | Queue queue; 55 | initializeQueue(queue); 56 | 57 | int n; 58 | std::cin >> n; 59 | 60 | for (int i = 0; i < n; i++) { 61 | int element; 62 | std::cin >> element; 63 | enqueue(queue, element); 64 | } 65 | 66 | int searchElement; 67 | std::cin >> searchElement; 68 | 69 | int occurrences = countOccurrences(queue, searchElement); 70 | 71 | std::cout << "Occurrences of " << searchElement << " in the queue: " << occurrences << std::endl; 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /DPP/Unit 3/event reg conference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class EventRegistrationQueue { 5 | private: 6 | std::vector queue; 7 | 8 | public: 9 | void enqueue(int registration_id) { 10 | if (registration_id % 2 == 0) { 11 | queue.push_back(registration_id); 12 | } else { 13 | std::cout << "Invalid element " << registration_id << ", only even numbers can be enqueued" << std::endl; 14 | } 15 | } 16 | 17 | int dequeue() { 18 | if (!queue.empty()) { 19 | int front = queue.front(); 20 | queue.erase(queue.begin()); 21 | return front; 22 | } 23 | return -1; // Return -1 for an empty queue, but you can choose another suitable value. 24 | } 25 | 26 | bool isEmpty() const { 27 | return queue.empty(); 28 | } 29 | }; 30 | 31 | int main() { 32 | int n; 33 | std::cin >> n; 34 | EventRegistrationQueue eventQueue; 35 | 36 | for (int i = 0; i < n; ++i) { 37 | int registration_id; 38 | std::cin >> registration_id; 39 | eventQueue.enqueue(registration_id); 40 | } 41 | 42 | std::vector even_registration_ids; 43 | while (!eventQueue.isEmpty()) { 44 | int registration_id = eventQueue.dequeue(); 45 | if (registration_id != -1) { 46 | even_registration_ids.push_back(registration_id); 47 | } 48 | } 49 | 50 | for (int id : even_registration_ids) { 51 | std::cout << id << " "; 52 | } 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /DPP/Unit 4/harsh.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node { 5 | int data; 6 | struct node *left; 7 | struct node *right; 8 | }; 9 | 10 | struct node* insert(struct node* root, int data) { 11 | if (root == NULL) { 12 | struct node* node = (struct node*)malloc(sizeof(struct node)); 13 | node->data = data; 14 | node->left = NULL; 15 | node->right = NULL; 16 | return node; 17 | } else { 18 | struct node* cur = (struct node*)malloc(sizeof(struct node)); 19 | cur->data = data; 20 | cur->left = NULL; 21 | cur->right = NULL; 22 | 23 | if (root->left == NULL) { 24 | root->left = cur; 25 | } else if (root->right == NULL) { 26 | root->right = cur; 27 | } else { 28 | // If both left and right children are already present, 29 | // you can choose one side to insert the new node, e.g., left. 30 | root->left = insert(root->left, data); 31 | } 32 | 33 | return root; 34 | } 35 | } 36 | 37 | void postOrder(struct node* root) { 38 | if (root != NULL) { 39 | postOrder(root->left); 40 | postOrder(root->right); 41 | printf("%d ", root->data); 42 | } 43 | } 44 | 45 | int main() { 46 | struct node* root = NULL; 47 | int n; 48 | int data; 49 | 50 | scanf("%d", &n); 51 | 52 | while (n-- > 0) { 53 | scanf("%d", &data); 54 | root = insert(root, data); 55 | } 56 | 57 | postOrder(root); 58 | return 0; 59 | } -------------------------------------------------------------------------------- /DPP/Unit 1/Teacher at a School.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int merge(int arr[], int temp[], int left, int mid, int right) { 5 | int inv_count = 0; 6 | int i = left; 7 | int j = mid; 8 | int k = left; 9 | 10 | while (i <= mid - 1 && j <= right) { 11 | if (arr[i] <= arr[j]) { 12 | temp[k++] = arr[i++]; 13 | } else { 14 | temp[k++] = arr[j++]; 15 | inv_count += mid - i; 16 | } 17 | } 18 | 19 | while (i <= mid - 1) 20 | temp[k++] = arr[i++]; 21 | 22 | while (j <= right) 23 | temp[k++] = arr[j++]; 24 | 25 | for (i = left; i <= right; i++) 26 | arr[i] = temp[i]; 27 | 28 | return inv_count; 29 | } 30 | 31 | int mergeSort(int arr[], int temp[], int left, int right) { 32 | int inv_count = 0; 33 | if (right > left) { 34 | int mid = (left + right) / 2; 35 | inv_count += mergeSort(arr, temp, left, mid); 36 | inv_count += mergeSort(arr, temp, mid + 1, right); 37 | 38 | inv_count += merge(arr, temp, left, mid + 1, right); 39 | } 40 | return inv_count; 41 | } 42 | 43 | int inversionCount(int arr[], int n) { 44 | int temp[n]; 45 | return mergeSort(arr, temp, 0, n - 1); 46 | } 47 | 48 | int main() { 49 | int n; 50 | cin >> n; 51 | 52 | int arr[n]; 53 | for (int i = 0; i < n; i++) { 54 | cin >> arr[i]; 55 | } 56 | 57 | int inv_count = inversionCount(arr, n); 58 | cout << inv_count << endl; 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /DPP/Unit 4/Venugopal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node { 5 | int data; 6 | struct node *left; 7 | struct node *right; 8 | }; 9 | 10 | struct node* insert(struct node* root, int data) { 11 | if (root == NULL) { 12 | struct node* node = (struct node*)malloc(sizeof(struct node)); 13 | node->data = data; 14 | node->left = NULL; 15 | node->right = NULL; 16 | return node; 17 | } else { 18 | struct node* cur = (struct node*)malloc(sizeof(struct node)); 19 | cur->data = data; 20 | cur->left = NULL; 21 | cur->right = NULL; 22 | 23 | if (root->left == NULL) { 24 | root->left = cur; 25 | } else if (root->right == NULL) { 26 | root->right = cur; 27 | } else { 28 | // If both left and right children are already present, 29 | // you can choose one side to insert the new node, e.g., left. 30 | root->left = insert(root->left, data); 31 | } 32 | 33 | return root; 34 | } 35 | } 36 | 37 | void postOrder(struct node* root) { 38 | if (root != NULL) { 39 | postOrder(root->left); 40 | postOrder(root->right); 41 | printf("%d ", root->data); 42 | } 43 | } 44 | 45 | int main() { 46 | struct node* root = NULL; 47 | int n; 48 | int data; 49 | 50 | scanf("%d", &n); 51 | 52 | while (n-- > 0) { 53 | scanf("%d", &data); 54 | root = insert(root, data); 55 | } 56 | 57 | postOrder(root); 58 | return 0; 59 | } -------------------------------------------------------------------------------- /DPP/Unit 1/Merge two arr[ ] Corresponding sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int* mergeArrays(const int* arr1, int size1, const int* arr2, int size2, int& mergedSize) { 5 | int maxSize = std::max(size1, size2); 6 | int* mergedArray = new int[maxSize]; 7 | 8 | for (int i = 0; i < maxSize; i++) { 9 | int sum = 0; 10 | 11 | if (i < size1) 12 | sum -= arr1[i]; 13 | 14 | if (i < size2) 15 | sum -= arr2[i]; 16 | 17 | mergedArray[i] = sum; 18 | } 19 | 20 | mergedSize = maxSize; 21 | return mergedArray; 22 | } 23 | 24 | int main() { 25 | int size1, size2; 26 | std::cin >> size1; 27 | int* arr1 = new int[size1]; 28 | for (int i = 0; i < size1; i++) { 29 | std::cin >> arr1[i]; 30 | } 31 | std::cin >> size2; 32 | int* arr2 = new int[size2]; 33 | for (int i = 0; i < size2; i++) { 34 | std::cin >> arr2[i]; 35 | } 36 | 37 | int mergedSize = 0; 38 | int* mergedArray = mergeArrays(arr1, size1, arr2, size2, mergedSize); 39 | 40 | for (int i = 0; i < mergedSize; i++) { 41 | std::cout << mergedArray[i] << " "; 42 | } 43 | std::cout << std::endl; 44 | 45 | delete[] arr1; 46 | delete[] arr2; 47 | delete[] mergedArray; 48 | 49 | return 0; 50 | } 51 | 52 | //Sushant 53 | 54 | // So two question are there in which output are positive and negative 55 | // so just assign the sign 56 | // sum -= arr1[i]; Line 12 57 | // sum -= arr2[i]; LIne15 58 | // here respectively 59 | -------------------------------------------------------------------------------- /DPP/Unit 2/Milton.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node { 5 | int data; 6 | Node* next; 7 | 8 | Node(int val) : data(val), next(nullptr) {} 9 | }; 10 | 11 | bool isSorted(Node* head) { 12 | if (!head) { 13 | return true; } 14 | 15 | Node* current = head; 16 | do { 17 | if (current->data > current->next->data) { 18 | return false; 19 | } 20 | current = current->next; 21 | } while (current->next != head); 22 | 23 | return true; 24 | } 25 | 26 | int main() { 27 | int n; 28 | cin >> n; 29 | 30 | Node* head = nullptr; 31 | Node* tail = nullptr; 32 | 33 | for (int i = 0; i < n; i++) { 34 | int value; 35 | cin >> value; 36 | Node* newNode = new Node(value); 37 | if (!head) { 38 | head = newNode; 39 | tail = newNode; 40 | newNode->next = head; 41 | } else { 42 | tail->next = newNode; 43 | tail = newNode; 44 | newNode->next = head; 45 | } 46 | } 47 | 48 | if (isSorted(head)) { 49 | cout << "The list is sorted in non-decreasing order." << endl; 50 | } else { 51 | cout << "The list is not sorted in non-decreasing order." << endl; 52 | } 53 | 54 | if (head) { 55 | Node* current = head; 56 | do { 57 | Node* temp = current; 58 | current = current->next; 59 | delete temp; 60 | } while (current != head); 61 | } 62 | 63 | return 0; 64 | } 65 | 66 | //sUSHANT 67 | 68 | -------------------------------------------------------------------------------- /DPP/Unit 2/deleting the first student's record.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct Node { 6 | int data; 7 | Node* next; 8 | }; 9 | 10 | Node* createNode(int data) { 11 | Node* newNode = new Node; 12 | newNode->data = data; 13 | newNode->next = nullptr; 14 | return newNode; 15 | } 16 | 17 | Node* deleteFirstNode(Node* head) { 18 | if (head == nullptr) { 19 | return nullptr; 20 | } 21 | Node* temp = head; 22 | head = head->next; 23 | delete temp; 24 | return head; 25 | } 26 | 27 | void printLinkedList(Node* head) { 28 | Node* current = head; 29 | while (current != nullptr) { 30 | cout << current->data << " "; 31 | current = current->next; 32 | } 33 | } 34 | 35 | int main() { 36 | int n; 37 | cin >> n; 38 | 39 | Node* head = nullptr; 40 | 41 | for (int i = 0; i < n; i++) { 42 | int studentID; 43 | cin >> studentID; 44 | if (head == nullptr) { 45 | head = createNode(studentID); 46 | } else { 47 | Node* current = head; 48 | while (current->next != nullptr) { 49 | current = current->next; 50 | } 51 | current->next = createNode(studentID); 52 | } 53 | } 54 | 55 | head = deleteFirstNode(head); 56 | 57 | printLinkedList(head); 58 | 59 | Node* current = head; 60 | while (current != nullptr) { 61 | Node* temp = current; 62 | current = current->next; 63 | delete temp; 64 | } 65 | 66 | return 0; 67 | } 68 | 69 | -------------------------------------------------------------------------------- /DPP/Unit 4/technical interview.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // Definition for a binary tree node. 6 | struct TreeNode { 7 | int val; 8 | TreeNode* left; 9 | TreeNode* right; 10 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 11 | }; 12 | 13 | void kthLargestUtil(TreeNode* root, int& k, int& result) { 14 | if (!root || k == 0) { 15 | return; 16 | } 17 | 18 | // Traverse the tree in reverse inorder (right, root, left) to find the kth largest element 19 | kthLargestUtil(root->right, k, result); 20 | k--; 21 | 22 | if (k == 0) { 23 | result = root->val; 24 | return; 25 | } 26 | 27 | kthLargestUtil(root->left, k, result); 28 | } 29 | 30 | int kthLargest(TreeNode* root, int k) { 31 | int result = -1; 32 | kthLargestUtil(root, k, result); 33 | return result; 34 | } 35 | 36 | TreeNode* insert(TreeNode* root, int val) { 37 | if (!root) { 38 | return new TreeNode(val); 39 | } 40 | 41 | if (val < root->val) { 42 | root->left = insert(root->left, val); 43 | } else { 44 | root->right = insert(root->right, val); 45 | } 46 | 47 | return root; 48 | } 49 | 50 | int main() { 51 | int val; 52 | TreeNode* root = nullptr; 53 | 54 | // Input values to create the BST 55 | while (cin >> val && val != -1) { 56 | root = insert(root, val); 57 | } 58 | 59 | int k; 60 | cin >> k; 61 | 62 | int kth_largest = kthLargest(root, k); 63 | 64 | cout << kth_largest << endl; 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /DPP/Unit 3/Student registration system for a university achievemenets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct Student { 6 | int id; 7 | int priority; 8 | Student* next; 9 | 10 | Student(int _id, int _priority) : id(_id), priority(_priority), next(nullptr) {} 11 | }; 12 | 13 | class PriorityQueue { 14 | private: 15 | Student* front; 16 | 17 | public: 18 | PriorityQueue() : front(nullptr) {} 19 | 20 | void enqueue(int id, int priority) { 21 | Student* newStudent = new Student(id, priority); 22 | 23 | if (!front || priority < front->priority) { 24 | newStudent->next = front; 25 | front = newStudent; 26 | } else { 27 | Student* current = front; 28 | while (current->next && priority >= current->next->priority) { 29 | current = current->next; 30 | } 31 | newStudent->next = current->next; 32 | current->next = newStudent; 33 | } 34 | } 35 | 36 | void display() { 37 | Student* current = front; 38 | while (current) { 39 | cout << current->id << " "; 40 | current = current->next; 41 | } 42 | cout << endl; 43 | } 44 | }; 45 | 46 | int main() { 47 | int n; 48 | cin >> n; 49 | 50 | PriorityQueue pq; 51 | 52 | for (int i = 0; i < n; ++i) { 53 | int id, priority; 54 | cin >> id >> priority; 55 | pq.enqueue(id, priority); 56 | } 57 | 58 | cout << "Priority Queue: "; 59 | pq.display(); 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /DPP/Unit 2/Developing a smart device binary data from sensor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct ListNode { 5 | int val; 6 | struct ListNode* next; 7 | }; 8 | 9 | int getDecimalValue(struct ListNode* head) { 10 | int decimalValue = 0; 11 | 12 | while (head != NULL) { 13 | decimalValue = (decimalValue << 1) | head->val; 14 | head = head->next; 15 | } 16 | 17 | return decimalValue; 18 | } 19 | 20 | int main() { 21 | struct ListNode* head = NULL; 22 | struct ListNode* current = NULL; 23 | 24 | int n; 25 | scanf("%d", &n); 26 | 27 | for (int i = 0; i < n; i++) { 28 | int val; 29 | scanf("%d", &val); 30 | 31 | struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode)); 32 | newNode->val = val; 33 | newNode->next = NULL; 34 | 35 | if (head == NULL) { 36 | head = newNode; 37 | current = newNode; 38 | } else { 39 | current->next = newNode; 40 | current = newNode; 41 | } 42 | } 43 | 44 | int decimalValue = getDecimalValue(head); 45 | 46 | printf("Linked List: "); 47 | current = head; 48 | while (current != NULL) { 49 | printf("%d ", current->val); 50 | current = current->next; 51 | } 52 | printf("\nDecimal Value: %d\n", decimalValue); 53 | 54 | while (head != NULL) { 55 | current = head; 56 | head = head->next; 57 | free(current); 58 | } 59 | 60 | return 0; 61 | } 62 | //Sushant 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /DPP/Unit 3/challenging developing a queue data .cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | struct Node { 6 | int data; 7 | struct Node* next; 8 | }; 9 | 10 | struct Queue { 11 | struct Node* front; 12 | struct Node* rear; 13 | }; 14 | 15 | void initializeQueue(struct Queue* q) { 16 | q->front = q->rear = NULL; 17 | } 18 | 19 | int isEmpty(struct Queue* q) { 20 | return q->front == NULL; 21 | } 22 | 23 | void enqueue(struct Queue* q, int value) { 24 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 25 | newNode->data = value; 26 | newNode->next = NULL; 27 | 28 | if (isEmpty(q)) { 29 | q->front = q->rear = newNode; 30 | } else { 31 | q->rear->next = newNode; 32 | q->rear = newNode; 33 | } 34 | } 35 | 36 | int dequeue(struct Queue* q) { 37 | if (isEmpty(q)) { 38 | printf("Queue is empty.\n"); 39 | return -1; 40 | } 41 | 42 | struct Node* temp = q->front; 43 | int value = temp->data; 44 | 45 | q->front = q->front->next; 46 | free(temp); 47 | 48 | return value; 49 | } 50 | 51 | int main() { 52 | struct Queue q; 53 | initializeQueue(&q); 54 | 55 | int input; 56 | while (1) { 57 | scanf("%d", &input); 58 | 59 | if (input == -1) { 60 | break; 61 | } 62 | 63 | enqueue(&q, input); 64 | } 65 | 66 | printf("Dequeued elements:"); 67 | 68 | while (!isEmpty(&q)) { 69 | int value = dequeue(&q); 70 | printf(" %d", value); 71 | } 72 | 73 | printf("\n"); 74 | 75 | return 0; 76 | } -------------------------------------------------------------------------------- /DPP/Unit 3/Mira.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | stack mystack; 9 | 10 | while (true) 11 | { 12 | int choice; 13 | cin >> choice; 14 | 15 | switch (choice) 16 | { 17 | case 1: 18 | { 19 | int value; 20 | cin >> value; 21 | mystack.push(value); 22 | break; 23 | } 24 | case 2: 25 | { 26 | if (mystack.empty()) 27 | { 28 | cout << "Stack is empty. Cannot perform pop operation." << endl; 29 | } 30 | else 31 | { 32 | mystack.pop(); 33 | } 34 | break; 35 | } 36 | case 3: 37 | { 38 | if (mystack.empty()) 39 | { 40 | cout << "Stack is empty." << endl; 41 | } 42 | else 43 | { 44 | cout << "Elements in the stack:"; 45 | stack tempStack = mystack; 46 | while (!tempStack.empty()) 47 | { 48 | cout << " " << tempStack.top(); 49 | tempStack.pop(); 50 | } 51 | cout << endl; 52 | } 53 | break; 54 | } 55 | case 4: 56 | { 57 | cout << "Exiting the program." << endl; 58 | return 0; 59 | } 60 | default: 61 | cout << "Invalid choice." << endl; 62 | } 63 | } 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /DPP/Unit 4/John student.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void merge(float arr[], int left, int mid, int right) { 4 | int n1 = mid - left + 1; 5 | int n2 = right - mid; 6 | 7 | float leftArr[n1]; 8 | float rightArr[n2]; 9 | 10 | for (int i = 0; i < n1; i++) { 11 | leftArr[i] = arr[left + i]; 12 | } 13 | for (int j = 0; j < n2; j++) { 14 | rightArr[j] = arr[mid + 1 + j]; 15 | } 16 | 17 | int i = 0, j = 0, k = left; 18 | 19 | while (i < n1 && j < n2) { 20 | if (leftArr[i] >= rightArr[j]) { 21 | arr[k] = leftArr[i]; 22 | i++; 23 | } else { 24 | arr[k] = rightArr[j]; 25 | j++; 26 | } 27 | k++; 28 | } 29 | 30 | while (i < n1) { 31 | arr[k] = leftArr[i]; 32 | i++; 33 | k++; 34 | } 35 | 36 | while (j < n2) { 37 | arr[k] = rightArr[j]; 38 | j++; 39 | k++; 40 | } 41 | } 42 | 43 | void mergeSort(float arr[], int left, int right) { 44 | if (left < right) { 45 | int mid = left + (right - left) / 2; 46 | 47 | mergeSort(arr, left, mid); 48 | mergeSort(arr, mid + 1, right); 49 | 50 | merge(arr, left, mid, right); 51 | } 52 | } 53 | 54 | int main() { 55 | int n; 56 | scanf("%d", &n); 57 | float arr[n]; 58 | for (int i = 0; i < n; i++) { 59 | scanf("%f", &arr[i]); 60 | } 61 | mergeSort(arr, 0, n - 1); 62 | printf("Sorted Array:\n"); 63 | for (int i = 0; i < n; i++) { 64 | printf("%.2f ", arr[i]); 65 | } 66 | printf("\n"); 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /DPP/Unit 3/Restaurant Get Rear.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Queue { 4 | int* arr; // array to store queue elements 5 | int front; // front points to the front element in the queue 6 | int rear; // rear points to the last element in the queue 7 | int capacity; // maximum capacity of the queue 8 | int size; // current size of the queue 9 | }; 10 | 11 | void initializeQueue(Queue& q, int capacity) { 12 | q.capacity = capacity; 13 | q.arr = new int[capacity]; 14 | q.front = 0; 15 | q.rear = -1; 16 | q.size = 0; 17 | } 18 | 19 | bool isEmpty(const Queue& q) { 20 | return q.size == 0; 21 | } 22 | 23 | bool isFull(const Queue& q) { 24 | return q.size == q.capacity; 25 | } 26 | 27 | void enqueue(Queue& q, int item) { 28 | if (isFull(q)) { 29 | return; 30 | } 31 | q.rear = (q.rear + 1) % q.capacity; 32 | q.arr[q.rear] = item; 33 | q.size++; 34 | } 35 | 36 | int getFront(const Queue& q) { 37 | if (isEmpty(q)) { 38 | std::cout << "Queue is empty. No front element." << std::endl; 39 | return -1; 40 | } 41 | return q.arr[q.front]; 42 | } 43 | 44 | int getRear(const Queue& q) { 45 | if (isEmpty(q)) { 46 | std::cout << "Queue is empty. No rear element." << std::endl; 47 | return -1; 48 | } 49 | return q.arr[q.rear]; 50 | } 51 | 52 | int main() { 53 | int N; 54 | std::cin >> N; 55 | Queue queue; 56 | initializeQueue(queue, N); // Creating a queue of capacity N 57 | 58 | for (int i = 0; i < N; i++) { 59 | int num; 60 | std::cin >> num; 61 | enqueue(queue, num); 62 | } 63 | 64 | std::cout << "Front element: " << getFront(queue) << std::endl; 65 | std::cout << "Rear element: " << getRear(queue) << std::endl; 66 | 67 | return 0; 68 | } -------------------------------------------------------------------------------- /DPP/Unit 2/Dharun.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct Node { 6 | string data; 7 | Node* next; 8 | Node(string value) : data(value), next(nullptr) {} 9 | }; 10 | 11 | class LinkedList { 12 | public: 13 | Node* head; 14 | LinkedList() : head(nullptr) {} 15 | 16 | void insertNode(string value) { 17 | Node* newNode = new Node(value); 18 | if (head == nullptr) { 19 | head = newNode; 20 | } else { 21 | Node* current = head; 22 | while (current->next != nullptr) { 23 | current = current->next; 24 | } 25 | current->next = newNode; 26 | } 27 | } 28 | 29 | void deleteLastNode() { 30 | if (head == nullptr || head->next == nullptr) { 31 | return; 32 | } 33 | Node* current = head; 34 | while (current->next->next != nullptr) { 35 | current = current->next; 36 | } 37 | delete current->next; 38 | current->next = nullptr; 39 | } 40 | 41 | void printList() { 42 | Node* current = head; 43 | while (current != nullptr) { 44 | cout << current->data << " "; 45 | current = current->next; 46 | } 47 | } 48 | }; 49 | 50 | int main() { 51 | int n; 52 | cin >> n; 53 | LinkedList list; 54 | 55 | for (int i = 0; i < n; i++) { 56 | string value; 57 | cin >> value; 58 | list.insertNode(value); 59 | } 60 | 61 | list.deleteLastNode(); 62 | list.printList(); 63 | 64 | return 0; 65 | } 66 | 67 | //Sushant -------------------------------------------------------------------------------- /DPP/Unit 2/Imagine Cashier.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | #include 3 | using namespace std; 4 | 5 | struct Node { 6 | int data; 7 | Node* next; 8 | Node(int val) : data(val), next(nullptr) {} 9 | }; 10 | 11 | int sumOfLastMNodes(Node* head, int m) { 12 | if (head == nullptr || m <= 0) { 13 | return 0; 14 | } 15 | 16 | Node* fast = head; 17 | Node* slow = head; 18 | int count = 0; 19 | int sum = 0; 20 | 21 | while (count < m && fast != nullptr) { 22 | fast = fast->next; 23 | count++; 24 | } 25 | 26 | if (count < m) { 27 | return 0; 28 | } 29 | 30 | while (fast != nullptr) { 31 | slow = slow->next; 32 | fast = fast->next; 33 | } 34 | 35 | while (slow != nullptr) { 36 | sum += slow->data; 37 | slow = slow->next; 38 | } 39 | 40 | return sum; 41 | } 42 | 43 | int main() { 44 | int n, m; 45 | cin >> n; 46 | Node* head = nullptr; 47 | Node* tail = nullptr; 48 | 49 | for (int i = 0; i < n; i++) { 50 | int val; 51 | cin >> val; 52 | Node* newNode = new Node(val); 53 | if (head == nullptr) { 54 | head = newNode; 55 | tail = newNode; 56 | } else { 57 | tail->next = newNode; 58 | tail = newNode; 59 | } 60 | } 61 | 62 | cin >> m; 63 | 64 | int result = sumOfLastMNodes(head, m); 65 | cout << result << endl; 66 | 67 | while (head != nullptr) { 68 | Node* temp = head; 69 | head = head->next; 70 | delete temp; 71 | } 72 | 73 | return 0; 74 | } 75 | 76 | 77 | //Sushant -------------------------------------------------------------------------------- /DPP/Unit 3/Raja.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int evaluatePostfix(const string &expression) { 10 | stack operandStack; 11 | 12 | for (char c : expression) { 13 | if (isdigit(c)) { 14 | operandStack.push(c - '0'); 15 | } else if (c == '+') { 16 | int operand2 = operandStack.top(); 17 | operandStack.pop(); 18 | int operand1 = operandStack.top(); 19 | operandStack.pop(); 20 | operandStack.push(operand1 + operand2); 21 | } else if (c == '-') { 22 | int operand2 = operandStack.top(); 23 | operandStack.pop(); 24 | int operand1 = operandStack.top(); 25 | operandStack.pop(); 26 | operandStack.push(operand1 - operand2); 27 | } else if (c == '*') { 28 | int operand2 = operandStack.top(); 29 | operandStack.pop(); 30 | int operand1 = operandStack.top(); 31 | operandStack.pop(); 32 | operandStack.push(operand1 * operand2); 33 | } else if (c == '/') { 34 | int operand2 = operandStack.top(); 35 | operandStack.pop(); 36 | int operand1 = operandStack.top(); 37 | operandStack.pop(); 38 | operandStack.push(operand1 / operand2); 39 | } 40 | } 41 | 42 | return operandStack.top(); 43 | } 44 | 45 | int main() { 46 | string expression; 47 | cin >> expression; 48 | 49 | int result = evaluatePostfix(expression); 50 | 51 | cout << result << endl; 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /DPP/Unit 3/The result is Calculator RPN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int MAX_SIZE = 100; // Adjust the maximum stack size as needed 8 | 9 | int stack[MAX_SIZE]; 10 | int top = -1; 11 | 12 | void push(int value) { 13 | if (top < MAX_SIZE - 1) { 14 | stack[++top] = value; 15 | } 16 | } 17 | 18 | int pop() { 19 | if (top >= 0) { 20 | return stack[top--]; 21 | } 22 | } 23 | 24 | int topValue() { 25 | if (top >= 0) { 26 | return stack[top]; 27 | } 28 | } 29 | 30 | bool isEmpty() { 31 | return top == -1; 32 | } 33 | 34 | int evaluateRPN(string expression) { 35 | stringstream ss(expression); 36 | string token; 37 | 38 | while (getline(ss, token, ' ')) { 39 | if (token == "+" || token == "-" || token == "*" || token == "/") { 40 | int operand2 = pop(); 41 | int operand1 = pop(); 42 | 43 | if (token == "+") 44 | push(operand1 + operand2); 45 | else if (token == "-") 46 | push(operand1 - operand2); 47 | else if (token == "*") 48 | push(operand1 * operand2); 49 | else if (token == "/") 50 | push(operand1 / operand2); 51 | } else { 52 | int number = stoi(token); 53 | push(number); 54 | } 55 | } 56 | 57 | return topValue(); 58 | } 59 | 60 | int main() { 61 | string expression; 62 | getline(cin, expression); 63 | 64 | int result = evaluateRPN(expression); 65 | cout << "The result is: " << result << endl; 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /DPP/Unit 3/Agalya.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_SIZE = 100; 5 | 6 | int stack[MAX_SIZE]; 7 | int top = -1; 8 | 9 | void push(int value) { 10 | if (top == MAX_SIZE - 1) { 11 | return; 12 | } 13 | 14 | stack[++top] = value; 15 | cout << "Element " << value << " pushed onto the stack." << endl; 16 | } 17 | 18 | void pop() { 19 | if (top == -1) { 20 | cout << "Stack Underflow. Cannot perform pop operation." << endl; 21 | return; 22 | } 23 | 24 | int element = stack[top--]; 25 | cout << "Element " << element << " popped from the stack." << endl; 26 | } 27 | 28 | void displayStack() { 29 | if (top == -1) { 30 | cout << "Stack is empty." << endl; 31 | return; 32 | } 33 | 34 | std::cout << "Elements in the stack: "; 35 | for (int i = top; i >= 0; --i) { 36 | cout << stack[i] << " "; 37 | } 38 | cout << endl; 39 | } 40 | 41 | int main() { 42 | int choice, value; 43 | 44 | do { 45 | 46 | std::cin >> choice; 47 | 48 | switch (choice) { 49 | case 1: 50 | 51 | cin >> value; 52 | push(value); 53 | break; 54 | case 2: 55 | pop(); 56 | break; 57 | case 3: 58 | displayStack(); 59 | break; 60 | case 4: 61 | cout << "Exiting the program." << endl; 62 | break; 63 | default: 64 | cout << "Invalid choice." << endl; 65 | } 66 | } while (choice != 4); 67 | 68 | return 0; 69 | } 70 | 71 | -------------------------------------------------------------------------------- /DPP/Unit 4/Arsha.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void merge(int arr[], int left, int mid, int right) { 4 | int n1 = mid - left + 1; 5 | int n2 = right - mid; 6 | 7 | int leftArr[n1]; 8 | int rightArr[n2]; 9 | 10 | for (int i = 0; i < n1; i++) { 11 | leftArr[i] = arr[left + i]; 12 | } 13 | for (int j = 0; j < n2; j++) { 14 | rightArr[j] = arr[mid + 1 + j]; 15 | } 16 | 17 | int i = 0, j = 0, k = left; 18 | 19 | while (i < n1 && j < n2) { 20 | if (leftArr[i] <= rightArr[j]) { 21 | arr[k] = leftArr[i]; 22 | i++; 23 | } else { 24 | arr[k] = rightArr[j]; 25 | j++; 26 | } 27 | k++; 28 | } 29 | 30 | while (i < n1) { 31 | arr[k] = leftArr[i]; 32 | i++; 33 | k++; 34 | } 35 | 36 | while (j < n2) { 37 | arr[k] = rightArr[j]; 38 | j++; 39 | k++; 40 | } 41 | } 42 | 43 | void mergeSort(int arr[], int left, int right) { 44 | if (left < right) { 45 | int mid = left + (right - left) / 2; 46 | 47 | mergeSort(arr, left, mid); 48 | mergeSort(arr, mid + 1, right); 49 | 50 | merge(arr, left, mid, right); 51 | } 52 | } 53 | 54 | int main() { 55 | int n; 56 | scanf("%d", &n); 57 | int arr[n]; 58 | for (int i = 0; i < n; i++) { 59 | scanf("%d", &arr[i]); 60 | if (arr[i] != 0 && arr[i] != 1) { 61 | printf("Invalid input.\n"); 62 | return 0; 63 | } 64 | } 65 | mergeSort(arr, 0, n - 1); 66 | for (int i = 0; i < n; i++) { 67 | printf("%d ", arr[i]); 68 | } 69 | printf("\n"); 70 | 71 | return 0; 72 | } -------------------------------------------------------------------------------- /DPP/Unit 2/Developing operates circular header Standard linked list points back.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | #include 3 | #include 4 | 5 | struct Node { 6 | int data; 7 | struct Node* next; 8 | }; 9 | 10 | typedef struct Node Node; 11 | 12 | Node* createNode(int data) { 13 | Node* newNode = (Node*)malloc(sizeof(Node)); 14 | newNode->data = data; 15 | newNode->next = NULL; 16 | return newNode; 17 | } 18 | 19 | void insertEnd(Node** head, int data) { 20 | Node* newNode = createNode(data); 21 | if (*head == NULL) { 22 | *head = newNode; 23 | (*head)->next = *head; 24 | } else { 25 | Node* temp = *head; 26 | while (temp->next != *head) { 27 | temp = temp->next; 28 | } 29 | temp->next = newNode; 30 | newNode->next = *head; 31 | } 32 | } 33 | 34 | void printAlternateNodes(Node* head) { 35 | if (head == NULL) { 36 | printf("Linked List is empty.\n"); 37 | return; 38 | } 39 | 40 | printf("Alternate Nodes:"); 41 | Node* current = head; 42 | int isEven = 1; 43 | do { 44 | if (isEven) { 45 | printf(" %d", current->data); 46 | } 47 | isEven = !isEven; 48 | current = current->next; 49 | } while (current != head); 50 | printf("\n"); 51 | } 52 | 53 | int main() { 54 | Node* head = NULL; 55 | int n, data; 56 | 57 | scanf("%d", &n); 58 | 59 | if (n > 0) { 60 | for (int i = 0; i < n; i++) { 61 | scanf("%d", &data); 62 | insertEnd(&head, data); 63 | } 64 | } 65 | 66 | printAlternateNodes(head); 67 | 68 | return 0; 69 | } 70 | 71 | //sushant -------------------------------------------------------------------------------- /DPP/Unit 3/task scheduling double-ended queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node* next; 8 | Node* prev; 9 | 10 | Node(int data) : data(data), next(nullptr), prev(nullptr) {} 11 | }; 12 | 13 | void insertFront(Node** deque, int data) { 14 | Node* newNode = new Node(data); 15 | if (*deque == nullptr) { 16 | *deque = newNode; 17 | } else { 18 | newNode->next = *deque; 19 | (*deque)->prev = newNode; 20 | *deque = newNode; 21 | } 22 | } 23 | 24 | void insertBack(Node** deque, int data) { 25 | Node* newNode = new Node(data); 26 | if (*deque == nullptr) { 27 | *deque = newNode; 28 | } else { 29 | Node* temp = *deque; 30 | while (temp->next != nullptr) { 31 | temp = temp->next; 32 | } 33 | temp->next = newNode; 34 | newNode->prev = temp; 35 | } 36 | } 37 | 38 | void printDeque(Node* deque) { 39 | Node* temp = deque; 40 | while (temp != nullptr) { 41 | cout << temp->data << " "; 42 | temp = temp->next; 43 | } 44 | cout << endl; 45 | } 46 | 47 | int main() { 48 | Node* deque = nullptr; 49 | 50 | int element; 51 | cin >> element; 52 | insertFront(&deque, element); 53 | 54 | cin >> element; 55 | insertFront(&deque, element); 56 | 57 | cin >> element; 58 | insertBack(&deque, element); 59 | 60 | cin >> element; 61 | insertBack(&deque, element); 62 | 63 | cin >> element; 64 | insertFront(&deque, element); 65 | 66 | cin >> element; 67 | insertBack(&deque, element); 68 | 69 | printDeque(deque); 70 | 71 | return 0; 72 | } -------------------------------------------------------------------------------- /DPP/Unit 2/Dhanush.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node* next; 8 | 9 | Node(int value) { 10 | data = value; 11 | next = nullptr; 12 | } 13 | }; 14 | 15 | class LinkedList { 16 | public: 17 | Node* head; 18 | 19 | LinkedList() { 20 | head = nullptr; 21 | } 22 | 23 | void insertNode(int value, int position) { 24 | Node* newNode = new Node(value); 25 | if (position == 1) { 26 | newNode->next = head; 27 | head = newNode; 28 | return; 29 | } 30 | 31 | Node* current = head; 32 | int count = 1; 33 | while (current != nullptr && count < position - 1) { 34 | current = current->next; 35 | count++; 36 | } 37 | 38 | if (current == nullptr) { 39 | cout << "Invalid position." << endl; 40 | return; 41 | } 42 | 43 | newNode->next = current->next; 44 | current->next = newNode; 45 | } 46 | 47 | void printList() { 48 | Node* current = head; 49 | while (current != nullptr) { 50 | cout << current->data << " "; 51 | current = current->next; 52 | } 53 | } 54 | }; 55 | 56 | int main() { 57 | int n; 58 | cin >> n; 59 | LinkedList list; 60 | 61 | for (int i = 0; i < n; i++) { 62 | int value; 63 | cin >> value; 64 | list.insertNode(value, i + 1); 65 | } 66 | 67 | int position; 68 | cin >> position; 69 | int value; 70 | cin >> value; 71 | list.insertNode(value, position); 72 | list.printList(); 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /DPP/Unit 2/Student record system.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class StudentNode { 4 | public: 5 | int student_id; 6 | StudentNode* next; 7 | 8 | StudentNode(int id) : student_id(id), next(nullptr) {} 9 | }; 10 | 11 | StudentNode* createStudentRecords(int n, int* studentIds) { 12 | if (n == 0) { 13 | return nullptr; 14 | } 15 | 16 | StudentNode* head = new StudentNode(studentIds[0]); 17 | StudentNode* current = head; 18 | 19 | for (int i = 1; i < n; ++i) { 20 | StudentNode* newNode = new StudentNode(studentIds[i]); 21 | current->next = newNode; 22 | current = newNode; 23 | } 24 | 25 | return head; 26 | } 27 | 28 | StudentNode* deleteFirstStudent(StudentNode* head) { 29 | if (head == nullptr) { 30 | return nullptr; 31 | } 32 | 33 | StudentNode* newHead = head->next; 34 | delete head; 35 | return newHead; 36 | } 37 | 38 | void printStudentRecords(StudentNode* head) { 39 | StudentNode* current = head; 40 | while (current) { 41 | std::cout << current->student_id << " "; 42 | current = current->next; 43 | } 44 | } 45 | 46 | int main() { 47 | int n; 48 | std::cin >> n; 49 | int* studentIds = new int[n]; 50 | for (int i = 0; i < n; ++i) { 51 | std::cin >> studentIds[i]; 52 | } 53 | 54 | StudentNode* head = createStudentRecords(n, studentIds); 55 | 56 | head = deleteFirstStudent(head); 57 | 58 | printStudentRecords(head); 59 | 60 | while (head) { 61 | StudentNode* temp = head; 62 | head = head->next; 63 | delete temp; 64 | } 65 | 66 | delete[] studentIds; 67 | 68 | return 0; 69 | } 70 | 71 | 72 | //Sushant 73 | -------------------------------------------------------------------------------- /DPP/Unit 3/Nandha.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | #include 3 | using namespace std; 4 | 5 | struct Node { 6 | int data; 7 | Node* next; 8 | }; 9 | 10 | class Stack { 11 | private: 12 | Node* top; 13 | int size; 14 | 15 | public: 16 | Stack() { 17 | top = nullptr; 18 | size = 0; 19 | } 20 | 21 | void push(int data) { 22 | Node* newNode = new Node(); 23 | newNode->data = data; 24 | newNode->next = top; 25 | top = newNode; 26 | size++; 27 | } 28 | 29 | void pop() { 30 | if (isEmpty()) { 31 | cout << "Stack is empty" << endl; 32 | return; 33 | } 34 | Node* temp = top; 35 | top = top->next; 36 | delete temp; 37 | size--; 38 | } 39 | 40 | void display() { 41 | Node* current = top; 42 | while (current != nullptr) { 43 | cout << current->data << " "; 44 | current = current->next; 45 | } 46 | cout << endl; 47 | } 48 | 49 | void displayTop() { 50 | if (isEmpty()) { 51 | cout << "Stack is empty" << endl; 52 | } else { 53 | cout << "Top element is " << top->data; 54 | } 55 | } 56 | 57 | bool isEmpty() { 58 | return size == 0; 59 | } 60 | }; 61 | 62 | int main() { 63 | Stack stack; 64 | 65 | int n; 66 | cin >> n; 67 | 68 | for (int i = 0; i < n; i++) { 69 | int data; 70 | cin >> data; 71 | stack.push(data); 72 | } 73 | 74 | stack.display(); 75 | stack.displayTop(); 76 | 77 | stack.pop(); 78 | 79 | stack.display(); 80 | stack.displayTop(); 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /DPP/Unit 4/rearrangeNegativeBeforePositive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // Function to rearrange the elements so that negative numbers appear before positive numbers 7 | void rearrangeNegativesBeforePositives(int arr[], int n) { 8 | int i = 0; 9 | int j = n - 1; 10 | 11 | while (i <= j) { 12 | if (arr[i] < 0 && arr[j] >= 0) { 13 | swap(arr[i], arr[j]); 14 | i++; 15 | j--; 16 | } else if (arr[i] >= 0) { 17 | i++; 18 | } else { 19 | j--; 20 | } 21 | } 22 | } 23 | 24 | // Function to partition the array for Quick Sort 25 | int partition(int arr[], int low, int high) { 26 | int pivot = arr[high]; 27 | int i = low - 1; 28 | 29 | for (int j = low; j <= high - 1; j++) { 30 | if (arr[j] <= pivot) { 31 | i++; 32 | swap(arr[i], arr[j]); 33 | } 34 | } 35 | 36 | swap(arr[i + 1], arr[high]); 37 | return i + 1; 38 | } 39 | 40 | // Function to perform Quick Sort on the rearranged array 41 | void quickSort(int arr[], int low, int high) { 42 | if (low < high) { 43 | int pi = partition(arr, low, high); 44 | 45 | quickSort(arr, low, pi - 1); 46 | quickSort(arr, pi + 1, high); 47 | } 48 | } 49 | 50 | int main() { 51 | int n; 52 | cin >> n; 53 | 54 | int arr[n]; 55 | 56 | for (int i = 0; i < n; i++) { 57 | cin >> arr[i]; 58 | } 59 | 60 | rearrangeNegativesBeforePositives(arr, n); 61 | quickSort(arr, 0, n - 1); 62 | 63 | for (int i = 0; i < n; i++) { 64 | cout << arr[i] << " "; 65 | } 66 | 67 | cout << endl; 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /DPP/Unit 3/Laddu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define MAX_SIZE 10 6 | 7 | bool isEmpty(int top) { 8 | return (top == -1); 9 | } 10 | 11 | bool isFull(int top) { 12 | return (top == MAX_SIZE - 1); 13 | } 14 | 15 | void push(int stack[], int &top, int item) { 16 | if (isFull(top)) { 17 | cout << "Stack is full!" << endl; 18 | return; 19 | } 20 | stack[++top] = item; 21 | } 22 | 23 | int pop(int stack[], int &top) { 24 | if (isEmpty(top)) { 25 | cout << "Stack is empty!" << endl; 26 | return -1; 27 | } 28 | return stack[top--]; 29 | } 30 | 31 | int main() { 32 | int stack[MAX_SIZE]; 33 | int top = -1; 34 | int choice, item; 35 | 36 | while (true) { 37 | cin >> choice; 38 | 39 | switch (choice) { 40 | case 1: 41 | cin >> item; 42 | push(stack, top, item); 43 | break; 44 | case 2: 45 | pop(stack, top); 46 | break; 47 | case 3: 48 | if (isFull(top)) { 49 | cout << "Stack is full!" << endl; 50 | } else { 51 | cout << "Stack is not full." << endl; 52 | } 53 | break; 54 | case 4: 55 | if (isEmpty(top)) { 56 | cout << "Stack is empty!" << endl; 57 | } else { 58 | cout << "Stack is not empty." << endl; 59 | } 60 | break; 61 | case 5: 62 | exit(0); 63 | default: 64 | cout << "Invalid choice" << endl; 65 | } 66 | } 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /DPP/Unit 2/text-editing-application_INDEX.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Node { 4 | char data; 5 | Node* next; 6 | }; 7 | 8 | class GroundedHeaderLinkedList { 9 | private: 10 | Node* header; 11 | int length; 12 | 13 | public: 14 | GroundedHeaderLinkedList() { 15 | header = new Node(); 16 | header->next = nullptr; 17 | length = 0; 18 | } 19 | 20 | void insertCharacter(int index, char character) { 21 | if (index < 0 || index > length) { 22 | std::cout << "Invalid position." << std::endl; 23 | return; 24 | } 25 | 26 | Node* newNode = new Node(); 27 | newNode->data = character; 28 | 29 | Node* current = header; 30 | for (int i = 0; i < index; ++i) { 31 | current = current->next; 32 | } 33 | 34 | newNode->next = current->next; 35 | current->next = newNode; 36 | ++length; 37 | } 38 | 39 | void displayList() { 40 | Node* current = header->next; 41 | std::cout << "Updated list: "; 42 | while (current != nullptr) { 43 | std::cout << current->data << ' '; 44 | current = current->next; 45 | } 46 | std::cout << std::endl; 47 | } 48 | }; 49 | 50 | int main() { 51 | int n; 52 | std::cin >> n; 53 | 54 | GroundedHeaderLinkedList linkedList; 55 | 56 | char character; 57 | for (int i = 0; i < n; ++i) { 58 | std::cin >> character; 59 | linkedList.insertCharacter(i, character); 60 | } 61 | 62 | int index; 63 | std::cin >> index; 64 | std::cin >> character; 65 | 66 | linkedList.insertCharacter(index, character); 67 | linkedList.displayList(); 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /DPP/Unit 4/Dictionary.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | #define MAX_Char_LENGTH 30 6 | 7 | void merge(char arr[][MAX_Char_LENGTH], int left, int mid, int right) { 8 | int n1 = mid - left + 1; 9 | int n2 = right - mid; 10 | 11 | char L[n1][MAX_Char_LENGTH], R[n2][MAX_Char_LENGTH]; 12 | 13 | for (int i = 0; i < n1; i++) { 14 | strcpy(L[i], arr[left + i]); 15 | } 16 | for (int i = 0; i < n2; i++) { 17 | strcpy(R[i], arr[mid + 1 + i]); 18 | } 19 | 20 | int i = 0, j = 0, k = left; 21 | while (i < n1 && j < n2) { 22 | if (strcmp(L[i], R[j]) <= 0) { 23 | strcpy(arr[k], L[i]); 24 | i++; 25 | } else { 26 | strcpy(arr[k], R[j]); 27 | j++; 28 | } 29 | k++; 30 | } 31 | 32 | while (i < n1) { 33 | strcpy(arr[k], L[i]); 34 | i++; 35 | k++; 36 | } 37 | 38 | while (j < n2) { 39 | strcpy(arr[k], R[j]); 40 | j++; 41 | k++; 42 | } 43 | } 44 | 45 | void mergeSort(char arr[][MAX_Char_LENGTH], int left, int right) { 46 | if (left < right) { 47 | int mid = left + (right - left) / 2; 48 | mergeSort(arr, left, mid); 49 | mergeSort(arr, mid + 1, right); 50 | merge(arr, left, mid, right); 51 | } 52 | } 53 | 54 | int main() { 55 | int size; 56 | scanf("%d", &size); 57 | char arr[size][MAX_Char_LENGTH]; 58 | 59 | for (int i = 0; i < size; i++) { 60 | scanf("%s", arr[i]); 61 | } 62 | 63 | mergeSort(arr, 0, size - 1); 64 | 65 | printf("Sorted Characters: "); 66 | for (int i = 0; i < size; i++) { 67 | printf("%s ", arr[i]); 68 | } 69 | printf("\n"); 70 | 71 | return 0; 72 | } -------------------------------------------------------------------------------- /DPP/Unit 3/Meenu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const int MAX_SIZE = 100; 7 | 8 | bool isOperator(char ch) { 9 | if (ch == '+' || ch == '-' || ch == '*' || ch == '/') { 10 | return true; 11 | } 12 | return false; 13 | } 14 | 15 | int precedence(char ch) { 16 | if (ch == '+' || ch == '-') { 17 | return 1; 18 | } else if (ch == '*' || ch == '/') { 19 | return 2; 20 | } 21 | return 0; 22 | } 23 | 24 | void infixToPostfix(string infix, string& postfix) { 25 | char stack[MAX_SIZE]; 26 | int top = -1; 27 | 28 | for (int i = 0; i < infix.length(); i++) { 29 | char ch = infix[i]; 30 | if (isalnum(ch)) { 31 | postfix += ch; 32 | } else if (isOperator(ch)) { 33 | while (top >= 0 && stack[top] != '(' && precedence(ch) <= precedence(stack[top])) { 34 | postfix += stack[top]; 35 | top--; 36 | } 37 | top++; 38 | stack[top] = ch; 39 | } else if (ch == '(') { 40 | top++; 41 | stack[top] = ch; 42 | } else if (ch == ')') { 43 | while (top >= 0 && stack[top] != '(') { 44 | postfix += stack[top]; 45 | top--; 46 | } 47 | if (top >= 0 && stack[top] == '(') { 48 | top--; 49 | } 50 | } 51 | } 52 | 53 | while (top >= 0) { 54 | postfix += stack[top]; 55 | top--; 56 | } 57 | } 58 | 59 | int main() { 60 | string infix, postfix; 61 | cin >> infix; 62 | 63 | infixToPostfix(infix, postfix); 64 | 65 | cout << "Postfix expression: " << postfix << endl; 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /Self Paced/Stack_Data/Stack_Array/Stack_Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Stack 4 | { 5 | int size; 6 | int top; 7 | int *S; 8 | }; 9 | void create(struct Stack *st) 10 | { 11 | printf("Enter Size"); 12 | scanf("%d", &st->size); 13 | st->top = -1; 14 | st->S = (int *)malloc(st->size * sizeof(int)); 15 | } 16 | void Display(struct Stack st) 17 | { 18 | int i; 19 | for (i = st.top; i >= 0; i--) 20 | printf("%d ", st.S[i]); 21 | printf("\n"); 22 | } 23 | void push(struct Stack *st, int x) 24 | { 25 | if (st->top == st->size - 1) 26 | printf("Stack overflow\n"); 27 | else 28 | { 29 | st->top++; 30 | st->S[st->top] = x; 31 | } 32 | } 33 | int pop(struct Stack *st) 34 | { 35 | int x = -1; 36 | 37 | if (st->top == -1) 38 | printf("Stack Underflow\n"); 39 | else 40 | { 41 | x = st->S[st->top--]; 42 | } 43 | return x; 44 | } 45 | int peek(struct Stack st, int index) 46 | { 47 | int x = -1; 48 | if (st.top - index + 1 < 0) 49 | printf("Invalid Index \n"); 50 | x = st.S[st.top - index + 1]; 51 | 52 | return x; 53 | } 54 | int isEmpty(struct Stack st) 55 | { 56 | if (st.top == -1) 57 | return 1; 58 | return 0; 59 | } 60 | int isFull(struct Stack st) 61 | { 62 | return st.top == st.size - 1; 63 | } 64 | int stackTop(struct Stack st) 65 | { 66 | if (!isEmpty(st)) 67 | return st.S[st.top]; 68 | return -1; 69 | } 70 | int main() 71 | { 72 | struct Stack st; 73 | create(&st); 74 | 75 | push(&st, 10); 76 | push(&st, 20); 77 | push(&st, 30); 78 | push(&st, 40); 79 | 80 | printf("%d \n", peek(st, 2)); 81 | 82 | Display(st); 83 | 84 | return 0; 85 | } -------------------------------------------------------------------------------- /DPP/Unit 2/Text editing application.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Node { 4 | char data; 5 | Node* next; 6 | }; 7 | 8 | class GroundedHeaderLinkedList { 9 | private: 10 | Node* header; 11 | int length; 12 | 13 | public: 14 | GroundedHeaderLinkedList() { 15 | header = new Node(); 16 | header->next = nullptr; 17 | length = 0; 18 | } 19 | 20 | void insertCharacter(int index, char character) { 21 | if (index < 0 || index > length) { 22 | std::cout << "Invalid position." << std::endl; 23 | return; 24 | } 25 | 26 | Node* newNode = new Node(); 27 | newNode->data = character; 28 | 29 | Node* current = header; 30 | for (int i = 0; i < index; ++i) { 31 | current = current->next; 32 | } 33 | 34 | newNode->next = current->next; 35 | current->next = newNode; 36 | ++length; 37 | } 38 | 39 | void displayList() { 40 | Node* current = header->next; 41 | std::cout << "Updated list: "; 42 | while (current != nullptr) { 43 | std::cout << current->data << ' '; 44 | current = current->next; 45 | } 46 | std::cout << std::endl; 47 | } 48 | }; 49 | 50 | int main() { 51 | int n; 52 | std::cin >> n; 53 | 54 | GroundedHeaderLinkedList linkedList; 55 | 56 | char character; 57 | for (int i = 0; i < n; ++i) { 58 | std::cin >> character; 59 | linkedList.insertCharacter(i, character); 60 | } 61 | 62 | int index; 63 | std::cin >> index; 64 | std::cin >> character; 65 | 66 | linkedList.insertCharacter(index, character); 67 | linkedList.displayList(); 68 | 69 | return 0; 70 | } 71 | 72 | 73 | //Susahnt -------------------------------------------------------------------------------- /DPP/Unit 2/Imagine teacher seating arrangement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node* next; 8 | 9 | Node(int data) { 10 | this->data = data; 11 | this->next = nullptr; 12 | } 13 | }; 14 | 15 | class LinkedList { 16 | public: 17 | Node* head; 18 | 19 | LinkedList() { 20 | head = nullptr; 21 | } 22 | 23 | void insert(int data) { 24 | Node* newNode = new Node(data); 25 | if (head == nullptr) { 26 | head = newNode; 27 | } else { 28 | Node* current = head; 29 | while (current->next != nullptr) { 30 | current = current->next; 31 | } 32 | current->next = newNode; 33 | } 34 | } 35 | 36 | void swapPairwise() { 37 | Node* current = head; 38 | while (current != nullptr && current->next != nullptr) { 39 | swap(current->data, current->next->data); 40 | current = current->next->next; 41 | } 42 | } 43 | 44 | void display() { 45 | Node* current = head; 46 | while (current != nullptr) { 47 | cout << current->data << " "; 48 | current = current->next; 49 | } 50 | cout << endl; 51 | } 52 | }; 53 | 54 | int main() { 55 | LinkedList list; 56 | int n, data; 57 | cin >> n; 58 | 59 | for (int i = 0; i < n; i++) { 60 | cin >> data; 61 | list.insert(data); 62 | } 63 | 64 | cout << "Linked list before swapping pairwise: "; 65 | list.display(); 66 | 67 | list.swapPairwise(); 68 | 69 | cout << "Linked list after swapping pairwise: "; 70 | list.display(); 71 | 72 | return 0; 73 | } 74 | 75 | 76 | //Sushant -------------------------------------------------------------------------------- /DPP/Unit 2/Web analytics.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | struct Node { 10 | string data; 11 | Node* prev; 12 | Node* next; 13 | Node(const string& value) : data(value), prev(nullptr), next(nullptr) {} 14 | }; 15 | 16 | int countUniqueInteractions(Node* head) { 17 | unordered_set uniqueSessions; 18 | int count = 0; 19 | 20 | Node* current = head; 21 | 22 | while (current != nullptr) { 23 | if (uniqueSessions.find(current->data) == uniqueSessions.end()) { 24 | uniqueSessions.insert(current->data); 25 | count++; 26 | } 27 | current = current->next; 28 | } 29 | 30 | return count; 31 | } 32 | 33 | int main() { 34 | int N; 35 | cin >> N; 36 | cin.ignore(); 37 | 38 | Node* head = nullptr; 39 | Node* tail = nullptr; 40 | 41 | for (int i = 0; i < N; i++) { 42 | string sessionID; 43 | getline(cin, sessionID); 44 | 45 | Node* newNode = new Node(sessionID); 46 | 47 | if (tail != nullptr) { 48 | tail->next = newNode; 49 | newNode->prev = tail; 50 | } else { 51 | head = newNode; 52 | } 53 | 54 | tail = newNode; 55 | } 56 | 57 | int uniqueCount = countUniqueInteractions(head); 58 | 59 | cout << "Number of unique user interactions: " << uniqueCount << endl; 60 | 61 | Node* current = head; 62 | while (current != nullptr) { 63 | Node* temp = current; 64 | current = current->next; 65 | delete temp; 66 | } 67 | 68 | return 0; 69 | } 70 | 71 | 72 | 73 | ///Sushant -------------------------------------------------------------------------------- /DPP/Unit 3/responsibility array based methodology.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | class Queue { 6 | private: 7 | int front; 8 | int rear; 9 | int maxSize; 10 | int* arr; 11 | 12 | public: 13 | Queue(int size) { 14 | maxSize = size; 15 | arr = new int[maxSize]; 16 | front = -1; 17 | rear = -1; 18 | } 19 | 20 | bool isFull() { 21 | return (rear == maxSize - 1); 22 | } 23 | 24 | bool isEmpty() { 25 | return (front == -1); 26 | } 27 | 28 | void enQueue(int data) { 29 | if (isFull()) { 30 | cout << "Queue is full. Cannot enqueue." << endl; 31 | } else { 32 | arr[++rear] = data; 33 | if (front == -1) { 34 | front = 0; 35 | } 36 | } 37 | } 38 | 39 | int deQueue() { 40 | if (isEmpty()) { 41 | cout << "Queue is empty. Cannot dequeue." << endl; 42 | return -1; // Return a placeholder value indicating an error 43 | } else { 44 | int data = arr[front]; 45 | if (front == rear) { 46 | front = rear = -1; // Reset front and rear when the last element is dequeued 47 | } else { 48 | front++; 49 | } 50 | return data; 51 | } 52 | } 53 | }; 54 | 55 | int main() { 56 | int N; 57 | cin >> N; 58 | 59 | Queue q(N); 60 | 61 | for (int i = 0; i < N; i++) { 62 | int num; 63 | cin >> num; 64 | q.enQueue(num); 65 | } 66 | 67 | cout << "Dequeuing elements:"; 68 | while (!q.isEmpty()) { 69 | int element = q.deQueue(); 70 | cout << " " << element; 71 | } 72 | cout << endl; 73 | 74 | return 0; 75 | } -------------------------------------------------------------------------------- /DPP/Unit 2/Vijay.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | #include 3 | using namespace std; 4 | 5 | class Node { 6 | public: 7 | int data; 8 | Node* next; 9 | 10 | Node(int value) { 11 | data = value; 12 | next = nullptr; 13 | } 14 | }; 15 | 16 | class LinkedList { 17 | public: 18 | Node* head; 19 | LinkedList() { 20 | head = nullptr; 21 | } 22 | 23 | void appendLeft(int value) { 24 | Node* newNode = new Node(value); 25 | newNode->next = head; 26 | head = newNode; 27 | } 28 | 29 | void appendRight(int value) { 30 | Node* newNode = new Node(value); 31 | if (!head) { 32 | head = newNode; 33 | return; 34 | } 35 | Node* current = head; 36 | while (current->next) { 37 | current = current->next; 38 | } 39 | current->next = newNode; 40 | } 41 | 42 | void printList() { 43 | Node* current = head; 44 | cout << "Linked List: "; 45 | while (current) { 46 | cout << current->data << " "; 47 | current = current->next; 48 | } 49 | cout << endl; 50 | } 51 | }; 52 | 53 | int main() { 54 | LinkedList linkedList; 55 | int choice, value; 56 | while (true) { 57 | cin >> choice; 58 | if (choice == 1) { 59 | cin >> value; 60 | linkedList.appendLeft(value); 61 | } else if (choice == 2) { 62 | cin >> value; 63 | linkedList.appendRight(value); 64 | } else if (choice == 3) { 65 | linkedList.printList(); 66 | } else if (choice == 4) { 67 | break; 68 | } else { 69 | cout << "Invalid choice" << endl; 70 | } 71 | } 72 | return 0; 73 | } 74 | 75 | -------------------------------------------------------------------------------- /DPP/Unit 3/generate and print binary sequence user's input.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | }; 8 | 9 | struct Queue { 10 | struct Node* front; 11 | struct Node* rear; 12 | }; 13 | 14 | struct Node* createNode(int data) { 15 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 16 | newNode->data = data; 17 | newNode->next = NULL; 18 | return newNode; 19 | } 20 | 21 | struct Queue* createQueue() { 22 | struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue)); 23 | queue->front = queue->rear = NULL; 24 | return queue; 25 | } 26 | 27 | void enqueue(struct Queue* queue, int data) { 28 | struct Node* newNode = createNode(data); 29 | if (queue->rear == NULL) { 30 | queue->front = queue->rear = newNode; 31 | return; 32 | } 33 | queue->rear->next = newNode; 34 | queue->rear = newNode; 35 | } 36 | 37 | int dequeue(struct Queue* queue) { 38 | if (queue->front == NULL) { 39 | return -1; 40 | } 41 | int data = queue->front->data; 42 | struct Node* temp = queue->front; 43 | queue->front = queue->front->next; 44 | if (queue->front == NULL) { 45 | queue->rear = NULL; 46 | } 47 | free(temp); 48 | return data; 49 | } 50 | 51 | void generateBinarySequence(int N) { 52 | struct Queue* queue = createQueue(); 53 | enqueue(queue, 1); 54 | 55 | for (int i = 0; i < N; i++) { 56 | int front = dequeue(queue); 57 | printf("%d ", front); 58 | 59 | enqueue(queue, front * 10); 60 | enqueue(queue, front * 10 + 1); 61 | } 62 | } 63 | 64 | int main() { 65 | int N; 66 | scanf("%d", &N); 67 | 68 | generateBinarySequence(N); 69 | 70 | return 0; 71 | } -------------------------------------------------------------------------------- /DPP/Unit 3/ticket system for a popular music concert.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #define MAX_QUEUE_SIZE 100 5 | 6 | struct Queue { 7 | int front, rear; 8 | int data[MAX_QUEUE_SIZE]; 9 | }; 10 | 11 | void initializeQueue(struct Queue *q) { 12 | q->front = 0; 13 | q->rear = -1; 14 | } 15 | 16 | int isFull(struct Queue *q) { 17 | return q->rear == MAX_QUEUE_SIZE - 1; 18 | } 19 | 20 | int isEmpty(struct Queue *q) { 21 | return q->front > q->rear; 22 | } 23 | 24 | void enqueue(struct Queue *q, int item) { 25 | if (isFull(q)) { 26 | printf("Queue is full. Cannot enqueue.\n"); 27 | return; 28 | } 29 | q->data[++q->rear] = item; 30 | } 31 | 32 | void reverseArray(int arr[], int start, int end) { 33 | while (start < end) { 34 | int temp = arr[start]; 35 | arr[start] = arr[end]; 36 | arr[end] = temp; 37 | start++; 38 | end--; 39 | } 40 | } 41 | 42 | void reverseFirstK(struct Queue *q, int k) { 43 | if (isEmpty(q) || k <= 0 || k > (q->rear - q->front + 1)) { 44 | printf("Invalid reverse operation.\n"); 45 | return; 46 | } 47 | 48 | reverseArray(q->data, q->front, q->front + k - 1); 49 | } 50 | 51 | void printQueue(struct Queue *q) { 52 | for (int i = q->front; i <= q->rear; i++) { 53 | printf("%d ", q->data[i]); 54 | } 55 | printf("\n"); 56 | } 57 | 58 | int main() { 59 | struct Queue ticketQueue; 60 | initializeQueue(&ticketQueue); 61 | 62 | int n, k; 63 | scanf("%d", &n); 64 | scanf("%d", &k); 65 | 66 | for (int i = 0; i < n; i++) { 67 | int ticket; 68 | scanf("%d", &ticket); 69 | enqueue(&ticketQueue, ticket); 70 | } 71 | 72 | reverseFirstK(&ticketQueue, k); 73 | printQueue(&ticketQueue); 74 | 75 | return 0; 76 | } -------------------------------------------------------------------------------- /DPP/Unit 3/Nithish.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | bool isOperator(char c) { 9 | return (c == '+' || c == '-' || c == '*' || c == '/'); 10 | } 11 | 12 | int getPrecedence(char op) { 13 | if (op == '+' || op == '-') 14 | return 1; 15 | if (op == '*' || op == '/') 16 | return 2; 17 | return 0; 18 | } 19 | 20 | string infixToPostfix(const string& infix) { 21 | stack operatorStack; 22 | string postfix = ""; 23 | 24 | for (char c : infix) { 25 | if (isalnum(c)) { 26 | postfix += c; 27 | } else if (c == '(') { 28 | operatorStack.push(c); 29 | } else if (c == ')') { 30 | while (!operatorStack.empty() && operatorStack.top() != '(') { 31 | postfix += operatorStack.top(); 32 | operatorStack.pop(); 33 | } 34 | if (!operatorStack.empty() && operatorStack.top() == '(') { 35 | operatorStack.pop(); // Pop '(' from the stack 36 | } 37 | } else if (isOperator(c)) { 38 | while (!operatorStack.empty() && operatorStack.top() != '(' && 39 | getPrecedence(c) <= getPrecedence(operatorStack.top())) { 40 | postfix += operatorStack.top(); 41 | operatorStack.pop(); 42 | } 43 | operatorStack.push(c); 44 | } 45 | } 46 | 47 | while (!operatorStack.empty()) { 48 | postfix += operatorStack.top(); 49 | operatorStack.pop(); 50 | } 51 | 52 | return postfix; 53 | } 54 | 55 | int main() { 56 | string infix; 57 | getline(cin, infix); 58 | 59 | string postfix = infixToPostfix(infix); 60 | 61 | cout << "Postfix expression: " << postfix << endl; 62 | 63 | return 0; 64 | } -------------------------------------------------------------------------------- /DPP/Unit 4/Binary Search Tree integer K.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Definition for a binary tree node. 5 | struct TreeNode { 6 | int val; 7 | TreeNode* left; 8 | TreeNode* right; 9 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 10 | }; 11 | 12 | int kthSmallest(TreeNode* root, int& k) { 13 | if (!root) { 14 | return -1; // Invalid case 15 | } 16 | 17 | // Inorder traversal to find kth smallest element 18 | int leftVal = kthSmallest(root->left, k); 19 | 20 | if (leftVal != -1) { 21 | return leftVal; 22 | } 23 | 24 | k--; 25 | 26 | if (k == 0) { 27 | return root->val; // Found the kth smallest element 28 | } 29 | 30 | return kthSmallest(root->right, k); 31 | } 32 | 33 | int main() { 34 | int root_val; 35 | cin >> root_val; 36 | 37 | TreeNode* root = new TreeNode(root_val); 38 | 39 | // Build the BST 40 | int val; 41 | while (cin >> val && val != -1) { 42 | TreeNode* newNode = new TreeNode(val); 43 | TreeNode* current = root; 44 | 45 | while (true) { 46 | if (val < current->val) { 47 | if (current->left) { 48 | current = current->left; 49 | } else { 50 | current->left = newNode; 51 | break; 52 | } 53 | } else { 54 | if (current->right) { 55 | current = current->right; 56 | } else { 57 | current->right = newNode; 58 | break; 59 | } 60 | } 61 | } 62 | } 63 | 64 | int k; 65 | cin >> k; 66 | 67 | int kth_smallest = kthSmallest(root, k); 68 | cout << kth_smallest << endl; 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /DPP/Unit 2/Imagine teacher preparing seating arrangement.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | Node* next; 7 | }; 8 | 9 | Node* createNode(int data) { 10 | Node* newNode = new Node; 11 | newNode->data = data; 12 | newNode->next = nullptr; 13 | return newNode; 14 | } 15 | 16 | Node* swapPairs(Node* head) { 17 | Node* dummy = createNode(-1); 18 | dummy->next = head; 19 | Node* prev = dummy; 20 | Node* current = head; 21 | 22 | while (current && current->next) { 23 | Node* first = current; 24 | Node* second = current->next; 25 | 26 | prev->next = second; 27 | first->next = second->next; 28 | second->next = first; 29 | 30 | prev = first; 31 | current = first->next; 32 | } 33 | 34 | return dummy->next; 35 | } 36 | 37 | void printList(Node* head) { 38 | Node* current = head; 39 | while (current) { 40 | std::cout << current->data << " "; 41 | current = current->next; 42 | } 43 | std::cout << std::endl; 44 | } 45 | 46 | int main() { 47 | int n; 48 | std::cin >> n; 49 | 50 | Node* head = nullptr; 51 | Node* tail = nullptr; 52 | 53 | for (int i = 0; i < n; ++i) { 54 | int data; 55 | std::cin >> data; 56 | 57 | Node* newNode = createNode(data); 58 | if (!head) { 59 | head = newNode; 60 | tail = newNode; 61 | } else { 62 | tail->next = newNode; 63 | tail = newNode; 64 | } 65 | } 66 | 67 | std::cout << "Linked list before swapping pairwise: "; 68 | printList(head); 69 | 70 | head = swapPairs(head); 71 | 72 | std::cout << "Linked list after swapping pairwise: "; 73 | printList(head); 74 | 75 | return 0; 76 | } 77 | 78 | 79 | //Sushant 80 | -------------------------------------------------------------------------------- /DPP/Unit 2/Sita.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | }; 8 | 9 | struct Node* createNode(int data) { 10 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 11 | newNode->data = data; 12 | newNode->next = NULL; 13 | return newNode; 14 | } 15 | 16 | void insertNode(struct Node** head, int data) { 17 | struct Node* newNode = createNode(data); 18 | if (*head == NULL) { 19 | *head = newNode; 20 | return; 21 | } 22 | struct Node* current = *head; 23 | while (current->next != NULL) { 24 | current = current->next; 25 | } 26 | current->next = newNode; 27 | } 28 | 29 | void removeDuplicates(struct Node* head) { 30 | struct Node* current = head; 31 | while (current != NULL && current->next != NULL) { 32 | if (current->data == current->next->data) { 33 | struct Node* temp = current->next; 34 | current->next = temp->next; 35 | free(temp); 36 | } else { 37 | current = current->next; 38 | } 39 | } 40 | } 41 | 42 | void printLinkedList(struct Node* head) { 43 | struct Node* current = head; 44 | while (current != NULL) { 45 | printf("%d ", current->data); 46 | current = current->next; 47 | } 48 | printf("\n"); 49 | } 50 | 51 | int main() { 52 | int n; 53 | scanf("%d", &n); 54 | struct Node* head = NULL; 55 | for (int i = 0; i < n; i++) { 56 | int data; 57 | scanf("%d", &data); 58 | insertNode(&head, data); 59 | } 60 | 61 | printf("Original Linked List: "); 62 | printLinkedList(head); 63 | 64 | removeDuplicates(head); 65 | 66 | printf("Linked List after removing duplicates: "); 67 | printLinkedList(head); 68 | 69 | return 0; 70 | } 71 | 72 | 73 | //Sushant -------------------------------------------------------------------------------- /DPP/Unit 3/Bindu.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | #include 3 | using namespace std; 4 | 5 | struct Node { 6 | int data; 7 | Node* next; 8 | }; 9 | 10 | class LinkedListStack { 11 | private: 12 | Node* top; 13 | int size; 14 | 15 | public: 16 | LinkedListStack() { 17 | top = nullptr; 18 | size = 0; 19 | } 20 | 21 | void push(int value) { 22 | Node* newNode = new Node(); 23 | newNode->data = value; 24 | newNode->next = top; 25 | top = newNode; 26 | size++; 27 | } 28 | 29 | int pop() { 30 | if (isEmpty()) { 31 | cout << "Stack is empty!" << endl; 32 | return -1; 33 | } else { 34 | Node* temp = top; 35 | int poppedValue = top->data; 36 | top = top->next; 37 | delete temp; 38 | size--; 39 | return poppedValue; 40 | } 41 | } 42 | 43 | 44 | bool isEmpty() { 45 | return top == nullptr; 46 | } 47 | 48 | 49 | bool isFull() { 50 | return size >= 32; 51 | } 52 | }; 53 | 54 | 55 | string decimalToBinary(int decimal) { 56 | LinkedListStack stack; 57 | while (decimal > 0) { 58 | int remainder = decimal % 2; 59 | stack.push(remainder); 60 | decimal /= 2; 61 | } 62 | 63 | string binary = ""; 64 | while (!stack.isEmpty()) { 65 | binary += to_string(stack.pop()); 66 | } 67 | 68 | return binary; 69 | } 70 | 71 | int main() { 72 | int decimal; 73 | cin >> decimal; 74 | 75 | if (decimal >= 1 && decimal <= 512) { 76 | string binary = decimalToBinary(decimal); 77 | cout << "Binary representation: " << binary << endl; 78 | } else { 79 | cout << "Invalid input: Decimal number must be between 1 and 512." << endl; 80 | } 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /DPP/Unit 4/Imagine you librarian.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node { 5 | int data; 6 | Node* left; 7 | Node* right; 8 | }; 9 | 10 | Node* newNode(int data) { 11 | Node* newNode = new Node(); 12 | newNode->data = data; 13 | newNode->left = nullptr; 14 | newNode->right = nullptr; 15 | return newNode; 16 | } 17 | 18 | Node* insert(Node* root, int data) { 19 | if (root == nullptr) { 20 | return newNode(data); 21 | } 22 | if (data < root->data) { 23 | root->left = insert(root->left, data); 24 | } else if (data > root->data) { 25 | root->right = insert(root->right, data); 26 | } 27 | return root; 28 | } 29 | 30 | void findPredecessor(Node* root, int val) { 31 | Node* predecessor = nullptr; 32 | while (root != nullptr) { 33 | if (val < root->data) { 34 | root = root->left; 35 | } else if (val > root->data) { 36 | predecessor = root; 37 | root = root->right; 38 | } else { 39 | if (root->left != nullptr) { 40 | Node* temp = root->left; 41 | while (temp->right != nullptr) { 42 | temp = temp->right; 43 | } 44 | predecessor = temp; 45 | } 46 | break; 47 | } 48 | } 49 | if (predecessor != nullptr) { 50 | cout << "Inorder Predecessor: " << predecessor->data << endl; 51 | } else { 52 | cout << "Doesn't exist" << endl; 53 | } 54 | } 55 | 56 | int main() { 57 | int n; 58 | cin >> n; 59 | 60 | Node* root = nullptr; 61 | 62 | for (int i = 0; i < n; i++) { 63 | int value; 64 | cin >> value; 65 | root = insert(root, value); 66 | } 67 | 68 | int m; 69 | cin >> m; 70 | 71 | findPredecessor(root, m); 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Self Paced/Stack_Data/Paranthesis matching/Paranthesis Match.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | struct Node 7 | { 8 | char data; 9 | struct Node *next; 10 | } * top = NULL; 11 | 12 | void push(char x) 13 | { 14 | struct Node *t; 15 | t = (struct Node *)malloc(sizeof(struct Node)); 16 | 17 | if (t == NULL) 18 | printf("stack is full\n"); 19 | else 20 | { 21 | t->data = x; 22 | t->next = top; 23 | top = t; 24 | } 25 | } 26 | 27 | char pop() 28 | { 29 | struct Node *t; 30 | char x = -1; 31 | 32 | if (top == NULL) 33 | printf("Stack is Empty\n"); 34 | else 35 | { 36 | t = top; 37 | top = top->next; 38 | x = t->data; 39 | free(t); 40 | } 41 | return x; 42 | } 43 | 44 | void Display() 45 | { 46 | struct Node *p; 47 | p = top; 48 | while (p != NULL) 49 | { 50 | printf("%c ", p->data); 51 | p = p->next; 52 | } 53 | printf("\n"); 54 | } 55 | 56 | int isBalanced(const char *exp) 57 | { 58 | int i; 59 | 60 | for (i = 0; exp[i] != '\0'; i++) 61 | { 62 | if (exp[i] == '(') 63 | push(exp[i]); 64 | else if (exp[i] == ')') 65 | { 66 | if (top == NULL) 67 | { 68 | printf("Not Balanced\n"); 69 | return 0; 70 | } 71 | pop(); 72 | } 73 | } 74 | if (top == NULL) 75 | { 76 | printf("Balanced\n"); 77 | return 1; 78 | } 79 | else 80 | { 81 | printf("Not Balanced\n"); 82 | return 0; 83 | } 84 | } 85 | 86 | int main() 87 | { 88 | char expression[100]; 89 | printf("Enter the expression: \n"); 90 | scanf("%s", expression); 91 | 92 | isBalanced(expression); 93 | 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /DPP/Unit 2/Program to maintain STUDENT ROSTER.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct Node { 7 | string name; 8 | Node* next; 9 | 10 | Node(const string& _name) : name(_name), next(nullptr) {} 11 | }; 12 | 13 | class StudentRoster { 14 | private: 15 | Node* head; 16 | 17 | public: 18 | StudentRoster() : head(nullptr) {} 19 | 20 | void insertNode(const string& newName, int position) { 21 | Node* newNode = new Node(newName); 22 | 23 | if (position == 1 || head == nullptr) { 24 | newNode->next = head; 25 | head = newNode; 26 | } else { 27 | Node* current = head; 28 | int currentPosition = 1; 29 | 30 | while (currentPosition < position - 1 && current->next != nullptr) { 31 | current = current->next; 32 | currentPosition++; 33 | } 34 | 35 | newNode->next = current->next; 36 | current->next = newNode; 37 | } 38 | } 39 | 40 | void printRoster() { 41 | Node* current = head; 42 | while (current != nullptr) { 43 | cout << current->name << " "; 44 | current = current->next; 45 | } 46 | cout << endl; 47 | } 48 | }; 49 | 50 | int main() { 51 | int n; 52 | cin >> n; 53 | 54 | StudentRoster roster; 55 | 56 | for (int i = 0; i < n; i++) { 57 | string name; 58 | cin >> name; 59 | roster.insertNode(name, i + 1); 60 | } 61 | 62 | string newName; 63 | int position; 64 | cin >> newName >> position; 65 | 66 | cout << "Current Linked List:" << endl; 67 | roster.printRoster(); 68 | 69 | roster.insertNode(newName, position); 70 | 71 | cout << "Updated Linked List:" << endl; 72 | roster.printRoster(); 73 | 74 | return 0; 75 | } 76 | 77 | 78 | 79 | //Sushant --------------------------------------------------------------------------------