diff --git a/December 01/c++_dArK-sEiD05_01.cpp b/December 01/c++_dArK-sEiD05_01.cpp new file mode 100644 index 0000000..1f41503 --- /dev/null +++ b/December 01/c++_dArK-sEiD05_01.cpp @@ -0,0 +1,19 @@ +#include +#include +using namespace std; + +int findMissingNumber(int N, vector &array) { + int totalSum = N * (N + 1) / 2; + int arraySum = 0; + for (int num : array) { + arraySum += num; + } + return totalSum - arraySum; +} + +int main() { + int N = 5; + vector array = {1, 2, 4, 5}; + cout << findMissingNumber(N, array) << endl; + return 0; +} diff --git a/December 02/c++_dArK-sEiD05_02.cpp.cpp b/December 02/c++_dArK-sEiD05_02.cpp.cpp new file mode 100644 index 0000000..9851ec2 --- /dev/null +++ b/December 02/c++_dArK-sEiD05_02.cpp.cpp @@ -0,0 +1,31 @@ + +#include +#include +using namespace std; +int main() { + // Write C++ code here + vector arr= {10, 5, 6, 3, 2, 20, 100} +; + + + for (int i = 1; i < arr.size(); i += 2) { + + if(arr[i - 1] < arr[i]) { + swap(arr[i - 1], arr[i]); + } + + if(i+1arr[i + 1]) { + swap(arr[i], arr[i + 1]); + } + } + + + + for(int i=0;i +using namespace std; +int main() { + + int r,b; + cout<<"Enter red boxes:"; + cin>>r; + cout<<"Enter blue boxes:"; + cin>>b;cout< +using namespace std; + +int main() { + int n; + cout<<"Enter months:"; + cin>>n; + int i=1;int a=1;int b=1; + if(n==1 || n==2){cout<<1;} + while(i<=n-2){ + int c=a+b;a=b;b=c;i++; + } + cout< +using namespace std; + +int main() { + + int n,k; + cout<<"Enter persons:"; + cin>>n; + cout<<"Enter k:"; + cin>>k; + int i=1;int ans=0; + while(i<=5){ + ans=(ans+k)%i; + i++; + } + cout< +using namespace std; +#include +#include +int main() { + int n; + cout << "Enter the size of the vector: "; + cin >> n; + + vector vec(n); + cout<<"Enter target:"; + int tar; + cin>>tar; + + cout << "Enter " << n << " elements: "; + for (int i = 0; i < n; i++) { + cin >> vec[i]; + } + + vector> pairs; + unordered_map hm; + for(int i=0;i0){ + if(vec[i]*2==tar){ + if(hm[vec[i]]>1){ + pairs.push_back({vec[i],(tar-vec[i])}); + hm.erase(vec[i]); + + } + }else{ + pairs.push_back({vec[i],(tar-vec[i])}); + hm[vec[i]]--;hm[vec[i]-tar]--; + }} + } + for (const auto& p : pairs) { + cout << "(" << p.first << ", " << p.second << ")" << endl; + } + + return 0; +} \ No newline at end of file diff --git a/December 07/c++_dArK-sEiD05_07.cpp b/December 07/c++_dArK-sEiD05_07.cpp new file mode 100644 index 0000000..b668a06 --- /dev/null +++ b/December 07/c++_dArK-sEiD05_07.cpp @@ -0,0 +1,37 @@ + +#include +using namespace std; +#include +int main() { + int n; + cout << "Enter the size of the vector: "; + cin >> n; + vector> ans; + ans.push_back({1}); + for (int i=1;i<=n;i++){ + vector temp; + int x=0; + while(x<=i){ + int sum=0; + if (x < ans[i - 1].size()) { + sum += ans[i - 1][x]; + } + if (x - 1 >= 0) { + sum += ans[i - 1][x - 1]; + } + + temp.push_back(sum);x++; + } + ans.push_back(temp); + } + + + for(int i=0;i +using namespace std; +#include +int main() { + int n; + cout << "Enter the size: "; + cin >> n;int i=1;int sum=0; + while(i<=n){ + int k=i; + while(k>0){ + int x=k%10; + sum+=x*x; + k=k/10; + } + i++; + } + cout< +#include +#include +#include +using namespace std; + + +int main() { + + int n; + cout<<"Enter number of elements:"; + cin>>n; + vector vec(n); + + for(int i=0;i>vec[i]; + } + int cnt=0; + for(int i=0;i +#include +#include +#include +#include + +using namespace std; + +vector> findTaskOrder(const vector>> &tasks) { + unordered_map> graph; + unordered_map in_degree; + unordered_set all_tasks; + + for (const auto &task : tasks) { + string current = task.first; + all_tasks.insert(current); + + for (const string &dependency : task.second) { + graph[dependency].push_back(current); + in_degree[current]++; + all_tasks.insert(dependency); + } + } + + queue q; + for (const string &task : all_tasks) { + if (in_degree[task] == 0) { + q.push(task); + } + } + + vector> result; + + while (!q.empty()) { + vector concurrent_tasks; + int size = q.size(); + + for (int i = 0; i < size; ++i) { + string task = q.front(); + q.pop(); + concurrent_tasks.push_back(task); + + for (const string &neighbor : graph[task]) { + in_degree[neighbor]--; + if (in_degree[neighbor] == 0) { + q.push(neighbor); + } + } + } + + result.push_back(concurrent_tasks); + } + + for (const auto &entry : in_degree) { + if (entry.second > 0) { + throw runtime_error("Error: Cyclic dependency detected"); + } + } + + return result; +} + +int main() { + vector>> tasks = { + {"TaskA", {}}, + {"TaskB", {"TaskA"}}, + {"TaskC", {"TaskB"}}, + {"TaskD", {"TaskB", "TaskC"}} + }; + + try { + vector> order = findTaskOrder(tasks); + for (const auto &level : order) { + for (const string &task : level) { + cout << task << " "; + } + cout << endl; + } + } catch (const runtime_error &e) { + cout << e.what() << endl; + } + + return 0; +} diff --git a/December 11/c++_dArK-sEiD05_11.cpp b/December 11/c++_dArK-sEiD05_11.cpp new file mode 100644 index 0000000..1540fab --- /dev/null +++ b/December 11/c++_dArK-sEiD05_11.cpp @@ -0,0 +1,29 @@ +#include +#include +using namespace std; + +bool judgeCircle(string moves) { + int x = 0, y = 0; + for (char move : moves) { + if (move == 'R') x++; + else if (move == 'L') x--; + else if (move == 'U') y++; + else if (move == 'D') y--; + } + + return (x == 0 && y == 0); +} + +int main() { + string moves; + cout << "Enter the moves: "; + cin >> moves; + + if (judgeCircle(moves)) { + cout << "The robot returns to the origin." << endl; + } else { + cout << "The robot does not return to the origin." << endl; + } + + return 0; +} diff --git a/December 12/c++_dArK-sEiD05_12.cpp b/December 12/c++_dArK-sEiD05_12.cpp new file mode 100644 index 0000000..0348893 --- /dev/null +++ b/December 12/c++_dArK-sEiD05_12.cpp @@ -0,0 +1,74 @@ +// Online C++ compiler to run C++ program online +#include +#include +#include +#include +using namespace std; +vector splitStringBySpace(const string &str) { + vector tokens; + stringstream ss(str); + string word; + + + while (ss >> word) { + tokens.push_back(word); + } + return tokens; +} + +int main() { + + + vector ans; + + vector arr={"Eve 4","Diana 3 VIP","Adam 5","Frank 6 VIP"}; + + int total; + cout<<"Enter total tickets"; + cin>>total;bool visited[arr.size()]={false}; + for(int i=0;i0){ + vector result = splitStringBySpace(arr[i]); + if(result[result.size()-1]=="VIP"){ + string userw=result[1];int a=stoi(userw); + if(a>total){userw=to_string(total);} + string str=result[0]+" purchased "+userw+" tickets"; + ans.push_back(str); + total-=stoi(userw); + visited[i]=true; + } + }else{break;}} + for(int i=0;i result = splitStringBySpace(arr[i]); + if(total>0){ + + if(result[result.size()-1]!="VIP"){ + string userw=result[1];int a=stoi(userw); + if(a>total){userw=to_string(total);} + string str=result[0]+" purchased "+userw+" tickets"; + ans.push_back(str); + total-=stoi(userw);visited[i]=true; + + } + }else{ + break; + + }} + for(int i=0;i result = splitStringBySpace(arr[i]); + + string str=result[0]+" no tickets "; + ans.push_back(str);} + } + + for(int i=0;i +#include +#include +#include + +using namespace std; + +void swap(vector& arr, int i, int j) +{ + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; +} + + + +int minSwaps(vector arr, int N) +{ + int ans = 0; + vector temp = arr; + + + map h; + + sort(temp.begin(), temp.end()); + for (int i = 0; i < N; i++) { + h[arr[i]] = i; + } + for (int i = 0; i < N; i++) { + + + if (arr[i] != temp[i]) { + ans++; + int init = arr[i]; + + swap(arr, i, h[temp[i]]); + + h[init] = h[temp[i]]; + h[temp[i]] = i; + } + } + return ans; +} + + +int main() +{ int n; + cout<<"Enter length:"; + cin>>n; + vector a(n); + cout<<"Enter elements:"; + for(int i=0;i>a[i]; + } + + cout << minSwaps(a, n); +} diff --git a/December 14/c++_dArK-sEiD05_14.cpp b/December 14/c++_dArK-sEiD05_14.cpp new file mode 100644 index 0000000..a7d4167 --- /dev/null +++ b/December 14/c++_dArK-sEiD05_14.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +using namespace std; + +string canSplitSquad(int N, int K, int D, const vector &A) { + unordered_map subject_counts; + for (int subject : A) { + subject_counts[subject]++; + } + + int unique_subjects = subject_counts.size(); + if (unique_subjects < K) { + return "NO"; + } + + int max_team_size = (N + D) / 2; + int min_team_size = (N - D) / 2; + + int count_within_limit = 0; + for (const auto &entry : subject_counts) { + if (entry.second <= min_team_size) { + count_within_limit++; + } + } + + if (count_within_limit < K) { + return "NO"; + } + + return "YES"; +} + +int main() { + int N = 10, K = 3, D = 2; + vector A = {1, 2, 2, 3, 3, 3, 4, 5, 5, 5}; + cout << canSplitSquad(N, K, D, A) << endl; + return 0; +} diff --git a/December 15/c++_dArK-sEiD05_15.cpp b/December 15/c++_dArK-sEiD05_15.cpp new file mode 100644 index 0000000..3e48b62 --- /dev/null +++ b/December 15/c++_dArK-sEiD05_15.cpp @@ -0,0 +1,40 @@ +// Online C++ compiler to run C++ program online +#include +#include +#include +#include +using namespace std; + + +int main() { + + int trips=0;int n; + cout<<"Enter number of houses:"; + cin>>n; + vector houses(n); + + for(int i=0;i>houses[i]; + } + int w; + cout<<"Enter weight:"; + cin>>w; + int i=0; + while(i +#include +#include + +using namespace std; + +int min_platforms(vector& arrivals, vector& departures) { + sort(arrivals.begin(), arrivals.end()); + sort(departures.begin(), departures.end()); + + int platforms_needed = 0; + int current_platforms = 0; + + int i = 0, j = 0; + + while (i < arrivals.size()) { + if (arrivals[i] <= departures[j]) { + current_platforms++; + i++; + } else { + current_platforms--; + j++; + } + + platforms_needed = max(platforms_needed, current_platforms); + } + + return platforms_needed; +} + +int main() { + vector arrivals = {100, 180, 270, 320, 450}; + vector departures = {200, 250, 350, 400, 500}; + + cout << "Minimum platforms required: " << min_platforms(arrivals, departures) << endl; + + return 0; +} diff --git a/December 17/c++_dArK-sEiD05_17.cpp b/December 17/c++_dArK-sEiD05_17.cpp new file mode 100644 index 0000000..9982401 --- /dev/null +++ b/December 17/c++_dArK-sEiD05_17.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +class AlertManager { +private: + struct Alert { + chrono::system_clock::time_point timestamp; + int threat_level; + + bool operator>(const Alert& other) const { + return timestamp > other.timestamp; + } + }; + + unordered_map alerts; + priority_queue, vector>, greater<>> alert_queue; + unordered_map last_seen; + chrono::minutes alert_expiry_time = chrono::minutes(5); + chrono::seconds duplicate_time_limit = chrono::seconds(30); + + chrono::system_clock::time_point str_to_time(const string& timestamp) { + int hours, minutes, seconds; + sscanf(timestamp.c_str(), "%d:%d:%d", &hours, &minutes, &seconds); + auto time = chrono::hours(hours) + chrono::minutes(minutes) + chrono::seconds(seconds); + return chrono::system_clock::now() - time; + } + +public: + void process_alert(const string& alert_id, const string& timestamp, int threat_level) { + chrono::system_clock::time_point current_time = str_to_time(timestamp); + + if (last_seen.find(alert_id) != last_seen.end() && + chrono::duration_cast(current_time - last_seen[alert_id]) < duplicate_time_limit) { + return; // Skip duplicate alert + } + + if (alerts.find(alert_id) == alerts.end() || current_time > alerts[alert_id].timestamp) { + alerts[alert_id] = {current_time, threat_level}; + alert_queue.push({current_time, alert_id}); + } + + last_seen[alert_id] = current_time; + } + + vector>> get_alerts() { + chrono::system_clock::time_point current_time = chrono::system_clock::now(); + vector>> valid_alerts; + + while (!alert_queue.empty() && + chrono::duration_cast(current_time - alert_queue.top().first) > alert_expiry_time) { + string expired_alert_id = alert_queue.top().second; + alert_queue.pop(); + alerts.erase(expired_alert_id); // Remove expired alerts + } + + for (const auto& alert : alerts) { + const auto& alert_data = alert.second; + if (chrono::duration_cast(current_time - alert_data.timestamp) <= alert_expiry_time) { + valid_alerts.push_back({alert.first, {"Timestamp: " + to_string(chrono::duration_cast(alert_data.timestamp.time_since_epoch()).count()), alert_data.threat_level}}); + } + } + + return valid_alerts; + } +}; + +int main() { + AlertManager alert_manager; + + vector>> alerts = { + {"A123", {"00:00:10", 3}}, + {"A123", {"00:00:15", 3}}, + {"B456", {"00:00:20", 2}}, + {"A123", {"00:00:30", 5}}, + {"B456", {"00:05:05", 2}} + }; + + for (const auto& alert : alerts) { + alert_manager.process_alert(alert.first, alert.second.first, alert.second.second); + } + + vector>> stored_alerts = alert_manager.get_alerts(); + for (const auto& alert : stored_alerts) { + cout << "Alert ID: " << alert.first << ", Timestamp: " << alert.second.first + << ", Threat Level: " << alert.second.second << endl; + } + + return 0; +} diff --git a/December 18/c++_dArK-sEiD05_18.cpp b/December 18/c++_dArK-sEiD05_18.cpp new file mode 100644 index 0000000..0c69d83 --- /dev/null +++ b/December 18/c++_dArK-sEiD05_18.cpp @@ -0,0 +1,36 @@ +// Online C++ compiler to run C++ program online +#include +#include +using namespace std; +int main() { + // Write C++ code here + string str="RDEREDR";int start;int end;int max=-1; + for(int i=0;i<(str.size());i++){ + int k=i;int j=i;int mid=0;int curr=1; + while(k>=0 && j<(str.size()) ){ + + if(str[k]==str[j] ){ + curr+=2; + if(curr>max){ + start=k;end=j;mid=i;max=curr; + } + + } + k--;j++; + } + } + + int sum=0; + for(int i=start;i<=end;i++){ + if(str[i]=='D') + sum+=500; + + else if(str[i]=='R') + sum+=250; + else if(str[i]=='E') + sum+=100; + } + + cout< +#include +#include + +using namespace std; + +vector> tower_of_hanoi(int n, string source, string destination, string auxiliary) { + vector> moves; + + if (n == 0) { + return moves; + } + + vector> first_half = tower_of_hanoi(n - 1, source, auxiliary, destination); + moves.insert(moves.end(), first_half.begin(), first_half.end()); + + moves.push_back({source, destination}); + + vector> second_half = tower_of_hanoi(n - 1, auxiliary, destination, source); + moves.insert(moves.end(), second_half.begin(), second_half.end()); + + return moves; +} + +int main() { + int num_disks = 4; + string start = "A"; + string destination = "C"; + string auxiliary = "B"; + + vector> moves = tower_of_hanoi(num_disks, start, destination, auxiliary); + + cout << "Minimum number of moves: " << moves.size() << endl; + cout << "Sequence of moves:" << endl; + for (int i = 0; i < moves.size(); i++) { + cout << i + 1 << ". Move disk from " << moves[i].first << " to " << moves[i].second << endl; + } + + return 0; +} diff --git a/December 20/c++_dArK-sEiD05_20.cpp b/December 20/c++_dArK-sEiD05_20.cpp new file mode 100644 index 0000000..8f16496 --- /dev/null +++ b/December 20/c++_dArK-sEiD05_20.cpp @@ -0,0 +1,37 @@ +#include +#include +using namespace std; + +int countPermutations(vector& steps, int distance) { + // Create a DP array initialized to 0 + + vector dp(distance + 1, 0); + + // Base case: 1 way to reach distance 0 + dp[0] = 1; + + // Fill the DP array + for (int j = 1; j <= distance; j++) { + for (int s : steps) { + if (j >= s) { + dp[j] += dp[j - s]; + } + + } + cout<<"\n"; + cout< steps = {1, 2, 3}; // Example step sizes + int distance = 4; // Example target distance + + cout << "Number of distinct ways: " << countPermutations(steps, distance) << endl; + return 0; +} diff --git a/December 21/c++_dArK-sEiD05_21.cpp b/December 21/c++_dArK-sEiD05_21.cpp new file mode 100644 index 0000000..2c819e1 --- /dev/null +++ b/December 21/c++_dArK-sEiD05_21.cpp @@ -0,0 +1,79 @@ +// Online C++ compiler to run C++ program online +#include +using namespace std; + + +struct Node { + int data; + Node* next; + Node(int val) : data(val), next(nullptr) {} +}; + + +Node* createLinkedList(int arr[], int size) { + if (size == 0) return nullptr; + Node* head = new Node(arr[0]); + Node* current = head; + for (int i = 1; i < size; ++i) { + current->next = new Node(arr[i]); + current = current->next; + } + return head; +} +void attachLists(Node* list1, Node* list2, int position) { + if (position == 0) return; + Node* current = list1; + for (int i = 1; i < position && current != nullptr; ++i) { + current = current->next; + } + if (current != nullptr) { + Node* temp = list2; + while (temp->next != nullptr) { + temp = temp->next; + } + temp->next = current; + } +} + +int main() { + + int n; + cout << "Enter the number of nodes in the first linked list: "; + cin >> n; + int arr1[n]; + cout << "Enter the node values: "; + for (int i = 0; i < n; ++i) { + cin >> arr1[i]; + } + int m; + cout << "Enter the number of nodes in the second linked list: "; + cin >> m; + int arr2[m]; + cout << "Enter the node values: "; + for (int i = 0; i < m; ++i) { + cin >> arr2[i]; + } + int pos; + cout << "Enter the position of intersection (0 if no intersection): "; + cin >> pos; + + + Node* list1 = createLinkedList(arr1, n); + Node* list2 = createLinkedList(arr2, m); + + attachLists(list1, list2, pos); + + if (pos == 0) { + cout << "No intersection found." << endl; + } else { + Node* current = list1; + for (int i = 1; i < pos && current != nullptr; ++i) { + current = current->next; + } + if (current) { + cout << "The intersection point is: " << current->data << endl; + } + } + + return 0; +}