├── 3Sum.cpp ├── Algorithms ├── Arithmetic_Slices.cpp └── Dutch_National_Flag_Algorithm.cpp ├── Base Conversion ├── base_to_dec.cpp └── dec_to_base.cpp ├── CONTRIBUTING.md ├── Code _ Queue Using LL.cpp ├── Graphs ├── DFSofGraph.cpp ├── dijkstras.cpp ├── floyedWarshal.cpp └── mst.cpp ├── LinkedList ├── ReverseLinkedList.cpp └── singlylinkedlist_143.c ├── Longest Consecutive Sequence.cpp ├── Queue ├── circularqueue.c └── queue.c ├── README.md ├── Recursion ├── 1 printNNames.cpp ├── 10 factorial.cpp ├── 11 factorial (parameterized).cpp ├── 12[1] reverseArray(for loop).cpp ├── 12[2] reverseArray(recursion).cpp ├── 13[1] palindrome(for loop).cpp ├── 13[2] palindrome(recursion).cpp ├── 14[1]fibonacci(for loop).cpp ├── 14[2]fibonacci(recursion).cpp ├── 15 subsequence.cpp ├── 16[1] printingSubsequenceWhooseSumIsK.cpp ├── 16[2] printingOnlyOneSubsequenceWhooseSumIsK.cpp ├── 16[3] printingCountOfSubsequenceWhooseSumIsK.cpp ├── 17 COmbinationSum-I Leetcode39.cpp ├── 18 combinationSum-II Leetcode40.cpp ├── 19 subsetSum-I.cpp ├── 2 1toN.cpp ├── 20 SubsetSumII Leetcode90.cpp ├── 21 PermutationsOfArrOrStr Leetcode46.cpp ├── 21[1] PermutationsOfArrOrStr Leetcode46 .cpp ├── 21[2] PermutationsOfArrOrStr Leetcode46 .cpp ├── 22 NQueens Leetcode51.cpp ├── 22[2] NQueens effecient Leetcode51.cpp ├── 23 sudokuSolver Leetcode 37.cpp ├── 3 Nto1.cpp ├── 39-combination-sum │ ├── 39-combination-sum.cpp │ └── Notes.md ├── 4 1toNBacktrack.cpp ├── 5 Nto1Backtrack.cpp ├── 6 sumOfFirstN.cpp ├── 7 test.cpp ├── 8 sumOfFirstN(Functional).cpp ├── 8 sumOfFirstN(paramiterized).cpp ├── 9 sumOfFirstN(Functional).cpp ├── 90-Subsets-II │ ├── 90-subsets-II.cpp │ └── notes.md ├── BInary_Search.cpp ├── LetterCombinationsPhoneNum.cpp ├── N-Knights.cpp ├── a ├── a.out ├── akash ├── input.txt └── output.txt ├── Searching └── modified_search_143.cpp ├── Sorting ├── MergeSort.cpp ├── QuickSort.cpp ├── Selection_Sort.cpp ├── Selection_Sort.exe ├── bubble_sort.cpp ├── heapSort.cpp ├── insertion sort_143.c └── shell_sort.cpp ├── SpiralMatrixSort.cpp ├── Stack └── stack.c ├── Trie.cpp └── Implement_trie.cpp ├── Unique tricky programs └── Subarray_sum_equals_k.cpp ├── binary_tree └── binary_tree_level_order_travesals.cpp ├── circularQueueArray.cpp ├── circular_queue.cpp ├── queue.cpp └── queue_using_stack.cpp /3Sum.cpp: -------------------------------------------------------------------------------- 1 | //Given an integer array nums, return any one triplet [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. 2 | 3 | //Notice that the solution set must not contain duplicate triplets. 4 | //Input: nums = [-1,0,1,2,-1,-4] 5 | //Output: [-1,-1,2] 6 | #include 7 | using namespace std; 8 | h_ans(vector& nums) { 9 | sort(nums.begin(),nums.end());//sort the array to apply two pointer 10 | int n=nums.size(); 11 | for(int i=0;i0)){ 13 | int low=i+1; 14 | int high=n-1; 15 | 16 | int hit=nums[i];//sum 1 17 | 18 | while(lowtemp; 23 | temp.push_back(nums[i]); 24 | temp.push_back(nums[low]); 25 | temp.push_back(nums[high]); 26 | return ans; 27 | 28 | while(low0){ 36 | high--; 37 | } 38 | } 39 | } 40 | } 41 | return {-1}; 42 | } 43 | int main(){ 44 | vectorarr; 45 | for(int i=0;i>arr[i]; 47 | } 48 | vectorans=h_ans(arr); 49 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int numberOfArithmeticSlices(vector& nums) { 7 | // if nums size is less than 3 return false 8 | if(nums.size() < 3) 9 | return 0; 10 | 11 | int cnt = 0, diff; 12 | 13 | for(int i = 0; i>n; 42 | int c; 43 | vector ve; 44 | for(int i = 0; i>c; 47 | ve.push_back(c); 48 | c = 0; 49 | } 50 | 51 | cout <<"Number of Arithmetic Slices = " <<(ob.numberOfArithmeticSlices(ve)); 52 | } -------------------------------------------------------------------------------- /Algorithms/Dutch_National_Flag_Algorithm.cpp: -------------------------------------------------------------------------------- 1 | //Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue. 2 | 3 | //We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively. 4 | 5 | //Input: nums = [2,0,2,1,1,0] 6 | //Output: [0,0,1,1,2,2] 7 | 8 | 9 | //Code in CPP 10 | 11 | #include 12 | using namespace std; 13 | 14 | int main(){ 15 | int n; 16 | cin>>n; 17 | int arr[n]; 18 | for(int i=0;i>arr[i]; 20 | } 21 | int low=0; 22 | int mid=0; 23 | int high=n-1; 24 | 25 | 26 | while(mid<=high){ 27 | if(nums[mid]==0){ 28 | swap(nums[mid],nums[low]); 29 | low++; 30 | mid++; 31 | }else if(nums[mid]==1)mid++; 32 | else { 33 | swap(nums[mid],nums[high]); 34 | high--;//if we increment mid pointer then there will be conflicts which can be understood by this 35 | //example 0 0(low) 0 1(mid) 0 1(high) 2 36 | } 37 | 38 | } 39 | 40 | for(int i=0;i 2 | using namespace std; 3 | 4 | int val(char c) { 5 | if (c >= '0' && c <= '9') 6 | return (int)c - '0'; 7 | else 8 | return (int)c - 'A' + 10; 9 | } 10 | 11 | int convert(string str, int base) { 12 | int len = str.length(); 13 | int power = 1; 14 | int num = 0; 15 | int i; 16 | for (i = len - 1; i >= 0; i--) { 17 | if (val(str[i]) >= base) { 18 | printf("Invalid Number"); 19 | return -1; 20 | } 21 | num += val(str[i]) * power; 22 | power = power * base; 23 | } 24 | return num; 25 | } 26 | 27 | int main(){ 28 | cout << "Enter number and its base" << endl; 29 | string num; 30 | int base; 31 | cin >> num >> base; 32 | cout << convert(num, base) << endl; 33 | } -------------------------------------------------------------------------------- /Base Conversion/dec_to_base.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | char reVal(int num) 6 | { 7 | if (num >= 0 && num <= 9) 8 | return (char)(num + '0'); 9 | else 10 | return (char)(num - 10 + 'A'); 11 | } 12 | 13 | string convert(string res, int base, int inputNum) 14 | { 15 | int index = 0; 16 | while (inputNum > 0) { 17 | res.push_back(reVal(inputNum % base)); 18 | index++; 19 | inputNum /= base; 20 | } 21 | reverse(res.begin(), res.end()); 22 | 23 | return res; 24 | } 25 | 26 | int main(){ 27 | cout << "Enter decimal number and base to convert it to" << endl; 28 | int num, base; 29 | string res; 30 | cin >> num >> base; 31 | cout << convert(res, base, num) << endl; 32 | } -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | #Add only Data Structure and Algorithams programs (must be unique). 2 | # Programs must be runable by compiling them don't contribute function based programs like **leetcode** . 3 | 4 | 5 | -------------------------------------------------------------------------------- /Code _ Queue Using LL.cpp: -------------------------------------------------------------------------------- 1 | Code : Queue Using LL 2 | Send Feedback 3 | You need to implement a Queue class using linked list. 4 | All the required data members should be private. 5 | Implement the following public functions : 6 | 1. Constructor - 7 | Initialises the data members. 8 | 2. enqueue : 9 | This function should take one argument of type T and has return type void. This function should insert an element in the queue. Time complexity should be O(1). 10 | 3. dequeue : 11 | This function takes no input arguments and has return type T. This should removes the first element which is entered and return that element as an answer. Time complexity should be O(1). 12 | 4. front : 13 | This function takes no input arguments and has return type T. This should return the first element which is entered and return that element as an answer. Time complexity should be O(1). 14 | 5. size : 15 | Return the size of stack i.e. count of elements which are present ins stack right now. Time complexity should be O(1). 16 | 6. isEmpty : 17 | Checks if the queue is empty or not. Return true or false. 18 | 19 | 20 | 21 | PRACTICE 22 | 23 | 24 | 25 | template 26 | class Queue { 27 | 28 | 29 | public : 30 | 31 | 32 | Queue() { 33 | 34 | } 35 | 36 | void enqueue(T data) { 37 | 38 | } 39 | 40 | int getSize() { 41 | 42 | } 43 | 44 | bool isEmpty() { 45 | 46 | } 47 | 48 | T dequeue() { 49 | // Return 0 if queue is empty 50 | } 51 | 52 | T front() { 53 | // Return 0 if queue is empty 54 | } 55 | }; 56 | 57 | 58 | SOLUTION 59 | 60 | 61 | 62 | template 63 | class Queue { 64 | Node *head; 65 | Node *tail; 66 | int size; 67 | public : 68 | 69 | 70 | Queue() { 71 | head = NULL; 72 | tail = NULL; 73 | size = 0; 74 | } 75 | 76 | void enqueue(T data) { 77 | Node *newNode = new Node(data); 78 | if(head==NULL) 79 | { 80 | head = newNode; 81 | tail = newNode; 82 | 83 | } 84 | else 85 | { 86 | tail->next = newNode; 87 | tail = newNode; 88 | } 89 | size++; 90 | } 91 | 92 | int getSize() { 93 | return size; 94 | } 95 | 96 | bool isEmpty() { 97 | return size==0; 98 | } 99 | 100 | T dequeue() { 101 | // Return 0 if queue is empty 102 | if(isEmpty()) 103 | {return 0;} 104 | T ans = head->data; 105 | Node *temp = head; 106 | head = head->next; 107 | delete temp; 108 | size--; 109 | return ans; 110 | } 111 | 112 | T front() { 113 | // Return 0 if queue is empty 114 | if(isEmpty()) 115 | {return 0;} 116 | return head->data; 117 | } 118 | }; 119 | 120 | 121 | 122 | #include 123 | using namespace std; 124 | 125 | 126 | template 127 | class Node { 128 | public : 129 | T data; 130 | Node *next; 131 | 132 | Node(T data) { 133 | this -> data = data; 134 | next = NULL; 135 | } 136 | }; 137 | 138 | #include "Queue.h" 139 | int main() { 140 | 141 | Queue q; 142 | 143 | int choice; 144 | cin >> choice; 145 | int input; 146 | 147 | while (choice !=-1) { 148 | if(choice == 1) { 149 | cin >> input; 150 | q.enqueue(input); 151 | } 152 | else if(choice == 2) { 153 | int ans = q.dequeue(); 154 | if(ans != 0) { 155 | cout << ans << endl; 156 | } 157 | else { 158 | cout << "-1" << endl; 159 | } 160 | } 161 | else if(choice == 3) { 162 | int ans = q.front(); 163 | if(ans != 0) { 164 | cout << ans << endl; 165 | } 166 | else { 167 | cout << "-1" << endl; 168 | } 169 | } 170 | else if(choice == 4) { 171 | cout << q.getSize() << endl; 172 | } 173 | else if(choice == 5) { 174 | if(q.isEmpty()) { 175 | cout << "true" << endl; 176 | } 177 | else { 178 | cout << "false" << endl; 179 | } 180 | } 181 | cin >> choice; 182 | } 183 | 184 | } 185 | 186 | -------------------------------------------------------------------------------- /Graphs/DFSofGraph.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to print DFS traversal from 2 | // a given vertex in a given graph 3 | #include 4 | using namespace std; 5 | 6 | // Graph class represents a directed graph 7 | // using adjacency list representation 8 | class Graph { 9 | public: 10 | map visited; 11 | map > adj; 12 | 13 | // function to add an edge to graph 14 | void addEdge(int v, int w); 15 | 16 | // DFS traversal of the vertices 17 | // reachable from v 18 | void DFS(int v); 19 | }; 20 | 21 | void Graph::addEdge(int v, int w) 22 | { 23 | adj[v].push_back(w); // Add w to v’s list. 24 | } 25 | 26 | void Graph::DFS(int v) 27 | { 28 | // Mark the current node as visited and 29 | // print it 30 | visited[v] = true; 31 | cout << v << " "; 32 | 33 | // Recur for all the vertices adjacent 34 | // to this vertex 35 | list::iterator i; 36 | for (i = adj[v].begin(); i != adj[v].end(); ++i) 37 | if (!visited[*i]) 38 | DFS(*i); 39 | } 40 | 41 | int main() 42 | { 43 | // Create a graph given in the above diagram 44 | Graph g; 45 | g.addEdge(0, 1); 46 | g.addEdge(0, 2); 47 | g.addEdge(1, 2); 48 | g.addEdge(2, 0); 49 | g.addEdge(2, 3); 50 | g.addEdge(3, 3); 51 | 52 | cout << "Following is Depth First Traversal" 53 | " (starting from vertex 2) \n"; 54 | 55 | // Function call 56 | g.DFS(2); 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Graphs/dijkstras.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | const int N = 1e5+7; 7 | const int INF = 1e9+7; 8 | 9 | vector> g[N];//for weighted graphs we have to insert the weight between 2 nodes 10 | // first element is node value and second element is the weight 11 | void dijkstra(int source){ 12 | vector dis(N, INF); 13 | vector vis(N, 0); 14 | 15 | set> st; // first element would be weight and second element would be node value 16 | st.insert({0, source}); 17 | dis[source] = 0; 18 | while(st.size()>0){ 19 | auto node = *st.begin(); // this is how we take the minimum value from a set 20 | int v = node.second; 21 | int distance = node.first; 22 | st.erase(st.begin()); 23 | if(vis[v]==1){ 24 | continue; 25 | } 26 | vis[v] = 1; 27 | for(auto child: g[v]){ 28 | int child_v = child.first; 29 | int child_wt = child.second; 30 | if(dis[v] + child_wt < dis[child_v]){ 31 | st.insert({dis[v]+child_wt, child_v}); 32 | dis[child_v] = dis[v] + child_wt; 33 | } 34 | } 35 | } 36 | 37 | } 38 | int main() 39 | { 40 | int n,m; 41 | for(int i=0; i>x>>y>>wt; 44 | g[x].push_back({y,wt}); // for an undirected graph we have to push for g[y] also 45 | 46 | } 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Graphs/floyedWarshal.cpp: -------------------------------------------------------------------------------- 1 | // this algorithm is used to find the shortest distance between all pair of vertices 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int INF = 1e9+1; 7 | const int N = 100; 8 | int dist[N][N]; 9 | 10 | void floyd_warshal(int n){ 11 | for(int k=1; k<=n; k++){ 12 | for(int i=1; i<=n; i++){ 13 | for(int j=1; j<=n; j++){ 14 | dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]); 15 | } 16 | } 17 | } 18 | for(int i=1; i<=n; i++){ 19 | for(int j=1; j<=n; j++){ 20 | if(dist[i][j] == INF){ 21 | cout<<"I"<<" "; 22 | } 23 | else{ 24 | cout<>n>>m; 46 | for(int i=0; i>x>>y>>wt; 49 | dist[x][y] = wt; 50 | } 51 | floyd_warshal(n); 52 | return 0; 53 | } 54 | 55 | // 6 9 56 | // 1 2 1 57 | // 1 3 5 58 | // 2 3 2 59 | // 3 5 2 60 | // 2 5 1 61 | // 2 4 2 62 | // 4 5 3 63 | // 4 6 1 64 | // 5 6 2 65 | -------------------------------------------------------------------------------- /Graphs/mst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define V 7 7 | 8 | int main () { 9 | // create a 2d array of size 7x7 10 | //for adjacency matrix to represent graph 11 | int G[V][V] = { 12 | {0,28,0,0,0,10,0}, 13 | {28,0,16,0,0,0,14}, 14 | {0,16,0,12,0,0,0}, 15 | {0,0,12,22,0,18}, 16 | {0,0,0,22,0,25,24}, 17 | {10,0,0,0,25,0,0}, 18 | {0,14,0,18,24,0,0} 19 | }; 20 | 21 | int edge; // number of edge 22 | 23 | // create an array to check visited vertex 24 | int visit[V]; 25 | 26 | //initialise the visit array to false 27 | for(int i=0;i G[i][j]) { 57 | min = G[i][j]; 58 | x = i; 59 | y = j; 60 | } 61 | 62 | } 63 | } 64 | } 65 | } 66 | cout << x << " ---> " << y << " : " << G[x][y]; 67 | cout << endl; 68 | visit[y] = true; 69 | edge++; 70 | } 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /LinkedList/ReverseLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Node 5 | { 6 | int data; 7 | struct Node *next; 8 | }*first=NULL,*second=NULL,*third=NULL; 9 | 10 | void Display(struct Node *p) 11 | { 12 | while(p!=NULL) 13 | { 14 | printf("%d ",p->data); 15 | p=p->next; 16 | } 17 | } 18 | 19 | void create(int A[],int n) 20 | { 21 | int i; 22 | struct Node *t,*last; 23 | first=(struct Node *)malloc(sizeof(struct Node)); 24 | first->data=A[0]; 25 | first->next=NULL; 26 | last=first; 27 | 28 | for(i=1;idata=A[i]; 32 | t->next=NULL; 33 | last->next=t; 34 | last=t; 35 | } 36 | } 37 | 38 | void Reverse1(struct Node *p) 39 | { 40 | int *A,i=0; 41 | struct Node *q=p; 42 | 43 | A=(int *)malloc(sizeof(int)*count(p)); 44 | 45 | while(q!=NULL) 46 | { 47 | A[i]=q->data; 48 | q=q->next; 49 | i++; 50 | } 51 | q=p; 52 | i--; 53 | while(q!=NULL) 54 | { 55 | q->data=A[i]; 56 | q=q->next; 57 | i--; 58 | } 59 | } 60 | 61 | void Reverse2(struct Node *p) 62 | { 63 | struct Node *q=NULL,*r=NULL; 64 | 65 | while(p!=NULL) 66 | { 67 | r=q; 68 | q=p; 69 | p=p->next; 70 | q->next=r; 71 | } 72 | first=q; 73 | } 74 | 75 | void Reverse3(struct Node *q,struct Node *p) 76 | { 77 | if(p) 78 | { 79 | Reverse3(p,p->next); 80 | p->next=q; 81 | } 82 | else 83 | first=q; 84 | } 85 | 86 | int main() 87 | { 88 | 89 | int A[]={10,20,40,50,60}; 90 | create(A,5); 91 | 92 | 93 | Reverse1(first); 94 | Display(first); 95 | 96 | return 0; 97 | } -------------------------------------------------------------------------------- /LinkedList/singlylinkedlist_143.c: -------------------------------------------------------------------------------- 1 | //name : deepak s. patil 2 | //roll no : 143 3 | //program for singly linked list. 4 | /*operations : 5 | 1. Insert at Start of SLL 6 | 3. Insert at location in SLL 7 | 4. Insert after or before data in SLL 8 | 5. Insert at End of SLL 9 | 6. Delete at Start of SLL 10 | 7. Delete at location in SLL 11 | 8. Delete the node with given data in SLL 12 | 9. Delete at End of SLL 13 | 10. Traverse the SLL 14 | 11. Search the SLL 15 | */ 16 | 17 | #include 18 | #include 19 | 20 | void insertstart(); 21 | void insertlocation(); 22 | void insertafter(); 23 | void insertend(); 24 | void deletestart(); 25 | void deletelocation(); 26 | void deletebydata(); 27 | void deleteend(); 28 | void traverse(); 29 | void search(); 30 | 31 | struct data{ 32 | int roll; 33 | int age; 34 | char name[20]; 35 | struct data *next; 36 | }; 37 | 38 | struct data *new; 39 | struct data *st; 40 | struct data *pr; 41 | struct data *temp; 42 | 43 | int main() 44 | { 45 | int ch; 46 | 47 | do 48 | { 49 | printf("which operation you want to perform on SLL :\n"); 50 | printf("1.INSERT AT START\n2.INSERT AFTER NODE\n3.INSERT BY LOCATION\n4.INSERT AT END\n5.DELETE AT START\n6.DELETE AT LOCATION\n7.DELETE THE NODE\n8.DELETE AT END\n9.TRAVERSE SLL\n10.SEARCH SLL\n11.EXIT\n"); 51 | scanf("%d",&ch); 52 | 53 | switch(ch) 54 | { 55 | case 1: 56 | insertstart(); 57 | break; 58 | case 2: 59 | insertafter(); 60 | break; 61 | case 3: 62 | insertlocation(); 63 | break; 64 | case 4: 65 | insertend(); 66 | break; 67 | case 5: 68 | deletestart(); 69 | break; 70 | case 6: 71 | deletelocation(); 72 | break; 73 | case 7: 74 | deletebydata(); 75 | break; 76 | case 8: 77 | deleteend(); 78 | break; 79 | case 9: 80 | traverse(); 81 | break; 82 | case 10: 83 | search(); 84 | break; 85 | case 11: 86 | return 0; 87 | default: 88 | printf("enter a valid option\n"); 89 | } 90 | 91 | }while(1); 92 | 93 | return 0; 94 | } 95 | 96 | void insertstart() 97 | { 98 | new=(struct data*) malloc(sizeof(struct data)); 99 | 100 | if(new==NULL) 101 | { 102 | printf("MEMORY FULL\n"); 103 | return; 104 | } 105 | 106 | printf("enter the data :\n"); 107 | printf("enter your roll no :\n"); 108 | scanf("%d",&new->roll); 109 | printf("enter your age :\n"); 110 | scanf("%d",&new->age); 111 | printf("enter your name :\n"); 112 | scanf("%s",new->name); 113 | 114 | if(st==NULL) 115 | { 116 | st=new; 117 | new->next=NULL; 118 | } 119 | else 120 | { 121 | new->next=st; 122 | st=new; 123 | } 124 | printf("DATA INSERTED SUCCESSFULLY AT START\n"); 125 | return; 126 | } 127 | 128 | void insertafter() 129 | { 130 | new=(struct data*)malloc(sizeof(struct data)); 131 | 132 | if(new==NULL) 133 | { 134 | printf("MEMORY FULL\n"); 135 | return; 136 | } 137 | 138 | if(st==NULL) 139 | { 140 | printf("NO LINKED LIST PRESENT\n"); 141 | return; 142 | } 143 | 144 | printf("enter the data :\n"); 145 | printf("enter your roll no :\n"); 146 | scanf("%d",&new->roll); 147 | printf("enter your age :\n"); 148 | scanf("%d",&new->age); 149 | printf("enter your name :\n"); 150 | scanf("%s",new->name); 151 | 152 | int element; 153 | 154 | printf("enter roll no in node after which you want to insert data :\n"); 155 | scanf("%d",&element); 156 | 157 | temp=st; 158 | while(temp->roll!=element && temp->next!=NULL) 159 | { 160 | temp=temp->next; 161 | } 162 | 163 | if(temp->roll==element) 164 | { 165 | new->next=temp->next; 166 | temp->next=new; 167 | printf("DATA INSERTED SUCCESSFULLY AFTER NODE WITH ROLL NO %d.\n",element); 168 | } 169 | else 170 | { 171 | printf("ROLL NUMBER NOT FOUND IN LINKED LIST\n"); 172 | } 173 | 174 | return; 175 | } 176 | 177 | void insertlocation() 178 | { 179 | int ct=1,loc; 180 | temp=st; 181 | 182 | new=(struct data*)malloc(sizeof(struct data)); 183 | 184 | if(new==NULL) 185 | { 186 | printf("MEMORY FULL\n"); 187 | return; 188 | } 189 | 190 | printf("enter location at which you want to insert data :\n"); 191 | scanf("%d",&loc); 192 | 193 | if(loc==1) 194 | { 195 | insertstart(); 196 | return; 197 | } 198 | 199 | while(ctnext!=NULL) 200 | { 201 | pr=temp; 202 | temp=temp->next; 203 | ct++; 204 | } 205 | 206 | if(ct==loc) 207 | { 208 | printf("enter the data :\n"); 209 | printf("enter your roll no :\n"); 210 | scanf("%d",&new->roll); 211 | printf("enter your age :\n"); 212 | scanf("%d",&new->age); 213 | printf("enter your name :\n"); 214 | scanf("%s",new->name); 215 | new->next=pr->next; 216 | pr->next=new; 217 | printf("DATA INSERTED SUCCESSFULLY AT LOCATION %d \n",loc); 218 | } 219 | else 220 | { 221 | printf("LOCATION NOT FOUND IN SLL\n"); 222 | } 223 | } 224 | 225 | void insertend() 226 | { 227 | new=(struct data*) malloc(sizeof(struct data)); 228 | 229 | if(new==NULL) 230 | { 231 | printf("MEMORY FULL\n"); 232 | return; 233 | } 234 | 235 | printf("enter the data :\n"); 236 | printf("enter your roll no :\n"); 237 | scanf("%d",&new->roll); 238 | printf("enter your age :\n"); 239 | scanf("%d",&new->age); 240 | printf("enter your name :\n"); 241 | scanf("%s",new->name); 242 | 243 | if(st==NULL) 244 | { 245 | st=new; 246 | new->next=NULL; 247 | } 248 | else 249 | { 250 | temp=st; 251 | 252 | while(temp->next!=NULL) 253 | { 254 | temp=temp->next; 255 | } 256 | temp->next=new; 257 | new->next=NULL; 258 | } 259 | 260 | printf("DATA LINKED SUCCESSFULLY AT END\n"); 261 | return; 262 | } 263 | 264 | void deletestart() 265 | { 266 | if(st==NULL) 267 | { 268 | printf("NO LINKED LIST PRESENT\n"); 269 | return; 270 | } 271 | else 272 | { 273 | if(st->next==NULL) 274 | { 275 | temp=st; 276 | printf("DATA FROM DELETED NODE IS : %d,%d,%s\n",temp->roll,temp->age,temp->name); 277 | free(temp); 278 | st=NULL; 279 | printf("DATA FROM 1st NODE DELETED SUCCESSFULLY\n"); 280 | } 281 | else 282 | { 283 | temp=st; 284 | st=temp->next; 285 | printf("DATA FROM 1st NODE DELETED SUCCESSFULLY\n"); 286 | printf("DATA FROM DELETED NODE IS : %d,%d,%s\n",temp->roll,temp->age,temp->name); 287 | free(temp); 288 | } 289 | } 290 | return; 291 | } 292 | 293 | void deletelocation() 294 | { 295 | if(st==NULL) 296 | { 297 | printf("NO LINKED LIST PRESENT\n"); 298 | return; 299 | } 300 | 301 | int loc,ct=1; 302 | temp=st; 303 | 304 | printf("enter location of node you want to delete :\n"); 305 | scanf("%d",&loc); 306 | 307 | if(loc==1) 308 | { 309 | deletestart(); 310 | return; 311 | } 312 | 313 | while(ctnext!=NULL) 314 | { 315 | pr=temp; 316 | temp=temp->next; 317 | ct++; 318 | } 319 | 320 | if(ct==loc) 321 | { 322 | pr->next=temp->next; 323 | printf("DATA FROM %d NODE DELETED SUCCESSFULLY\n",loc); 324 | printf("DATA FROM DELETED NODE IS : %d,%d,%s\n",temp->roll,temp->age,temp->name); 325 | free(temp); 326 | } 327 | else 328 | { 329 | printf("LOCATION NOT FOUND IN SLL\n"); 330 | } 331 | 332 | return; 333 | } 334 | 335 | void deletebydata() 336 | { 337 | if(st==NULL) 338 | { 339 | printf("NO LINKED LIST PRESENT\n"); 340 | return; 341 | } 342 | 343 | int element; 344 | 345 | printf("enter roll no of node you want to delete :\n"); 346 | scanf("%d",&element); 347 | 348 | temp=st; 349 | 350 | if(temp->next==NULL) 351 | { 352 | deletestart(); 353 | return; 354 | } 355 | 356 | while(temp->roll!=element && temp->next!=NULL) 357 | { 358 | pr=temp; 359 | temp=temp->next; 360 | } 361 | 362 | if(temp->roll==element) 363 | { 364 | pr->next=temp->next; 365 | printf("DATA OF NODE WITH ROLL NO %d DELETED SUCCESSFULLY\n",element); 366 | printf("DATA FROM DELETED NODE IS : %d,%d,%s\n",temp->roll,temp->age,temp->name); 367 | free(temp); 368 | } 369 | else 370 | { 371 | printf("ROLL NUMBER NOT FOUND IN LINKED LIST\n"); 372 | } 373 | 374 | return; 375 | } 376 | 377 | void deleteend() 378 | { 379 | if(st==NULL) 380 | { 381 | printf("NO LINKED LIST PRESENT\n"); 382 | return; 383 | } 384 | else 385 | { 386 | if(st->next==NULL) 387 | { 388 | temp=st; 389 | printf("DATA FROM LAST NODE DELETED SUCCESSFULLY\n"); 390 | printf("DATA FROM DELETED NODE IS : %d,%d,%s\n",temp->roll,temp->age,temp->name); 391 | free(temp); 392 | st=NULL; 393 | } 394 | else 395 | { 396 | temp=st; 397 | while(temp->next!=NULL) 398 | { 399 | pr=temp; 400 | temp=temp->next; 401 | } 402 | pr->next=NULL; 403 | printf("DATA FROM DELETED NODE IS : %d,%d,%s\n",temp->roll,temp->age,temp->name); 404 | free(temp); 405 | printf("DATA FROM LAST NODE DELETED SUCCESSFULLY\n"); 406 | } 407 | } 408 | return; 409 | } 410 | 411 | void traverse() 412 | { 413 | int i=1; 414 | 415 | struct data *t; 416 | t=st; 417 | if(st==NULL) 418 | { 419 | printf("NO LINKED LIST PRESENT\n"); 420 | return; 421 | } 422 | else 423 | { 424 | do{ 425 | printf("data from %d node is :\n",i); 426 | printf("roll no : %d\nage :%d\nname : %s\n",t->roll,t->age,t->name); 427 | t=t->next; 428 | i++; 429 | }while(t!=NULL); 430 | } 431 | return; 432 | } 433 | 434 | void search() 435 | { 436 | if(st==NULL) 437 | { 438 | printf("NO LINKED LIST PRESENT \n"); 439 | return; 440 | } 441 | int r_no; 442 | 443 | printf("enter the roll no to find the data :\n"); 444 | scanf("%d",&r_no); 445 | 446 | temp=st; 447 | 448 | while(temp->roll!=r_no && temp->next!=NULL) 449 | { 450 | temp=temp->next; 451 | } 452 | 453 | if(temp->roll==r_no) 454 | { 455 | printf("data for roll no %d is :\n",r_no); 456 | printf("roll no : %d\nage :%d\nname : %s\n",temp->roll,temp->age,temp->name); 457 | } 458 | else 459 | { 460 | printf("ROLL NO NOT FOUND IN SLL"); 461 | } 462 | } 463 | -------------------------------------------------------------------------------- /Longest Consecutive Sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int h_ans(vector& nums) { 4 | mapmp; 5 | for(int i=0;i>n; 28 | vectorarr; 29 | for(int i=0;i>arr[i]; 31 | } 32 | int ans=h_ans(arr); 33 | cout< 2 | //circular queue 3 | static int REAR=-1; 4 | static int FRONT=-1; 5 | 6 | void enque(int queue[],int size) 7 | { 8 | if((REAR==size-1 && FRONT==0) || REAR==FRONT-1) 9 | { 10 | printf("Queue Overflow\n"); 11 | return; 12 | } 13 | else 14 | { 15 | if(FRONT==-1) 16 | { 17 | FRONT++; 18 | REAR++; 19 | } 20 | else if(REAR==size-1) 21 | { 22 | REAR=0; 23 | } 24 | else 25 | { 26 | REAR++; 27 | } 28 | int item; 29 | printf("Enter Item insert: \n"); 30 | scanf("%d",&item); 31 | queue[REAR]=item; 32 | printf("%d inserted successfully\n",item); 33 | } 34 | return; 35 | } 36 | 37 | void deque(int queue[],int size) 38 | { 39 | if(FRONT==-1) 40 | { 41 | printf("Queue Underflow\n"); 42 | return; 43 | } 44 | else 45 | { 46 | printf("%d deleted successfully\n",queue[FRONT]); 47 | if(FRONT==REAR) 48 | { 49 | FRONT=-1; 50 | REAR=-1; 51 | } 52 | else if(FRONT==size-1) 53 | { 54 | FRONT=0; 55 | } 56 | else 57 | { 58 | FRONT++; 59 | } 60 | } 61 | return; 62 | } 63 | 64 | void display(int queue[],int size) 65 | { 66 | printf("%d %d\n",FRONT,REAR); 67 | if(FRONT==-1) 68 | { 69 | printf("Queue Empty\n"); 70 | } 71 | else 72 | { 73 | if(FRONT>REAR) 74 | { 75 | printf("Queue:\n"); 76 | for(int i=FRONT;i 2 | //queue 3 | static int front=-1; 4 | static int rear=-1; 5 | 6 | void enque(int queue[],int size) 7 | { 8 | if(rear==size-1) 9 | { 10 | printf("Queue Overflow\n"); 11 | return; 12 | } 13 | else if(front==-1) 14 | { 15 | int item; 16 | printf("Enter item\n"); 17 | scanf("%d",&item); 18 | 19 | front++; 20 | rear++; 21 | queue[rear]=item; 22 | printf("%d inserted successfully\n",item); 23 | } 24 | else 25 | { 26 | int item; 27 | printf("Enter item\n"); 28 | scanf("%d",&item); 29 | 30 | rear++; 31 | queue[rear]=item; 32 | printf("%d inserted successfully\n",item); 33 | } 34 | return; 35 | } 36 | 37 | void deque(int queue[]) 38 | { 39 | if(front==-1 || front>rear) 40 | { 41 | printf("Queue Underflow\n"); 42 | return; 43 | } 44 | else 45 | { 46 | int item=queue[front]; 47 | front++; 48 | printf("%d deque successful\n",item); 49 | } 50 | return; 51 | } 52 | 53 | void display(int queue[]) 54 | { 55 | if(front==-1) 56 | { 57 | printf("Queue Empty\n"); 58 | return; 59 | } 60 | for(int i=front;i<=rear;i++) 61 | { 62 | printf("%d\t",queue[i]); 63 | } 64 | printf("\n"); 65 | } 66 | 67 | int main() 68 | { 69 | int size; 70 | 71 | printf("Enter size of queue\n"); 72 | scanf("%d",&size); 73 | 74 | int queue[size]; 75 | 76 | int ch; 77 | do{ 78 | printf("Enter Your choice\n"); 79 | printf("1.Enque\n2.Deque\n3.Display\n4.Exit\n"); 80 | scanf("%d",&ch); 81 | 82 | switch(ch) 83 | { 84 | case 1: 85 | enque(queue,size); 86 | break; 87 | case 2: 88 | deque(queue); 89 | break; 90 | case 3: 91 | display(queue); 92 | break; 93 | case 4: 94 | break; 95 | default: 96 | printf("Enter a valid choice\n"); 97 | } 98 | }while(ch!=4); 99 | return 0; 100 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CPP 2 | 3 | 4 |

