├── .vscode ├── launch.json └── settings.json ├── Backtracking ├── Hamiltonian-Cycle.cpp ├── Hamiltonian-Cycle.exe ├── M-Coloring-Problem.cpp ├── M-Coloring-Problem.exe ├── Rat-in-Maze.cpp └── Rat-in-Maze.exe ├── Binary Search ├── Binary-Search-on-Reverse-Sorted-Array.cpp ├── Binary-Search-on-Reverse-Sorted-Array.exe ├── Ceil-of-Element-in-Sorted-Array.cpp ├── Ceil-of-Element-in-Sorted-Array.exe ├── Count-of-Element-in-Sorted-Array.cpp ├── Count-of-Element-in-Sorted-Array.exe ├── Find-Element-in-Rotated-Sorted-Array.cpp ├── Find-Element-in-Rotated-Sorted-Array.exe ├── First-and-Last-Occurence.cpp ├── First-and-Last-Occurence.exe ├── Floor-of-Element-in-Sorted-Array.cpp ├── Floor-of-Element-in-Sorted-Array.exe ├── Index-of-First-1-in-Binary-Sorted-Infinite-Array.cpp ├── Next-Alphabetical-Element.cpp ├── Next-Alphabetical-Element.exe ├── Number-of-times-Sorted-Array-Rotated.cpp ├── Number-of-times-Sorted-Array-Rotated.exe ├── Order-not-Known-Search.exe ├── Order-not-known-Search.cpp ├── Position-of-Element-in-Infinite-Sorted-Array.cpp ├── Position-of-Element-in-Infinite-Sorted-Array.exe ├── Searching-in-Nearly-Sorted-Array.cpp └── Searching-in-Nearly-Sorted-Array.exe ├── Dynamic Programming ├── 0-1 Knapsack Problem.cpp ├── Coin Change I.cpp ├── Coin Change II.cpp ├── Count of Subset Sum Problem.cpp ├── Count of Subsets with given Difference.cpp ├── Equal Sum Partition Problem.cpp ├── Longest Common Subsequence.cpp ├── Longest Common Substring.cpp ├── Longest Palindromic Subsequence.cpp ├── Minimum Subset Sum Difference.cpp ├── Print Longest Common Subsequence.cpp ├── Rod Cutting Problem.cpp ├── Shortest Common Supersequence.cpp ├── Subset Sum Problem.cpp └── Unbounded Knapsack Problem.cpp ├── Graph ├── BFS.cpp ├── DFS.cpp ├── Matrix-Related-Problems │ ├── Far-From-Land.cpp │ ├── Flood-Fill.cpp │ ├── Max-Area-of-Island.cpp │ ├── Number-of-Closed-Islands.cpp │ ├── Number-of-Enclaves.cpp │ └── Number-of-Islands.cpp └── adjListNodes.cpp ├── Heap ├── Connect-Ropes-to-Minimize-Cost.cpp ├── Connect-Ropes-to-Minimize-Cost.exe ├── Frequency-Sort.cpp ├── Frequency-Sort.exe ├── K-Closest-Numbers.cpp ├── K-Closest-Numbers.exe ├── K-Closest-Points-To-Origin.cpp ├── K-Closest-Points-To-Origin.exe ├── K-Largest-Elements-in-Array.cpp ├── K-Largest-Elements-in-Array.exe ├── Kth-Largest-Element-in-Array.cpp ├── Kth-Largest-Element-in-Array.exe ├── Kth-Smallest-Element-in-Array.cpp ├── Kth-Smallest-Element-in-Array.exe ├── Maximum-Performance.cpp ├── Maximum-Performance.exe ├── Sort-a-Nearly-Sorted-Array.cpp ├── Sort-a-Nearly-Sorted-Array.exe ├── Sum-of-Elements-Between.cpp ├── Sum-of-Elements-Between.exe ├── Top-K-Frequent-Numbers.cpp └── Top-K-Frequent-Numbers.exe ├── Linked-Lists ├── Circular-LL-Implementation.cpp ├── Circular-LL-Implementation.exe ├── Detect-Loop-in-LL.cpp ├── Detect-Loop-in-LL.exe ├── Intersection-of-Sorted-LL.cpp ├── Intersection-of-Sorted-LL.exe ├── Length-of-Linked-List.cpp ├── Length-of-Linked-List.exe ├── Length-of-Loop-in-LL.cpp ├── Length-of-Loop-in-LL.exe ├── Merge-k-sorted-LL.cpp ├── Mid-Element-of-LL.cpp ├── Mid-Element-of-LL.exe ├── Nth-Node-from-End-in-LL.cpp ├── Nth-Node-from-End-in-LL.exe ├── Nth-node-in-LL.cpp ├── Nth-node-in-LL.exe ├── Remove-duplicates-from-sorted-LL.cpp ├── Remove-duplicates-from-sorted-LL.exe ├── Remove-loop-from-unsorted-LL.cpp ├── Remove-loop-from-unsorted-LL.exe ├── Reverse-of-LL.cpp ├── Reverse-of-LL.exe ├── Segregate-even-odd-in-LL.cpp ├── Segregate-even-odd-in-LL.exe ├── Singly-Linked-List-Implementation.cpp ├── Singly-Linked-List-Implementation.exe ├── Union-of-Sorted-LL.cpp └── Union-of-Sorted-LL.exe ├── README.md ├── Recursion ├── Balanced-Parentheses.cpp ├── Balanced-Parentheses.exe ├── Delete-Middle-Element-of-Stack.cpp ├── Delete-Middle-Element-of-Stack.exe ├── Height-of-Binary-Tree.cpp ├── Josephus-Problem.cpp ├── Josephus-Problem.exe ├── Kth-Symbol-in-Grammar.cpp ├── Kth-Symbol-in-Grammar.exe ├── Letter-Case-Permutation.cpp ├── Letter-Case-Permutation.exe ├── Permutation-with-CaseChange.cpp ├── Permutation-with-CaseChange.exe ├── Permutation-with-Spaces.cpp ├── Permutation-with-Spaces.exe ├── Print-N-bit-numbers-having-more-1s-more-than-0s-for-any-prefix.cpp ├── Print-N-bit-numbers-having-more-1s-more-than-0s-for-any-prefix.exe ├── Print-Powerset.cpp ├── Print-Powerset.exe ├── Print-Unique-Subsets.cpp ├── Print-Unique-Subsets.exe ├── Reverse-a-Stack.cpp ├── Reverse-a-Stack.exe ├── Sort-a-Stack.cpp ├── Sort-a-Stack.exe ├── Sort-an-Array-using-Recursion.cpp ├── Sort-an-Array-using-Recursion.exe ├── Tower-of-Hanoi.cpp └── Tower-of-Hanoi.exe ├── Searching Algorithms ├── Jump Search.cpp └── Linear Search.cpp ├── Sorting Algorithms ├── Heap Sort.cpp ├── Insertion Sort.cpp ├── Merge Sort.cpp ├── Quick Sort.cpp ├── Selection Sort.cpp └── Shell Sort.cpp ├── Stack ├── Maximum-Area-Histogram.cpp ├── Maximum-Area-Histogram.exe ├── Maximum-Area-Rectangle-in-Binary-Matrix.cpp ├── Maximum-Area-Rectangle-in-Binary-Matrix.exe ├── Maximum-of-all-Subarrays-of-size-K.cpp ├── Maximum-of-all-Subarrays-of-size-K.exe ├── Minimum-Element-in-Stack-with-Extra-Space.cpp ├── Minimum-Element-in-Stack-with-Extra-Space.exe ├── Minimum-Element-in-Stack-without-Extra-Space.cpp ├── Minimum-Element-in-Stack-without-Extra-Space.exe ├── Nearest-Greater-to-Right.cpp ├── Nearest-Greater-to-Right.exe ├── Nearest-Greater-to-left.cpp ├── Nearest-Greater-to-left.exe ├── Nearest-Smaller-to-Left.cpp ├── Nearest-Smaller-to-Left.exe ├── Nearest-Smaller-to-Right.cpp ├── Nearest-Smaller-to-Right.exe ├── Rainwater-Trapping-Problem.cpp ├── Rainwater-Trapping-Problem.exe ├── Stock-Span-Problem.cpp └── Stock-Span-Problem.exe ├── Tree ├── BST-Implementation.cpp ├── BST-Implementation.exe ├── Boundary-Traversal-of-Binary-Tree.cpp ├── Construct-Binary-tree-from-String.cpp ├── Construct-Binary-tree-from-String.exe ├── Convert-Binary-tree-into-Mirror-tree.cpp ├── Level-Order-Traversal.cpp ├── Lowest-Common-Ancestor-BST.cpp ├── Lowest-Common-Ancestor-in-BT.cpp ├── Maximum-Difference-between-Node-and-Ancestor.cpp ├── Maximum-Width-of-Binary-Tree.cpp ├── Path-Sum.cpp ├── Symmetric-Tree.cpp └── ZigZag-Traversal.cpp ├── Trie ├── Longest-Word-with-all-Prefixes.cpp ├── Longest-Word-with-all-Prefixes.exe ├── Trie-Implementation-2.cpp ├── Trie-Implementation-2.exe ├── Trie-Implementation.cpp └── Trie-Implementation.exe └── index.html /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "cl.exe - Build and debug active file", 9 | "type": "cppvsdbg", 10 | "request": "launch", 11 | "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", 12 | "args": [], 13 | "stopAtEntry": false, 14 | "cwd": "${fileDirname}", 15 | "environment": [], 16 | "console": "externalTerminal", 17 | "preLaunchTask": "C/C++: cl.exe build active file" 18 | } 19 | ] 20 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp", 4 | "vector": "cpp", 5 | "utility": "cpp", 6 | "ostream": "cpp", 7 | "algorithm": "cpp", 8 | "cstdlib": "cpp", 9 | "queue": "cpp", 10 | "xstddef": "cpp", 11 | "stack": "cpp", 12 | "xstring": "cpp", 13 | "cmath": "cpp", 14 | "cstddef": "cpp", 15 | "cstdint": "cpp", 16 | "cstdio": "cpp", 17 | "cstring": "cpp", 18 | "cwchar": "cpp", 19 | "deque": "cpp", 20 | "exception": "cpp", 21 | "initializer_list": "cpp", 22 | "iosfwd": "cpp", 23 | "istream": "cpp", 24 | "limits": "cpp", 25 | "list": "cpp", 26 | "map": "cpp", 27 | "new": "cpp", 28 | "stdexcept": "cpp", 29 | "string": "cpp", 30 | "tuple": "cpp", 31 | "type_traits": "cpp", 32 | "unordered_map": "cpp", 33 | "xhash": "cpp", 34 | "xmemory": "cpp", 35 | "xmemory0": "cpp", 36 | "xtr1common": "cpp", 37 | "xtree": "cpp", 38 | "xutility": "cpp", 39 | "ios": "cpp", 40 | "memory": "cpp", 41 | "streambuf": "cpp", 42 | "unordered_set": "cpp", 43 | "xiosbase": "cpp", 44 | "climits": "cpp", 45 | "xlocale": "cpp" 46 | }, 47 | "C_Cpp.errorSquiggles": "Disabled" 48 | } -------------------------------------------------------------------------------- /Backtracking/Hamiltonian-Cycle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | bool isSafe(int v, vector>& graph, vector& path, int n, int pos){ 7 | if(graph[path[pos-1]][v]==0){ 8 | return false; //if v is not adjacent to previous vertex 9 | } 10 | for(int i=0;i>& graph, vector& path, int n, int pos){ 19 | if(pos==n){ 20 | if(graph[path[pos-1]][path[0]]){ 21 | return true; //if there is an edge from last included vertex to first vertex 22 | } 23 | else{ 24 | return false; 25 | } 26 | } 27 | for(int v=1;v> & graph){ 41 | int n=graph[0].size(); 42 | vector path(n,-1); 43 | path[0]=0; 44 | if(cycle(graph,path,n,1)){ 45 | for(int i=0;i>n; 59 | vector> graph(n,vector(n)); 60 | for(int i=0;i>graph[i][j]; 63 | } 64 | } 65 | hamCycle(graph); 66 | return 0; 67 | } -------------------------------------------------------------------------------- /Backtracking/Hamiltonian-Cycle.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Backtracking/Hamiltonian-Cycle.exe -------------------------------------------------------------------------------- /Backtracking/M-Coloring-Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool isSafe(vector> &graph, int n, int v, int c, vector &color) 6 | { 7 | for (int i = 0; i < n; i++) 8 | { 9 | if (graph[v][i] == 1 && c == color[i]) 10 | { // if there is an adj node exist and its color is same as c 11 | return false; 12 | } 13 | } 14 | return true; 15 | } 16 | 17 | bool graphColoring(vector> &graph, int m, int n, vector &color, int v) 18 | { 19 | if (v == n) 20 | { // base condition: v = total number of vertex 21 | return true; 22 | } 23 | for (int c = 1; c <= m; c++) 24 | { 25 | if (isSafe(graph, n, v, c, color)) 26 | { 27 | color[v] = c; 28 | if (graphColoring(graph, m, n, color, v + 1)) 29 | { 30 | return true; 31 | } 32 | color[v] = 0; 33 | } 34 | } 35 | return false; 36 | } 37 | 38 | bool mColoring(vector> &graph, int m) 39 | { 40 | vector color(m); 41 | if (graphColoring(graph, m, graph[0].size(), color, 0)) 42 | { 43 | for (int i = 0; i < graph[0].size(); i++) 44 | { 45 | cout << color[i] << " "; 46 | } 47 | return true; 48 | } 49 | else 50 | { 51 | return false; 52 | } 53 | } 54 | 55 | int main() 56 | { 57 | int n, m; 58 | cin >> n; 59 | vector> graph(n, vector(n)); 60 | for (int i = 0; i < n; i++) 61 | { 62 | for (int j = 0; j < n; j++) 63 | { 64 | cin >> graph[i][j]; 65 | } 66 | } 67 | cin >> m; 68 | mColoring(graph, m); 69 | return 0; 70 | } -------------------------------------------------------------------------------- /Backtracking/M-Coloring-Problem.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Backtracking/M-Coloring-Problem.exe -------------------------------------------------------------------------------- /Backtracking/Rat-in-Maze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | bool isSafe(vector> &arr, int x, int y, int n) 7 | { 8 | if (x < n && y < n && x >= 0 && y >= 0 && arr[x][y] == 1) 9 | { 10 | return true; 11 | } 12 | return false; 13 | } 14 | 15 | bool ratInMaze(vector> &arr, int x, int y, int n, vector> &sol) 16 | { 17 | if (x == n - 1 && y == n - 1) 18 | { 19 | sol[x][y] = 1; 20 | return true; 21 | } 22 | if (isSafe(arr, x, y, n) == true) 23 | { 24 | sol[x][y] = 1; 25 | if (ratInMaze(arr, x + 1, y, n, sol) == true) 26 | { 27 | return true; 28 | } 29 | if (ratInMaze(arr, x, y + 1, n, sol) == true) 30 | { 31 | return true; 32 | } 33 | sol[x][y] = 0; 34 | return false; 35 | } 36 | return false; 37 | } 38 | 39 | int main() 40 | { 41 | int n; 42 | cin >> n; 43 | vector> arr(n, vector(n)); 44 | vector> sol(n, vector(n)); 45 | 46 | for (int i = 0; i < n; i++) 47 | { 48 | for (int j = 0; j < n; j++) 49 | { 50 | cin >> arr[i][j]; 51 | } 52 | } 53 | if (ratInMaze(arr, 0, 0, n, sol) == true) 54 | { 55 | for (int i = 0; i < n; i++) 56 | { 57 | for (int j = 0; j < n; j++) 58 | { 59 | cout << sol[i][j] << " "; 60 | } 61 | cout << "\n"; 62 | } 63 | } 64 | else 65 | { 66 | cout << "No Solution exist"; 67 | } 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Backtracking/Rat-in-Maze.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Backtracking/Rat-in-Maze.exe -------------------------------------------------------------------------------- /Binary Search/Binary-Search-on-Reverse-Sorted-Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int BinarySearchDesc(vector& arr, int ele){ 6 | int start=0; 7 | int end=arr.size()-1; 8 | while(start<=end){ 9 | int mid= start+(end-start)/2; 10 | if(ele==arr[mid]){ 11 | return mid; 12 | } 13 | else if(ele>n; 26 | vector arr(n); 27 | for(int i=0;i>arr[i]; 29 | } 30 | cin>>ele; 31 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int ceilSortedArray(vector& arr, int start, int end, int ele){ 7 | int result=INT_MAX; 8 | while(start<=end){ 9 | int mid= start+(end-start)/2; 10 | if(arr[mid]==ele){ 11 | return arr[mid]; 12 | } 13 | if(arr[mid]>n; 27 | vector arr(n); 28 | for(int i=0;i>arr[i]; 30 | } 31 | cin>>ele; 32 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int firstOccurence(vector& arr, int ele){ 7 | int start=0, result=-1; 8 | int end=arr.size()-1; 9 | while(start<=end){ 10 | int mid=start+(end-start)/2; 11 | if(ele==arr[mid]){ 12 | result=mid; 13 | end=mid-1; 14 | } 15 | else if(ele& arr, int ele){ 26 | int start=0, result=-1; 27 | int end=arr.size()-1; 28 | while(start<=end){ 29 | int mid=start+(end-start)/2; 30 | if(ele==arr[mid]){ 31 | result=mid; 32 | start=mid+1; 33 | } 34 | else if(ele& arr, int ele){ 45 | int first=firstOccurence(arr,ele); 46 | int last=lastOccurence(arr,ele); 47 | return last-first+1; 48 | } 49 | 50 | int main(){ 51 | int n,ele; 52 | cin>>n; 53 | vector arr(n); 54 | for(int i=0;i>arr[i]; 56 | } 57 | cin>>ele; 58 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int minIndex(vector& arr){ 7 | int start = 0; 8 | int n=arr.size(); 9 | int end = arr.size()-1; 10 | while(start<=end){ 11 | int mid=(end+start)/2; 12 | int next=(mid+1)%n; 13 | int prev=(mid+n-1)%n; 14 | if(arr[mid]& arr, int start, int end, int ele){ 27 | while(start<=end){ 28 | int mid=start+(end-start)/2; 29 | if(arr[mid]==ele){ 30 | return mid; 31 | } 32 | if(ele& arr, int ele){ 42 | int m=minIndex(arr); 43 | cout<>n; 52 | vector arr(n); 53 | for(int i=0;i>arr[i]; 55 | } 56 | cin>>ele; 57 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int firstOccurence(vector& arr, int ele){ 7 | int start=0, result=-1; 8 | int end=arr.size()-1; 9 | while(start<=end){ 10 | int mid=start+(end-start)/2; 11 | if(ele==arr[mid]){ 12 | result=mid; 13 | end=mid-1; 14 | } 15 | else if(ele& arr, int ele){ 26 | int start=0, result=-1; 27 | int end=arr.size()-1; 28 | while(start<=end){ 29 | int mid=start+(end-start)/2; 30 | if(ele==arr[mid]){ 31 | result=mid; 32 | start=mid+1; 33 | } 34 | else if(ele>n; 47 | vector arr(n); 48 | for(int i=0;i>arr[i]; 50 | } 51 | cin>>ele; 52 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int floorSortedArray(vector& arr, int start, int end, int ele){ 7 | int result=INT_MIN; 8 | while(start<=end){ 9 | int mid= start+(end-start)/2; 10 | if(arr[mid]==ele){ 11 | return arr[mid]; 12 | } 13 | if(arr[mid]>n; 27 | vector arr(n); 28 | for(int i=0;i>arr[i]; 30 | } 31 | cin>>ele; 32 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | char nextAlphabetElement(vector& arr, int start, int end, char ele){ 7 | char result='#'; 8 | while(start<=end){ 9 | int mid= start+(end-start)/2; 10 | if(arr[mid]<=ele){ 11 | start=mid+1; 12 | } 13 | else{ 14 | result=arr[mid]; 15 | end=mid-1; 16 | } 17 | } 18 | return result; 19 | } 20 | 21 | int main(){ 22 | int n; 23 | char ele; 24 | cin>>n; 25 | vector arr(n); 26 | for(int i=0;i>arr[i]; 28 | } 29 | cin>>ele; 30 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int rotatedArray(vector& arr){ 7 | int start = 0; 8 | int n=arr.size(); 9 | int end = arr.size()-1; 10 | while(start<=end){ 11 | int mid=(end+start)/2; 12 | int next=(mid+1)%n; 13 | int prev=(mid+n-1)%n; 14 | if(arr[mid]>n; 30 | vector arr(n); 31 | for(int i=0;i>arr[i]; 33 | } 34 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int Search(vector& arr, int ele){ 7 | if(arr.size()==1){ 8 | if(arr[0]==ele) 9 | return 0; 10 | else 11 | return -1; 12 | } 13 | else{ 14 | int start=0; 15 | int end=arr.size()-1; 16 | if(arr[0]arr[mid]){ 38 | end=mid-1; 39 | } 40 | else{ 41 | start=mid+1; 42 | } 43 | } 44 | return -1; 45 | } 46 | } 47 | } 48 | 49 | int main(){ 50 | int n,ele; 51 | cin>>n; 52 | vector arr(n); 53 | for(int i=0;i>arr[i]; 55 | } 56 | cin>>ele; 57 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int BSinInfinite(vector& arr, int start, int end, int ele){ 7 | //define end index in infinite array 8 | while(ele>arr[end]){ 9 | start=end; 10 | end*=2; 11 | } 12 | 13 | while(start<=end){ 14 | int mid=start+(end-start)/2; 15 | if(arr[mid]==ele){ 16 | return mid; 17 | } 18 | else if(arr[mid]>ele){ 19 | end=mid-1; 20 | } 21 | else{ 22 | start=mid+1; 23 | } 24 | } 25 | return -1; 26 | } 27 | 28 | int main(){ 29 | int n,ele; 30 | cin>>n; 31 | vector arr(n); 32 | for(int i=0;i>arr[i]; 34 | } 35 | cin>>ele; 36 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int nearlySortedBS(vector& arr, int start, int end, int ele){ 7 | while(start<=end){ 8 | int mid=start+(end-start)/2; 9 | if(ele==arr[mid]) 10 | return mid; 11 | 12 | if(mid-1>=start && arr[mid-1]==ele) 13 | return mid-1; 14 | 15 | if(mid+1<=end && arr[mid+1]==ele) 16 | return mid+1; 17 | 18 | if(ele>n; 31 | vector arr(n); 32 | for(int i=0;i>arr[i]; 34 | } 35 | cin>>ele; 36 | cout< 10 | 11 | using namespace std; 12 | 13 | int knapsack(vector& wt, vector& val, int W, int n){ 14 | vector< vector > dp(n+1 , vector(W+1,0)); 15 | 16 | for(int i=1;i>n; 32 | vector wt(n); 33 | vector val(n); 34 | cout<<"Enter Items Weight:\n"; 35 | for(int i=0;i>wt[i]; 37 | } 38 | cout<<"Enter Items Value:\n"; 39 | for(int i=0;i>val[i]; 41 | } 42 | cout<<"Enter Knapsack Capacity: "; 43 | cin>>W; 44 | cout<<"\nWeight Value\n"; 45 | for(int i=0;i 2 | using namespace std; 3 | 4 | int coinChangeI(vector& coins, int sum, int n){ 5 | vector > dp(n+1, vector(sum+1,0)); 6 | for(int i=0;i>n; 26 | vector coins(n); 27 | cout<<"Enter Coin Values:\n"; 28 | for(int i=0;i>coins[i]; 30 | } 31 | cout<<"Enter Sum:"; 32 | cin>>sum; 33 | cout<<"\nNumber of Ways: "< 2 | using namespace std; 3 | 4 | int coinChangeII(vector& coins, int sum, int n){ 5 | vector > dp(n+1, vector(sum+1,0)); 6 | for(int i=0;i>n; 35 | vector coins(n); 36 | cout<<"Enter Coin Values:\n"; 37 | for(int i=0;i>coins[i]; 39 | } 40 | cout<<"Enter Sum:"; 41 | cin>>sum; 42 | cout<<"\nMinimum Number of Coins: "< 2 | using namespace std; 3 | 4 | int countOfSubsetSum(vector& arr, int sum, int n){ 5 | vector > dp(n+1, vector(sum+1,0)); 6 | for(int i=0;i>n; 26 | vector arr(n); 27 | cout<<"Enter Elements:\n"; 28 | for(int i=0;i>arr[i]; 30 | } 31 | cout<<"Enter Sum:"; 32 | cin>>sum; 33 | cout<<"\nNumber of Subsets Present: "< 2 | #include 3 | using namespace std; 4 | 5 | int countOfSubsetSum(vector& arr, int sum, int n){ 6 | vector > dp(n+1, vector(sum+1,0)); 7 | for(int i=0;i& arr, int diff, int n){ 29 | int sum; 30 | for(int i=0;i>n; 42 | vector arr(n); 43 | cout<<"Enter Elements:\n"; 44 | for(int i=0;i>arr[i]; 46 | } 47 | cout<<"Enter Difference:"; 48 | cin>>diff; 49 | cout<<"\nNumber of Subsets: "< 2 | using namespace std; 3 | 4 | bool subsetSum(vector& arr, int sum, int n){ 5 | vector< vector > dp(n+1, vector(sum+1,false)); 6 | for(int i=0;i& arr){ 25 | int sum=0, n=arr.size(); 26 | for(int i=0;i>n; 36 | vector arr(n); 37 | cout<<"Enter Elements:\n"; 38 | for(int i=0;i>arr[i]; 40 | } 41 | 42 | cout<<"\nEqual Sum Partition Possible: "<<(equalSumPartition(arr)?"YES":"NO"); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Dynamic Programming/Longest Common Subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int longestCommonSubsequence(string X, string Y, int m, int n){ 5 | vector< vector > dp(m+1 , vector(n+1,0)); 6 | for(int i=1;i 2 | using namespace std; 3 | 4 | int longestCommonSubstring(string X, string Y, int m, int n){ 5 | vector< vector > dp(m+1 , vector(n+1,0)); 6 | for(int i=1;i 2 | using namespace std; 3 | 4 | int LPS(string X, string Y, int m, int n){ 5 | vector< vector > dp(m+1 , vector(n+1,0)); 6 | for(int i=1;i 2 | using namespace std; 3 | 4 | vector subsetSum(vector& arr, int sum, int n){ 5 | vector > dp(n+1, vector(sum+1,false)); 6 | for(int i=0;i& arr, int n){ 22 | int sum=0, minDiff=INT_MAX; 23 | vector v; 24 | for(int i=0;i>n; 39 | vector arr(n); 40 | cout<<"Enter Elements:\n"; 41 | for(int i=0;i>arr[i]; 43 | } 44 | cout<<"\nMinimum Subset Sum Difference: "< 2 | using namespace std; 3 | 4 | string printLCS(string X, string Y, int m, int n){ 5 | string s=""; 6 | vector< vector< int> > dp(m+1,vector(n+1,0)); 7 | for(int i=1;i0 && j>0){ 28 | if(X[i-1]==Y[j-1]){ 29 | s.push_back(X[i-1]); 30 | i--; 31 | j--; 32 | } 33 | else 34 | { 35 | if(dp[i-1]>dp[j-1]) 36 | i--; 37 | else 38 | j--; 39 | } 40 | } 41 | reverse(s.begin(),s.end()); 42 | return s; 43 | } 44 | 45 | 46 | int main(void){ 47 | string X, Y; 48 | cout<<"Enter String 1: "; 49 | getline(cin,X); 50 | cout<<"Enter String 2: "; 51 | getline(cin,Y); 52 | cout<<"\nLongest Common Subsequence: "< 2 | using namespace std; 3 | 4 | int rodCutting(vector& len, vector& price, int N, int n){ 5 | vector< vector > dp(n+1 , vector(N+1,0)); 6 | 7 | for(int i=1;i>n; 23 | vector len(n); 24 | vector price(n); 25 | 26 | for(int i=0;i>len[i]; 29 | cout<<"Enter its Price:"; 30 | cin>>price[i]; 31 | } 32 | cout<<"Enter Rod Length: "; 33 | cin>>N; 34 | cout<<"\nLength Price\n"; 35 | for(int i=0;i "< 2 | using namespace std; 3 | 4 | int shortestCommonSupersequence(string X, string Y, int m, int n){ 5 | vector< vector > dp(m+1 , vector(n+1,0)); 6 | for(int i=1;i 2 | using namespace std; 3 | 4 | bool subsetSum(vector& arr, int sum, int n){ 5 | vector > dp(n+1, vector(sum+1,false)); 6 | for(int i=0;i>n; 28 | vector arr(n); 29 | cout<<"Enter Elements:\n"; 30 | for(int i=0;i>arr[i]; 32 | } 33 | cout<<"Enter Sum:"; 34 | cin>>sum; 35 | cout<<"\nSubset Present: "<<(subsetSum(arr,sum,n)?"YES":"NO"); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Dynamic Programming/Unbounded Knapsack Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int unboundedKnapsack(vector& wt, vector& val, int W, int n){ 5 | vector< vector > dp(n+1 , vector(W+1,0)); 6 | 7 | for(int i=1;i>n; 23 | vector wt(n); 24 | vector val(n); 25 | cout<<"Enter Items Weight:\n"; 26 | for(int i=0;i>wt[i]; 28 | } 29 | cout<<"Enter Items Value:\n"; 30 | for(int i=0;i>val[i]; 32 | } 33 | cout<<"Enter Knapsack Capacity: "; 34 | cin>>W; 35 | cout<<"\nWeight Value\n"; 36 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | template 8 | class Graph{ 9 | map> l; 10 | public: 11 | void addEdge(int u, int v){ 12 | l[u].push_back(v); 13 | l[v].push_back(u); 14 | } 15 | void BFS(T src){ 16 | map visited; 17 | queue q; 18 | q.push(src); 19 | visited[src]=true; 20 | while(!q.empty() ){ 21 | T node = q.front(); 22 | q.pop(); 23 | cout< g; 35 | g.addEdge(0,1); 36 | g.addEdge(1,2); 37 | g.addEdge(2,3); 38 | g.addEdge(3,4); 39 | g.addEdge(3,5); 40 | g.BFS(0); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Graph/DFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | template 8 | class Graph{ 9 | map> l; 10 | public: 11 | void addEdge(int u, int v){ 12 | l[u].push_back(v); 13 | l[v].push_back(u); 14 | } 15 | void DFSrec(T src,map &visited){ 16 | cout< visited; 26 | for(auto p:l){ 27 | T node = p.first; 28 | visited[node]=false; 29 | } 30 | DFSrec(src, visited); 31 | } 32 | }; 33 | int main(){ 34 | Graph g; 35 | g.addEdge(0,1); 36 | g.addEdge(1,2); 37 | g.addEdge(2,3); 38 | g.addEdge(3,4); 39 | g.addEdge(4,5); 40 | g.DFS(1); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Graph/Matrix-Related-Problems/Far-From-Land.cpp: -------------------------------------------------------------------------------- 1 | include 2 | using namespace std; 3 | 4 | int maxDistance(vector>& grid) { 5 | int step=0; 6 | queue> q; 7 | for(int i=0;i=0 && j>=0 && i 2 | using namespace std; 3 | 4 | void fill(vector>& image, int sr, int sc, int newColor, int currColor){ 5 | if(sr>=0 && sr=0 && sc> floodFill(vector>& image, int sr, int sc, int newColor) { 17 | int currColor = image[sr][sc]; 18 | if(newColor==currColor){ 19 | return image; 20 | } 21 | fill(image,sr,sc,newColor,currColor); 22 | return image; 23 | } -------------------------------------------------------------------------------- /Graph/Matrix-Related-Problems/Max-Area-of-Island.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int area(int i, int j, vector>& grid){ 5 | if(i>=0 && i=0 && j>& grid){ 13 | int maxArea = 0; 14 | for(int i=0;i>n>>m; 24 | vector> grid(n,vector(m)); 25 | 26 | for(int i=0;i>grid[i][j]; 29 | } 30 | } 31 | 32 | cout< 2 | using namespace std; 3 | 4 | void DFS(vector>& grid, int i, int j){ 5 | if(i<0 || j<0 || i>=grid.size() || j>=grid[0].size() || grid[i][j]){ 6 | return; 7 | } 8 | grid[i][j]=1; 9 | DFS(grid, i+1, j); 10 | DFS(grid, i-1, j); 11 | DFS(grid, i, j+1); 12 | DFS(grid, i, j-1); 13 | } 14 | 15 | int closedIsland(vector>& grid) { 16 | int count=0; 17 | int m=grid.size(), n=grid[0].size(); 18 | for(int i=0;i 2 | using namespace std; 3 | 4 | void DFS(vector>& grid, int i, int j, int& count){ 5 | if(i>=grid.size() || i<0 || j<0 || j>=grid[0].size() || grid[i][j]==0){ 6 | return; 7 | } 8 | count++; 9 | grid[i][j]=0; 10 | DFS(grid,i+1,j,count); 11 | DFS(grid,i-1,j,count); 12 | DFS(grid,i,j+1,count); 13 | DFS(grid,i,j-1,count); 14 | } 15 | 16 | int numEnclaves(vector>& grid) { 17 | int m=grid.size(); 18 | int n=grid[0].size(); 19 | int count=0; 20 | for(int i=0;i 2 | using namespace std; 3 | 4 | void DFS(vector>& grid, int i, int j, int& count){ 5 | if(i>=0 && j>=0 && i>& grid) { 15 | int count=0; 16 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Graph{ 7 | 8 | unordered_map>> l; 9 | public: 10 | void addEdge(string u,string v, bool bidir, int wt){ 11 | l[u].push_back(make_pair(v,wt)); 12 | if(bidir){ 13 | l[v].push_back(make_pair(u,wt)); 14 | } 15 | } 16 | void printAdjList(){ 17 | for(auto p:l){ 18 | string city=p.first; 19 | list> neighbours = p.second; 20 | cout<"; 21 | for(auto n:neighbours){ 22 | string dest=n.first; 23 | int d=n.second; 24 | 25 | cout << "(" << dest << "," < 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int minimizeCost(vector& arr){ 9 | priority_queue,greater> minHeap; 10 | for(int i=0;i=2){ 14 | int first=minHeap.top(); 15 | minHeap.pop(); 16 | int second=minHeap.top(); 17 | minHeap.pop(); 18 | cost = cost + first + second; 19 | minHeap.push(first+second); 20 | } 21 | return cost; 22 | } 23 | 24 | int main(){ 25 | int n; 26 | cin>>n; 27 | vector arr(n); 28 | for(int i=0;i>arr[i]; 30 | } 31 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | void frequencySort(vector& arr, int n){ 10 | priority_queue> maxHeap; 11 | unordered_map mp; 12 | for(int i=0;isecond,i->first)); 17 | } 18 | while(!maxHeap.empty()){ 19 | int freq=maxHeap.top().first; 20 | int ele=maxHeap.top().second; 21 | for(int i=0;i>n; 31 | vector arr(n); 32 | for(int i=0;i>arr[i]; 34 | } 35 | frequencySort(arr,n); 36 | } -------------------------------------------------------------------------------- /Heap/K-Closest-Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | void kClosestNumbers(vector& arr, int n, int k, int x){ 10 | priority_queue> maxHeap; 11 | for(int i=0;i k){ 14 | maxHeap.pop(); 15 | } 16 | } 17 | while(!maxHeap.empty()){ 18 | cout<>n; 26 | vector arr(n); 27 | for(int i=0;i>arr[i]; 29 | } 30 | cin>>k; 31 | cin>>x; 32 | kClosestNumbers(arr,n,k,x); 33 | } -------------------------------------------------------------------------------- /Heap/K-Closest-Numbers.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Heap/K-Closest-Numbers.exe -------------------------------------------------------------------------------- /Heap/K-Closest-Points-To-Origin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | void kClosestPointsToOrigin(vector> arr, int n, int k){ 9 | priority_queue>> maxHeap; 10 | for(int i=0;i k){ 13 | maxHeap.pop(); 14 | } 15 | } 16 | while(!maxHeap.empty()){ 17 | pair p = maxHeap.top().second; 18 | cout<<"{"<>n; 26 | vector> arr(n,vector(2)); 27 | for(int i=0;i>arr[i][0]>>arr[i][1]; 29 | } 30 | cin>>k; 31 | kClosestPointsToOrigin(arr,n,k); 32 | } -------------------------------------------------------------------------------- /Heap/K-Closest-Points-To-Origin.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Heap/K-Closest-Points-To-Origin.exe -------------------------------------------------------------------------------- /Heap/K-Largest-Elements-in-Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | void kLargestElements(vector& arr, int n, int k){ 9 | priority_queue, greater> minHeap; 10 | for(int i=0;ik){ 13 | minHeap.pop(); 14 | } 15 | } 16 | while(!minHeap.empty()){ 17 | cout<>n; 24 | vector arr(n); 25 | for(int i=0;i>arr[i]; 27 | } 28 | cin>>k; 29 | kLargestElements(arr,n,k); 30 | } -------------------------------------------------------------------------------- /Heap/K-Largest-Elements-in-Array.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Heap/K-Largest-Elements-in-Array.exe -------------------------------------------------------------------------------- /Heap/Kth-Largest-Element-in-Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int KthLargest(vector& arr, int n,int k){ 9 | priority_queue, greater> minHeap; 10 | 11 | for(int i=0;i k){ 14 | minHeap.pop(); 15 | } 16 | } 17 | return minHeap.top(); 18 | } 19 | 20 | int main(){ 21 | int n,k; 22 | cin>>n; 23 | vector arr(n); 24 | for(int i=0;i>arr[i]; 26 | } 27 | cin>>k; 28 | cout< 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int KthSmallest(vector& arr, int n,int k){ 9 | priority_queue maxHeap; 10 | 11 | for(int i=0;i k){ 14 | maxHeap.pop(); 15 | } 16 | } 17 | return maxHeap.top(); 18 | } 19 | 20 | int main(){ 21 | int n,k; 22 | cin>>n; 23 | vector arr(n); 24 | for(int i=0;i>arr[i]; 26 | } 27 | cin>>k; 28 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int maxPerformance(int n, vector& speed, vector& efficiency, int k) { 9 | priority_queue, vector>, greater>> minh; 10 | int performance=0, m=INT_MAX; 11 | for(int i=0;ik){ 14 | minh.pop(); 15 | } 16 | } 17 | while(!minh.empty()){ 18 | int x=minh.top().second; 19 | m=min(m,efficiency[x]); 20 | performance+=speed[x]; 21 | minh.pop(); 22 | } 23 | return (m*performance)%1000000007; 24 | } 25 | 26 | int main() { 27 | int n,k; 28 | cin>>n; 29 | vector efficiency(n), speed(n); 30 | for(int i=0;i>speed[i]; 32 | } 33 | for(int i=0;i>efficiency[i]; 35 | } 36 | cin>>k; 37 | cout< 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | //Complexity: O(nlogk) 8 | 9 | int sortK(vector& arr, int n, int k){ 10 | priority_queue, greater> minHeap; 11 | 12 | int index=0; 13 | for(int i=0;ik){ 16 | arr[index++]=minHeap.top(); 17 | minHeap.pop(); 18 | } 19 | } 20 | while(!minHeap.empty()){ 21 | arr[index++]=minHeap.top(); 22 | minHeap.pop(); 23 | } 24 | } 25 | int main(){ 26 | int n,k; 27 | cin>>n; 28 | vector arr(n); 29 | for(int i=0;i>arr[i]; 31 | } 32 | cin>>k; 33 | sortK(arr,n,k); 34 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int kthSmallest(vector& arr, int k){ 9 | priority_queue maxHeap; 10 | 11 | for(int i=0;i k){ 14 | maxHeap.pop(); 15 | } 16 | } 17 | return maxHeap.top(); 18 | } 19 | 20 | int main(){ 21 | int n,k1,k2,sum=0; 22 | cin>>n; 23 | vector arr(n); 24 | for(int i=0;i>arr[i]; 26 | } 27 | cin>>k1>>k2; 28 | int first = kthSmallest(arr,k1); 29 | int second = kthSmallest(arr,k2); 30 | for(int i=0;ifirst && arr[i] 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | void topKFrequentNumbers(vector& arr, int n, int k){ 10 | priority_queue,vector>, greater< pair > > minHeap; 11 | unordered_map mp; 12 | for(int i=0;isecond,i->first)); 17 | if(minHeap.size()>k){ 18 | minHeap.pop(); 19 | } 20 | } 21 | while(!minHeap.empty()){ 22 | cout<>n; 30 | vector arr(n); 31 | for(int i=0;i>arr[i]; 33 | } 34 | cin>>k; 35 | topKFrequentNumbers(arr,n,k); 36 | } -------------------------------------------------------------------------------- /Heap/Top-K-Frequent-Numbers.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Heap/Top-K-Frequent-Numbers.exe -------------------------------------------------------------------------------- /Linked-Lists/Circular-LL-Implementation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int value; 7 | Node *next; 8 | Node(int value){ 9 | this->value=value; 10 | this->next=NULL; 11 | } 12 | }; 13 | void push(Node** last, int val){ 14 | Node *node=new Node(val); 15 | Node *ptr=*last; 16 | if(*last==NULL){ 17 | *last=node; 18 | node->next=*last; 19 | return; 20 | } 21 | node->next=ptr->next; 22 | ptr->next=node; 23 | *last=node; 24 | return; 25 | } 26 | 27 | void printList(Node* last){ 28 | if(last==NULL){ 29 | return; 30 | } 31 | Node *node=last->next; 32 | do{ 33 | cout<value<<"->"; 34 | node=node->next; 35 | }while(node!=last->next); 36 | cout<>n; 43 | Node *last=NULL; 44 | for(int i=0;i>x; 46 | push(&last, x); 47 | } 48 | printList(last); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Linked-Lists/Circular-LL-Implementation.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Linked-Lists/Circular-LL-Implementation.exe -------------------------------------------------------------------------------- /Linked-Lists/Detect-Loop-in-LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int value; 7 | Node* next; 8 | Node(int value){ 9 | this->value=value; 10 | this->next=NULL; 11 | } 12 | }; 13 | 14 | void insertAtEnd(Node** head, int val){ 15 | Node* node=new Node(val); 16 | Node* last=*head; 17 | if(*head==NULL){ 18 | *head=node; 19 | return; 20 | } 21 | while(last->next!=NULL){ 22 | last=last->next; 23 | } 24 | last->next=node; 25 | return; 26 | } 27 | 28 | bool detectLoop(Node* head){ 29 | Node* slowPtr=head; 30 | Node* fastPtr=head; 31 | while(slowPtr && fastPtr && fastPtr->next){ 32 | slowPtr=slowPtr->next; 33 | fastPtr=fastPtr->next->next; 34 | if(slowPtr==fastPtr){ 35 | return true; 36 | } 37 | } 38 | return false; 39 | } 40 | 41 | int main(){ 42 | Node* head=NULL; 43 | insertAtEnd(&head, 5); 44 | insertAtEnd(&head, 8); 45 | insertAtEnd(&head, 7); 46 | insertAtEnd(&head, 9); 47 | insertAtEnd(&head, 2); 48 | head->next->next->next->next->next=head->next->next; 49 | if(detectLoop(head)){ 50 | cout<<"Loop Exist"; 51 | } 52 | else{ 53 | cout<<"No Loop Exist"; 54 | } 55 | return 0; 56 | } -------------------------------------------------------------------------------- /Linked-Lists/Detect-Loop-in-LL.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Linked-Lists/Detect-Loop-in-LL.exe -------------------------------------------------------------------------------- /Linked-Lists/Intersection-of-Sorted-LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int value; 7 | Node* next; 8 | Node(int value){ 9 | this->next=NULL; 10 | this->value=value; 11 | } 12 | }; 13 | 14 | void push(Node** head, int val){ 15 | Node* node=new Node(val); 16 | Node* last=*head; 17 | if(*head==NULL){ 18 | *head=node; 19 | return; 20 | } 21 | while(last->next!=NULL){ 22 | last=last->next; 23 | } 24 | last->next=node; 25 | return; 26 | } 27 | 28 | Node* sortedIntersection(Node* headA, Node* headB){ 29 | Node* result=NULL; 30 | Node** temp=&result; 31 | Node* A=headA; 32 | Node* B=headB; 33 | 34 | while(A!=NULL && B!=NULL){ 35 | if(A->value==B->value){ 36 | push(temp,A->value); 37 | temp=&((*temp)->next); 38 | A=A->next; 39 | B=B->next; 40 | } 41 | else if(A->valuevalue){ 42 | A=A->next; 43 | } 44 | else{ 45 | B=B->next; 46 | } 47 | } 48 | return result; 49 | } 50 | 51 | void printList(Node* head){ 52 | if(head==NULL){ 53 | return; 54 | } 55 | while(head!=NULL){ 56 | cout<value<<"->"; 57 | head=head->next; 58 | } 59 | cout<>n1; 69 | for(int i=0;i>x; 71 | push(&headA,x); 72 | } 73 | 74 | cin>>n2; 75 | for(int i=0;i>x; 77 | push(&headB,x); 78 | } 79 | 80 | printList(headA); 81 | printList(headB); 82 | 83 | headC=sortedIntersection(headA, headB); 84 | 85 | printList(headC); 86 | return 0; 87 | } -------------------------------------------------------------------------------- /Linked-Lists/Intersection-of-Sorted-LL.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Linked-Lists/Intersection-of-Sorted-LL.exe -------------------------------------------------------------------------------- /Linked-Lists/Length-of-Linked-List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int value; 7 | Node* next; 8 | }; 9 | 10 | void insertAtBegin(Node** head, int val){ 11 | Node* node=new Node(); 12 | node->value=val; 13 | node->next=(*head); 14 | *head=node; 15 | } 16 | 17 | int count(Node* node){ 18 | int count=0; 19 | while(node!=NULL){ 20 | count++; 21 | node=node->next; 22 | } 23 | return count; 24 | } 25 | void printList(Node* node){ 26 | while(node!=NULL){ 27 | cout<value<<"->"; 28 | node=node->next; 29 | } 30 | } 31 | int main(){ 32 | Node* head=NULL; 33 | char ch; 34 | int x; 35 | do{ 36 | cin>>x; 37 | insertAtBegin(&head,x); 38 | cout<<"Do you want to add more?(Y/N)"; 39 | cin>>ch; 40 | }while(ch=='y'||ch=='Y'); 41 | cout< 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int value; 7 | Node* next; 8 | Node(int value){ 9 | this->value=value; 10 | this->next=NULL; 11 | } 12 | }; 13 | 14 | void insertAtEnd(Node** head, int val){ 15 | Node* node=new Node(val); 16 | Node* last=*head; 17 | if(*head==NULL){ 18 | *head=node; 19 | return; 20 | } 21 | while(last->next!=NULL){ 22 | last=last->next; 23 | } 24 | last->next=node; 25 | return; 26 | } 27 | 28 | int countNodes(Node* ptr){ 29 | int count=1; 30 | Node* temp=ptr; 31 | while(temp->next!=ptr){ 32 | count++; 33 | temp=temp->next; 34 | } 35 | return count; 36 | } 37 | 38 | int loopLength(Node* head){ 39 | Node* slowPtr=head; 40 | Node* fastPtr=head; 41 | while(slowPtr && fastPtr && fastPtr->next){ 42 | slowPtr=slowPtr->next; 43 | fastPtr=fastPtr->next->next; 44 | if(slowPtr->value==fastPtr->value){ 45 | return countNodes(slowPtr); 46 | } 47 | } 48 | return 0; 49 | } 50 | 51 | int main(){ 52 | Node* head=NULL; 53 | insertAtEnd(&head, 5); 54 | insertAtEnd(&head, 8); 55 | insertAtEnd(&head, 7); 56 | insertAtEnd(&head, 9); 57 | insertAtEnd(&head, 2); 58 | head->next->next->next->next->next=head->next->next; 59 | cout< 2 | using namespace std; 3 | 4 | ListNode* mergeKLL(vector& lists){ 5 | if(lists.empty()) return NULL; 6 | while(lists.size()>1){ 7 | lists.push_back(merge2LL(lists[0],lists[1])); 8 | lists.erase(lists.begin()); 9 | lists.erase(lists.begin()); 10 | } 11 | lists.front(); 12 | } 13 | 14 | ListNode* merge2LL(ListNode *l1, ListNode *l2){ 15 | if(l1==NULL) return l2; 16 | if(l2==NULL) return l1; 17 | 18 | if(l1->val<=l2->val){ 19 | l1->next=merge2LL(l1->next, l2); 20 | return l1; 21 | } 22 | else{ 23 | l2->next=merge2LL(l1, l2->next); 24 | return l2; 25 | } 26 | } -------------------------------------------------------------------------------- /Linked-Lists/Mid-Element-of-LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int value; 7 | Node* next; 8 | Node(int value){ 9 | this->value=value; 10 | this->next=NULL; 11 | } 12 | }; 13 | 14 | void insertAtEnd(Node** head,int val){ 15 | Node* node=new Node(val); 16 | Node* last=*head; 17 | if(*head==NULL){ 18 | *head=node; 19 | return; 20 | } 21 | while(last->next!=NULL){ 22 | last=last->next; 23 | } 24 | last->next=node; 25 | return; 26 | } 27 | 28 | int midElement(Node* head){ 29 | Node* ptr1=head; 30 | Node* ptr2=head; 31 | if(head!=NULL){ 32 | while(ptr2!=NULL && ptr2->next!=NULL){ 33 | ptr1=ptr1->next; 34 | ptr2=ptr2->next->next; 35 | } 36 | return ptr1->value; 37 | } 38 | return -1; 39 | } 40 | 41 | int main(){ 42 | int n,x; 43 | cin>>n; 44 | Node* head=NULL; 45 | for(int i=0;i>x; 47 | insertAtEnd(&head,x); 48 | } 49 | cout< 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int value; 7 | Node* next; 8 | Node(int value){ 9 | this->value=value; 10 | this->next=NULL; 11 | } 12 | }; 13 | 14 | void insertAtEnd(Node** head, int val){ 15 | Node* node=new Node(val); 16 | Node* last=*head; 17 | if(*head==NULL){ 18 | *head=node; 19 | return; 20 | } 21 | while(last->next!=NULL){ 22 | last=last->next; 23 | } 24 | last->next=node; 25 | return; 26 | } 27 | 28 | int NthNodeFromEnd(Node* head,int index){ 29 | int len=0; 30 | Node* ptr=head; 31 | while(ptr!=NULL){ 32 | len++; 33 | ptr=ptr->next; 34 | } 35 | if(index>len || index<1){ 36 | return (-1); 37 | } 38 | ptr=head; 39 | for(int i=0;inext; 41 | } 42 | return ptr->value; 43 | } 44 | 45 | int main(){ 46 | int n,x, index; 47 | Node* head=NULL; 48 | cin>>n; 49 | for(int i=0;i>x; 51 | insertAtEnd(&head,x); 52 | } 53 | cin>>index; 54 | cout< 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int value; 7 | Node* next; 8 | Node(int value){ 9 | this->value = value; 10 | this->next=NULL; 11 | } 12 | }; 13 | 14 | void insertAtEnd(Node** head, int val){ 15 | Node* node=new Node(val); 16 | Node* last=*head; 17 | if(*head==NULL){ 18 | *head=node; 19 | return; 20 | } 21 | while(last->next!=NULL){ 22 | last=last->next; 23 | } 24 | last->next=node; 25 | return; 26 | } 27 | 28 | int NthNode(Node* head, int index){ 29 | int count=0; 30 | Node* curr=head; 31 | while(curr!=NULL){ 32 | if(count==index){ 33 | return curr->value; 34 | } 35 | count++; 36 | curr=curr->next; 37 | } 38 | return (-1); 39 | } 40 | 41 | int main(){ 42 | Node* head=NULL; 43 | int n,x,index; 44 | cin>>n; 45 | for(int i=0;i>x; 47 | insertAtEnd(&head,x); 48 | } 49 | cin>>index; 50 | cout< 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int value; 7 | Node* next; 8 | Node(int value){ 9 | this->value=value; 10 | this->next=NULL; 11 | } 12 | }; 13 | 14 | void insertAtEnd(Node** head, int val){ 15 | Node* node=new Node(val); 16 | Node* last=*head; 17 | if(*head==NULL){ 18 | *head=node; 19 | return; 20 | } 21 | while(last->next!=NULL){ 22 | last=last->next; 23 | } 24 | last->next=node; 25 | return; 26 | } 27 | 28 | void removeDuplicates(Node* head){ 29 | Node* current=head; 30 | Node* temp; 31 | if(current==NULL) 32 | return; 33 | 34 | while(current->next!=NULL){ 35 | if(current->value==current->next->value){ 36 | temp=current->next->next; 37 | free(current->next); 38 | current->next=temp; 39 | } 40 | else{ 41 | current=current->next; 42 | } 43 | } 44 | } 45 | 46 | void printList(Node* head){ 47 | Node* ptr=head; 48 | while(ptr!=NULL){ 49 | cout<value<<"->"; 50 | ptr=ptr->next; 51 | } 52 | cout<>n; 59 | for(int i=0;i>x; 61 | insertAtEnd(&head,x); 62 | } 63 | printList(head); 64 | removeDuplicates(head); 65 | printList(head); 66 | return 0; 67 | } -------------------------------------------------------------------------------- /Linked-Lists/Remove-duplicates-from-sorted-LL.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Linked-Lists/Remove-duplicates-from-sorted-LL.exe -------------------------------------------------------------------------------- /Linked-Lists/Remove-loop-from-unsorted-LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Node{ 6 | public: 7 | int value; 8 | Node* next; 9 | Node(int value){ 10 | this->value=value; 11 | this->next=NULL; 12 | } 13 | }; 14 | 15 | void insertAtEnd(Node** head, int val){ 16 | Node* node=new Node(val); 17 | Node* last=*head; 18 | if(*head==NULL){ 19 | *head=node; 20 | return; 21 | } 22 | while(last->next!=NULL){ 23 | last=last->next; 24 | } 25 | last->next=node; 26 | return; 27 | } 28 | 29 | void removeDuplicates(Node* head){ 30 | unordered_set seen; 31 | 32 | Node* current=head; 33 | Node* prev=NULL; 34 | if(current==NULL) 35 | return; 36 | 37 | while(current!=NULL){ 38 | if(seen.find(current->value)!=seen.end()){ 39 | prev->next=current->next; 40 | delete(current); 41 | } 42 | else{ 43 | seen.insert(current->value); 44 | prev=current; 45 | } 46 | current=prev->next; 47 | } 48 | } 49 | 50 | void printList(Node* head){ 51 | Node* ptr=head; 52 | while(ptr!=NULL){ 53 | cout<value<<"->"; 54 | ptr=ptr->next; 55 | } 56 | cout<>n; 63 | for(int i=0;i>x; 65 | insertAtEnd(&head,x); 66 | } 67 | printList(head); 68 | removeDuplicates(head); 69 | printList(head); 70 | return 0; 71 | } -------------------------------------------------------------------------------- /Linked-Lists/Remove-loop-from-unsorted-LL.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Linked-Lists/Remove-loop-from-unsorted-LL.exe -------------------------------------------------------------------------------- /Linked-Lists/Reverse-of-LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int value; 7 | Node* next; 8 | Node(int value){ 9 | this->value=value; 10 | this->next=NULL; 11 | } 12 | }; 13 | 14 | void push(Node** head, int val){ 15 | Node *node=new Node(val); 16 | Node *last= *head; 17 | 18 | if(*head==NULL){ 19 | *head=node; 20 | return; 21 | } 22 | 23 | while(last->next!=NULL){ 24 | last=last->next; 25 | } 26 | last->next=node; 27 | return; 28 | } 29 | 30 | void printList(Node* head){ 31 | Node *node=head; 32 | if(node==NULL){ 33 | return; 34 | } 35 | while(node!=NULL){ 36 | cout<value<<"->"; 37 | node=node->next; 38 | } 39 | cout<next; 50 | current->next=prev; 51 | prev=current; 52 | current=next; 53 | } 54 | *head=prev; 55 | } 56 | 57 | int main(){ 58 | int n,x; 59 | cin>>n; 60 | Node *head=NULL; 61 | for(int i=0;i>x; 63 | push(&head,x); 64 | } 65 | printList(head); 66 | reverseLL(&head); 67 | printList(head); 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Linked-Lists/Reverse-of-LL.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Linked-Lists/Reverse-of-LL.exe -------------------------------------------------------------------------------- /Linked-Lists/Segregate-even-odd-in-LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int value; 7 | Node* next; 8 | Node(int value){ 9 | this->value=value; 10 | this->next=NULL; 11 | } 12 | }; 13 | 14 | void push(Node** head,int val){ 15 | Node* node=new Node(val); 16 | if(*head==NULL){ 17 | *head=node; 18 | return; 19 | } 20 | Node* last=*head; 21 | while(last->next!=NULL){ 22 | last=last->next; 23 | } 24 | last->next=node; 25 | return; 26 | } 27 | 28 | void EvenOddLL(Node** head){ 29 | Node* last=*head; 30 | Node* current=*head; 31 | Node* prev=NULL; 32 | while(last->next!=NULL){ 33 | last=last->next; 34 | } 35 | Node* lastptr=last; 36 | while(current->value%2!=0 && current!=last){ 37 | lastptr->next=current; 38 | current=current->next; 39 | last->next->next=NULL; 40 | lastptr=lastptr->next; 41 | } 42 | if(current->value%2==0){ 43 | *head=current; 44 | while(current!=last){ 45 | if(current->value%2==0){ 46 | prev=current; 47 | current=current->next; 48 | } 49 | else{ 50 | prev->next=current->next; 51 | current->next=NULL; 52 | lastptr->next=current; 53 | lastptr=current; 54 | current=prev->next; 55 | } 56 | } 57 | } 58 | else{ 59 | prev=current; 60 | } 61 | if (lastptr != last && (last->value) % 2 != 0) 62 | { 63 | prev->next = last->next; 64 | last->next = NULL; 65 | lastptr->next = last; 66 | } 67 | return; 68 | } 69 | 70 | void printList(Node* head){ 71 | if(head==NULL){ 72 | return; 73 | } 74 | while(head!=NULL){ 75 | cout<value<<"->"; 76 | head=head->next; 77 | } 78 | cout<>x; 87 | push(&head,x); 88 | } 89 | printList(head); 90 | EvenOddLL(&head); 91 | printList(head); 92 | return 0; 93 | } -------------------------------------------------------------------------------- /Linked-Lists/Segregate-even-odd-in-LL.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Linked-Lists/Segregate-even-odd-in-LL.exe -------------------------------------------------------------------------------- /Linked-Lists/Singly-Linked-List-Implementation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int value; 7 | Node* next; 8 | 9 | }; 10 | void insertAtBegin(Node** head, int val){ 11 | Node* node=new Node(); 12 | node->value=val; 13 | node->next=(*head); 14 | *head=node; 15 | 16 | } 17 | void insertAtEnd(Node** head, int val){ 18 | Node* node=new Node(); 19 | node->value=val; 20 | node->next=NULL; 21 | Node* last=*head; 22 | if(*head==NULL){ 23 | *head=node; 24 | return; 25 | } 26 | while(last->next!=NULL){ 27 | last=last->next; 28 | } 29 | last->next=node; 30 | return; 31 | } 32 | 33 | void deleteNode(Node** head, int val){ 34 | Node* ptr=*head; 35 | Node* prev=NULL; 36 | if(ptr!=NULL && ptr->value==val){ 37 | *head=ptr->next; 38 | delete ptr; 39 | return; 40 | } 41 | else{ 42 | while(ptr!=NULL && ptr->value!=val){ 43 | prev=ptr; 44 | ptr=ptr->next; 45 | } 46 | if(ptr==NULL){ 47 | return; 48 | } 49 | prev->next=ptr->next; 50 | delete ptr; 51 | } 52 | } 53 | 54 | void printList(Node* node){ 55 | while(node!=NULL){ 56 | cout<value<<"->"; 57 | node=node->next; 58 | } 59 | } 60 | int main(){ 61 | Node* head=NULL; 62 | int n,x; 63 | cin>>n; 64 | for(int i=0;i>x; 66 | insertAtEnd(&head,x); 67 | } 68 | printList(head); 69 | cout<<"\n Delete:"; 70 | cin>>x; 71 | deleteNode(&head,x); 72 | printList(head); 73 | return 0; 74 | } -------------------------------------------------------------------------------- /Linked-Lists/Singly-Linked-List-Implementation.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Linked-Lists/Singly-Linked-List-Implementation.exe -------------------------------------------------------------------------------- /Linked-Lists/Union-of-Sorted-LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int value; 7 | Node* next; 8 | Node(int value){ 9 | this->value=value; 10 | this->next=NULL; 11 | } 12 | }; 13 | 14 | void push(Node** head, int val){ 15 | Node* node=new Node(val); 16 | Node* last=*head; 17 | if(*head==NULL){ 18 | *head=node; 19 | return; 20 | } 21 | while(last->next!=NULL){ 22 | last=last->next; 23 | } 24 | last->next=node; 25 | return; 26 | } 27 | 28 | Node* sortedUnion(Node* headA, Node* headB){ 29 | Node* result=NULL; 30 | Node* A=headA; 31 | Node* B=headB; 32 | 33 | while(A!=NULL && B!=NULL){ 34 | if(A->valuevalue){ 35 | push(&result,A->value); 36 | A=A->next; 37 | } 38 | else if(B->valuevalue){ 39 | push(&result,B->value); 40 | B=B->next; 41 | } 42 | else{ 43 | push(&result,A->value); 44 | A=A->next; 45 | B=B->next; 46 | } 47 | } 48 | while(A!=NULL){ 49 | push(&result,A->value); 50 | A=A->next; 51 | } 52 | while(B!=NULL){ 53 | push(&result,B->value); 54 | B=B->next; 55 | } 56 | return result; 57 | } 58 | 59 | void printList(Node* head){ 60 | if(head==NULL){ 61 | return; 62 | } 63 | while(head!=NULL){ 64 | cout<value<<"->"; 65 | head=head->next; 66 | } 67 | cout<>n1; 77 | for(int i=0;i>x; 79 | push(&headA,x); 80 | } 81 | 82 | cin>>n2; 83 | for(int i=0;i>x; 85 | push(&headB,x); 86 | } 87 | 88 | printList(headA); 89 | printList(headB); 90 | 91 | headC=sortedUnion(headA, headB); 92 | 93 | printList(headC); 94 | return 0; 95 | } -------------------------------------------------------------------------------- /Linked-Lists/Union-of-Sorted-LL.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Linked-Lists/Union-of-Sorted-LL.exe -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data-Structures-and-Algorithms 2 | Data Structures and Algorithms 3 | ## Searching Algorithms 4 | 1. [Linear Search](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Searching%20Algorithms/Linear%20Search.cpp) 5 | 2. [Binary Search](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Searching%20Algorithms/Binary%20Search.cpp) 6 | 3. [Jump Search](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Searching%20Algorithms/Jump%20Search.cpp) 7 | 4. [Recursive Binary & Linear Search](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Searching%20Algorithms/Recursive%20Linear%20and%20Binary%20Search.cpp) 8 | 9 | ## Sorting Algorithms 10 | 1. [Bubble Sort](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Sorting%20Algorithms/Bubble%20Sort.cpp) 11 | 2. [Selection Sort](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Sorting%20Algorithms/Selection%20Sort.cpp) 12 | 3. [Insertion Sort](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Sorting%20Algorithms/Insertion%20Sort.cpp) 13 | 4. [Merge Sort](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Sorting%20Algorithms/Merge%20Sort.cpp) 14 | 5. [Quick Sort](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Sorting%20Algorithms/Quick%20Sort.cpp) 15 | 6. [Heap Sort](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Sorting%20Algorithms/Heap%20Sort.cpp) 16 | 7. [Shell Sort](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Sorting%20Algorithms/Shell%20Sort.cpp) 17 | 18 | ## Recursion 19 | 1. [Sort an Array using Recursion](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Recursion/Sort-an-Array-using-Recursion.cpp) 20 | 2. [Sort a Stack using Recursion](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Recursion/Sort-a-Stack.cpp) 21 | 3. [Delete Middle Element of Stack](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Recursion/Delete-Middle-Element-of-Stack.cpp) 22 | 4. [Reverse a Stack using Recursion](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Recursion/Reverse-a-Stack.cpp) 23 | 5. [Kth Symbol in Grammar](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Recursion/Kth-Symbol-in-Grammar.cpp) 24 | 6. [Print Powerset](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Recursion/Print-Powerset.cpp) 25 | 7. [Print Unique Subset](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Recursion/Print-Unique-Subsets.cpp) 26 | 8. [Permutation with Spaces](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Recursion/Permutation-with-Spaces.cpp) 27 | 9. [Letter Case Permutation](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Recursion/Letter-Case-Permutation.cpp) 28 | 10. [Balanced Parentheses](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Recursion/Balance-Parentheses.cpp) 29 | 11. [Josephus Problem](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Recursion/Josephus-Problem.cpp) 30 | 31 | ## Stack 32 | 1. [Nearest Greater to Left](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Stack/Nearest-Greater-to-light.cpp) 33 | 2. [Nearest Greater to Right](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Stack/Nearest-Greater-to-Right.cpp) 34 | 3. [Nearest Smaller to Left](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Stack/Nearest-Smaller-to-Left.cpp) 35 | 4. [Nearest Smaller to Right](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Stack/Nearest-Smaller-to-Right.cpp) 36 | 5. [Stock Span Problem](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Stack/Stock-Span-Problem.cpp) 37 | 6. [Maximum Area of Histogram](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Stack/Maximum-Area-Histogram.cpp) 38 | 7. [Maximum Area of Rectangle in Binary Matrix](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Stack/Maximum-Area-Rectangle-in-Binary-Matrix.cpp) 39 | 8. [Rain Water Trapping](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Stack/Rainwater-Trapping-Problem.cpp) 40 | 9. Minimum Element in Stack 41 | - [With Extra Space](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Stack/Minimum-Element-in-Stack-with-Extra-Space.cpp) 42 | - [With O(1) Space](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Stack/Minimum-Element-in-Stack-without-Extra-Space.cpp) 43 | 44 | ## Heap 45 | 1. [Kth Smallest Element in Array](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Heap/Kth-Smallest-Element-in-Array.cpp) 46 | 2. [K Largest Elements in Array](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Heap/K-Largest-Elements-in-Array.cpp) 47 | 3. [Sort a Nearly Sorted (or K Sorted) Array](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Heap/Sort-a-Nearly-Sorted-Array.cpp) 48 | 4. [K Closest Numbers](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Heap/K-Closest-Numbers.cpp) 49 | 5. [Top K Frequent Numbers](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Heap/Top-K-Frequent-Numbers.cpp) 50 | 6. [Frequency Sort](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Heap/Frequency-Sort.cpp) 51 | 7. [K Closest Points To Origin](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Heap/K-Closest-Points-To-Origin.cpp) 52 | 8. [Connect Ropes to Minimize Cost](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Heap/Connect-Ropes-to-Minimize-Cost.cpp) 53 | 9. [Sum of Elements Between](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Heap/Sum-of-Elements-Between.cpp) 54 | 55 | ## Binary Search 56 | 1. [Binary Search on Reverse Sorted Array](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Binary-Search-on-Reverse-Sorted-Array.cpp) 57 | 2. [Order not known Search](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Order-not-known-Search.cpp) 58 | 3. [First and Last Occurence](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/First-and-Last-Occurence.cpp) 59 | 4. [Count of Element in Sorted Array](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Count-of-Element-in-Sorted-Array.cpp) 60 | 5. [Number of times Sorted Array Rotated](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Number-of-times-Sorted-Array-Rotated.cpp) 61 | 6. [Find Element in Rotated Sorted Array](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Find-Element-in-Rotated-Sorted-Array.cpp) 62 | 7. [Searching in Nearly Sorted Array](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Searching-in-Nearly-Sorted-Array.cpp) 63 | 8. [Floor of Element in Sorted Array](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Floor-of-Element-in-Sorted-Array.cpp) 64 | 9. [Ceil of Element in Sorted Array](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Ceil-of-Element-in-Sorted-Array.cpp) 65 | 10. [Next Alphabetical Element](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Next-Alphabetical-Element.cpp) 66 | 11. [Position of Element in Infinite Sorted Array](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Position-of-Element-in-Infinite-Sorted-Array.cpp) 67 | 12. [Index of First 1 in Binary Sorted Infinite Array](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Index-of-First-1-in-Binary-Sorted-Infinite-Array.cpp) 68 | 13. [Minimum Difference Element in Sorted Array](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Minimum-Difference-Element-in-Sorted-Array.cpp) 69 | 14. [Peak Element](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Peak-Element.cpp) 70 | 15. [Maximum Element in Bitonic Array](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Maximum-Element-in-Bitonic-Array.cpp) 71 | 16. [Search Element in Bitonic Array](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Search-Element-in-Bitonic-Array.cpp) 72 | 17. [Search in Row wise and Column wise Sorted Array](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Search-in-Row-wise-and-Column-wise-Sorted-Array.cpp) 73 | 18. [Allocate Minimum Number Of Pages](https://github.com/rohan-khurana/Data-Structures-and-Algorithms/blob/master/Binary%20Search/Allocate-Minimum-Number-of-Pages.cpp) 74 | 75 | ## Dynamic Programming 76 | 1. [0/1 Knapsack Problem](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/0-1%20Knapsack%20Problem.cpp) 77 | - [Subset Sum Problem](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Subset%20Sum%20Problem.cpp) 78 | - [Equal Sum Partition Problem](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Partition%20Equal%20Subset%20Sum%20Problem.cpp) 79 | - [Count of Subset Sum Problem](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Count%20of%20Subset%20Sum%20Problem.cpp) 80 | - [Minimum Subset Sum Difference](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Minimum%20Subset%20Sum%20Difference.cpp) 81 | - [Count of Subsets with given Difference](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Count%20of%20Subsets%20with%20given%20Difference.cpp) 82 | 2. [Unbounded Knapsack Problem](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Unbounded%20Knapsack%20Problem.cpp) 83 | - [Rod Cutting Problem](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Rod%20Cutting%20Problem.cpp) 84 | - [Coin Change I](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Coin%20Change%20I.cpp) 85 | - [Coin Change II](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Coin%20Change%20II.cpp) 86 | 3. [Longest Common Subequence](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Longest%20Common%20Subsequence.cpp) 87 | - [Longest Common Substring](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Longest%20Common%20Substring.cpp) 88 | - [Print Longest Common Subsequence](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Print%20Longest%20Common%20Subsequence.cpp) 89 | - [Shortest Common Supersequence](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Shortest%20Common%20Supersequence.cpp) 90 | - [Print Shortest Common Supersequence](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Print%20Shortest%20Common%20Supersequence.cpp) 91 | - [Longest Palindromic Subequence](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Longest%20Palindromic%20Subsequence.cpp) 92 | - [Longest Repeating Subsequence](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Longest%20Repeating%20Subsequence.cpp) 93 | - [Minimum Number of Insertion and Deletion to convert String A to String B](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Minimum%20Number%20of%20Insertion%20and%20Deletion.cpp) 94 | - [Minimum Number of Insertion to make Palindrome](https://github.com/rohankhdbzmf/Data-Structures-and-Algorithms/blob/master/Dynamic%20Programming/Minimum%20Number%20of%20Insertion%20to%20make%20Palindrome.cpp) 95 | 4. Matrix Chain Multiplication 96 | -------------------------------------------------------------------------------- /Recursion/Balanced-Parentheses.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void solve(int open, int close, string op, vector& v){ 8 | if(open==0 && close==0){ 9 | v.push_back(op); 10 | return; 11 | } 12 | if(open!=0){ 13 | string op1=op; 14 | op1.push_back('('); 15 | solve(open-1, close, op1, v); 16 | } 17 | if(open balancedParentheses(int n){ 26 | int open=n; 27 | int close=n; 28 | vector v; 29 | string op; 30 | solve(open, close, op, v); 31 | return v; 32 | } 33 | 34 | int main(){ 35 | int n; 36 | cin>>n; 37 | vector v; 38 | v=balancedParentheses(n); 39 | for(auto i:v){ 40 | cout< 2 | #include 3 | using namespace std; 4 | 5 | void solve(stack& s, int k){ 6 | if(k==1){ 7 | s.pop(); 8 | return; 9 | } 10 | int top=s.top(); 11 | s.pop(); 12 | solve(s,k-1); 13 | s.push(top); 14 | } 15 | 16 | void deleteMiddle(stack& s){ 17 | if(s.empty()) 18 | return; 19 | int k = (s.size()/2)+1; 20 | solve(s,k); 21 | } 22 | 23 | void printStack(stack& s){ 24 | if (s.empty()) 25 | return; 26 | int x = s.top(); 27 | s.pop(); 28 | printStack(s); 29 | cout< s; 36 | cin>>n; 37 | for(int i=0;i>x; 39 | s.push(x); 40 | } 41 | deleteMiddle(s); 42 | printStack(s); 43 | } -------------------------------------------------------------------------------- /Recursion/Height-of-Binary-Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct Node{ 7 | int data; 8 | Node* left; 9 | Node* right; 10 | }; 11 | 12 | int main(){ 13 | 14 | } -------------------------------------------------------------------------------- /Recursion/Josephus-Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void solve(vector v, int k, int index){ 8 | if(v.size()==1){ 9 | cout<>n>>k; 22 | vector v; 23 | for(int i=1;i<=n;i++){ 24 | v.push_back(i); 25 | } 26 | k=k-1; 27 | solve(v,k,index); 28 | } -------------------------------------------------------------------------------- /Recursion/Josephus-Problem.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Recursion/Josephus-Problem.exe -------------------------------------------------------------------------------- /Recursion/Kth-Symbol-in-Grammar.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int solve(int n, int k){ 7 | if(n==1 && k==1) 8 | return 0; 9 | int m2id=pow(2,n-1)/2; 10 | if(k<=mid) 11 | return solve(n-1,k); 12 | else 13 | return !(solve(n-1,k-mid)); 14 | } 15 | 16 | int main(){ 17 | int n,k; 18 | cin>>n; 19 | cin>>k; 20 | 21 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void letterCasePermutation(string ip, string op, vector& v){ 7 | if(ip.length()==0){ 8 | v.push_back(op); 9 | return; 10 | } 11 | if(isalpha(ip[0])){ 12 | string op1=op; 13 | string op2=op; 14 | op1.push_back(toupper(ip[0])); 15 | op2.push_back(tolower(ip[0])); 16 | ip.erase(ip.begin()+0); 17 | letterCasePermutation(ip,op1,v); 18 | letterCasePermutation(ip,op2,v); 19 | return; 20 | } 21 | else{ 22 | string op1=op; 23 | op1.push_back(ip[0]); 24 | ip.erase(ip.begin()+0); 25 | letterCasePermutation(ip,op1,v); 26 | return; 27 | } 28 | } 29 | 30 | int main(){ 31 | string ip; 32 | cin>>ip; 33 | string op=""; 34 | vector v; 35 | letterCasePermutation(ip,op,v); 36 | for(auto i:v){ 37 | cout< 2 | #include 3 | //assumption: all input letters are small 4 | using namespace std; 5 | void printSubsets(string ip, string op){ 6 | if(ip.length()==0){ 7 | cout<>ip; 25 | printSubsets(ip,op); 26 | } -------------------------------------------------------------------------------- /Recursion/Permutation-with-CaseChange.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Recursion/Permutation-with-CaseChange.exe -------------------------------------------------------------------------------- /Recursion/Permutation-with-Spaces.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void permutationWithSpaces(string ip, string op){ 7 | if(ip.length()==0){ 8 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | void printBinaryNumber(int ones, int zeroes, int n, string op){ 7 | if(n==0){ 8 | cout<zeroes){ 15 | string op2=op; 16 | op2.push_back('0'); 17 | printBinaryNumber(ones, zeroes+1, n-1, op2); 18 | } 19 | return; 20 | } 21 | 22 | int main(){ 23 | int n; 24 | cin>>n; 25 | int ones=0; 26 | int zeroes=0; 27 | string op=""; 28 | printBinaryNumber(ones, zeroes, n, op); 29 | } -------------------------------------------------------------------------------- /Recursion/Print-N-bit-numbers-having-more-1s-more-than-0s-for-any-prefix.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Recursion/Print-N-bit-numbers-having-more-1s-more-than-0s-for-any-prefix.exe -------------------------------------------------------------------------------- /Recursion/Print-Powerset.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | void printSubsets(string ip, string op){ 6 | if(ip.length()==0){ 7 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | unordered_map mp; 8 | 9 | void printSubsets(string ip, string op){ 10 | if(ip.length()==0){ 11 | if(mp.find(op)==mp.end()) 12 | mp[op]++; 13 | 14 | return; 15 | } 16 | string op1=op; 17 | string op2=op; 18 | op2.push_back(ip[0]); 19 | ip.erase(ip.begin()+0); 20 | 21 | printSubsets(ip,op1); 22 | printSubsets(ip,op2); 23 | return; 24 | } 25 | 26 | int main(){ 27 | string ip; 28 | string op=""; 29 | getline(cin,ip); 30 | printSubsets(ip,op); 31 | for (auto i : mp) 32 | cout< 2 | #include 3 | using namespace std; 4 | 5 | void insert(stack& s, int ele){ 6 | if(s.size()==0){ 7 | s.push(ele); 8 | return; 9 | } 10 | int temp=s.top(); 11 | s.pop(); 12 | insert(s,ele); 13 | s.push(temp); 14 | return; 15 | } 16 | 17 | void reverse(stack& s){ 18 | if(s.size()==1) 19 | return; 20 | int temp=s.top(); 21 | s.pop(); 22 | reverse(s); 23 | insert(s, temp); 24 | return; 25 | } 26 | 27 | void printStack(stack& s){ 28 | if(s.empty()) 29 | return; 30 | int x=s.top(); 31 | s.pop(); 32 | printStack(s); 33 | cout<>n; 40 | stack s; 41 | for(int i=0;i>x; 43 | s.push(x); 44 | } 45 | reverse(s); 46 | printStack(s); 47 | } -------------------------------------------------------------------------------- /Recursion/Reverse-a-Stack.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Recursion/Reverse-a-Stack.exe -------------------------------------------------------------------------------- /Recursion/Sort-a-Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void insert(stack& s, int temp){ 7 | if(s.size()==0 || s.top()<=temp){ 8 | s.push(temp); 9 | return; 10 | } 11 | int val=s.top(); 12 | s.pop(); 13 | insert(s,temp); 14 | s.push(val); 15 | } 16 | 17 | void sort(stack& s){ 18 | if(s.size()==1) 19 | return; 20 | int temp=s.top(); 21 | s.pop(); 22 | sort(s); 23 | insert(s,temp); 24 | } 25 | 26 | void printStack(stack& s){ 27 | if (s.empty()) 28 | return; 29 | int x = s.top(); 30 | s.pop(); 31 | printStack(s); 32 | cout< s; 39 | cin>>n; 40 | for(int i=0;i>x; 42 | s.push(x); 43 | } 44 | sort(s); 45 | printStack(s); 46 | } -------------------------------------------------------------------------------- /Recursion/Sort-a-Stack.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Recursion/Sort-a-Stack.exe -------------------------------------------------------------------------------- /Recursion/Sort-an-Array-using-Recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void insert(vector& v, int temp){ 6 | if(v.size()==0 || v[v.size()-1]<=temp){ 7 | v.push_back(temp); 8 | return; 9 | } 10 | int val=v[v.size()-1]; 11 | v.pop_back(); 12 | insert(v,temp); 13 | v.push_back(val); 14 | } 15 | 16 | void sort(vector& v){ 17 | if(v.size()==1) 18 | return; 19 | int temp=v[v.size()-1]; 20 | v.pop_back(); 21 | sort(v); 22 | insert(v,temp); 23 | } 24 | 25 | 26 | int main(){ 27 | int n; 28 | cin>>n; 29 | vector v(n); 30 | for(int i=0;i>v[i]; 32 | } 33 | sort(v); 34 | for(int i=0;i 2 | using namespace std; 3 | void toh(int n, int s, int h, int d, int& count){ 4 | count++; 5 | if(n==1){ 6 | cout<<"Move "<>n; 17 | toh(n,s,h,d, count); 18 | cout<<"Steps: "< arr, int x){ 2 | int n=arr.size(); 3 | int step=sqrt(n); 4 | int prev = 0; 5 | while( arr[min(step, n)-1] < x){ 6 | prev = step; 7 | step += sqrt(n); 8 | if(prev >= n) 9 | return -1 10 | } 11 | while(arr[prev] 2 | using namespace std; 3 | 4 | int linearSearch(int arr[], int n, int x){ 5 | for(int i = 0; i>n; 15 | for(int i=0;i>arr[i]; 17 | } 18 | cout< 15 | using namespace std; 16 | 17 | void swap(int *a, int *b){ 18 | int c = *a; 19 | *a = *b; 20 | *b = c; 21 | } 22 | 23 | void heapify(vector& arr, int n, int i){ 24 | int largest = i; 25 | int left = 2 * i + 1; 26 | int right = 2 * i + 2; 27 | 28 | if(leftarr[largest]){ 29 | largest=left; 30 | } 31 | 32 | if(rightarr[largest]){ 33 | largest=right; 34 | } 35 | 36 | if(largest!=i){ 37 | swap(&arr[i],&arr[largest]); 38 | heapify(arr, n, largest); 39 | } 40 | } 41 | 42 | void heapSort(vector& arr, int n){ 43 | int i; 44 | for(i=(n/2)-1; i>=0; i--){ 45 | heapify(arr,n,i); 46 | } 47 | for(i=n-1; i>=0; i--){ 48 | swap(&arr[0], &arr[i]); 49 | heapify(arr,i,0); 50 | } 51 | } 52 | 53 | int main(void) { 54 | int n; 55 | cout<<"Enter Size: "; 56 | cin>>n; 57 | vector arr(n); 58 | for(int i=0;i>arr[i]; 60 | } 61 | 62 | cout<<"\nBefore HeapSort:\n"; 63 | for(int i=0;i 14 | #include 15 | 16 | using namespace std; 17 | 18 | void insertionSort(vector& arr, int n){ 19 | int j,key; 20 | for(int i=1;i=0 && arr[j]>key){ 24 | arr[j+1]=arr[j]; 25 | j--; 26 | } 27 | arr[j+1]=key; 28 | } 29 | } 30 | 31 | int main(void) { 32 | int n; 33 | cout<<"Enter the size:"; 34 | cin>>n; 35 | vector arr(n); 36 | cout<<"Enter the elements:\n"; 37 | for(int i=0; i>arr[i]; 39 | 40 | cout<<"\nBefore Sorting:\n"; 41 | for(int i=0;i 16 | #include 17 | #include 18 | 19 | using namespace std; 20 | 21 | void merge(vector& arr, int l, int m, int r){ 22 | int i,j,k; 23 | int n1 = (m-l)+1, n2 = r-m; 24 | int L[n1], R[n2]; 25 | 26 | for(int i=0;i& arr, int l, int r){ 57 | if (l < r) { 58 | int m = l + (r - l) / 2; 59 | mergeSort(arr, l, m); 60 | mergeSort(arr, m + 1, r); 61 | merge(arr, l, m, r); 62 | } 63 | } 64 | 65 | int main(){ 66 | int n; 67 | cout<<"Enter Size: "; 68 | cin>>n; 69 | vector arr(n); 70 | for(int i=0;i>arr[i]; 72 | } 73 | 74 | cout<<"\nBefore MergeSort:\n"; 75 | for(int i=0;i 15 | using namespace std; 16 | 17 | void swap(int *a, int *b){ 18 | int c = *a; 19 | *a = *b; 20 | *b = c; 21 | } 22 | 23 | int partition(vector& arr, int low, int high){ 24 | int i,j,pivot; 25 | pivot = arr[high]; 26 | i = (low-1); 27 | for (j = low; j <= high- 1; j++) 28 | { 29 | if (arr[j] <= pivot) 30 | { 31 | i++; 32 | swap(&arr[i],&arr[j]); 33 | } 34 | } 35 | swap(&arr[i + 1], &arr[high]); 36 | return(i+1); 37 | } 38 | 39 | void quickSort(vector& arr, int low, int high){ 40 | int pi; 41 | if(low>n; 52 | vector arr(n); 53 | for(int i=0;i>arr[i]; 55 | } 56 | 57 | cout<<"\nBefore QuickSort:\n"; 58 | for(int i=0;i 13 | #include 14 | 15 | using namespace std; 16 | 17 | void swap(int *a, int *b){ 18 | int c = *a; 19 | *a = *b; 20 | *b = c; 21 | } 22 | 23 | void selectionSort(vector& arr, int n){ 24 | int min; 25 | for(int i=0;i>n; 40 | vector arr(n); 41 | cout<<"Enter the elements:\n"; 42 | for(int i=0; i>arr[i]; 44 | 45 | cout<<"\nBefore Sorting:\n"; 46 | for(int i=0;i 14 | #include 15 | using namespace std; 16 | 17 | void shellSort(vector& arr, int n){ 18 | int h=1, i, j, key; 19 | while(h<=n) 20 | h=3*h+1; 21 | 22 | while(h>=1){ 23 | for(i=h; i=0 && key>n; 40 | vector arr(n); 41 | cout<<"Enter the elements:\n"; 42 | for(int i=0; i>arr[i]; 44 | 45 | cout<<"\nBefore Sorting:\n"; 46 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | vector NSL(vector& arr, int n){ 11 | vector left; 12 | stack> s; 13 | for(int i=0;i=arr[i]){ 21 | while(!s.empty() && s.top().first>=arr[i]){ 22 | s.pop(); 23 | } 24 | if(s.empty()) 25 | left.push_back(-1); //pseudoindex=-1 26 | else 27 | left.push_back(s.top().second); 28 | } 29 | s.push(make_pair(arr[i],i)); 30 | } 31 | return left; 32 | } 33 | vector NSR(vector& arr, int n){ 34 | vector right; 35 | stack> s; 36 | 37 | for(int i=n-1;i>=0;i--){ 38 | if(s.empty()){ 39 | right.push_back(n); //pseudoindex=n 40 | } 41 | else if(!s.empty() && s.top().first=arr[i]){ 45 | while(!s.empty() && s.top().first>=arr[i]){ 46 | s.pop(); 47 | } 48 | if(s.empty()) 49 | right.push_back(n); //pseudoindex=n 50 | else 51 | right.push_back(s.top().second); 52 | } 53 | s.push(make_pair(arr[i],i)); 54 | } 55 | 56 | reverse(right.begin(),right.end()); 57 | return right; 58 | } 59 | 60 | int MAH(vector& arr,int n){ 61 | vector area(n), left(n), right(n); 62 | 63 | left=NSL(arr,n); 64 | right=NSR(arr,n); 65 | 66 | int mah=INT_MIN; 67 | 68 | for(int i=0;i>n; 78 | vector arr(n); 79 | for(int i=0;i>arr[i]; 81 | } 82 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | vector NSL(vector& arr, int n){ 11 | vector left; 12 | stack> s; 13 | for(int i=0;i=arr[i]){ 21 | while(!s.empty() && s.top().first>=arr[i]){ 22 | s.pop(); 23 | } 24 | if(s.empty()) 25 | left.push_back(-1); //pseudoindex=-1 26 | else 27 | left.push_back(s.top().second); 28 | } 29 | s.push(make_pair(arr[i],i)); 30 | } 31 | return left; 32 | } 33 | vector NSR(vector& arr, int n){ 34 | vector right; 35 | stack> s; 36 | 37 | for(int i=n-1;i>=0;i--){ 38 | if(s.empty()){ 39 | right.push_back(n); //pseudoindex=n 40 | } 41 | else if(!s.empty() && s.top().first=arr[i]){ 45 | while(!s.empty() && s.top().first>=arr[i]){ 46 | s.pop(); 47 | } 48 | if(s.empty()) 49 | right.push_back(n); //pseudoindex=n 50 | else 51 | right.push_back(s.top().second); 52 | } 53 | s.push(make_pair(arr[i],i)); 54 | } 55 | 56 | reverse(right.begin(),right.end()); 57 | return right; 58 | } 59 | 60 | int MAH(vector& arr,int n){ 61 | vector area(n), left(n), right(n); 62 | 63 | left=NSL(arr,n); 64 | right=NSR(arr,n); 65 | 66 | int mah=INT_MIN; 67 | 68 | for(int i=0;i>& arr){ 76 | int n=arr.size(), m=arr[0].size(); 77 | vector v; 78 | for(int j=0;j>n>>m; 96 | vector> arr(n,vector(m,0)); 97 | for(int i=0;i>arr[i][j]; 100 | } 101 | } 102 | cout< 2 | using namespace std; 3 | vector KSum(vector& arr, int n, int k){ 4 | vector ans; 5 | list l; 6 | int i=0,j=0; 7 | while(j0 && l.back()>n; 28 | vector arr(n); 29 | for(int i=0;i>arr[i]; 31 | } 32 | cin>>k; 33 | vector ans=KSum(arr,n,k); 34 | for(int i:ans){ 35 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | stack s,ss; 10 | 11 | void pushOpr(int a){ 12 | s.push(a); 13 | if(ss.empty() || ss.top()>a) 14 | ss.push(a); 15 | } 16 | 17 | int popOpr(){ 18 | if(s.empty()) 19 | return -1; 20 | int ans=s.top(); 21 | s.pop(); 22 | if(ss.top()==ans) 23 | ss.pop(); 24 | return ans; 25 | } 26 | 27 | int minElement(){ 28 | if(ss.size()==0) 29 | return -1; 30 | return ss.top(); 31 | } 32 | 33 | void printStack(){ 34 | if (s.empty()) 35 | return; 36 | int x = s.top(); 37 | s.pop(); 38 | printStack(); 39 | cout<>n; 48 | switch(n){ 49 | case 1: cin>>a; 50 | pushOpr(a); 51 | cout<<"Pushed "< 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int minEle=0; 10 | stack s; 11 | 12 | int minElement(){ 13 | if(s.size()==0){ 14 | return -1; 15 | } 16 | return minEle; 17 | } 18 | void pushOpr(int x){ 19 | if(s.size()==0){ 20 | s.push(x); 21 | minEle=x; 22 | } 23 | else{ 24 | if(x>=minEle) 25 | s.push(x); 26 | else{ 27 | s.push((2*x)-minEle); 28 | minEle=x; 29 | } 30 | } 31 | } 32 | int topOpr(){ 33 | if(s.size()==0) 34 | return -1; 35 | else{ 36 | if(s.top()>=minEle) 37 | return s.top(); 38 | else{ 39 | return minEle; 40 | } 41 | } 42 | } 43 | int popOpr(){ 44 | int t; 45 | if(s.size()==0) 46 | return -1; 47 | else{ 48 | if(s.top()>=minEle){ 49 | t=s.top(); 50 | s.pop(); 51 | return t; 52 | } 53 | else{ 54 | t=minEle; 55 | minEle = 2*minEle - s.top(); 56 | s.pop(); 57 | return t; 58 | } 59 | } 60 | } 61 | 62 | int main(){ 63 | int n,a; 64 | while(true){ 65 | cout<<"\nStack Operations\n1. Push\n2. Pop\n3. Min Element\n4. Top\n5. Stop\n"; 66 | cin>>n; 67 | switch(n){ 68 | case 1: cin>>a; 69 | pushOpr(a); 70 | cout<<"Pushed "< 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | vector nearestGreaterToRight(vector &arr){ 9 | vector v; 10 | stack s; 11 | for(int i=arr.size()-1;i>=0;i--){ 12 | if(s.size()==0){ 13 | v.push_back(-1); 14 | } 15 | else if(s.size()>0 && s.top()>arr[i]){ 16 | v.push_back(s.top()); 17 | } 18 | else if(s.size()>0 && s.top()0 && s.top()<=arr[i]){ 20 | s.pop(); 21 | } 22 | if(s.size()==0) 23 | v.push_back(-1); 24 | else 25 | v.push_back(s.top()); 26 | } 27 | s.push(arr[i]); 28 | } 29 | reverse(v.begin(),v.end()); 30 | return v; 31 | } 32 | 33 | int main(){ 34 | int n; 35 | cin>>n; 36 | vector arr(n), v(n); 37 | for(int i=0;i>arr[i]; 39 | } 40 | v=nearestGreaterToRight(arr); 41 | for(int i : v) 42 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | vector nearestGreaterToLeft(vector& arr){ 7 | stack s; 8 | vector v; 9 | for(int i=0;i0 && s.top()>arr[i]){ 14 | v.push_back(s.top()); 15 | } 16 | else if(s.size()>0 && s.top()0 && s.top()<=arr[i]){ 18 | s.pop(); 19 | } 20 | if(s.size()==0) 21 | v.push_back(-1); 22 | else 23 | v.push_back(s.top()); 24 | } 25 | s.push(arr[i]); 26 | } 27 | return v; 28 | } 29 | 30 | int main(){ 31 | int n; 32 | cin>>n; 33 | vector arr(n), v(n); 34 | for(int i=0;i>arr[i]; 36 | } 37 | v=nearestGreaterToLeft(arr); 38 | for(int i:v) 39 | cout< 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | vector nearestSmallerToLeft(vector &arr, int n){ 9 | stack s; 10 | vector v; 11 | 12 | for(int i=0;i0 && s.top()0 && s.top()>arr[i]){ 20 | while(s.size()>0 && s.top()>=arr[i]){ 21 | s.pop(); 22 | } 23 | if(s.size()==0) 24 | v.push_back(-1); 25 | else 26 | v.push_back(s.top()); 27 | } 28 | s.push(arr[i]); 29 | } 30 | return v; 31 | } 32 | 33 | int main(){ 34 | int n; 35 | cin>>n; 36 | vector arr(n),v(n); 37 | for(int i=0;i>arr[i]; 39 | } 40 | v=nearestSmallerToLeft(arr,n); 41 | for(int i : v){ 42 | cout< 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | vector nearestSmallerToRight(vector &arr, int n){ 9 | stack s; 10 | vector v; 11 | 12 | for(int i=n-1;i>=0;i--){ 13 | if(s.size()==0){ 14 | v.push_back(-1); 15 | } 16 | else if(s.size()>0 && s.top()0 && s.top()>arr[i]){ 20 | while(s.size()>0 && s.top()>=arr[i]){ 21 | s.pop(); 22 | } 23 | if(s.size()==0) 24 | v.push_back(-1); 25 | else 26 | v.push_back(s.top()); 27 | } 28 | s.push(arr[i]); 29 | } 30 | reverse(v.begin(),v.end()); 31 | return v; 32 | } 33 | 34 | int main(){ 35 | int n; 36 | cin>>n; 37 | vector arr(n),v(n); 38 | for(int i=0;i>arr[i]; 40 | } 41 | v=nearestSmallerToRight(arr,n); 42 | for(int i : v){ 43 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int rainwaterTrapping(vector& arr, int n){ 8 | vector maxL(n), maxR(n), water(n); 9 | int sum=0; 10 | maxL[0]=arr[0]; 11 | for(int i=1;i=0;i--){ 16 | maxR[i]=max(maxR[i+1],arr[i]); 17 | } 18 | 19 | for(int i=0;i>n; 29 | vector arr(n); 30 | for(int i=0;i>arr[i]; 32 | } 33 | cout< 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | vector stockSpanProblem(vector arr,int n){ 9 | stack> s; 10 | vector v; 11 | for(int i=0;i0 && s.top().first>arr[i]){ 16 | v.push_back(s.top().second); 17 | 18 | } 19 | else if(s.size()>0 && s.top().first<=arr[i]){ 20 | while(s.size()>0 && s.top().first<=arr[i]){ 21 | s.pop(); 22 | } 23 | if(s.size()==0){ 24 | v.push_back(-1); 25 | } 26 | else{ 27 | v.push_back(s.top().second); 28 | } 29 | } 30 | s.push(make_pair(arr[i],i)); 31 | } 32 | for(int i=0;i>n; 41 | vector arr(n),v(n); 42 | for(int i=0;i>arr[i]; 44 | } 45 | v=stockSpanProblem(arr,n); 46 | for(int i : v){ 47 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class BST{ 8 | int data; 9 | BST* left; 10 | BST* right; 11 | public: 12 | BST(); 13 | BST(int); 14 | BST* insert(BST*,int); 15 | void inorder(BST*); 16 | int height(BST*); 17 | }; 18 | 19 | BST ::BST(){ 20 | this->data=0; 21 | this->left=NULL; 22 | this->right=NULL; 23 | } 24 | BST ::BST(int data){ 25 | this->data=data; 26 | this->left=NULL; 27 | this->right=NULL; 28 | } 29 | 30 | BST* BST ::insert(BST* root, int value){ 31 | if(root==NULL){ 32 | return new BST(value); 33 | } 34 | 35 | if(value > root->data){ 36 | root->right=insert(root->right,value); 37 | } 38 | else if(value <= root->data){ 39 | root->left=insert(root->left,value); 40 | } 41 | 42 | return root; 43 | } 44 | 45 | void BST ::inorder(BST* root){ 46 | if(root==NULL) 47 | return; 48 | inorder(root->left); 49 | cout<< root->data<<" "; 50 | inorder(root->right); 51 | } 52 | 53 | int BST::height(BST* root){ 54 | if(root=NULL) 55 | return 0; 56 | int lh=height(root->left); 57 | int rh=height(root->right); 58 | return 1 + max(lh,rh); 59 | } 60 | 61 | int main(){ 62 | int n; 63 | BST b, *root=NULL; 64 | cin>>n; 65 | vector arr(n); 66 | for(int i=0;i>arr[i]; 68 | } 69 | root=b.insert(root, arr[0]); 70 | for(int i=1;i 2 | using namespace std; 3 | 4 | void printleftboundary(TreeNode* root){ 5 | if(root == NULL) return; 6 | if(root->left){ 7 | cout<val; 8 | printleftboundary(root->left); 9 | } 10 | else if(root->right){ 11 | cout<val; 12 | printleftboundary(root->right); 13 | } 14 | } 15 | 16 | void printleaves(TreeNode* root){ 17 | if(root == NULL) return; 18 | printleaves(root->left); 19 | if(root->left==NULL && root->right==NULL){ 20 | cout<val; 21 | } 22 | printleaves(root->right); 23 | } 24 | 25 | void printrightbouundary(TreeNode* root){ 26 | if(root==NULL) return; 27 | if(root->right){ 28 | printrightboundary(root->right); 29 | cout<val; 30 | } 31 | else if(root->left){ 32 | printrightboundary(root->left); 33 | cout<val; 34 | } 35 | } 36 | 37 | void boundaryTraversal(TreeNode* root){ 38 | if(root==NULL){ 39 | return; 40 | } 41 | cout<val; 42 | 43 | printleftboundary(root->left); 44 | printleaves(root->left); 45 | printleaves(root->right); 46 | printrightboundary(root->right); 47 | 48 | } -------------------------------------------------------------------------------- /Tree/Construct-Binary-tree-from-String.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int val; 6 | Node* left; 7 | Node* right; 8 | Node(int val){ 9 | this->val=val; 10 | this->left=nullptr; 11 | this->right=nullptr; 12 | } 13 | }; 14 | Node* constructBT(string s){ 15 | Node* root=new Node(s[0]-'0'); 16 | stack st; 17 | 18 | for(int i=1;ileft==nullptr){ 28 | Node* left=new Node(s[i]-'0'); 29 | root->left=left; 30 | root=root->left; 31 | } 32 | else if(root->right==nullptr){ 33 | Node* right=new Node(s[i]-'0'); 34 | root->right=right; 35 | root=root->right; 36 | } 37 | } 38 | } 39 | return root; 40 | } 41 | void preorder(Node* root){ 42 | if(!root){ 43 | return; 44 | } 45 | cout<val<<" "; 46 | preorder(root->left); 47 | preorder(root->right); 48 | } 49 | 50 | int main(){ 51 | string s; 52 | getline(cin, s); 53 | Node* root=constructBT(s); 54 | preorder(root); 55 | } -------------------------------------------------------------------------------- /Tree/Construct-Binary-tree-from-String.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Tree/Construct-Binary-tree-from-String.exe -------------------------------------------------------------------------------- /Tree/Convert-Binary-tree-into-Mirror-tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int val; 6 | Node* left; 7 | Node* right; 8 | Node(int val){ 9 | this->val=val; 10 | this->left=nullptr; 11 | this->right=nullptr; 12 | } 13 | }; 14 | Node* constructBT(string s){ 15 | Node* root=new Node(s[0]-'0'); 16 | stack st; 17 | 18 | for(int i=1;ileft==nullptr){ 28 | Node* left=new Node(s[i]-'0'); 29 | root->left=left; 30 | root=root->left; 31 | } 32 | else if(root->right==nullptr){ 33 | Node* right=new Node(s[i]-'0'); 34 | root->right=right; 35 | root=root->right; 36 | } 37 | } 38 | } 39 | return root; 40 | } 41 | void preorder(Node* root){ 42 | if(!root){ 43 | return; 44 | } 45 | cout<val<<" "; 46 | preorder(root->left); 47 | preorder(root->right); 48 | } 49 | 50 | int main(){ 51 | string s; 52 | getline(cin, s); 53 | Node* root=constructBT(s); 54 | preorder(root); 55 | } -------------------------------------------------------------------------------- /Tree/Level-Order-Traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector levelOrder(TreeNode *root){ 5 | if(root==NULL){ 6 | return new vector(); 7 | } 8 | vector result; 9 | queue q; 10 | q.push(root); 11 | 12 | while(!q.empty()){ 13 | TreeNode* curr=q.top(); 14 | q.pop(); 15 | 16 | result.push_back(curr->data); 17 | 18 | if(curr->left!=NULL){ 19 | q.push(curr->left); 20 | } 21 | 22 | if(curr->right!=NULL){ 23 | q.push(curr->right); 24 | } 25 | 26 | } 27 | return result; 28 | } -------------------------------------------------------------------------------- /Tree/Lowest-Common-Ancestor-BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | TreeNode* LCA(TreeNode* root, int n1, int n2){ 5 | if(root == NULL) return root; 6 | 7 | if (root->data > n1 && root->data > n2) 8 | return LCA(root->left, n1, n2); 9 | 10 | if (root->data < n1 && root->data < n2) 11 | return LCA(root->right, n1, n2); 12 | 13 | return root; 14 | } -------------------------------------------------------------------------------- /Tree/Lowest-Common-Ancestor-in-BT.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Tree/Lowest-Common-Ancestor-in-BT.cpp -------------------------------------------------------------------------------- /Tree/Maximum-Difference-between-Node-and-Ancestor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node{ 5 | int val; 6 | Node* left; 7 | Node* right; 8 | Node(int val){ 9 | this->val=val; 10 | this->left=nullptr; 11 | this->right=nullptr; 12 | } 13 | }; 14 | 15 | int res; 16 | 17 | void maxDifference(Node* root, int maxs, int mins){ 18 | if(!root){ 19 | return; 20 | } 21 | 22 | int maxs=max(root->val, maxs); 23 | int mins=min(root->val, mins); 24 | 25 | res=max(res, maxs-mins); 26 | 27 | maxDifference(root->left, maxs, mins): 28 | maxDifference(root->right, maxs, mins); 29 | } 30 | 31 | 32 | int main(){ 33 | 34 | } -------------------------------------------------------------------------------- /Tree/Maximum-Width-of-Binary-Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int maxWidth(TreeNode *root){ 5 | if(right==NULL){ 6 | return 0; 7 | } 8 | 9 | queue q; 10 | q.push(root); 11 | int result=0; 12 | 13 | while(!q.empty()){ 14 | int n=q.size(); 15 | result=max(result,n); 16 | 17 | while(count--){ 18 | TreeNode *curr=q.front(); 19 | q.pop(); 20 | 21 | if(curr->left!=NULL){ 22 | q.push(curr->left); 23 | } 24 | if(curr->right!=NULL){ 25 | q.push(curr->right); 26 | } 27 | } 28 | } 29 | return result; 30 | } -------------------------------------------------------------------------------- /Tree/Path-Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct TreeNode{ 5 | int val; 6 | TreeNode *left; 7 | TreeNode *right; 8 | TreeNode(): val(0), left(nullptr), right(nullptr); 9 | TreeNode(int x) : val(x), left(nullptr), right(nullptr); 10 | TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right); 11 | }; 12 | 13 | bool pathSum(TreeNode* root, int targetSum){ 14 | if(root==NULL){ 15 | return false; 16 | } 17 | if(targetSum==root->val && root->left==NULL && root->right==NULL){ 18 | return true; 19 | } 20 | return pathSum(root->left, targetSum-root->val) || pathSum(root->right, targetSum-root->val); 21 | } 22 | 23 | //Complexity: O(n) 24 | 25 | void getAllPaths(TreeNode *root, int targetSum, vector temp, vector> &result){ 26 | if(root==NULL){ 27 | return; 28 | } 29 | if(root->data==targetSum && root->left==NULL && root->right==NULL){ 30 | temp.push_back(root->val); 31 | result.push_back(temp); 32 | return; 33 | } 34 | temp.push_back(root->val); 35 | getAllPaths(root->left, targetSum-root->val, temp, result); 36 | getAllPaths(root->right, targetSum-root->val, temp, result); 37 | } 38 | 39 | vector> pathSum(TreeNode* root, int targetSum){ 40 | vector result; 41 | getAllPaths(root, targetSum, {}, result); 42 | return result; 43 | } -------------------------------------------------------------------------------- /Tree/Symmetric-Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isSymTree(TreeNode *root){ 5 | if(root == NULL){ 6 | return true; 7 | } 8 | return solve(root->left && root->right); 9 | } 10 | 11 | bool solve(TreeNode *l, TreeNode *r){ 12 | if(l==NULL && r==NULL) return true; 13 | else if(l==NULL || r==NULL) return false; 14 | else if(l->val != r->val) return false; 15 | 16 | return solve(l->left,r->right) && solve(l->right, r->left); 17 | } -------------------------------------------------------------------------------- /Tree/ZigZag-Traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef struct Node{ 5 | int data; 6 | Node* left; 7 | Node* right; 8 | } Node; 9 | 10 | 11 | void zigzagTraversal(Node* root){ 12 | stack currentLevel; 13 | stack nextLevel; 14 | 15 | vector zigzag; 16 | 17 | currentLevel.push(root); 18 | bool lefttoright=true; 19 | 20 | while(!currentLevel.empty()){ 21 | Node* temp=currentLevel.top(); 22 | currentLevel.pop(); 23 | 24 | if(temp){ 25 | zigzag.push_back(temp->data); 26 | 27 | if(lefttoright==true){ 28 | if(temp->left){ 29 | nextLevel.push(temp->left); 30 | } 31 | if(temp->right){ 32 | nextLevel.push(temp->right); 33 | } 34 | } 35 | else{ 36 | if(temp->right){ 37 | nextLevel.push(temp->right); 38 | } 39 | if(temp->left){ 40 | nextLevel.push(temp->left); 41 | } 42 | } 43 | } 44 | 45 | if(currentLevel.empty()){ 46 | lefttoright=!lefttoright; 47 | swap(currentLevel,nextLevel); 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /Trie/Longest-Word-with-all-Prefixes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct TrieNode{ 5 | struct TrieNode *children[26]; 6 | bool flag=false; 7 | 8 | bool containsKey(char ch){ 9 | return children[ch-'a']!=NULL; 10 | } 11 | void put(char ch, TrieNode *node){ 12 | children[ch-'a']=node; 13 | } 14 | 15 | TrieNode* get(char ch){ 16 | return children[ch-'a']; 17 | } 18 | void setEnd(){ 19 | flag=true; 20 | } 21 | 22 | bool isEnd(){ 23 | return flag; 24 | } 25 | }; 26 | class Trie{ 27 | private: 28 | struct TrieNode *root; 29 | public: 30 | Trie(){ 31 | root=new TrieNode(); 32 | } 33 | 34 | void insert(string word){ 35 | struct TrieNode *node=root; 36 | for(int i=0;icontainsKey(word[i])){ 38 | node->put(word[i], new TrieNode()); 39 | } 40 | node=node->get(word[i]); 41 | } 42 | node->setEnd(); 43 | } 44 | 45 | bool checkIfPrefixExists(string word){ 46 | struct TrieNode *node=root; 47 | bool flag=true; 48 | for(int i=0;icontainsKey(word[i])){ 50 | node=node->get(word[i]); 51 | flag = flag && node->isEnd(); 52 | } 53 | } 54 | return flag; 55 | } 56 | 57 | }; 58 | string completeString(int n, vector &a){ 59 | Trie *trie = new Trie(); 60 | for(auto word : a){ 61 | trie->insert(word); 62 | } 63 | 64 | string longest=""; 65 | for(auto word : a){ 66 | if(trie->checkIfPrefixExists(word)){ 67 | if(word.size()>longest.size()){ 68 | longest=word; 69 | } 70 | else if(word.size()==longest.size() && word>n; 84 | vector a(n); 85 | for(int i=0;i>a[i]; 87 | } 88 | cout< 2 | using namespace std; 3 | 4 | struct TrieNode{ 5 | TrieNode *children[26]; 6 | int countEndWith=0; 7 | int countPrefix=0; 8 | 9 | bool containsKey(char ch){ 10 | return(children[ch-'a']!=NULL); 11 | } 12 | 13 | void put(char ch, TrieNode *node){ 14 | children[ch-'a']=node; 15 | } 16 | 17 | TrieNode* get(char ch){ 18 | return children[ch-'a']; 19 | } 20 | 21 | int getEnd(){ 22 | return countEndWith; 23 | } 24 | 25 | int getPrefix(){ 26 | return countPrefix; 27 | } 28 | 29 | void increaseEnd(){ 30 | countEndWith++; 31 | } 32 | 33 | void increasePrefix(){ 34 | countPrefix++; 35 | } 36 | 37 | void reducePrefix(){ 38 | countPrefix--; 39 | } 40 | 41 | void reduceEnd(){ 42 | countEndWith--; 43 | } 44 | 45 | }; 46 | 47 | class Trie{ 48 | private: 49 | struct TrieNode *root; 50 | 51 | public: 52 | Trie(){ 53 | root=new TrieNode(); 54 | } 55 | 56 | void insert(string word){ 57 | struct TrieNode *node=root; 58 | for(int i=0;icontainsKey(word[i])){ 60 | node->put(word[i], new TrieNode()); 61 | } 62 | node=node->get(word[i]); 63 | node->increasePrefix(); 64 | } 65 | node->increaseEnd(); 66 | } 67 | 68 | int countWordsEqualTo(string word){ 69 | struct TrieNode *node=root; 70 | 71 | for(int i=0;icontainsKey(word[i])){ 73 | return 0; 74 | } 75 | else{ 76 | node=node->get(word[i]); 77 | } 78 | } 79 | return node->getEnd(); 80 | } 81 | 82 | int countWordsStartWith(string word){ 83 | struct TrieNode *node=root; 84 | for(int i=0;icontainsKey(word[i])){ 86 | return 0; 87 | } 88 | else{ 89 | node=node->get(word[i]); 90 | } 91 | } 92 | return node->getPrefix(); 93 | } 94 | 95 | void erase(string word){ 96 | struct TrieNode *node=root; 97 | for(int i=0;icontainsKey(word[i])){ 99 | node=node->get(word[i]); 100 | node->reducePrefix(); 101 | } 102 | else{ 103 | return; 104 | } 105 | } 106 | node->reduce1End(); 107 | } 108 | 109 | }; 110 | 111 | int main() 112 | { 113 | int i; 114 | string word; 115 | Trie* t = new Trie(); 116 | while(true){ 117 | cout<<"Trie Implementation\n1. Insert\n2. Count Words\n3. Count Prefix\n4. Erase\n5. End\n"; 118 | cin>>i; 119 | switch(i){ 120 | case 1: cin>>word; 121 | t->insert(word); 122 | break; 123 | case 2: cin>> word; 124 | cout<countWordsEqualTo(word)<> word; 127 | cout<countWordsStartWith(word)<> word; 130 | t->erase(word); 131 | break; 132 | case 5: exit(0); 133 | default:cout<<"Incorrect Input"; 134 | } 135 | } 136 | } -------------------------------------------------------------------------------- /Trie/Trie-Implementation-2.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rohan-khurana/Data-Structures-and-Algorithms/f246e50adbad23bc9bf9df8db2d0fd1592b81196/Trie/Trie-Implementation-2.exe -------------------------------------------------------------------------------- /Trie/Trie-Implementation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct TrieNode{ 6 | TrieNode *children[26]; 7 | bool flag=false; 8 | 9 | bool containsKey(char ch){ 10 | return(children[ch-'a']!=NULL); 11 | } 12 | 13 | void put(char ch, TrieNode *node){ 14 | children[ch-'a']=node; 15 | } 16 | 17 | TrieNode* get(char ch){ 18 | return children[ch-'a']; 19 | } 20 | 21 | void setEnd(){ 22 | flag=true; 23 | } 24 | 25 | bool isEnd(){ 26 | return flag; 27 | } 28 | 29 | }; 30 | 31 | class Trie{ 32 | private: 33 | struct TrieNode *root; 34 | public: 35 | Trie(){ 36 | root=new TrieNode(); 37 | } 38 | 39 | void insert(string word){ 40 | struct TrieNode* node=root; 41 | for(int i=0;icontainsKey(word[i])){ 43 | node->put(word[i], new TrieNode()); 44 | } 45 | node=node->get(word[i]); 46 | } 47 | node->setEnd(); 48 | } 49 | 50 | bool search(string word){ 51 | struct TrieNode *node=root; 52 | 53 | for(int i=0;icontainsKey(word[i])){ 55 | return false; 56 | } 57 | node=node->get(word[i]); 58 | } 59 | if(node->isEnd()){ 60 | return true; 61 | } 62 | return false; 63 | } 64 | 65 | bool startsWith(string prefix){ 66 | struct TrieNode *node = root; 67 | for(int i=0;icontainsKey(prefix[i])){ 69 | return false; 70 | } 71 | node=node->get(prefix[i]); 72 | } 73 | return true; 74 | } 75 | }; 76 | 77 | 78 | int main(){ 79 | int i; 80 | string word; 81 | Trie* t = new Trie(); 82 | while(true){ 83 | cout<<"Trie Implementation\n1. Insert\n2. Search\n3. Search with Prefix\n4. End\n"; 84 | cin>>i; 85 | switch(i){ 86 | case 1: cin>>word; 87 | t->insert(word); 88 | break; 89 | case 2: cin>> word; 90 | cout<<(t->search(word)==1 ? "true" : "false")<> word; 93 | cout<<(t->startsWith(word)==1 ? "true" : "false")< 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 |

Data-Structures-and-Algorithms

13 |
14 |
15 |
16 |
17 |

Searching Algorithms

18 |
19 |
    20 |
  1. Linear Search
  2. 21 |
  3. Binary Search
  4. 22 |
  5. Jump Search
  6. 23 |
  7. Recursive Binary & Linear Search
  8. 24 |
25 |
26 |
27 |
28 |

Sorting Algorithms

29 |
30 |
    31 |
  1. Bubble Sort
  2. 32 |
  3. Selection Sort
  4. 33 |
  5. Insertion Sort
  6. 34 |
  7. Merge Sort
  8. 35 |
  9. Quick Sort
  10. 36 |
  11. Heap Sort
  12. 37 |
  13. Shell Sort
  14. 38 |
39 |
40 | 58 | 74 | 106 |
--------------------------------------------------------------------------------