From ed4c7ca890852b40642aff575987d16add3d7d4f Mon Sep 17 00:00:00 2001 From: naman_modi Date: Thu, 31 Oct 2019 14:46:20 +0530 Subject: [PATCH 1/2] Counting sort implementation in C++ --- Sorting/counting_sort.cpp | 91 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 Sorting/counting_sort.cpp diff --git a/Sorting/counting_sort.cpp b/Sorting/counting_sort.cpp new file mode 100644 index 0000000..bf5712c --- /dev/null +++ b/Sorting/counting_sort.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +vector split_string(string); + +vector countingSort(vector arr) { + int n = arr.size(); + vector ans(n); + vector temp(100); + int i, j, k = 0; + for(i = 0; i < 100; i++) + temp[i] = 0; + for(i = 0; i < n; i++) + temp[arr[i]]++; + for(i = 0; i < 100; i++) { + if(temp[i] == 0) + continue; + for(j = temp[i]; j > 0; j--) + ans[k++] = i; + } + return ans; +} + +int main() +{ + ofstream fout(getenv("OUTPUT_PATH")); + + int n; + cin >> n; + cin.ignore(numeric_limits::max(), '\n'); + + string arr_temp_temp; + getline(cin, arr_temp_temp); + + vector arr_temp = split_string(arr_temp_temp); + + vector arr(n); + + for (int i = 0; i < n; i++) { + int arr_item = stoi(arr_temp[i]); + + arr[i] = arr_item; + } + + vector result = countingSort(arr); + + for (int i = 0; i < result.size(); i++) { + fout << result[i]; + + if (i != result.size() - 1) { + fout << " "; + } + } + + fout << "\n"; + + fout.close(); + + return 0; +} + +vector split_string(string input_string) { + string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { + return x == y and x == ' '; + }); + + input_string.erase(new_end, input_string.end()); + + while (input_string[input_string.length() - 1] == ' ') { + input_string.pop_back(); + } + + vector splits; + char delimiter = ' '; + + size_t i = 0; + size_t pos = input_string.find(delimiter); + + while (pos != string::npos) { + splits.push_back(input_string.substr(i, pos - i)); + + i = pos + 1; + pos = input_string.find(delimiter, i); + } + + splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); + + return splits; +} + From f3f0c5f4dd57da7b2655a731aa35122a8827f3c6 Mon Sep 17 00:00:00 2001 From: naman_modi Date: Thu, 31 Oct 2019 15:07:04 +0530 Subject: [PATCH 2/2] Code for cycle detection in cpp. --- Singly Linked Lists/cycle_detection.cpp | 124 ++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 Singly Linked Lists/cycle_detection.cpp diff --git a/Singly Linked Lists/cycle_detection.cpp b/Singly Linked Lists/cycle_detection.cpp new file mode 100644 index 0000000..17c0a58 --- /dev/null +++ b/Singly Linked Lists/cycle_detection.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +class SinglyLinkedListNode { + public: + int data; + SinglyLinkedListNode *next; + + SinglyLinkedListNode(int node_data) { + this->data = node_data; + this->next = nullptr; + } +}; + +class SinglyLinkedList { + public: + SinglyLinkedListNode *head; + SinglyLinkedListNode *tail; + + SinglyLinkedList() { + this->head = nullptr; + this->tail = nullptr; + } + + void insert_node(int node_data) { + SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data); + + if (!this->head) { + this->head = node; + } else { + this->tail->next = node; + } + + this->tail = node; + } +}; + +void print_singly_linked_list(SinglyLinkedListNode* node, string sep, ofstream& fout) { + while (node) { + fout << node->data; + + node = node->next; + + if (node) { + fout << sep; + } + } +} + +void free_singly_linked_list(SinglyLinkedListNode* node) { + while (node) { + SinglyLinkedListNode* temp = node; + node = node->next; + + free(temp); + } +} + +bool has_cycle(SinglyLinkedListNode* head) { + set s; + SinglyLinkedListNode *curr = head; + while (curr != nullptr) { + auto it = s.find(curr); + if (it != s.end()) + return true; + s.insert(curr); + curr = curr->next; + } + return false; +} + +int main() +{ + ofstream fout(getenv("OUTPUT_PATH")); + + int tests; + cin >> tests; + cin.ignore(numeric_limits::max(), '\n'); + + for (int tests_itr = 0; tests_itr < tests; tests_itr++) { + int index; + cin >> index; + cin.ignore(numeric_limits::max(), '\n'); + + SinglyLinkedList* llist = new SinglyLinkedList(); + + int llist_count; + cin >> llist_count; + cin.ignore(numeric_limits::max(), '\n'); + + for (int i = 0; i < llist_count; i++) { + int llist_item; + cin >> llist_item; + cin.ignore(numeric_limits::max(), '\n'); + + llist->insert_node(llist_item); + } + + SinglyLinkedListNode* extra = new SinglyLinkedListNode(-1); + SinglyLinkedListNode* temp = llist->head; + + for (int i = 0; i < llist_count; i++) { + if (i == index) { + extra = temp; + } + + if (i != llist_count-1) { + temp = temp->next; + } + } + + temp->next = extra; + + bool result = has_cycle(llist->head); + + fout << result << "\n"; + } + + fout.close(); + + return 0; +} +