HacktoberFest Accepted 2022

5 | 6 | 7 | 8 | 9 |
10 | 11 |
12 | 13 |
14 | 15 | 16 | 17 | # Upload Data structure programs or Algorithms in CPP 18 | 19 | Use this project to make your first contribution to an open source project on GitHub. Practice making your first pull request to a public repository before doing the real thing! 20 | 21 | # What is Hacktoberfest? 22 | 23 | Hacktoberfest is a program by Digital Ocean, DEV and Github, where you can easily win a T-Shirt just by making 4 pull requests in the month of October to any open source projects on Github. 24 | 25 | ## Steps to follow: 26 | 27 | ### 1. Register for Hacktoberfest 28 | 29 | ##### https://hacktoberfest.com/ 30 | 31 | ### 2. Add Programs in CPP Language : 32 | 33 | Add any DSA algorithm with appropriate comments and runnable code 34 | 35 | ### 3. Create Pull Request: 36 | 37 | Once you have completed these steps, you are ready to start contributing by clicking on Create Pull Request. 38 | 39 | ### 4. Give this Project a Star: 40 | 41 | If you liked working on this project, please share this project as much as you can and star this project to help as many people in opensource as you can. 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Recursion/1 printNNames.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void f(int a,int n) 5 | { 6 | if(a>n) 7 | return; 8 | cout<>n; 21 | f(1,n); 22 | /* code */ 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Recursion/10 factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int f(int n) 5 | { 6 | if(n<=0) 7 | { 8 | 9 | return 0; 10 | } 11 | 12 | return n+f(n-1); //functional means without printing the value just return it to calling function 13 | 14 | 15 | 16 | } 17 | int main() 18 | { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt","r",stdin); 21 | freopen("output.txt","w",stdout); 22 | #endif 23 | 24 | int n; 25 | cin>>n; 26 | cout< 2 | using namespace std; 3 | 4 | int fact(int n,int res) 5 | { 6 | if(n<=0) 7 | { 8 | cout<>n; 26 | fact(n,1); 27 | 28 | /* code */ 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Recursion/12[1] reverseArray(for loop).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void reverse() 5 | { 6 | int n=7; 7 | int arr[n]={1,2,3,9,5,6,4}; 8 | 9 | 10 | for(int i=0;i>n; 38 | reverse(); 39 | 40 | /* code */ 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Recursion/12[2] reverseArray(recursion).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void reverseFun(int l,int n, int arr[]) 5 | { 6 | if(l >= n/2) 7 | { 8 | 9 | return; 10 | } 11 | 12 | // int temp; 13 | // temp=arr[l]; 14 | // arr[l]=arr[n-l-1]; 15 | // arr[n-l-1]=temp; 16 | 17 | swap(arr[l],arr[n-l-1]); 18 | reverseFun(l+1,n,arr); 19 | 20 | 21 | 22 | } 23 | int main() 24 | { 25 | #ifndef ONLINE_JUDGE 26 | freopen("input.txt","r",stdin); 27 | freopen("output.txt","w",stdout); 28 | #endif 29 | 30 | int n; 31 | cin>>n; 32 | int arr[5]={1,2,3,4,5}; 33 | reverseFun(0,5,arr); //array passed using pass by referance only 34 | for(int i=0;i<5;++i) cout< 2 | using namespace std; 3 | 4 | bool isPal(int n, string str) 5 | { 6 | 7 | for(int i=0;i>n; 30 | string str="madamo"; 31 | cout< 2 | using namespace std; 3 | 4 | bool isPal(int l,int n, string str) 5 | { 6 | 7 | if(l>=n/2) 8 | { 9 | return 1; 10 | } 11 | if(str[l]==str[n-l-1]) 12 | return isPal(l+1,n,str); 13 | else 14 | return 0; 15 | 16 | } 17 | int main() 18 | { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt","r",stdin); 21 | freopen("output.txt","w",stdout); 22 | #endif 23 | 24 | int n; 25 | cin>>n; 26 | string str="madamo"; 27 | cout< 2 | using namespace std; 3 | 4 | void fibo(int cur, int pre,int n) 5 | { 6 | cout<>n; 28 | string str="madamo"; 29 | fibo(1,0,n); 30 | 31 | /* code */ 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Recursion/14[2]fibonacci(recursion).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fibo(int n) 5 | { 6 | if(n<=1) 7 | return n; 8 | return fibo(n-1)+fibo(n-2); 9 | } 10 | int main() 11 | { 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt","r",stdin); 14 | freopen("output.txt","w",stdout); 15 | #endif 16 | 17 | int n; 18 | cin>>n; 19 | string str="madamo"; 20 | cout< 2 | using namespace std; 3 | //AKASH DINKAR PATIL , ROLL 136 TY.B.TECH CSE DIV-B 26/6/22 4 | //a subsequence is nothing but printing all the possible values in same direction 5 | //eg. [3,1,2]--> {},{3},{1},{2},{3,1},{1,2},{3,2},{3,1,2} ==> no.of subsequences=pow(2,length of array (3))=8 6 | //here {1,3},{2,1},{2,3}....... are not possible because they are not in order of the array. 7 | //**##technique is to take and not take the element 8 | void subsequence(int index, vector&temp ,vector&arr) 9 | { 10 | if(index >= arr.size()) 11 | { 12 | for(auto it:temp) 13 | { 14 | cout<>n; 44 | vectorarr(n),temp; 45 | for(int i=0;i>arr[i]; 46 | subsequence(0,temp,arr); 47 | 48 | /* code */ 49 | return 0; 50 | } 51 | 52 | //recursion tree 53 | 54 | /* arr=[3,1,2] 55 | 56 | sub(0,[]) 57 | +/ (take) -\ (not take) 58 | / \ 59 | / \ 60 | sub(1,[3]) sub(1,[]) 61 | / \ 62 | / \ 63 | / \ 64 | / \ 65 | sub(2,[3,1]) sub(2,[3]) 66 | 67 | 68 | Time Complexity = O(2^n * n) 69 | Aux space =O(n) 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | */ -------------------------------------------------------------------------------- /Recursion/16[1] printingSubsequenceWhooseSumIsK.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | //AKASH DINKAR PATIL , ROLL 136 TY.B.TECH CSE DIV-B 26/6/22 4 | //a subsequence is nothing but printing all the possible values in same direction 5 | //eg. [3,1,2]--> {},{3},{1},{2},{3,1},{1,2},{3,2},{3,1,2} ==> no.of subsequences=pow(2,length of array (3))=8 6 | //here {1,3},{2,1},{2,3}....... are not possible because they are not in order of the array. 7 | //**##technique is to take and not take the element 8 | void subsequence(int index, vector&temp , int sum,vector&arr,int k) 9 | { 10 | if(index >= arr.size()) 11 | { 12 | if(sum==k) 13 | { 14 | for(auto it:temp) 15 | { 16 | cout<>n>>k; 50 | vectorarr(n),temp; 51 | for(int i=0;i>arr[i]; 52 | subsequence(0,temp,0,arr,k); 53 | 54 | /* code */ 55 | return 0; 56 | } 57 | 58 | //recursion tree 59 | 60 | /* arr=[3,1,2] 61 | 62 | sub(0,[]) 63 | +/ (take) -\ (not take) 64 | / \ 65 | / \ 66 | sub(1,[3]) sub(1,[]) 67 | / \ 68 | / \ 69 | / \ 70 | / \ 71 | sub(2,[3,1]) sub(2,[3]) 72 | 73 | 74 | Time Complexity = O(2^n * n) 75 | Aux space =O(n) 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | */ -------------------------------------------------------------------------------- /Recursion/16[2] printingOnlyOneSubsequenceWhooseSumIsK.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | //AKASH DINKAR PATIL , ROLL 136 TY.B.TECH CSE DIV-B 28/6/22 4 | //technique --> as soon the condition is satisfied return the true else return false 5 | //and calling of recursive function is handled if it returns true --> return true such that no further calls will happen 6 | //else return false for next call 7 | bool subsequence(int index, vector&temp , int sum,vector&arr,int k) 8 | { 9 | if(index >= arr.size()) 10 | { 11 | if(sum==k) 12 | { 13 | for(auto it:temp) 14 | { 15 | cout<>n>>k; 55 | vectorarr(n),temp; 56 | for(int i=0;i>arr[i]; 57 | subsequence(0,temp,0,arr,k); 58 | 59 | /* code */ 60 | return 0; 61 | } 62 | 63 | //recursion tree 64 | 65 | /* arr=[3,1,2] 66 | 67 | sub(0,[]) 68 | +/ (take) -\ (not take) 69 | / \ 70 | / \ 71 | sub(1,[3]) sub(1,[]) 72 | / \ 73 | / \ 74 | / \ 75 | / \ 76 | sub(2,[3,1]) sub(2,[3]) 77 | 78 | 79 | Time Complexity = O(2^n * n) 80 | Aux space =O(n) 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | */ -------------------------------------------------------------------------------- /Recursion/16[3] printingCountOfSubsequenceWhooseSumIsK.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | //AKASH DINKAR PATIL , ROLL 136 TY.B.TECH CSE DIV-B 28/6/22 4 | 5 | //technique --> return 1 if condition satisfy else 0 6 | //store the returned result in to var and at the end return the result of both the calls 7 | //here we not required additional temp datastructure because we are not printing we just counting 8 | int subsequence(int index, int sum,vector&arr,int k) 9 | { 10 | if(index >= arr.size()) 11 | { 12 | if(sum==k) 13 | { 14 | 15 | return 1; //if condi satisfys return 1 to add the sum 16 | } 17 | 18 | return 0; //else return 0 19 | } 20 | //subsequence(index+1,temp,arr); //if you result in reverse order (dont remove pop_back() operation) 21 | 22 | //--------------------main logic take-not take the element----------------------------- 23 | 24 | 25 | sum+=arr[index]; //take condition (element included) 26 | 27 | int l=subsequence(index+1,sum,arr,k);// if it returns true return true to calling function 28 | 29 | 30 | sum-=arr[index]; //not take condition (element excluded) means element will deleted which is previously added 31 | int r=subsequence(index+1,sum,arr,k); // if it returns true return true to calling function 32 | 33 | 34 | return l+r; //return sum of not take tree(left) + sum of take tree(right) 35 | 36 | } 37 | int main() 38 | { 39 | #ifndef ONLINE_JUDGE 40 | freopen("input.txt","r",stdin); 41 | freopen("output.txt","w",stdout); 42 | #endif 43 | 44 | int n,k; 45 | cin>>n>>k; 46 | vectorarr(n); 47 | for(int i=0;i>arr[i]; 48 | cout< 2 | using namespace std; 3 | //AKASH DINKAR PATIL , ROLL 136 TY.B.TECH CSE DIV-B 28/6/22 4 | //Q--> print all the unique combination of target int .combination may in any order 5 | //eg. arr=[2,3,6,7], target=7 6 | //o/p--> [[2,2,3],[7]] 7 | //explanation--> 2+2+3=7 (here 2 is taken twice) and 7=7; 8 | 9 | //technique --> just dont increment the index of take element because it may be repeted 10 | 11 | void combinationSumI(int index,vector&ds,int target,vector&arr) 12 | { 13 | if(index>=arr.size()) 14 | { 15 | if(target==0) 16 | { 17 | for(auto it:ds){ 18 | cout<=0) 31 | { 32 | ds.push_back(arr[index]); 33 | 34 | combinationSumI(index,ds,target-arr[index],arr); 35 | ds.pop_back(); //push_back and pop_back are associated with "take" only because we take element 36 | //after call we have to relese or remove that element for "non-take" 37 | 38 | } 39 | 40 | 41 | //not take 42 | 43 | combinationSumI(index+1,ds,target,arr); 44 | } 45 | 46 | int main() 47 | { 48 | #ifndef ONLINE_JUDGE 49 | freopen("input.txt","r",stdin); 50 | freopen("output.txt","w",stdout); 51 | #endif 52 | 53 | int n,target; 54 | cin>>n>>target; 55 | vectorarr(n),ds; 56 | 57 | for(int i=0;i>arr[i]; 58 | combinationSumI(0,ds,target,arr); 59 | 60 | 61 | /* code */ 62 | return 0; 63 | } 64 | 65 | -------------------------------------------------------------------------------- /Recursion/18 combinationSum-II Leetcode40.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | //AKASH DINKAR PATIL , ROLL 136 TY.B.TECH CSE DIV-B 28/6/22 4 | //Q--> print all the unique combination of target int .combination may in any order 5 | //eg. arr=[1,1,1,2,2], target=4 6 | //o/p--> [[1,1,2],[2,2]] 7 | 8 | 9 | //technique --> just dont increment the index of take element because it may be repeted 10 | 11 | void combinationSumII(int index,vector&ds,int target,vector&arr,vector>&ans) 12 | { 13 | if(target==0) 14 | { 15 | ans.push_back(ds); 16 | return; 17 | } 18 | 19 | for(int i=index;iindex means it is the first element after that the duplicates are not allowed 24 | //means first element will always be picked up 25 | //arr[i]==arr[i-1] shows it should not pick same element later 26 | if(i>index && arr[i]==arr[i-1]) continue; //imp condition 27 | 28 | if(arr[i]>target) break; //means the target is going to negative for that break the current executing loop 29 | 30 | ds.push_back(arr[i]); //take 31 | combinationSumII(i+1,ds,target-arr[i],arr,ans); 32 | ds.pop_back(); 33 | 34 | } 35 | } 36 | 37 | int main() 38 | { 39 | #ifndef ONLINE_JUDGE 40 | freopen("input.txt","r",stdin); 41 | freopen("output.txt","w",stdout); 42 | #endif 43 | 44 | int n,target; 45 | cin>>n>>target; 46 | vectorarr(n),ds; 47 | vector>ans; 48 | for(int i=0;i>arr[i]; 49 | 50 | sort(arr.begin(),arr.end()); 51 | combinationSumII(0,ds,target,arr,ans); 52 | 53 | for(auto i:ans) 54 | { 55 | for(auto j:i) 56 | { 57 | cout< 2 | using namespace std; 3 | //AKASH DINKAR PATIL , ROLL 136 TY.B.TECH CSE DIV-B 28/6/22 4 | //Q--> print the sum of all the subsets of the given array in the assending order 5 | //eg. [3,1,2]--{{},{3},{1},{2},{3,1},{3,2},{1,2},{3,1,2}}==2^(no. of ele)=8 6 | //therefore sum of each sub set is given by-->[0,3,1,2,4,5,3,6]; 7 | //in ascending order the result will be --->[0,1,2,3,3,4,5,6] 8 | 9 | //technique --> just dont increment the index of take element because it may be repeted 10 | 11 | void subsetSumI(int index,int sum,vector&arr,vector&ans) 12 | { 13 | if(index>=arr.size()) 14 | { 15 | ans.push_back(sum); 16 | return; 17 | } 18 | 19 | //take 20 | //sum=sum+arr[index]; 21 | subsetSumI(index+1,sum+arr[index],arr,ans); 22 | //sum=sum-arr[index]; 23 | 24 | //not take 25 | subsetSumI(index+1,sum,arr,ans); 26 | } 27 | 28 | int main() 29 | { 30 | #ifndef ONLINE_JUDGE 31 | freopen("input.txt","r",stdin); 32 | freopen("output.txt","w",stdout); 33 | #endif 34 | 35 | int n; 36 | cin>>n; 37 | vectorarr(n); 38 | vectorans; 39 | for(int i=0;i>arr[i]; 40 | 41 | 42 | subsetSumI(0,0,arr,ans); 43 | 44 | sort(ans.begin(),ans.end()); 45 | 46 | for(auto i :ans) 47 | { 48 | 49 | cout< 2 | using namespace std; 3 | 4 | void f(int n) 5 | { 6 | if(n<=0) 7 | return; 8 | cout<>n; 21 | f(n); 22 | /* code */ 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Recursion/20 SubsetSumII Leetcode90.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define endl "\n" 3 | using namespace std; 4 | 5 | //AKASH DINKAR PATIL , ROLL 136 TY.B.TECH CSE DIV-B 28/6/22 6 | //Q--> print all the subsets from given list. list may contains duplicates ,subset should not contains 7 | //dupliocates. 8 | //eg->[1,2,2,2,3,3] 9 | //o/p--> [[],[1],[2],[2],[1,2],[1,3],[1,2],[1,3,3],[2,3,3],[2,2,3,3],.....] 10 | 11 | void subsetSumII(int index,vector&ds,vector&arr,vector>&ans) 12 | { 13 | ans.push_back(ds); 14 | 15 | 16 | for(int i=index;i>n; 42 | vectorarr(n); 43 | vectords; 44 | vector>ans; 45 | for(int i=0;i>arr[i]; 46 | 47 | 48 | subsetSumII(0,ds,arr,ans); 49 | 50 | 51 | for(auto i :ans) 52 | { 53 | 54 | for(auto j:i) 55 | { 56 | cout< 2 | #define endl "\n" 3 | using namespace std; 4 | 5 | //AKASH DINKAR PATIL , ROLL 136 TY.B.TECH CSE DIV-B 28/6/22 6 | //Q--> Print all the permutitions of string or array 7 | //eg. [1,2,3]-->[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]--> total 6 permutations 8 | // total permutations --> n!=3!=3*2*1=6 9 | 10 | 11 | void permutApp1(vector&ds,int flag[],vector&arr,vector>&ans) 12 | { 13 | if(ds.size()==arr.size()) 14 | { 15 | ans.push_back(ds); 16 | return; 17 | } 18 | 19 | for(int i=0;i>n; 41 | vectorarr(n); 42 | vectords; 43 | vector>ans; 44 | 45 | int flag[arr.size()]; 46 | for(int i=-;i>arr[i]; 49 | 50 | 51 | permutApp1(ds,flag,arr,ans); 52 | 53 | 54 | for(auto i :ans) 55 | { 56 | 57 | for(auto j:i) 58 | { 59 | cout< 2 | #define endl "\n" 3 | using namespace std; 4 | 5 | //AKASH DINKAR PATIL , ROLL 136 TY.B.TECH CSE DIV-B 28/6/22 6 | //Q--> Print all the permutitions of string or array 7 | //eg. [1,2,3]-->[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]--> total 6 permutations 8 | // total permutations --> n!=3!=3*2*1=6 9 | 10 | 11 | void permutApp1(vector&ds,int flag[],vector&arr,vector>&ans) 12 | { 13 | if(ds.size()==arr.size()) 14 | { 15 | ans.push_back(ds); 16 | return; 17 | } 18 | 19 | for(int i=0;i>n; 41 | vectorarr(n); 42 | vectords; 43 | vector>ans; 44 | 45 | int flag[arr.size()]; 46 | for(int i=0;i>arr[i]; 49 | 50 | 51 | permutApp1(ds,flag,arr,ans); 52 | 53 | 54 | for(auto i :ans) 55 | { 56 | 57 | for(auto j:i) 58 | { 59 | cout< 2 | #define endl "\n" 3 | using namespace std; 4 | 5 | //AKASH DINKAR PATIL , ROLL 136 TY.B.TECH CSE DIV-B 28/6/22 6 | //Q--> Print all the permutitions of string or array 7 | //eg. [1,2,3]-->[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]--> total 6 permutations 8 | // total permutations --> n!=3!=3*2*1=6 9 | 10 | 11 | void permutApp2(int index,vector&ds,vector&arr,vector>&ans) 12 | { 13 | if(index==arr.size()) 14 | { 15 | ans.push_back(arr); 16 | return; 17 | } 18 | 19 | for(int i=index;i>n; 36 | vectorarr(n); 37 | vectords; 38 | vector>ans; 39 | 40 | 41 | 42 | for(int i=0;i>arr[i]; 43 | 44 | 45 | permutApp2(0,ds,arr,ans); 46 | 47 | 48 | for(auto i :ans) 49 | { 50 | 51 | for(auto j:i) 52 | { 53 | cout< 2 | #define endl "\n" 3 | using namespace std; 4 | 5 | //AKASH DINKAR PATIL , ROLL 136 TY.B.TECH CSE DIV-B 28/6/22 6 | //Q--> Input: n = 4 7 | //Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]] 8 | //Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above 9 | bool ifSafe(int row,int col , vector &board,int n) 10 | { 11 | /* \ 12 | <--Q just check these directions whether there is any queen or not if there return false else true 13 | / 14 | */ 15 | int dupCol=col; 16 | int dupRow=row; 17 | 18 | //going back upper diagonal 19 | while(row >=0 && col >=0) 20 | { 21 | if(board[row][col]=='Q') 22 | return false; 23 | 24 | row--; 25 | col--; 26 | } 27 | 28 | //going back for center line 29 | col=dupCol; 30 | row=dupRow; 31 | while(col>=0) 32 | { 33 | if(board[row][col]=='Q') 34 | return false; 35 | 36 | col--; 37 | } 38 | 39 | //going back for lower diagonal 40 | col=dupCol; 41 | row=dupRow; 42 | while(col>=0 && row &board, vector>&ans ,int n ) 54 | { 55 | if(col==n) 56 | { 57 | ans.push_back(board); 58 | return; 59 | } 60 | 61 | for(int row=0;row> solveNQueens(int n) 74 | { 75 | vector>ans; 76 | vectorboard(n); 77 | string s(n,'.'); 78 | cout<>n; 95 | 96 | vector>akash; 97 | 98 | akash=solveNQueens(n); 99 | 100 | for(auto x :akash) 101 | { 102 | for(auto y:x) 103 | { 104 | cout< 2 | #define endl "\n" 3 | using namespace std; 4 | 5 | //AKASH DINKAR PATIL , ROLL 136 TY.B.TECH CSE DIV-B 28/6/22 6 | //Q--> Input: n = 4 7 | //Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]] 8 | //Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above 9 | 10 | 11 | void solve(int col,vector &board, vector&leftRow,vector&lowerDiagonal,vector&upperDiagonal, vector>&ans ,int n ) 12 | { 13 | if(col==n) 14 | { 15 | ans.push_back(board); 16 | return; 17 | } 18 | 19 | for(int row=0;row> solveNQueens(int n) 37 | { 38 | vector>ans; 39 | vectorboard(n); 40 | vector leftRow(n,0),lowerDiagonal(2*n-1,0),upperDiagonal(2*n-1,0); 41 | string s(n,'.'); 42 | //cout<>n; 59 | 60 | vector>akash; 61 | 62 | akash=solveNQueens(n); 63 | 64 | for(auto x :akash) 65 | { 66 | for(auto y:x) 67 | { 68 | cout< 2 | #define endl "\n" 3 | using namespace std; 4 | 5 | //AKASH DINKAR PATIL , ROLL 136 TY.B.TECH CSE DIV-B 28/6/22 6 | //Q--> SUDOKU 7 | 8 | /* 9 | conditions: 10 | 1) every row should contain unique number 11 | 2) every col should contain unique number 12 | 3) every inner 3x3 squre should contain unique number 13 | 14 | 15 | */ 16 | //i/p---> 17 | 18 | //[["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]] 19 | //Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]] 20 | //Explanation: if found any of the ans just return it 21 | 22 | class Solution { 23 | public: 24 | void solveSudoku(vector>& board) { 25 | solve(board); // board contaning entire sudoku with numbers and at empty space use the '.' 26 | } 27 | 28 | bool solve(vector>& board) 29 | { 30 | for(int i = 0; i < board.size(); ++i) //iterate row 31 | { 32 | for(int j=0; j 39 | { 40 | board[i][j]=c; // assighns the num to that empty space and 41 | 42 | if(solve(board)==true) // if returns true program going to be returning 43 | return true; 44 | else 45 | board[i][j]='.'; // else backtrack and try another combination 46 | 47 | } 48 | } 49 | return false; // used for backtrack condition 50 | } 51 | } 52 | 53 | } 54 | return true; // at the end the end if i and j reach the 8,8 the all the rows and col are filed and program gets terminated 55 | } 56 | 57 | bool isValid(int row,int col,char c,vector>&board) 58 | { 59 | for(int i=0;i<9;++i) 60 | { 61 | if(board[i][col]==c) //checking num row wise from row number 0 to 8 62 | return false; 63 | 64 | if(board[row][i]==c) //checking num col wise from col number 0 to 8 65 | return false; 66 | 67 | if(board[ 3 * (row / 3)+ i / 3 ][ 3 * (col / 3) + i%3 ]==c) //checking inner squres (3x3) ******imp logic 68 | { 69 | return false; 70 | } 71 | } 72 | return true; 73 | } 74 | }; 75 | 76 | 77 | int main() 78 | { 79 | #ifndef ONLINE_JUDGE 80 | freopen("input.txt","r",stdin); 81 | freopen("output.txt","w",stdout); 82 | #endif 83 | 84 | 85 | vector>inp; 86 | 87 | //akash=solveNQueens(n); 88 | 89 | for(int i=0;i<9;++i) 90 | for(int j=0;j<9;++j) 91 | cin>>inp[i][j]; 92 | 93 | /* 94 | for(auto x: inp) 95 | { 96 | for(auto y:x) 97 | { 98 | cout< 2 | using namespace std; 3 | 4 | void f(int n) 5 | { 6 | if(n<1) 7 | return; 8 | f(n-1); //dont us i+1 9 | cout<>n; 22 | f(n); 23 | /* code */ 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Recursion/39-combination-sum/39-combination-sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | //AKASH DINKAR PATIL 4 | //Q--> print all the unique combination of target int .combination may in any order 5 | //eg. arr=[2,3,6,7], target=7 6 | //o/p--> [[2,2,3],[7]] 7 | //explanation--> 2+2+3=7 (here 2 is taken twice) and 7=7; 8 | 9 | //technique --> just dont increment the index of take element because it may be repeted 10 | 11 | void combinationSumI(int index,vector&ds,int target,vector&arr) 12 | { 13 | if(index>=arr.size()) 14 | { 15 | if(target==0) 16 | { 17 | for(auto it:ds){ 18 | cout<=0) 31 | { 32 | ds.push_back(arr[index]); 33 | 34 | combinationSumI(index,ds,target-arr[index],arr); 35 | ds.pop_back(); //push_back and pop_back are associated with "take" only because we take element 36 | //after call we have to relese or remove that element for "non-take" 37 | 38 | } 39 | 40 | 41 | //not take 42 | 43 | combinationSumI(index+1,ds,target,arr); 44 | } 45 | 46 | int main() 47 | { 48 | /*#ifndef ONLINE_JUDGE 49 | freopen("input.txt","r",stdin); 50 | freopen("output.txt","w",stdout); 51 | #endif*/ 52 | 53 | int n,target; 54 | cin>>n>>target; 55 | vectorarr(n),ds; 56 | 57 | for(int i=0;i>arr[i]; 58 | combinationSumI(0,ds,target,arr); 59 | 60 | 61 | /* code */ 62 | return 0; 63 | } 64 | 65 | -------------------------------------------------------------------------------- /Recursion/39-combination-sum/Notes.md: -------------------------------------------------------------------------------- 1 | #Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order. 2 | 3 | The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different. 4 | 5 | The test cases are generated such that the number of unique combinations that sum up to target is less than 150 combinations for the given input. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: candidates = [2,3,6,7], target = 7 12 | Output: [[2,2,3],[7]] 13 | Explanation: 14 | 2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times. 15 | 7 is a candidate, and 7 = 7. 16 | These are the only two combinations. 17 | Example 2: 18 | 19 | Input: candidates = [2,3,5], target = 8 20 | Output: [[2,2,2,2],[2,3,3],[3,5]] 21 | Example 3: 22 | 23 | Input: candidates = [2], target = 1 24 | Output: [] 25 | 26 | 27 | Constraints: 28 | 29 | 1 <= candidates.length <= 30 30 | 2 <= candidates[i] <= 40 31 | All elements of candidates are distinct. 32 | 1 <= target <= 500 33 | -------------------------------------------------------------------------------- /Recursion/4 1toNBacktrack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void f(int a,int n) 5 | { 6 | if(n<=0) 7 | return; 8 | f(a,n-1); //dont us i+1 9 | cout<>n; 22 | f(1,n); 23 | /* code */ 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Recursion/5 Nto1Backtrack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void f(int n,int sum) 5 | { 6 | if(n<=0) 7 | { 8 | cout<>n; 25 | f(n,0); 26 | /* code */ 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Recursion/6 sumOfFirstN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void f(int n,int sum) 5 | { 6 | if(n<1) 7 | { 8 | cout<>n; 25 | f(n,0); 26 | /* code */ 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Recursion/7 test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void f(int a,int n) 5 | { 6 | if(a>n) 7 | return; 8 | f(a+1,n); //dont us i-1 9 | cout<>n; 22 | f(1,n); 23 | /* code */ 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Recursion/8 sumOfFirstN(Functional).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int f(int n) 5 | { 6 | if(n<=0) 7 | { 8 | 9 | return 0; 10 | } 11 | 12 | return n+f(n-1); //functional means without printing the value just return it to calling function. 13 | 14 | 15 | 16 | } 17 | int main() 18 | { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt","r",stdin); 21 | freopen("output.txt","w",stdout); 22 | #endif 23 | 24 | int n; 25 | cin>>n; 26 | cout< 2 | using namespace std; 3 | 4 | int f(int n) 5 | { 6 | if(n<=0) 7 | { 8 | 9 | return 0; 10 | } 11 | 12 | return n+f(n-1); 13 | 14 | 15 | 16 | } 17 | int main() 18 | { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt","r",stdin); 21 | freopen("output.txt","w",stdout); 22 | #endif 23 | 24 | int n; 25 | cin>>n; 26 | cout< 2 | using namespace std; 3 | 4 | int f(int n) 5 | { 6 | if(n<=0) 7 | { 8 | 9 | return 0; 10 | } 11 | 12 | return n+f(n-1); //functional means without printing the value just return it to calling function 13 | 14 | 15 | 16 | } 17 | int main() 18 | { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt","r",stdin); 21 | freopen("output.txt","w",stdout); 22 | #endif 23 | 24 | int n; 25 | cin>>n; 26 | cout< 2 | #define endl "\n" 3 | using namespace std; 4 | 5 | //AKASH DINKAR PATIL 6 | //Q--> print all the subsets from given list. list may contains duplicates ,subset should not contains 7 | //dupliocates. 8 | //eg->[1,2,2,2,3,3] 9 | //o/p--> [[],[1],[2],[2],[1,2],[1,3],[1,2],[1,3,3],[2,3,3],[2,2,3,3],.....] 10 | 11 | void subsetSumII(int index,vector&ds,vector&arr,vector>&ans) 12 | { 13 | ans.push_back(ds); 14 | 15 | 16 | for(int i=index;i>n; 40 | vectorarr(n); 41 | vectords; 42 | vector>ans; 43 | for(int i=0;i>arr[i]; 44 | 45 | 46 | subsetSumII(0,ds,arr,ans); 47 | 48 | 49 | for(auto i :ans) 50 | { 51 | 52 | for(auto j:i) 53 | { 54 | cout< 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | struct Node { 8 | int data; 9 | Node* left; 10 | Node* right; 11 | }; 12 | 13 | class BinaryTree { 14 | private: 15 | struct Node* root; 16 | public: 17 | BinaryTree() { 18 | root = NULL; 19 | } 20 | Node* createNode(int); 21 | Node* insertNode(Node*, int); 22 | Node* deleteNode(Node*, int); 23 | void inOrder(Node*); 24 | void preOrder(Node*); 25 | void postOrder(Node*); 26 | Node* findMinimum(Node*); 27 | 28 | Node* getRoot() { 29 | return root; 30 | } 31 | 32 | void setRoot(Node* ptr) { 33 | root = ptr; 34 | } 35 | }; 36 | 37 | Node* BinaryTree :: createNode(int n) { 38 | Node* newNode = new struct Node(); 39 | newNode->data = n; 40 | newNode->left = NULL; 41 | newNode->right = NULL; 42 | return newNode; 43 | } 44 | 45 | 46 | Node* BinaryTree :: findMinimum(Node* rootPtr) { 47 | while(rootPtr->left != NULL) { 48 | rootPtr = rootPtr->left; 49 | } 50 | return rootPtr; 51 | } 52 | 53 | 54 | Node* BinaryTree :: insertNode(Node* rootPtr, int n) { 55 | if(rootPtr == NULL) { 56 | return createNode(n); 57 | } 58 | if(n < rootPtr->data) { 59 | rootPtr->left = insertNode(rootPtr->left, n); 60 | } 61 | if(n > rootPtr->data) { 62 | rootPtr->right = insertNode(rootPtr->right, n); 63 | } 64 | return rootPtr; 65 | } 66 | 67 | 68 | Node* BinaryTree :: deleteNode(Node* rootPtr, int n) { 69 | if(rootPtr == NULL) { 70 | cout<<"Node to be deleted is not present.!"<data) { 74 | rootPtr->left = deleteNode(rootPtr->left, n); 75 | } else if(n > rootPtr->data) { 76 | rootPtr->right = deleteNode(rootPtr->right, n); 77 | } else { 78 | if(rootPtr->left == NULL && rootPtr->right == NULL) { 79 | delete rootPtr; 80 | rootPtr = NULL; 81 | } 82 | else if(root->left == NULL) { 83 | struct Node* temp = rootPtr; 84 | rootPtr = rootPtr->right; 85 | delete temp; 86 | } 87 | else if(rootPtr->right == NULL) { 88 | struct Node* temp = rootPtr; 89 | rootPtr = rootPtr->left; 90 | delete temp; 91 | } else { 92 | Node* temp = findMinimum(rootPtr->right); 93 | rootPtr->data = temp->data; 94 | rootPtr->left = deleteNode(rootPtr->right, temp->data); 95 | } 96 | } 97 | 98 | return rootPtr; 99 | } 100 | 101 | 102 | void BinaryTree :: inOrder(Node* root) { 103 | if(root == NULL) { 104 | return; 105 | } 106 | inOrder(root->left); 107 | cout<data<<"\t"; 108 | inOrder(root->right); 109 | } 110 | 111 | void BinaryTree :: preOrder(Node* root) { 112 | if(root == NULL) return; 113 | cout<data<<"\t"; 114 | preOrder(root->left); 115 | preOrder(root->right); 116 | } 117 | 118 | void BinaryTree :: postOrder(Node* root) { 119 | if(root == NULL) return; 120 | postOrder(root->left); 121 | postOrder(root->right); 122 | cout<data<<"\t"; 123 | } 124 | 125 | int main() { 126 | BinaryTree l1; 127 | int ch, ele, res; 128 | Node* ptr; 129 | do { 130 | cout<<"1 - Insert Node\n"; 131 | cout<<"2 - IN-ORDER Traversal\n"; 132 | cout<<"3 - PRE-ORDER Traversal\n"; 133 | cout<<"4 - POST-ORDER Traversal\n"; 134 | cout<<"Enter choice\n"; 135 | cin>>ch; 136 | switch(ch) { 137 | case 1: 138 | cout<<"Entre element to insert to the List\n"; 139 | cin>>ele; 140 | 141 | ptr = l1.insertNode(l1.getRoot(), ele); 142 | 143 | l1.setRoot(ptr); 144 | break; 145 | case 2: 146 | cout<<"---IN-ORDER TRAVERSAL---"<>ele; 163 | ptr = l1.deleteNode(l1.getRoot(), ele); 164 | l1.setRoot(ptr); 165 | default: cout<<"Invalid choice"<=1 && ch <= 5); 168 | return 0; 169 | } -------------------------------------------------------------------------------- /Recursion/LetterCombinationsPhoneNum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unordered_map umap = {{'2',"abc"},{'3',"def"}, 5 | {'4',"ghi"},{'5',"jkl"}, 6 | {'6',"mno"},{'7',"pqrs"}, 7 | {'8',"tuv"},{'9',"wxyz"}}; 8 | 9 | void getCombo(vector& ans,string digits,int index,int max,string ds) 10 | { 11 | if(index>=max) 12 | { 13 | ans.push_back(ds); 14 | return ; 15 | } 16 | 17 | int len = umap[digits[index]].size(); 18 | 19 | for(int i=0;i letterCombinations(string digits) { 29 | 30 | vector ans; 31 | if(digits=="") 32 | { 33 | 34 | return ans; 35 | } 36 | 37 | int max = digits.length(); 38 | 39 | string ds; 40 | 41 | getCombo(ans,digits,0,max,ds); 42 | 43 | 44 | 45 | return ans; 46 | 47 | 48 | 49 | } 50 | 51 | int main() 52 | { 53 | vector ans; 54 | ans = letterCombinations("22233"); 55 | // cout< 5 | using namespace std; 6 | 7 | /* m*n is the board dimension 8 | k is the number of knights to be placed on board 9 | count is the number of possible solutions */ 10 | int m, n, k; 11 | int count = 0; 12 | 13 | /* This function is used to create an empty m*n board */ 14 | void makeBoard(char** board) 15 | { 16 | for (int i = 0; i < m; i++) { 17 | for (int j = 0; j < n; j++) { 18 | board[i][j] = '_'; 19 | } 20 | } 21 | } 22 | 23 | /* This function displays our board */ 24 | void displayBoard(char** board) 25 | { 26 | for (int i = 0; i < m; i++) { 27 | for (int j = 0; j < n; j++) { 28 | cout << " " << board[i][j] << " "; 29 | } 30 | cout << endl; 31 | } 32 | cout << endl; 33 | } 34 | 35 | /* This function marks all the attacking 36 | position of a knight placed at board[i][j] 37 | position */ 38 | void attack(int i, int j, char a, char** board) 39 | { 40 | 41 | /* conditions to ensure that the 42 | block to be checked is inside the board */ 43 | if ((i + 2) < m && (j - 1) >= 0) { 44 | board[i + 2][j - 1] = a; 45 | } 46 | if ((i - 2) >= 0 && (j - 1) >= 0) { 47 | board[i - 2][j - 1] = a; 48 | } 49 | if ((i + 2) < m && (j + 1) < n) { 50 | board[i + 2][j + 1] = a; 51 | } 52 | if ((i - 2) >= 0 && (j + 1) < n) { 53 | board[i - 2][j + 1] = a; 54 | } 55 | if ((i + 1) < m && (j + 2) < n) { 56 | board[i + 1][j + 2] = a; 57 | } 58 | if ((i - 1) >= 0 && (j + 2) < n) { 59 | board[i - 1][j + 2] = a; 60 | } 61 | if ((i + 1) < m && (j - 2) >= 0) { 62 | board[i + 1][j - 2] = a; 63 | } 64 | if ((i - 1) >= 0 && (j - 2) >= 0) { 65 | board[i - 1][j - 2] = a; 66 | } 67 | } 68 | 69 | /* If the position is empty, 70 | place the knight */ 71 | bool canPlace(int i, int j, char** board) 72 | { 73 | if (board[i][j] == '_') 74 | return true; 75 | else 76 | return false; 77 | } 78 | 79 | /* Place the knight at [i][j] position 80 | on board */ 81 | void place(int i, int j, char k, char a, char** board, 82 | char** new_board) 83 | { 84 | 85 | /* Copy the configurations of 86 | old board to new board */ 87 | for (int y = 0; y < m; y++) { 88 | for (int z = 0; z < n; z++) { 89 | new_board[y][z] = board[y][z]; 90 | } 91 | } 92 | 93 | /* Place the knight at [i][j] 94 | position on new board */ 95 | new_board[i][j] = k; 96 | 97 | /* Mark all the attacking positions 98 | of newly placed knight on the new board */ 99 | attack(i, j, a, new_board); 100 | } 101 | 102 | /* Function for placing knights on board 103 | such that they don't attack each other */ 104 | void kkn(int k, int sti, int stj, char** board) 105 | { 106 | 107 | /* If there are no knights left to be placed, 108 | display the board and increment the count */ 109 | if (k == 0) { 110 | displayBoard(board); 111 | count++; 112 | } 113 | else { 114 | 115 | /* Loop for checking all the 116 | positions on m*n board */ 117 | for (int i = sti; i < m; i++) { 118 | for (int j = stj; j < n; j++) { 119 | 120 | /* Is it possible to place knight at 121 | [i][j] position on board? */ 122 | if (canPlace(i, j, board)) { 123 | 124 | /* Create a new board and place the 125 | new knight on it */ 126 | char** new_board = new char*[m]; 127 | for (int x = 0; x < m; x++) { 128 | new_board[x] = new char[n]; 129 | } 130 | place(i, j, 'K', 'A', board, new_board); 131 | 132 | /* Call the function recursively for 133 | (k-1) leftover knights */ 134 | kkn(k - 1, i, j, new_board); 135 | 136 | /* Delete the new board 137 | to free up the memory */ 138 | for (int x = 0; x < m; x++) { 139 | delete[] new_board[x]; 140 | } 141 | delete[] new_board; 142 | } 143 | } 144 | stj = 0; 145 | } 146 | } 147 | } 148 | 149 | // Driver code 150 | int main() 151 | { 152 | m = 4, n = 3, k = 6; 153 | 154 | /* Creation of a m*n board */ 155 | char** board = new char*[m]; 156 | for (int i = 0; i < m; i++) { 157 | board[i] = new char[n]; 158 | } 159 | 160 | /* Make all the places are empty */ 161 | makeBoard(board); 162 | 163 | kkn(k, 0, 0, board); 164 | 165 | cout << endl << "Total number of solutions : " << count; 166 | return 0; 167 | } -------------------------------------------------------------------------------- /Recursion/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akashpatil3640/CPP/cacafd7ccb2d6de8a2532a872865aaa2099cdcd5/Recursion/a -------------------------------------------------------------------------------- /Recursion/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akashpatil3640/CPP/cacafd7ccb2d6de8a2532a872865aaa2099cdcd5/Recursion/a.out -------------------------------------------------------------------------------- /Recursion/akash: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akashpatil3640/CPP/cacafd7ccb2d6de8a2532a872865aaa2099cdcd5/Recursion/akash -------------------------------------------------------------------------------- /Recursion/input.txt: -------------------------------------------------------------------------------- 1 | 25 -------------------------------------------------------------------------------- /Recursion/output.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akashpatil3640/CPP/cacafd7ccb2d6de8a2532a872865aaa2099cdcd5/Recursion/output.txt -------------------------------------------------------------------------------- /Searching/modified_search_143.cpp: -------------------------------------------------------------------------------- 1 | //name : deepak s. patil 2 | //roll no : 143 3 | //program for modified linear search algorithm. 4 | //28-09-2020 16:28 5 | 6 | #include 7 | 8 | int main() 9 | { 10 | int size; 11 | 12 | printf("enter the size of an array :\n"); 13 | scanf("%d",&size); 14 | 15 | int data[size+1],i,item,LOC=0; 16 | 17 | printf("enter the %d elements of an array:\n",size); 18 | for(i=0;i 2 | using namespace std; 3 | 4 | void merge(int arr[], int left, int mid, int right) { 5 | 6 | int n1 = mid - left + 1; 7 | int n2 = right - mid; 8 | 9 | int L[n1], R[n2]; // Dividing our array into two parts 10 | 11 | for (int i = 0; i < n1; i++) { 12 | L[i] = arr[left+ i]; 13 | } 14 | 15 | for (int j = 0; j < n2; j++) { 16 | R[j] = arr[mid + 1 + j]; 17 | } 18 | 19 | int i = 0; 20 | int j = 0; 21 | int k = left; 22 | 23 | while (i < n1 && j < n2) { 24 | if (L[i] <= R[j]) { 25 | arr[k] = L[i]; 26 | i++; 27 | } 28 | else { 29 | arr[k] = R[j]; 30 | j++; 31 | } 32 | k++; 33 | } 34 | 35 | while (i < n1) { 36 | arr[k] = L[i]; 37 | i++; 38 | k++; 39 | } 40 | 41 | while (j < n2){ 42 | arr[k] = R[j]; 43 | j++; 44 | k++; 45 | } 46 | } 47 | 48 | void mergeSort(int *arr,int left,int right) { 49 | 50 | if(left >= right) 51 | return; 52 | 53 | int mid =left+ (right-left)/2; 54 | mergeSort(arr,left,mid); 55 | mergeSort(arr,mid+1,right); 56 | merge(arr,left,mid,right); 57 | } 58 | 59 | int main() { 60 | 61 | int arr[] = {11,22,34,54,12,98,10,14,21,30,34,11}; 62 | int n = sizeof(arr)/sizeof(arr[0]); 63 | 64 | cout << "The Unsorted Array is: "; 65 | for(int i = 0; i 2 | using namespace std; 3 | 4 | void swap(int* a, int* b) { 5 | int t = *a; 6 | *a = *b; 7 | *b = t; 8 | } 9 | 10 | int partition(int arr[], int start, int end) { 11 | int pivot = arr[end]; 12 | int i = (start - 1); 13 | 14 | for (int j = start; j <= end - 1; j++) { 15 | if (arr[j] < pivot) { 16 | i++; 17 | swap(&arr[i], &arr[j]); 18 | } 19 | } 20 | 21 | swap(&arr[i + 1], &arr[end]); 22 | return (i + 1); 23 | } 24 | 25 | 26 | void quickSort(int arr[], int start, int end) { 27 | if (start < end) { 28 | int pi = partition(arr, start, end); 29 | 30 | quickSort(arr, start, pi - 1); 31 | quickSort(arr, pi + 1, end); 32 | } 33 | } 34 | 35 | 36 | int main() { 37 | 38 | int arr[] = {11,22,34,54,12,98,10,14,21,30,34,11}; 39 | int n = sizeof(arr)/sizeof(arr[0]); 40 | 41 | cout << "The Unsorted Array is: "; 42 | for(int i = 0; i 4 | using namespace std; 5 | 6 | //Swap function 7 | void swap(int *xp, int *yp) 8 | { 9 | int temp = *xp; 10 | *xp = *yp; 11 | *yp = temp; 12 | } 13 | 14 | void selectionSort(int arr[], int n) 15 | { 16 | int i, j, min_idx; 17 | 18 | // One by one move boundary of 19 | // unsorted subarray 20 | for (i = 0; i < n-1; i++) 21 | { 22 | 23 | // Find the minimum element in 24 | // unsorted array 25 | min_idx = i; 26 | for (j = i+1; j < n; j++) 27 | if (arr[j] < arr[min_idx]) 28 | min_idx = j; 29 | 30 | // Swap the found minimum element 31 | // with the first element 32 | if(min_idx!=i) 33 | swap(&arr[min_idx], &arr[i]); 34 | } 35 | } 36 | 37 | //Function to print an array 38 | void printArray(int arr[], int size) 39 | { 40 | int i; 41 | for (i=0; i < size; i++) 42 | cout << arr[i] << " "; 43 | cout << endl; 44 | } 45 | 46 | // Driver program to test above functions 47 | int main() 48 | { 49 | int arr[] = {64, 25, 12, 22, 11}; 50 | int n = sizeof(arr)/sizeof(arr[0]); 51 | selectionSort(arr, n); 52 | cout << "Sorted array: \n"; 53 | printArray(arr, n); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Sorting/Selection_Sort.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akashpatil3640/CPP/cacafd7ccb2d6de8a2532a872865aaa2099cdcd5/Sorting/Selection_Sort.exe -------------------------------------------------------------------------------- /Sorting/bubble_sort.cpp: -------------------------------------------------------------------------------- 1 | // bubblesort implementation 2 | #include 3 | using namespace std; 4 | int main () 5 | { 6 | int i, j,temp; 7 | int a[5] = {10,2,0,43,12}; 8 | cout <<"Input list ...\n"; 9 | for(i = 0; i<5; i++) { 10 | cout < 2 | using namespace std; 3 | 4 | void heapify(int arr[], int n, int i) 5 | { 6 | int largest = i; 7 | int l = 2*i + 1; 8 | int r = 2*i + 2; 9 | 10 | if (l < n && arr[l] > arr[largest]) 11 | largest = l; 12 | 13 | if (r < n && arr[r] > arr[largest]) 14 | largest = r; 15 | 16 | if (largest != i) 17 | { 18 | swap(arr[i], arr[largest]); 19 | 20 | //Recursively heapifying the sub-tree 21 | heapify(arr, n, largest); 22 | } 23 | } 24 | 25 | void heapSort(int arr[], int n) 26 | { 27 | for (int i = n / 2 - 1; i >= 0; i--) 28 | heapify(arr, n, i); 29 | 30 | for (int i=n-1; i>=0; i--) 31 | { 32 | swap(arr[0], arr[i]); 33 | heapify(arr, i, 0); 34 | } 35 | } 36 | 37 | void display(int arr[], int n) 38 | { 39 | for (int i = 0; i < n; i++) 40 | { 41 | cout << arr[i] << "\t"; 42 | } 43 | cout << "\n"; 44 | } 45 | int main() 46 | { 47 | int arr[] = {1, 14, 3, 7, 0}; 48 | int n = sizeof(arr)/sizeof(arr[0]); 49 | cout << "Unsorted array \n"; 50 | display(arr, n); 51 | 52 | heapSort(arr, n); 53 | 54 | cout << "Sorted array \n"; 55 | display(arr, n); 56 | } 57 | -------------------------------------------------------------------------------- /Sorting/insertion sort_143.c: -------------------------------------------------------------------------------- 1 | //name : deepak s. patil 2 | //roll no : 143 3 | //program for the insertion sort. 4 | 5 | #include 6 | 7 | int main() 8 | { 9 | int N,ptr,temp,i; 10 | 11 | printf("enter the size of data :\n"); 12 | scanf("%d",&N); 13 | 14 | int data[N]; 15 | 16 | printf("enter the data :\n"); 17 | for(i=0;i=0 ) 27 | { 28 | data[ptr+1]=data[ptr]; 29 | ptr=ptr-1; 30 | } 31 | data[ptr+1]=temp; 32 | } 33 | 34 | printf("the sorted data is :\n"); 35 | for(i=0;i 3 | using namespace std; 4 | int shellSort(int arr[], int N) 5 | { 6 | for (int gap = N/2; gap > 0; gap /= 2) { 7 | for (int i = gap; i < N; i += 1) { 8 | //sort sub lists created by applying gap 9 | int temp = arr[i]; 10 | int j; 11 | for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) 12 | arr[j] = arr[j - gap]; 13 | 14 | arr[j] = temp; 15 | } 16 | } 17 | return 0; 18 | } 19 | 20 | int main() 21 | { 22 | int arr[] = {45,23,53,43,18}; 23 | //Calculate size of array 24 | int N = sizeof(arr)/sizeof(arr[0]); 25 | 26 | cout << "Array to be sorted: \n"; 27 | for (int i=0; i 4 | using namespace std; 5 | 6 | const int MAX = 1000; 7 | 8 | // Function to convert the array to Spiral 9 | void ToSpiral(int m, int n, 10 | int Sorted[], int a[MAX][MAX]) 11 | { 12 | // For Array pointer 13 | int index = 0; 14 | 15 | // k - starting row index 16 | // m - ending row index 17 | // l - starting column index 18 | // n - ending column index 19 | int k = 0, l = 0; 20 | 21 | while (k < m && l < n) 22 | { 23 | 24 | // Print the first row 25 | // from the remaining rows 26 | for (int i = l; i < n; ++i) 27 | { 28 | a[k][i] = Sorted[index]; 29 | index++; 30 | } 31 | 32 | k++; 33 | 34 | // Print the last column 35 | // from the remaining columns 36 | for (int i = k; i < m; ++i) 37 | { 38 | a[i][n - 1] = Sorted[index]; 39 | index++; 40 | } 41 | n--; 42 | 43 | // Print the last row 44 | // from the remaining rows 45 | if (k < m) 46 | { 47 | for (int i = n - 1; i >= l; --i) 48 | { 49 | a[m - 1][i] = Sorted[index]; 50 | index++; 51 | } 52 | m--; 53 | } 54 | 55 | // Print the first column 56 | // from the remaining columns 57 | if (l < n) 58 | { 59 | for (int i = m - 1; i >= k; --i) 60 | { 61 | a[i][l] = Sorted[index]; 62 | index++; 63 | } 64 | l++; 65 | } 66 | } 67 | } 68 | 69 | // Function to convert 2D array to 1D array 70 | void convert2Dto1D(int y[MAX][MAX], 71 | int m, int n,int x[]) 72 | { 73 | 74 | int index = 0; 75 | 76 | // Store value 2D Matrix To 1D array 77 | for (int i = 0; i < m; i++) 78 | { 79 | for (int j = 0; j < n; j++) 80 | { 81 | x[index] = y[i][j]; 82 | index++; 83 | } 84 | } 85 | } 86 | 87 | // Function to print the Matrix 88 | void PrintMatrix(int a[MAX][MAX], 89 | int m, int n) 90 | { 91 | 92 | // Print Spiral Matrix 93 | for (int i = 0; i < m; i++) 94 | { 95 | for (int j = 0; j < n; j++) 96 | { 97 | cout << a[i][j] << " "; 98 | } 99 | cout << endl; 100 | } 101 | } 102 | 103 | 104 | // Function to Convert given Matrix 105 | // into sorted Spiral Matrix 106 | void convertMatrixToSortedSpiral( 107 | int y[MAX][MAX], int m, int n) 108 | { 109 | int a[MAX][MAX] = {0}; 110 | int x[m * n]; 111 | 112 | convert2Dto1D(y, m, n,x); 113 | sort(x, x + n * m); 114 | ToSpiral(m, n, x, a); 115 | PrintMatrix(a, m, n); 116 | } 117 | 118 | // Driver code 119 | int main() 120 | { 121 | int m = 4, n = 3; 122 | int y[MAX][MAX] = { 123 | { 2, 5, 12 }, 124 | { 22, 45, 55 }, 125 | { 1, 6, 8 }, 126 | { 13, 56, 10 }}; 127 | 128 | convertMatrixToSortedSpiral(y, m, n); 129 | 130 | return 0; 131 | } 132 | -------------------------------------------------------------------------------- /Stack/stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | //stack 3 | int TOP=-1; 4 | 5 | void push(int stack[],int item,int size) 6 | { 7 | if(TOP==size-1) 8 | { 9 | printf("Stack Overflow\n"); 10 | } 11 | else 12 | { 13 | TOP++; 14 | stack[TOP]=item; 15 | printf("%d pushed successfully\n",item); 16 | } 17 | } 18 | 19 | void pop(int stack[]) 20 | { 21 | if(TOP==-1) 22 | { 23 | printf("Stack Underflow\n"); 24 | } 25 | else 26 | { 27 | TOP--; 28 | printf("%d popped successfully\n",stack[TOP+1]); 29 | } 30 | } 31 | 32 | void display(int stack[]) 33 | { 34 | if(TOP==-1) 35 | { 36 | printf("Stack is Empty\n"); 37 | } 38 | else 39 | { 40 | for(int i=0;i<=TOP ;i++) 41 | { 42 | printf("%d\t",stack[i]); 43 | } 44 | } 45 | } 46 | 47 | int main() 48 | { 49 | int size; 50 | printf("Enter the size of Stack\n"); 51 | scanf("%d",&size); 52 | 53 | int stack[size]; 54 | int ch; 55 | do{ 56 | printf("Enter your choice: \n"); 57 | printf("1.Push\n2.Pop\n3.Display\n4.Exit\n"); 58 | scanf("%d",&ch); 59 | switch(ch) 60 | { 61 | case 1: 62 | printf("Enter item to push:\n"); 63 | int item; 64 | scanf("%d",&item); 65 | push(stack,item,size); 66 | break; 67 | case 2: 68 | pop(stack); 69 | break; 70 | case 3: 71 | display(stack); 72 | break; 73 | case 4: 74 | break; 75 | default: 76 | printf("Enter a valid choice\n"); 77 | } 78 | }while(ch!=4); 79 | 80 | return 0; 81 | } 82 | 83 | -------------------------------------------------------------------------------- /Trie.cpp/Implement_trie.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class trienode 4 | { 5 | public: 6 | char data; 7 | trienode *children[26]; 8 | bool isterminal; 9 | trienode(char ch) 10 | { 11 | data = ch; 12 | for (int i = 0; i < 26; i++) 13 | { 14 | children[i] = NULL; 15 | } 16 | isterminal = false; 17 | } 18 | }; 19 | class trie 20 | { 21 | public: 22 | trienode *root; 23 | trie() 24 | { 25 | root = new trienode('\0'); 26 | } 27 | void insertutil(trienode *root, string word) 28 | { 29 | // base case 30 | 31 | if (word.length() == 0) 32 | { 33 | root->isterminal = true; 34 | return; 35 | } 36 | trienode *child; 37 | int index = word[0] - 'a'; 38 | if (root->children[index] != NULL) 39 | { 40 | // character is present 41 | 42 | child = root->children[index]; 43 | } 44 | else 45 | { 46 | child = new trienode(word[0]); 47 | root->children[index] = child; 48 | } 49 | insertutil(child, word.substr(1)); 50 | } 51 | void insertword(string word) 52 | { 53 | insertutil(root, word); 54 | } 55 | bool searchutil(trienode *root, string word) 56 | { 57 | // base case 58 | 59 | if (word.length() == 0) 60 | { 61 | return root->isterminal; 62 | } 63 | trienode *child; 64 | int index = word[0] - 'a'; 65 | if (root->children[index] != NULL) 66 | { 67 | child = root->children[index]; 68 | } 69 | else 70 | { 71 | return false; 72 | } 73 | return searchutil(child, word.substr(1)); 74 | } 75 | bool search(string word) 76 | { 77 | return searchutil(root, word); 78 | } 79 | }; 80 | int main() 81 | { 82 | trie *t = new trie; 83 | t->insertword("abcd"); 84 | t->insertword("spd"); 85 | t->insertword("spdking"); 86 | cout << t->search("abcdk") << endl; 87 | cout << t->search("spd") << endl; 88 | cout << t->search("spdking") << endl; 89 | return 0; 90 | } -------------------------------------------------------------------------------- /Unique tricky programs/Subarray_sum_equals_k.cpp: -------------------------------------------------------------------------------- 1 | //Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k. 2 | 3 | //A subarray is a contiguous non-empty sequence of elements within an array. 4 | 5 | //Input: nums = [1,1,1], k = 2 6 | //Output: 2 7 | 8 | int main(){ 9 | int n; 10 | cin>>n; 11 | vectorarr; 12 | for(int i=0;i>arr[i]; 14 | } 15 | if(n==0){return 0;} 16 | 17 | unordered_map mp; 18 | int currSUM = 0; 19 | int i = 0; 20 | int cnt = 0; 21 | for(int i=0;i 2 | using namespace std; 3 | class node 4 | { 5 | public: 6 | int data; 7 | node *left; 8 | node *right; 9 | node(int data) 10 | { 11 | this->data = data; 12 | this->left = NULL; 13 | this->right = NULL; 14 | } 15 | }; 16 | node *buildTree(node *root) 17 | { 18 | cout << "Enter the data: " << endl; 19 | int data; 20 | cin >> data; 21 | root = new node(data); 22 | 23 | if (data == -1) 24 | { 25 | return NULL; 26 | } 27 | 28 | cout << "Enter data for inserting in left of " << data << endl; 29 | root->left = buildTree(root->left); 30 | cout << "Enter data for inserting in right of " << data << endl; 31 | root->right = buildTree(root->right); 32 | return root; 33 | } 34 | void levelOrder(node *root) 35 | { 36 | queue q; 37 | q.push(root); 38 | while (!q.empty()) 39 | { 40 | node *temp = q.front(); 41 | cout << temp->data << " "; 42 | q.pop(); 43 | if (temp->left) 44 | { 45 | q.push(temp->left); 46 | } 47 | if (temp->right) 48 | { 49 | q.push(temp->right); 50 | } 51 | } 52 | } 53 | void levelorder2(node *root) 54 | { 55 | queue q; 56 | q.push(root); 57 | q.push(NULL); 58 | while (!q.empty()) 59 | { 60 | node *temp = q.front(); 61 | q.pop(); 62 | if (temp == NULL) 63 | { 64 | cout << endl; 65 | if (!q.empty()) 66 | q.push(NULL); 67 | } 68 | else 69 | { 70 | cout << temp->data << " "; 71 | if (temp->left) 72 | q.push(temp->left); 73 | if (temp->right) 74 | q.push(temp->right); 75 | } 76 | } 77 | } 78 | 79 | int main() 80 | { 81 | node *root = NULL; 82 | // 1 3 7 -1 -1 11 -1 -1 5 17 -1 -1 -1 83 | 84 | // 1 3 5 7 11 17 -1 -1 -1 -1 -1 -1 -1 85 | // root = buildTree(root); 86 | levelorder2(root); 87 | return 0; 88 | } -------------------------------------------------------------------------------- /circularQueueArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class queue 4 | { 5 | private: 6 | int size, cap, front; 7 | int *arr; 8 | public: 9 | queue(int capcaity) 10 | { 11 | this->cap = capcaity; 12 | this->size = 0; 13 | this->front = 0; 14 | this->arr = new int[cap]; 15 | } 16 | ~queue() 17 | { 18 | delete []arr; 19 | } 20 | bool isEmpty() 21 | { 22 | return (this->size == 0); 23 | } 24 | bool isFull() 25 | { 26 | return (this->size == this->cap); 27 | } 28 | int getFront() 29 | { 30 | if(isEmpty()) 31 | { 32 | return -1; 33 | } 34 | else 35 | { 36 | return this->front; 37 | } 38 | } 39 | int getRear() 40 | { 41 | if(isEmpty()) 42 | { 43 | return -1; 44 | } 45 | else 46 | { 47 | return((this->front + this->size -1) % this->cap); 48 | } 49 | } 50 | int enqueue(int item) 51 | { 52 | if(isFull()) 53 | { 54 | return 1; 55 | } 56 | int rear = getRear(); 57 | rear = (rear + 1) % cap; 58 | this->arr[rear] = item; 59 | this->size++; 60 | return 0; 61 | } 62 | int dequeue() 63 | { 64 | if(isEmpty()) 65 | { 66 | return -1; 67 | } 68 | this->arr[front] = -1; 69 | front = (front+1)%cap; 70 | this->size--; 71 | return 0; 72 | } 73 | void display() 74 | { 75 | int rear = getRear(); 76 | if(isEmpty()) 77 | { 78 | cout << "Queue is empty." ; 79 | } 80 | if(front <= rear) 81 | { 82 | for(int i = front; i <= rear ; i++) 83 | { 84 | cout << arr[i] << " "; 85 | } 86 | } 87 | else 88 | { 89 | for(int i = front; i < size ; i++) 90 | { 91 | cout << arr[i] << " "; 92 | } 93 | for(int i = 0 ;i < rear; i++) 94 | { 95 | cout << arr[i] << " "; 96 | } 97 | } 98 | 99 | cout << endl; 100 | } 101 | }; 102 | int main(int argv, char *argc[]) 103 | { 104 | queue q(5); 105 | q.enqueue(10); 106 | q.enqueue(20); 107 | q.enqueue(30); 108 | q.enqueue(40); 109 | q.enqueue(50); 110 | q.display(); 111 | q.dequeue(); 112 | q.dequeue(); 113 | q.dequeue(); 114 | q.dequeue(); 115 | q.display(); 116 | q.enqueue(60); 117 | q.display(); 118 | return 0; 119 | } -------------------------------------------------------------------------------- /circular_queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Queue 4 | { 5 | int size; 6 | int front; 7 | int rear; 8 | int *Q; 9 | }; 10 | void create(struct Queue *q,int size) 11 | { 12 | q->size=size; 13 | q->front=q->rear=0; 14 | q->Q=(int *)malloc(q->size*sizeof(int)); 15 | } 16 | void enqueue(struct Queue *q,int x) 17 | { 18 | if((q->rear+1)%q->size==q->front) 19 | printf("Queue is Full"); 20 | else 21 | { 22 | q->rear=(q->rear+1)%q->size; 23 | q->Q[q->rear]=x; 24 | } 25 | } 26 | int dequeue(struct Queue *q) 27 | { 28 | int x=-1; 29 | 30 | if(q->front==q->rear) 31 | printf("Queue is Empty\n"); 32 | else 33 | { 34 | q->front=(q->front+1)%q->size; 35 | x=q->Q[q->front]; 36 | } 37 | return x; 38 | } 39 | void Display(struct Queue q) 40 | { 41 | int i=q.front+1; 42 | 43 | do 44 | { 45 | 46 | printf("%d ",q.Q[i]); 47 | i=(i+1)%q.size; 48 | }while(i!=(q.rear+1)%q.size); 49 | 50 | printf("\n"); 51 | } 52 | int main() 53 | { 54 | struct Queue q; 55 | create(&q,5); 56 | 57 | enqueue(&q,10); 58 | enqueue(&q,20); 59 | enqueue(&q,30); 60 | enqueue(&q,40); 61 | enqueue(&q,50); 62 | enqueue(&q,60); 63 | Display(q); 64 | 65 | printf("%d ",dequeue(&q)); 66 | return 0; } -------------------------------------------------------------------------------- /queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template class Queue 5 | { 6 | private: 7 | int size, capacity; 8 | X *arr; 9 | public: 10 | Queue(int capacity) 11 | { 12 | this->capacity = capacity; 13 | this->size = 0; 14 | arr = new X[this->capacity]; 15 | } 16 | ~Queue() 17 | { 18 | delete []arr; 19 | } 20 | // isEmpty() returns true if queue is empty 21 | bool isEmpty() 22 | { 23 | return(this->size == 0); 24 | } 25 | // isFull() returns true if queue is full 26 | bool isFull() 27 | { 28 | return(this->size == this->capacity); 29 | } 30 | // enqueue() used to insert a new item at the front of queue 31 | int enqueue(int item) 32 | { 33 | if(isFull()) 34 | { 35 | return 1; 36 | } 37 | else 38 | { 39 | this->arr[size] = item; 40 | this->size++; 41 | return 0; 42 | } 43 | } 44 | // dequeue() used to delete last item from queue 45 | int dequeue() 46 | { 47 | int i; 48 | if(isEmpty()) 49 | { 50 | return -1; 51 | } 52 | else 53 | { 54 | for(i = 0; i < this->size - 1; i++) 55 | { 56 | this->arr[i] = this->arr[i+1]; 57 | } 58 | this->size--; 59 | return 0; 60 | } 61 | } 62 | // getFront() returns front item of the queue 63 | X getFront() 64 | { 65 | if(isEmpty()) 66 | { 67 | return -1; 68 | } 69 | else 70 | { 71 | return this->arr[0]; 72 | } 73 | 74 | } 75 | // getRear() returns last item of the queue 76 | X getRear() 77 | { 78 | if(isEmpty()) 79 | { 80 | return -1; 81 | } 82 | else 83 | { 84 | return this->arr[this->size - 1]; 85 | } 86 | } 87 | 88 | // display() print out all the items of queue 89 | void display() 90 | { 91 | int i; 92 | if(isEmpty()) 93 | { 94 | cout << "Queue is Empty." << endl; 95 | return; 96 | } 97 | else 98 | { 99 | for(i = 0; i < this->size; i++) 100 | { 101 | cout << this->arr[i] << " "; 102 | } 103 | cout << endl; 104 | } 105 | } 106 | }; 107 | 108 | // main funciton 109 | int main(int argv, char *argc[]) 110 | { 111 | Queueq('s'); 112 | q.enqueue('a'); 113 | q.enqueue('c'); 114 | q.enqueue('h'); 115 | q.display(); 116 | q.dequeue(); 117 | q.display(); 118 | cout << q.getFront() << endl; 119 | cout << q.getRear() << endl; 120 | return 0; 121 | } -------------------------------------------------------------------------------- /queue_using_stack.cpp: -------------------------------------------------------------------------------- 1 | 2 | // CPP program to implement Queue using 3 | // two stacks with costly enQueue() 4 | #include 5 | using namespace std; 6 | 7 | struct Queue { 8 | stack s1, s2; 9 | 10 | void enQueue(int x) 11 | { 12 | // Move all elements from s1 to s2 13 | while (!s1.empty()) { 14 | s2.push(s1.top()); 15 | s1.pop(); 16 | } 17 | 18 | // Push item into s1 19 | s1.push(x); 20 | 21 | // Push everything back to s1 22 | while (!s2.empty()) { 23 | s1.push(s2.top()); 24 | s2.pop(); 25 | } 26 | } 27 | 28 | // Dequeue an item from the queue 29 | int deQueue() 30 | { 31 | // if first stack is empty 32 | if (s1.empty()) { 33 | cout << "Q is Empty"; 34 | exit(0); 35 | } 36 | 37 | // Return top of s1 38 | int x = s1.top(); 39 | s1.pop(); 40 | return x; 41 | } 42 | }; 43 | 44 | // Driver code 45 | int main() 46 | { 47 | Queue q; 48 | q.enQueue(1); 49 | q.enQueue(2); 50 | q.enQueue(3); 51 | 52 | cout << q.deQueue() << '\n'; 53 | cout << q.deQueue() << '\n'; 54 | cout << q.deQueue() << '\n'; 55 | 56 | return 0; 57 | } 58 | --------------------------------------------------------------------------------