Merge branch 'main' into add_fibonacchi_heap
commit
5e25eee553
|
@ -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.
|
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)_
|
||||||
|
|
||||||
<img align="right" width="300" src="https://user-images.githubusercontent.com/68538660/106238740-67a62b80-61cf-11eb-9892-6a0877a80fbf.png" alt="fork this repository" />
|
<img align="right" width="300" src="https://user-images.githubusercontent.com/68538660/106238740-67a62b80-61cf-11eb-9892-6a0877a80fbf.png" alt="fork this repository" />
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ git checkout -b add-new-file
|
||||||
|
|
||||||
## Make necessary changes and commit those changes
|
## 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.
|
||||||
|
|
||||||
<img align="right" width="450" src="https://firstcontributions.github.io/assets/Readme/git-status.png" alt="git status" />
|
<img align="right" width="450" src="https://firstcontributions.github.io/assets/Readme/git-status.png" alt="git status" />
|
||||||
|
|
||||||
|
|
|
@ -105,7 +105,7 @@ It can be any of the following ones
|
||||||
|
|
||||||
#### Source Code File
|
#### 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.
|
Again, the source codes must conform to a valid file structure convention that the programming language enforces.
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <algorithm>
|
||||||
|
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<<n<<endl;
|
||||||
|
sort(arr,arr+n);//sorting the array
|
||||||
|
//sort(a,a+n)-->a is the name of the array and n is the size of array a
|
||||||
|
cout<<arr[0]<<" --> smallest number "<<endl;
|
||||||
|
cout<<arr[n-1]<<" --> largest number "<<endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,60 @@
|
||||||
|
#include<iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
bool issafe(int** arr, int x, int y, int n){
|
||||||
|
if(x<n && y<n && arr[x][y]==1){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
bool ratinMaze(int** arr, int x, int y, int n, int** solArr){
|
||||||
|
if(x==n-1 && y==n-1){
|
||||||
|
solArr[x][y]=1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(issafe(arr, x, y, n)){
|
||||||
|
solArr[x][y]=1;
|
||||||
|
if(ratinMaze(arr, x+1, y, n, solArr)){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(ratinMaze(arr, x, y+1, n, solArr)){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
solArr[x][y]=0;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
int n;
|
||||||
|
cin>>n;
|
||||||
|
int** arr=new int*[n];
|
||||||
|
for(int i=0; i<n; i++){
|
||||||
|
arr[i]=new int[n];
|
||||||
|
}
|
||||||
|
for(int i=0; i<n; i++){
|
||||||
|
for(int j=0; j<n; j++){
|
||||||
|
cin>>arr[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int** solArr=new int*[n];
|
||||||
|
for(int i=0; i<n; i++){
|
||||||
|
solArr[i] = new int[n];
|
||||||
|
for(int j=0; j<n; j++){
|
||||||
|
solArr[i][j]=0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(ratinMaze(arr, 0, 0, n, solArr)){
|
||||||
|
for(int i=0; i<n; i++){
|
||||||
|
for(int j=0; j<n; j++){
|
||||||
|
cout<<solArr[i][j];
|
||||||
|
}cout<<endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Time complexity: O(2^(n^2)). The recursion can run upper-bound 2^(n^2) times.
|
||||||
|
Space Complexity: O(n^2). Output matrix is required so an extra space of size n*n is needed. */
|
|
@ -0,0 +1,79 @@
|
||||||
|
// Description := Reverse a linkedlist in groups of size K .
|
||||||
|
|
||||||
|
// Time and space complexity :-
|
||||||
|
// Time Complexity = O(N) and Space Complexity = O(N)
|
||||||
|
|
||||||
|
// Example :-
|
||||||
|
// Input: 1->2->3->4->5->6->7->8->NULL, K = 3
|
||||||
|
// Output: 3->2->1->6->5->4->8->7->NULL
|
||||||
|
|
||||||
|
#include<bits/stdc++.h>
|
||||||
|
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<<endl;
|
||||||
|
printList(head);
|
||||||
|
}
|
|
@ -33,7 +33,7 @@
|
||||||
- [Sparse Matrix](Arrays/sparse_matrix.cpp)
|
- [Sparse Matrix](Arrays/sparse_matrix.cpp)
|
||||||
- [Balanced Parenthesis](Arrays/balanced-parenthesis.cpp)
|
- [Balanced Parenthesis](Arrays/balanced-parenthesis.cpp)
|
||||||
- [Find special index](Arrays/specialindex2.cpp)
|
- [Find special index](Arrays/specialindex2.cpp)
|
||||||
|
- [Largest and smallest number in an array](Arrays/Largest-smallest.cpp)
|
||||||
|
|
||||||
## Dynamic-Programming
|
## Dynamic-Programming
|
||||||
|
|
||||||
|
@ -81,6 +81,7 @@
|
||||||
- [Segregate Even Odd Nodes of linked list](Linked-Lists/segregate-even-odd-nodes-of-linked-list.cpp)
|
- [Segregate Even Odd Nodes of linked list](Linked-Lists/segregate-even-odd-nodes-of-linked-list.cpp)
|
||||||
- [Remove Duplicate in Sorted linked list](Linked-Lists/remove-duplicates-in-sorted-linked-list.cpp)
|
- [Remove Duplicate in Sorted linked list](Linked-Lists/remove-duplicates-in-sorted-linked-list.cpp)
|
||||||
- [Reverse the linked list using stack](Linked-Lists/reverse-the-list-using-stack.cpp)
|
- [Reverse the linked list using stack](Linked-Lists/reverse-the-list-using-stack.cpp)
|
||||||
|
- [Reverse the linked list in groups of K](Linked-Lists/reverse-the-list-in-groups-of-k.cpp)
|
||||||
## Searching
|
## Searching
|
||||||
|
|
||||||
- [Linear Search](Searching/linear-search.cpp)
|
- [Linear Search](Searching/linear-search.cpp)
|
||||||
|
@ -136,6 +137,7 @@
|
||||||
- [Longest common prefix](Strings/longest-common-prefix.cpp)
|
- [Longest common prefix](Strings/longest-common-prefix.cpp)
|
||||||
- [First unique character in the string](Strings/first-unique-character.cpp)
|
- [First unique character in the string](Strings/first-unique-character.cpp)
|
||||||
- [Sliding Window to match target string](Strings/sliding-window.cpp)
|
- [Sliding Window to match target string](Strings/sliding-window.cpp)
|
||||||
|
- [Reverse String Wordwise](Strings/ReverseTheStringWordwise.cpp)
|
||||||
|
|
||||||
## Trees
|
## Trees
|
||||||
|
|
||||||
|
@ -160,6 +162,7 @@
|
||||||
- [Trie for searching](Trie/trie_search.cpp)
|
- [Trie for searching](Trie/trie_search.cpp)
|
||||||
- [Trie for insert search and prefix_search](Trie/trie_insert_search_startWith.cpp)
|
- [Trie for insert search and prefix_search](Trie/trie_insert_search_startWith.cpp)
|
||||||
- [Trie for delete](Trie/trie_delete.cpp)
|
- [Trie for delete](Trie/trie_delete.cpp)
|
||||||
|
- [Find Words Matching Pattern Dictionary](Trie/find_all_words_matching_pattern_in_given_dictionary.cpp)
|
||||||
|
|
||||||
# Maths
|
# Maths
|
||||||
|
|
||||||
|
@ -216,6 +219,8 @@
|
||||||
## Backtracking
|
## Backtracking
|
||||||
|
|
||||||
- [N-Queens Problem](Backtracking/n-queens.cpp)
|
- [N-Queens Problem](Backtracking/n-queens.cpp)
|
||||||
|
- [Rat In A Maze Problem](Backtracking/rat-in-a-maze-problem.cpp)
|
||||||
|
|
||||||
## Heaps
|
## Heaps
|
||||||
- [Fibonacci Heap](Heap/fibonacci_heap.cpp)
|
- [Fibonacci Heap](Heap/fibonacci_heap.cpp)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
// Description :- Given a string, the task is to reverse the order of the words in the given string.
|
||||||
|
// Example :-
|
||||||
|
// Input 1:
|
||||||
|
// A = "the sky is blue"
|
||||||
|
// Input 2:
|
||||||
|
// A = "this is ib"
|
||||||
|
// Output 1:
|
||||||
|
// "blue is sky the"
|
||||||
|
// Output 2:
|
||||||
|
// "ib is this"
|
||||||
|
|
||||||
|
|
||||||
|
// Time Complexity = O(N), Space Complexity = O(N)
|
||||||
|
|
||||||
|
#include<bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
string solve(string s) {
|
||||||
|
vector<string>v;
|
||||||
|
string str="";
|
||||||
|
for(int i=0;i<s.length();i++){
|
||||||
|
if(s[i]!=' '){
|
||||||
|
str+=s[i];
|
||||||
|
}
|
||||||
|
else if(str!="" && s[i]==' '){
|
||||||
|
v.push_back(str);
|
||||||
|
str="";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(str!=""){
|
||||||
|
v.push_back(str);
|
||||||
|
}
|
||||||
|
str="";
|
||||||
|
for(int i=v.size()-1;i>0;i--){
|
||||||
|
str+=v[i];
|
||||||
|
str+=' ';
|
||||||
|
}
|
||||||
|
str+=v[0];
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
string s;
|
||||||
|
getline(cin, s);
|
||||||
|
cout<<solve(s);
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,120 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <unordered_set>
|
||||||
|
#include <string>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// Data structure to store a Trie node
|
||||||
|
struct TrieNode
|
||||||
|
{
|
||||||
|
// each node stores a map to its child nodes
|
||||||
|
unordered_map<char, TrieNode*> 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<string> 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<string> 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<string> 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<string> dictionary {
|
||||||
|
"Hi", "HiTech", "HiTechCity", "Techie", "TechieDelight",
|
||||||
|
"Hello", "HelloWorld", "HiTechLab"
|
||||||
|
};
|
||||||
|
|
||||||
|
string pattern = "HT";
|
||||||
|
|
||||||
|
findAllWords(dictionary, pattern);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -13,7 +13,7 @@
|
||||||
1. Find the middle element of the array
|
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
|
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
|
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
|
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
|
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
|
> **Note:** The array should be sorted in ascending to descending order
|
||||||
|
@ -26,7 +26,7 @@ Element to search: **20**
|
||||||
|
|
||||||
Procedure:
|
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**
|
Middle element: **20** and yes the middle element is the key to found so return the index=**1**
|
||||||
|
|
||||||
|
|
|
@ -10,13 +10,20 @@ Linear search is usually very **simple to implement**.
|
||||||
## Steps/Algorithm:
|
## Steps/Algorithm:
|
||||||
**Linear Search( Array A, Value x)**
|
**Linear Search( Array A, Value x)**
|
||||||
|
|
||||||
Step 1: Set i to 1
|
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 2: if i > n then go to step 7
|
||||||
Step 4: Set i to i + 1
|
|
||||||
Step 5: Go to Step 2
|
Step 3: if A[i] = x then go to step 6
|
||||||
Step 6: Print Element x Found at index i and go to step 8
|
|
||||||
Step 7: Print element not found
|
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
|
Step 8: Exit
|
||||||
|
|
||||||
## Pseudocode
|
## Pseudocode
|
||||||
|
|
Loading…
Reference in New Issue