diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c714868c..b8565a3e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,7 +2,7 @@ This documentation aims to simplify and guide the way beginners make their first contribution. If you are looking to make your first contribution, follow the steps below. -_If you're not comfortable with command line, [here are tutorials using GUI tools.](#tutorials-using-other-tools)_ +_If you're not comfortable with the command line, [here are tutorials using GUI tools.](#tutorials-using-other-tools)_ fork this repository @@ -61,7 +61,7 @@ git checkout -b add-new-file ## Make necessary changes and commit those changes -Now open add or edit file in a text editor. Add code for any existing algorithm in other language or add some new algorithms. Make sure to update correspond README.md file if needed. Now, save the file. +Now open add or edit file in a text editor. Add code for any existing algorithm in other language or add some new algorithms. Make sure to update the corresponding README.md file if needed. Now, save the file. git status diff --git a/README.md b/README.md index c8d8263b..62ef05c0 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ It can be any of the following ones #### Source Code File -The source code files, should either be in `src/` folder (**Eg.** `src/main.cpp` or `src/main.js`) or the root folder (**Eg.** `palindrome.go` or `App.java`) where `ext` is the file extension for the specific programming language. +The source code files should either be in `src/` folder (**Eg.** `src/main.cpp` or `src/main.js`) or the root folder (**Eg.** `palindrome.go` or `App.java`) where `ext` is the file extension for the specific programming language. Again, the source codes must conform to a valid file structure convention that the programming language enforces. diff --git a/algorithms/CPlusPlus/Arrays/Largest-smallest.cpp b/algorithms/CPlusPlus/Arrays/Largest-smallest.cpp new file mode 100644 index 00000000..17b9829f --- /dev/null +++ b/algorithms/CPlusPlus/Arrays/Largest-smallest.cpp @@ -0,0 +1,20 @@ +#include +#include +using namespace std; + +//simple approach: +//sort the array in ascending order. +//the first element would be the smallest and the last element would be the largest + +int main() +{ + int arr[]={1,2,3,4,5}; + int n=sizeof(arr)/sizeof(arr[0]); + cout<a is the name of the array and n is the size of array a + cout< smallest number "< largest number "< +using namespace std; + +bool issafe(int** arr, int x, int y, int n){ + if(x>n; + int** arr=new int*[n]; + for(int i=0; i>arr[i][j]; + } + } + int** solArr=new int*[n]; + for(int i=0; i2->3->4->5->6->7->8->NULL, K = 3 +// Output: 3->2->1->6->5->4->8->7->NULL + +#include +using namespace std; + +class Node{ + public: + int data; + Node* next; +}; + +void push(Node* &head_ref, int new_data) +{ + Node* new_node = new Node(); + + new_node->data = new_data; + + new_node->next = head_ref; + + head_ref = new_node; +} + +void printList(Node* node) +{ + while (node != NULL) { + cout << node->data << " "; + node = node->next; + } +} + +Node* kReverse(Node* &head, int k) { + + // base case + if(head == NULL) { + return NULL; + } + + Node* next = NULL; + Node* curr = head; + Node* prev = NULL; + int count= 0; + + while( curr != NULL && count < k ) { + next = curr -> next; + curr -> next = prev; + prev = curr; + curr = next; + count++; + } + + if(next != NULL) { + head -> next = kReverse(next,k); + } + return prev; +} + +int main(){ + Node* head=NULL; + Node* ans =head; + push(head,1); + push(head,2); + push(head,3); + push(head,4); + push(head,5); + push(head,6); + push(head,7); + push(head,8); + printList(head); + head = kReverse(head,3); + cout< +using namespace std; + +string solve(string s) { + vectorv; + string str=""; + for(int i=0;i0;i--){ + str+=v[i]; + str+=' '; + } + str+=v[0]; + return str; +} + +int main() +{ + string s; + getline(cin, s); + cout< +#include +#include +#include +#include +using namespace std; + +// Data structure to store a Trie node +struct TrieNode +{ + // each node stores a map to its child nodes + unordered_map map; + + // true when the node is a leaf node + bool isLeaf = false; + + // collection to store a complete list of words in the leaf node + unordered_set word; +}; + +// Function to insert a string into a Trie +void insert(TrieNode*& head, string word) +{ + if (head == nullptr) { + head = new TrieNode(); + } + + // start from the head node + TrieNode* curr = head; + for (char c: word) + { + // insert only uppercase characters + if (isupper(c)) + { + // create a new node if the path doesn't exist + if (curr->map.find(c) == curr->map.end()) { + curr->map[c] = new TrieNode(); + } + + // go to the next node + curr = curr->map[c]; + } + } + + // mark the current node as a leaf + curr->isLeaf = true; + + // push the current word into the set associated with a leaf node + (curr->word).insert(word); +} + +// Function to print all children of a given Trie node +void printAllWords(TrieNode* root) +{ + // if the current node is a leaf, print all words associated with it + if (root->isLeaf) + { + unordered_set collection = root->word; + for (string s: collection) { + cout << s << endl; + } + } + + // recur for all children of the root node + for (auto pair: root->map) + { + TrieNode* child = pair.second; + if (child) { + printAllWords(child); + } + } +} + +// Function to print all words in the CamelCase dictionary, which +// matches the given pattern +void findAllWords(vector const &dictionary, string pattern) +{ + // base case + if (dictionary.size() == 0) { + return; + } + + // Trie head node + TrieNode* head = nullptr; + + // construct a Trie from the given dictionary + for (string s: dictionary) { + insert(head, s); + } + + // search for the given pattern in the Trie + TrieNode* curr = head; + for (char c: pattern) + { + // move to the child node + curr = curr->map[c]; + + // if the given pattern is not found (reached end of a path in the Trie) + if (curr == nullptr) { + return; + } + } + + // print all words matching the given pattern + printAllWords(curr); +} + +int main() +{ + vector dictionary { + "Hi", "HiTech", "HiTechCity", "Techie", "TechieDelight", + "Hello", "HelloWorld", "HiTechLab" + }; + + string pattern = "HT"; + + findAllWords(dictionary, pattern); + + return 0; +} diff --git a/docs/en/Searching/Binary-Search.MD b/docs/en/Searching/Binary-Search.MD index dbaadaac..6e4da5e1 100644 --- a/docs/en/Searching/Binary-Search.MD +++ b/docs/en/Searching/Binary-Search.MD @@ -13,7 +13,7 @@ 1. Find the middle element of the array 2. Check whether the key is equal to middle element if yes then return the index and exit the program 3. If the 2 step didn't run then test whether the element is less than the middle element if yes then run the step: 1 between the start to middle-1 index -4. If the 3 step didn't run then test whether the element is high than the middle element if yes then run the step: 1 between the middle+1 to last index. +4. If the 3 step didn't run then test whether the element is higher than the middle element if yes then run the step: 1 between the middle+1 to the last index. 5. Run the loop till the starting index is less than end index 6. If the loop over and data not found then return -1 that means data doesn't exist > **Note:** The array should be sorted in ascending to descending order @@ -26,7 +26,7 @@ Element to search: **20** Procedure: -Middle element:**30** and element is less then 30 so search between start to middle -1 index +Middle element:**30** and element is less than 30 so search between start to middle -1 index Middle element: **20** and yes the middle element is the key to found so return the index=**1** diff --git a/docs/en/Searching/Linear-Search.md b/docs/en/Searching/Linear-Search.md index 1d4487ad..26975c3c 100644 --- a/docs/en/Searching/Linear-Search.md +++ b/docs/en/Searching/Linear-Search.md @@ -10,13 +10,20 @@ Linear search is usually very **simple to implement**. ## Steps/Algorithm: **Linear Search( Array A, Value x)** -Step 1: Set i to 1 -Step 2: if i > n then go to step 7 -Step 3: if A[i] = x then go to step 6 -Step 4: Set i to i + 1 -Step 5: Go to Step 2 -Step 6: Print Element x Found at index i and go to step 8 -Step 7: Print element not found +Step 1: Set i to 1 + +Step 2: if i > n then go to step 7 + +Step 3: if A[i] = x then go to step 6 + +Step 4: Set i to i + 1 + +Step 5: Go to Step 2 + +Step 6: Print Element x Found at index i and go to step 8 + +Step 7: Print element not found + Step 8: Exit ## Pseudocode