├── 000 Test My Self └── Test Rules & Reg Code.txt ├── 001 - Basics of C++ ├── 001 - Loops and Functions │ ├── 001 - loops │ │ ├── fibbonacci series.cpp │ │ ├── sum upto n .cpp │ │ └── sum upto n even no.cpp │ ├── 002 - Patterns │ │ ├── 001 - Pattern 01.cpp │ │ ├── 002 - Pattern 02.cpp │ │ ├── 003 - Pattern 03.cpp │ │ ├── 004 - Pattern 04.cpp │ │ ├── 005 - Pattern 05.cpp │ │ ├── 006 - Pattern 06.cpp │ │ ├── 007 - Pattern 07.cpp │ │ ├── 008 - Pattern 08.cpp │ │ ├── 009 - Pattern 09.cpp │ │ ├── 010 - Pattern 10.cpp │ │ ├── 010 - Program Output.png │ │ ├── 011 - Program 11.cpp │ │ └── 011 - Program Output.png │ └── 003 - Functions │ │ ├── AP Functions.cpp │ │ ├── ncr functions.cpp │ │ ├── odd or even fn.cpp │ │ └── power functions.cpp └── 003 - Conditionals │ └── Check lower or upper case.cpp ├── 002 - Arrays ├── 000 - Intro to array │ ├── array ele print fn.cpp │ ├── min or max in array.cpp │ ├── reverse of array.cpp │ ├── sum of array.cpp │ └── swap alternatives.cpp ├── 001 - Searching and Sorting │ ├── Lineaer search on array.cpp │ ├── Sort_0_1.cpp │ └── binary_search.cpp └── GFG_Prac_01.cpp ├── 003 - Pointer ├── Dynamic Allocation │ ├── Max elemnt in array via DMA.cpp │ ├── MultiDim Array.cpp │ ├── example 1.cpp │ └── normal DMA.cpp ├── Macros & Globar Var │ ├── global variables.cpp │ ├── inline fn.cpp │ ├── macros.cpp │ ├── macros_ex 2.cpp │ └── macros_ex 3.cpp └── Pointes │ ├── double_pointers.cpp │ ├── one_example.cpp │ ├── pointer-1.cpp │ ├── pointer_arithmetic.cpp │ ├── pointer_array.cpp │ ├── pointer_array_sum.cpp │ └── two_example.cpp ├── 004 - Recursion ├── 001 Recursion 1 │ ├── 001 Power of any Number.cpp │ ├── 002 Print Number from 0 to that number.cpp │ ├── 003 Number of Diigit.cpp │ ├── 004 Sum of Array Elements.cpp │ ├── 005 Check Numbers in Array Elements.cpp │ ├── 006 Find First Index of a Number in Array.cpp │ ├── 007 Find Last Index of a Number in array.cpp │ ├── 008 Find all the index in the array.cpp │ ├── 009 Multiplication.cpp │ ├── 010 Count Zeroes.cpp │ ├── 011 Geometric Sum.cpp │ ├── 012 Check Palindrome .cpp │ ├── 013 Sum of Digits.cpp │ ├── Extra │ │ ├── example 1.cpp │ │ ├── factorial.cpp │ │ ├── fubbonacci.cpp │ │ └── sorted or not.cpp │ └── PRACTICE 01.cpp ├── 002 Recursion 1b │ ├── 001 Replace pi (recursive).cpp │ ├── 002 Remove x.cpp │ ├── 003 Pair Star.cpp │ ├── 004 String to Number Convert.cpp │ ├── PRACTICE 01.cpp │ └── Tower of hanoi.cpp ├── 003 Recursion 2 │ ├── 001 Replace Character Recursively.cpp │ ├── 002 Remove Duplicates Recursively.cpp │ ├── 003 Merge sort algo.cpp │ ├── 004 Quick sort Algo.cpp │ ├── 005 Return subsequinces of string.cpp │ ├── 006 Return Keypad Code.cpp │ ├── 007 Print Keypad Combinations Code.cpp │ ├── 008 Check AB.cpp │ ├── 009 Staircase.cpp │ ├── 010 Binary Searcg Recursive.cpp │ ├── 011 Return subset of an array.cpp │ ├── 012 Print Subsets of Array.cpp │ ├── 013 Return subsets sum to K.cpp │ ├── 014 Return all codes.cpp │ ├── 015 Print all codes.cpp │ └── 016 Return Permutations.cpp └── Time Complexity │ ├── Array Intersection.cpp │ ├── Check Array Rotation.cpp │ ├── Duplicate in array.cpp │ ├── Find the Unique Element.cpp │ ├── PRACTICE 01.cpp │ ├── Pair sum in array.cpp │ ├── Rotate array.cpp │ └── Triplet sum.cpp ├── 005 - Linked List ├── Linked List 1 │ ├── 001 linkedlist_program.cpp │ ├── 002 input_data_1.cpp │ ├── 003 input_data_2.cpp │ ├── 004 Length of Node.cpp │ ├── 005 Print ith node.cpp │ ├── 006 - Insert node at ith Position.cpp │ ├── 007 - Delete node at ith Position.cpp │ ├── 008 - Length of LL Recursively.cpp │ ├── 009 - Insert Node Recursevely.cpp │ ├── 010 - Delete Node Recursevly.cpp │ ├── 011 - FInd a Node in LL.cpp │ ├── 012 - appendLastNToFirst in LL.cpp │ ├── 013 - Remove Duplicates element.cpp │ ├── 014 - Print Reverse LL.cpp │ ├── 015 - Check Palindrome LL.cpp │ └── node.cpp ├── Linked List 2 │ ├── 001 Midpoint of LL.cpp │ ├── 004 Reverse Linked List(Rec).cpp │ ├── 005 Reverse LL (iterative).cpp │ ├── 006 Find a Node in LL Recursive.cpp │ ├── 007 Even after Odd LL.cpp │ ├── 008 - Skip M Delete N.cpp │ ├── 009 Swap two Nodes of LL.cpp │ ├── 010 kReverse.cpp │ └── 011 Bubble Sort.cpp └── Practice 01 │ └── Prac Rec - 01.cpp ├── 006 - Stack ├── Balanced Parantheis.cpp ├── Check Redundant Brackets.cpp ├── Minimum Brackets Redundance.cpp ├── PairClass Uses.cpp ├── PairClass.cpp ├── Practice 01 │ ├── StackUsingArray.h │ ├── StackUsingLL.h │ ├── StackUsingLL.h.gch │ └── practice 01.cpp ├── Reverse a Stack.cpp ├── StackBasicExample.cpp ├── StackClassUsingArray.cpp ├── StackUsingDynamicArray.cpp └── Stock Span.cpp ├── 007 - Queues ├── Node.h ├── Practice 01 │ ├── QueueUsingArray.h │ ├── QueueUsingArray.h.gch │ ├── QueueUsingLL.h │ └── queue Uses.cpp ├── QueueUsingArray used.cpp ├── QueueUsingArray.h ├── QueueUsingArray.h.gch ├── QueueUsingLinkedList.cpp ├── QueueUsingLinkedList.h └── Reveerse a Queue.cpp ├── 008 - Trees ├── 1. Take Input.cpp ├── 10. PostOrder Traversal .cpp ├── 10. depth at Node d.cpp ├── 11. Count leaf Nodes.cpp ├── 12. Post Order Traversal.cpp ├── 13. Contains x.cpp ├── 14. Count nodes.cpp ├── 15. Node with maximum child sum.cpp ├── 16. Structurally identical.cpp ├── 17. Next larger.cpp ├── 18. Second Largest Element In Tree.cpp ├── 19. Replace with depth.cpp ├── 2. Print Output - Copy.cpp ├── 3. Take Input Level Wise.cpp ├── 4. print_level_wise.cpp ├── 5. TreeNodeUses 1,2,3,4.cpp ├── 6. Count Nodes.cpp ├── 7.Find Sum of Nodes.cpp ├── 8. Max Data Node.cpp ├── 9. Height of Tree.cpp ├── Practice 01 │ ├── TreeUses.cpp │ └── TreeUses.exe └── TreeNode1.h ├── 009 - Binary Trees ├── 001 takeInput.cpp ├── 002 printTree.cpp ├── 003 takeInputLevelWise.cpp ├── 004 printLevelWise.cpp ├── 005 Binary Tree Use Case.cpp ├── 006 Count Nodes.cpp ├── 007 find a Node.cpp ├── 008 height of a Binary tree.cpp ├── 009 Mirror a Binary Tree.cpp ├── 010 Preorder traversal.cpp ├── 011 post Order Traversal.cpp ├── 012 in Order Traversal.cpp ├── 013 Diameter First Approch.cpp ├── 014 Diameter Optimize approch.cpp ├── 015 getMaxandMIN ele.cpp ├── 016 sumOfNodes.cpp ├── 017 isBalanced.cpp ├── 018 printLevel Wise Differ.cpp ├── 019 Remove Leaf Nodes.cpp ├── 020 Construct Linked List for Each Level.cpp ├── 021. Zig Zag Tree.cpp ├── 022. Nodes without sibling.cpp ├── BinaryTreeNode.h ├── BinaryTreeNode.h.gch ├── Node.h └── Practice │ └── practice.cpp ├── 010 - BST ├── 001 Search in BST.cpp ├── 002 Print Elements in Range.cpp ├── 003 BST from Sorted Array.cpp ├── 004 Check if a Binary Tree is BST.cpp ├── 005 BST to Sorted LL.cpp ├── 006 Find Path in BST.cpp ├── 007 BST Class.cpp ├── 008 Create & Insert Duplicate Node.cpp ├── 009 LCA of Binary Tree.cpp ├── 010 LCA of BST.cpp ├── 011 Largest BST.cpp ├── 012 Replace with Sum of greater nodes.cpp ├── 013 Path Sum Root to Leaf.cpp ├── 014 Print nodes at distance k from node.cpp └── 015 Pair sum in a BST.cpp ├── 011 - Priority Queues ├── 001 Remove Min.cpp ├── 002 Max Priority Queue.cpp ├── 003 In-place heap sort.cpp ├── 004 K smallest Elements.cpp ├── 005 K largest elements.cpp ├── 006 Check Max-Heap.cpp ├── 007 Kth largest element.cpp ├── 008 Merge K sorted arrays.cpp ├── 009 Running Median.cpp └── 010 Buy the ticket.cpp ├── 012 - Hashmaps ├── 001 Maximum Frequency Number.cpp ├── 002 Print Intersection.cpp ├── 003 Pair Sum to 0.cpp ├── 004 Extract Unique characters.cpp ├── 005 Longest Consecutive Sequence.cpp ├── 006 Pairs with difference K.cpp └── 007 Longest subset zero sum.cpp ├── 013 - Tries and Huffman Coding ├── Trie.h ├── TrieNode.h └── trie_practice.cpp ├── 015 - DP ├── 001 Fibbonacci.cpp ├── 002 Min Steps to 1.cpp ├── 003 Staircase using Dp.cpp ├── 004 Minimum count.cpp ├── 005 No. of balanced BTs using DP.cpp ├── 006 Min Cost Path(Memorization).cpp ├── 007 Min Cost Path(DP).cpp ├── 008 Longest Common Subsequence.cpp ├── 009 Edit Distance.cpp ├── 010 Edit Distance (Memoization).cpp └── 011 Edit Distance (DP).cpp ├── 016 - Graphs ├── 001 DFS traversal.cpp ├── 002 BFS traversal.cpp ├── 003 disconncted_graph_BFS,DFS.cpp ├── 004 hasPath.cpp ├── Graph Practice.cpp └── Kruskals ALgorithm.cpp ├── 017 - Oops ├── OOPS 1 │ ├── complex_no_class.cpp │ ├── complex_no_program.cpp │ ├── fraction_class.cpp │ ├── fraction_program.cpp │ ├── program_1.cpp │ └── student_class.cpp └── OOPS 2 │ ├── array_dynamic_class.cpp │ ├── array_dynamic_program.cpp │ ├── fraction_class.cpp │ ├── fraction_program.cpp │ ├── polynomial_class.cpp │ ├── polynomial_program.cpp │ ├── student_class.cpp │ └── student_program.cpp └── README.md /000 Test My Self/Test Rules & Reg Code.txt: -------------------------------------------------------------------------------- 1 | 3 Questions - 90 minitues 2 | 3 | 4 | 5 | 6 | 7 | 8 | Random random = new Random(); 9 | int min = 1; 10 | int max = 30; 11 | for(int i=0;i<3;i++){ 12 | int x = random.nextInt(max) + min; 13 | System.out.println("Question: "+x); 14 | } 15 | -------------------------------------------------------------------------------- /001 - Basics of C++/001 - Loops and Functions/001 - loops/fibbonacci series.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int f_term = 0,l_term = 1; 6 | int n; 7 | cin>>n; 8 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | int sum = 0; 8 | for(int i=1;i<=n;i++){ 9 | sum+=i; 10 | } 11 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | int sum=0; 8 | while(n>0){ 9 | if(n%2==0){ 10 | sum+=n; 11 | } 12 | n--; 13 | } 14 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | for(int i=1;i<=n;i++){ 8 | for(int j=1;j<=n;j++){ 9 | cout<<" * "; 10 | } 11 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | for(int i=1;i<=n;i++){ 8 | for(int j=1;j<=n;j++){ 9 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | for(int i=1;i<=n;i++){ 8 | for(int j=1;j<=n;j++){ 9 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | for(int i=1;i<=n;i++){ 8 | for(int j=1;j<=i;j++){ 9 | cout<<"*"<<" "; 10 | } 11 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | int m=1; 8 | for(int i=1;i<=n;i++){ 9 | for(int j=1;j<=i;j++){ 10 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | for(int i=1;i<=n;i++){ 8 | for(int j=n;j>=i;j--){ 9 | cout<<"*"<<" "; 10 | } 11 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | for(int i=1;i<=n;i++){ 8 | for(int j=1;j<=n;j++){ 9 | if(i-j>0){ 10 | cout<<" "; 11 | }else{ 12 | cout<<"*"; 13 | } 14 | } 15 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | for(int i=1;i<=n;i++){ 8 | for(int j=1;j<=n;j++){ 9 | if(i-j>0){ 10 | cout<<" "; 11 | }else{ 12 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | for(int i=1;i<=n;i++){ 8 | for(int j=1;j<=n;j++){ 9 | if(i-j>0){ 10 | cout<<" "; 11 | }else{ 12 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | for(int i=1;i<=n;i++){ 8 | for(int j=1;j<=(n-i);j++){ 9 | cout<<" "; 10 | } 11 | for(int k=1;k<=i;k++){ 12 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | for(int i=1;i<=n;i++){ 8 | int lo = 1; 9 | for(int j=n-i+1;j>=1;j--){ 10 | cout<=1;k--){ 18 | if((am-1)>0){ 19 | cout<<" "; 20 | am--; 21 | }else{ 22 | cout< 2 | using namespace std; 3 | int nthAPterm(int a,int d,int n){ 4 | return (a+(n-1)*d); 5 | } 6 | int nthAPsum(int a,int d,int n){ 7 | return ((n*(n-1))/2)*(a+nthAPterm(a,d,n)); 8 | } 9 | 10 | int main() { 11 | 12 | int a,d,n; 13 | cin>>a>>d>>n; 14 | cout<<"nth Term: "< 2 | using namespace std; 3 | int fact(int x){ 4 | int res=1; 5 | 6 | while(x!=1){ 7 | res*=x; 8 | x--; 9 | } 10 | return res; 11 | } 12 | float ncr(int n,int r){ 13 | return (float)fact(n)/(fact(n-r)*fact(r)); 14 | } 15 | 16 | int main() { 17 | 18 | int n,r; 19 | cin>>n>>r; 20 | cout< 2 | using namespace std; 3 | bool oddorev(int x){ 4 | if(x%2==0){ 5 | return true; 6 | }else{ 7 | return false; 8 | } 9 | } 10 | 11 | int main() { 12 | 13 | int m; 14 | cin>>m; 15 | if(oddorev(m)){ 16 | cout<<"Even no"< 2 | using namespace std; 3 | int power(int x,int y){ 4 | int ans=1; 5 | for(int i=1;i<=y;i++){ 6 | ans*=x; 7 | } 8 | return ans; 9 | } 10 | 11 | int main() { 12 | 13 | int m,n; 14 | cin>>m>>n; 15 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | char c; 6 | cin>>c; 7 | if(c>=97&&c<=122){ 8 | cout<=65&&c<=90){ 10 | cout<=48&&c<=57){ 12 | cout< 2 | using namespace std; 3 | void printFN(int arr[],int n){ 4 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | cin>>n; 8 | int arr[n],min=0,max=0; 9 | for(int i=0;i>arr[i]; 11 | if(maxarr[i]){ 18 | min=arr[i]; 19 | } 20 | } 21 | 22 | cout<<"MIN: "< 2 | using namespace std; 3 | 4 | int main(void) { 5 | int n; 6 | cin>>n; 7 | int temp; 8 | int arr[n]; 9 | for(int i=0;i>arr[i]; 11 | } 12 | for(int i=0;i<(n/2);i++){ 13 | temp=arr[n-i-1]; 14 | arr[n-i-1]=arr[i]; 15 | arr[i]=temp; 16 | } 17 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | cin>>n; 8 | int arr[n],sum=0; 9 | for(int i=0;i>arr[i]; 11 | sum+=arr[i]; 12 | } 13 | 14 | cout<<"SUM: "< 2 | using namespace std; 3 | 4 | int main(void) { 5 | int n; 6 | cin>>n; 7 | int temp; 8 | int arr[n]; 9 | for(int i=0;i>arr[i]; 11 | } 12 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | cin>>n; 8 | int arr[n],sum=0; 9 | for(int i=0;i>arr[i]; 11 | } 12 | int ele; 13 | cin>>ele; 14 | int val=1,i; 15 | for(i=0;i 2 | using namespace std; 3 | void Sort01(int arr[],int size){ 4 | int i=0,j=size-1; 5 | int temp; 6 | while(j>=i){ 7 | while(arr[i]==0 && j>=i){ 8 | i++; 9 | } 10 | while(arr[j]==1 && j>=i){ 11 | j--; 12 | } 13 | if(j>=i){ 14 | temp=arr[i]; 15 | arr[i]=arr[j]; 16 | arr[j]=temp; 17 | } 18 | 19 | } 20 | for(int i=0;i 2 | using namespace std; 3 | int BinarySearch(int arr[],int size,int key){ 4 | int start = 0; 5 | int end = size-1; 6 | int mid; 7 | while(start<=end){ 8 | mid = (start+end)/2; 9 | if(key==arr[mid]){ 10 | return mid; 11 | } 12 | if(key>arr[mid]){ 13 | start = mid+1; 14 | }else{ 15 | end=mid-1; 16 | } 17 | } 18 | return -2; 19 | } 20 | 21 | int main() { 22 | int arr[5] = {1,2,3,4,5}; 23 | int y = BinarySearch(arr,5,25); 24 | cout<<"Element found at : "< 4 | using namespace std; 5 | int main(){ 6 | cout<<"Enter element : "; 7 | int n; 8 | cin>>n; 9 | int *p = new int[n]; 10 | for(int i=0;i>*(p+i); 12 | } 13 | int max = *p; 14 | for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | int **p = new int*[5]; 5 | for(int i=0;i<5;i++){ 6 | *(p+i)=new int[5]; 7 | for(int j=0;j<5;j++){ 8 | cin>>*(*(p+i)+j); 9 | } 10 | } 11 | cout<<"\nOutput:\n"; 12 | for(int i=0;i<5;i++){ 13 | for(int j=0;j<5;j++){ 14 | cout<<*(*(p+i)+j)<<" "; 15 | } 16 | cout< 2 | using namespace std; 3 | 4 | void func(int i, int& j, int p){ 5 | i++; 6 | j++; 7 | p++; 8 | } 9 | 10 | int main(){ 11 | int i = 10; 12 | int j = 6; 13 | int &p = i; 14 | func(i, j, p); 15 | cout << i << " " << j << " " << p; 16 | } 17 | -------------------------------------------------------------------------------- /003 - Pointer/Dynamic Allocation/normal DMA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int *p = new int; 5 | *p = 10; 6 | cout<<*p< 3 | using namespace std; 4 | int a=5; 5 | void ju(){ 6 | cout< 2 | using namespace std; 3 | 4 | int getValue(int x = 0, int y = 0, int z){ 5 | return (x + y + z); 6 | } 7 | 8 | int main(){ 9 | cout << getValue(10); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /003 - Pointer/Macros & Globar Var/macros.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define PI 3.14 4 | int main(){ 5 | int r; 6 | cin>>r; 7 | cout< 2 | using namespace std; 3 | // The macro is expanded as 2 + 3 * 3 + 5, not as 5*8 4 | #define MULTIPLY(a, b) a*b 5 | 6 | int main(){ 7 | cout << MULTIPLY(2+3, 3+5); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /003 - Pointer/Macros & Globar Var/macros_ex 3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define SQUARE(x) x*x 5 | 6 | int main(){ 7 | int x = 36 / SQUARE(6); 8 | cout << x; 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /003 - Pointer/Pointes/double_pointers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int ptr = 10; 5 | int *ptr1 = &ptr; 6 | int **ptr2 = &ptr1; 7 | 8 | cout<<*ptr2<<" "< 2 | using namespace std; 3 | 4 | int f(int x, int *py, int **ppz) { 5 | int y, z; 6 | **ppz += 1; 7 | z = **ppz; 8 | *py += 2; 9 | y = *py; 10 | x += 3; 11 | return x + y + z; 12 | } 13 | 14 | int main() { 15 | int c, *b, **a; 16 | c = 4; 17 | b = &c; 18 | a = &b; 19 | cout << f(c, b, a); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /003 - Pointer/Pointes/pointer-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int i = 10; 5 | int *p = &i; 6 | cout<<"i Value"< 2 | using namespace std; 3 | int main(){ 4 | int i=9; 5 | int *p = &i,*q=NULL; 6 | cout< 2 | using namespace std; 3 | int main(){ 4 | int arr[] = {2,3,4,5}; 5 | int *p = arr; 6 | for(int i=0;i<4;i++){ 7 | cout<<*(p+i)< 2 | using namespace std; 3 | 4 | int sum(int *p,int size){ 5 | int sum=0; 6 | for(int i=0;i 2 | using namespace std; 3 | void Q(int z) 4 | { 5 | z += z; 6 | cout< 3 | using namespace std; 4 | int power(int x, int n) { 5 | /* Don't write main(). 6 | Don't read input, it is passed as function argument. 7 | Return output and don't print it. 8 | Taking input and printing output is handled automatically. 9 | */ 10 | if(n==0){ 11 | return 1; 12 | } 13 | int ans = power(x,--n); 14 | return(x*ans); 15 | } 16 | int main(){ 17 | cout< 3 | using namespace std; 4 | void print(int n){ 5 | if(n==0){ 6 | return; 7 | } 8 | print(n-1); 9 | cout< 2 | using namespace std; 3 | int digit(int n){ 4 | if(n==0) 5 | return 0; 6 | return 1+digit(n/10); 7 | } 8 | int main(){ 9 | cout< 2 | using namespace std; 3 | 4 | int sum(int input[], int n) { 5 | 6 | 7 | // static int res = 0; 8 | // if(n==0){ 9 | // return 0; 10 | // } 11 | // if(n == 1 ){ 12 | // return res+input[0]; 13 | // } 14 | // res = res+input[0]; 15 | 16 | // return sum(input+1,n-1); 17 | 18 | if(n==0) 19 | return 0; 20 | int ans = sum(input+1,n-1); 21 | return ans+input[0]; 22 | 23 | 24 | } 25 | 26 | int main(){ 27 | int arr[] = {2,9,4,5,6}; 28 | cout< 2 | using namespace std; 3 | 4 | bool checkNumber(int input[], int size, int x) { 5 | 6 | 7 | // if(size==0){ 8 | // return false; 9 | // } 10 | // if(size == 1 && input[0]==x){ 11 | // return true; 12 | // } 13 | // if(input[0]==x){ 14 | // return true; 15 | // } 16 | // return checkNumber(input+1,size-1,x); 17 | 18 | 19 | if(size==0){ 20 | return false; 21 | } 22 | bool res = checkNumber(input+1,size-1,x); 23 | if(input[0]==x){ 24 | return true; 25 | }else{ 26 | return res; 27 | } 28 | 29 | } 30 | 31 | int main(){ 32 | int arr[] = {2,9,4,5,6}; 33 | cout< 2 | using namespace std; 3 | int firstIndex(int input[], int size, int x) { 4 | /* Don't write main(). 5 | Don't read input, it is passed as function argument. 6 | Return output and don't print it. 7 | Taking input and printing output is handled automatically. 8 | */ 9 | 10 | 11 | // if(size==0){ 12 | // return -1; 13 | // } 14 | // if(input[0]==x){ 15 | // return 0; 16 | // } 17 | // int ans = firstIndex(input+1,size-1,x); 18 | // if(ans==-1){ 19 | // return -1; 20 | // }else{ 21 | // return ans+1; 22 | // } 23 | 24 | 25 | if(size==0) 26 | return -1; 27 | int ans = firstIndex(input+1,size-1,x); 28 | if(input[0]==x) 29 | return 0; 30 | else 31 | if(ans== -1) 32 | return ans; 33 | else 34 | return ans+1; 35 | 36 | } 37 | int main(){ 38 | int arr[]={2,3,4,3,2,5}; 39 | cout< 2 | using namespace std; 3 | int lastIndex(int input[], int size, int x) { 4 | /* Don't write main(). 5 | Don't read input, it is passed as function argument. 6 | Return output and don't print it. 7 | Taking input and printing output is handled automatically. 8 | */ 9 | // if(size==0){ 10 | // return -1; 11 | // } 12 | // int ans = lastIndex(input+1,size-1,x); 13 | // if(ans==-1){ 14 | // if(input[0]==x){ 15 | // return 0; 16 | // }else{ 17 | // return -1; 18 | // } 19 | // }else{ 20 | // return ans+1; 21 | // } 22 | 23 | 24 | if(size==0){ 25 | return -1; 26 | } 27 | int ans = lastIndex(input+1,size-1,x); 28 | if(ans==-1) 29 | if(input[0]==x) 30 | return 0; 31 | else 32 | return ans; 33 | else 34 | return ans+1; 35 | 36 | 37 | } 38 | 39 | int main(){ 40 | int arr[]={2,3,4,3,2,5}; 41 | cout< 2 | using namespace std; 3 | int allIndexes(int input[], int size, int x, int output[]) { 4 | /* Don't write main(). 5 | Don't read input, it is passed as function argument. 6 | Save all the indexes in the output array passed and return the size of output array. 7 | Taking input and printing output is handled automatically. 8 | */ 9 | 10 | if(size==0) 11 | return 0; 12 | int ans = allIndexes(input+1,size-1,x,output); 13 | for(int i=0;i0;i--){ 19 | output[i]=output[i-1]; 20 | } 21 | output[0]=0; 22 | return ans+1; 23 | }else{ 24 | return ans; 25 | } 26 | 27 | 28 | } 29 | int main(){ 30 | int arr[] = {12,5,4,1,12}; 31 | int output[]={}; 32 | cout< 2 | using namespace std; 3 | int multiplyNumbers(int m, int n) { 4 | // Write your code here 5 | if(n==0) 6 | return 0; 7 | int ans = multiplyNumbers(m,--n); 8 | return ans+m; 9 | } 10 | int main(){ 11 | cout< 2 | using namespace std; 3 | 4 | int countZeros(int n) { 5 | // Write your code here 6 | if(n==0) 7 | return 1; 8 | int ans; 9 | if(n/10 == 0){ 10 | return 0; 11 | }else{ 12 | ans = countZeros(n/10); 13 | } 14 | //ans = 30056; ans = 3005 ; ans= 300 ; ans=1 30 ; ans=1 15 | if(n%10==0){ 16 | return ans+1; 17 | }else{ 18 | return ans; 19 | } 20 | 21 | } 22 | 23 | int main(){ 24 | cout< 4 | #include 5 | using namespace std; 6 | double geometricSum(int k) { 7 | // Write your code here 8 | if(k==0) 9 | return 1; 10 | double ans = geometricSum(k-1); 11 | return ans+((double)1/pow(2,k)); 12 | 13 | 14 | } 15 | 16 | int main(){ 17 | cout< 2 | #include 3 | using namespace std; 4 | bool helper(char str[],int s,int e){ 5 | 6 | if(s>=e-1){ 7 | return true; 8 | } 9 | if(str[s]!=str[e]){ 10 | return false; 11 | }else{ 12 | return helper(str,s+1,e-1); 13 | } 14 | 15 | } 16 | 17 | bool checkPalindrome(char input[]) { 18 | int start=0,end = strlen(input); 19 | return helper(input,start,end-1); 20 | } 21 | 22 | int main() { 23 | char arr[] = {"utyuuytu"}; 24 | cout < 2 | #include 3 | using namespace std; 4 | int sumOfDigits(int n) { 5 | // Write your code here 6 | if(n==0) 7 | return 0; 8 | int ans = sumOfDigits(n/10); 9 | return ans+(n%10); 10 | 11 | } 12 | 13 | 14 | int main(){ 15 | cout< 2 | using namespace std; 3 | 4 | void print(int n){ 5 | if(n < 0){ 6 | return; 7 | } 8 | if(n == 0){ 9 | cout << n << " " ; 10 | return; 11 | } 12 | print(n --); 13 | cout << n << " " ; 14 | } 15 | 16 | int main() { 17 | int num = 3; 18 | print(num); 19 | } 20 | -------------------------------------------------------------------------------- /004 - Recursion/001 Recursion 1/Extra/factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int Fact(int n){ 4 | if(n==0){ 5 | return 1; 6 | } 7 | int ans = Fact(n-1); 8 | return ans*n; 9 | } 10 | int main(){ 11 | int ko; 12 | cin>>ko; 13 | cout< 2 | using namespace std; 3 | int fib(int n){ 4 | if(n==0){ 5 | return 0; 6 | } 7 | if(n==1){ 8 | return 1; 9 | } 10 | return (fib(n-1)+fib(n-2)); 11 | } 12 | int main(){ 13 | cout< 2 | using namespace std; 3 | 4 | bool isSorted(int a[],int size){ 5 | if(size==0 || size==1){ 6 | return true; 7 | } 8 | if(a[0]>a[1]){ 9 | return false; 10 | } 11 | bool ans = isSorted(a+1,size-1); 12 | return ans; 13 | } 14 | int main(){ 15 | int arr[] = {2,9,4,5,6}; 16 | cout< 2 | #include 3 | using namespace std; 4 | int power(int x,int n){ 5 | if(n==0) return 1; 6 | return power(x,n-1)*x; 7 | } 8 | 9 | void printNum(int n){ 10 | if(n==0) return; 11 | printNum(n-1); 12 | cout<e) return true; 87 | bool ans; 88 | if(input[s]==input[e]) ans=true; 89 | else ans=false; 90 | return ans&palindrome(input+1,s+1,e-1); 91 | } 92 | 93 | int sumOfDigit(int n){ 94 | if(n==0) return 0; 95 | return (n%10)+sumOfDigit(n/10); 96 | } 97 | 98 | int main(){ 99 | int arr[] = {3,2,3,3,5}; 100 | int *output = new int[5]; 101 | char input[] = {"abcdssdcbtya"}; 102 | int n = allIndices(arr,5,3,output); 103 | for(int i=0;i 7 | #include 8 | using namespace std; 9 | 10 | void replacePi(char input[]) { 11 | // Write your code here 12 | if(strlen(input)==0 || strlen(input)==1){ 13 | return; 14 | } 15 | replacePi(input+1); 16 | if(input[0]=='p' && input[1]=='i'){ 17 | for(int i=strlen(input);i>1;i--){ 18 | input[i+1]=input[i-1]; 19 | } 20 | input[0]='3'; 21 | input[1]='.'; 22 | input[2]='1'; 23 | input[3]='4'; 24 | } 25 | 26 | } 27 | int main() { 28 | char input[]="pixxpi"; 29 | replacePi(input); 30 | cout << input << endl; 31 | } 32 | -------------------------------------------------------------------------------- /004 - Recursion/002 Recursion 1b/002 Remove x.cpp: -------------------------------------------------------------------------------- 1 | //Given a string, compute recursively a new string where all 'x' chars have been removed. 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void removeX(char input[]) { 7 | // Write your code here 8 | if(strlen(input)==0){ 9 | return; 10 | } 11 | removeX(input+1); 12 | if(input[0]=='x'){ 13 | int i=0; 14 | for(i=0;i 7 | #include 8 | using namespace std; 9 | void pairStar(char input[]) { 10 | // Write your code here 11 | if(strlen(input)==0 || strlen(input)==1){ 12 | return; 13 | } 14 | pairStar(input+1); 15 | if(input[0]==input[1]){ 16 | for(int i=strlen(input);i>0;i--){ 17 | input[i+1]=input[i]; 18 | } 19 | input[1]='*'; 20 | // input[strlen(input)]='\0'; 21 | } 22 | 23 | } 24 | 25 | int main() { 26 | char input[]="hello"; 27 | pairStar(input); 28 | cout << input << endl; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /004 - Recursion/002 Recursion 1b/004 String to Number Convert.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | int helper(char input[],int end){ 6 | if(end==0){ 7 | return (int)input[0]-48; 8 | } 9 | int ans = (int)input[end]-48; 10 | int res = helper(input,end-1); 11 | return (res*10)+ans; 12 | } 13 | int stringToNumber(char input[]) { 14 | // Write your code here 15 | 16 | return helper(input,strlen(input)-1); 17 | 18 | } 19 | 20 | 21 | int main() { 22 | char input[50]; 23 | cin >> input; 24 | cout << stringToNumber(input) << endl; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /004 - Recursion/002 Recursion 1b/PRACTICE 01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void replacePI(char input[]){ 6 | if(strlen(input)==0 || strlen(input)==1) return; 7 | replacePI(input+1); 8 | if(input[0]=='p' && input[1]=='i'){ 9 | for(int i=strlen(input);i>1;i--){ 10 | input[i+1]=input[i-1]; 11 | } 12 | input[0]='3'; 13 | input[1]='.'; 14 | input[2]='1'; 15 | input[3]='4'; 16 | } 17 | } 18 | 19 | void removeX(char input[]){ 20 | if(strlen(input)==0) return; 21 | replacePI(input+1); 22 | if(input[0]=='x'){ 23 | for(int i=0;i1;i--){ 41 | input[i]=input[i-1]; 42 | } 43 | input[1]='*'; 44 | } 45 | } 46 | 47 | int tOh(){ 48 | 49 | } 50 | 51 | int main(){ 52 | 53 | char input[] = "aaaaa"; 54 | pairStar(input,5); 55 | cout< 2 | #include 3 | using namespace std; 4 | void replaceCharacter(char input[], char c1, char c2) { 5 | /* Don't write main(). 6 | * Don't read input, it is passed as function argument. 7 | * No need to print or return the output. 8 | * Change in the given input string itself. 9 | * Taking input and printing output is handled automatically. 10 | */ 11 | 12 | if(strlen(input)==0){ 13 | return; 14 | } 15 | replaceCharacter(input+1,c1,c2); 16 | if(input[0]==c1){ 17 | input[0]=c2; 18 | } 19 | 20 | } 21 | 22 | int main() { 23 | char input[]="Hellllo"; 24 | char c1='l', c2='p'; 25 | replaceCharacter(input, c1, c2); 26 | cout << input << endl; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /004 - Recursion/003 Recursion 2/002 Remove Duplicates Recursively.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | void removeConsecutiveDuplicates(char *input) { 5 | /* Don't write main(). 6 | * Don't read input, it is passed as function argument. 7 | * Change in the given string itself. 8 | * No need to return or print anything 9 | * Taking input and printing output is handled automatically. 10 | */ 11 | if(strlen(input)<=1){ 12 | return; 13 | } 14 | removeConsecutiveDuplicates(input+1); 15 | if(input[0]==input[1]){ 16 | for(int i=1;i 2 | using namespace std; 3 | int helloStSequences(string input,string* output){ 4 | if(input.size()==0){ 5 | output[0]=""; 6 | return 1; 7 | } 8 | string str = input.substr(1); 9 | int temp = helloStSequences(str,output); 10 | for(int i=0;i>input; 18 | string* output = new string[1000]; 19 | int count = helloStSequences(input,output); 20 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int keypad(int num, string output[]){ 7 | 8 | 9 | string key[]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 10 | 11 | if(num==0){ 12 | output[0]=""; 13 | return 1; 14 | } 15 | int last = num%10; 16 | int smallOutputSize = keypad(num/10,output); 17 | 18 | // Copy output array 19 | string* temp = new string[smallOutputSize]; 20 | for(int i=0;i> num; 35 | 36 | string output[10000]; 37 | int count = keypad(num, output); 38 | for(int i = 0; i < count && i < 10000; i++){ 39 | cout << output[i] << endl; 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /004 - Recursion/003 Recursion 2/007 Print Keypad Combinations Code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int keypad(int num, string output[]){ 7 | /* Insert all the possible combinations of the integer number into the output string array. You do not need to 8 | print anything, just return the number of strings inserted into the array. 9 | */ 10 | string key[]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 11 | 12 | if(num==0){ 13 | output[0]=""; 14 | return 1; 15 | } 16 | int last = num%10; 17 | int smallOutputSize = keypad(num/10,output); 18 | 19 | // Copy output array 20 | string* temp = new string[smallOutputSize]; 21 | for(int i=0;i> num; 51 | 52 | printKeypad(num); 53 | 54 | return 0; 55 | } 56 | 57 | -------------------------------------------------------------------------------- /004 - Recursion/003 Recursion 2/008 Check AB.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maityamit/Cpp-DSA-Notes-and-Programs/c3039713ff2305a787b11987e539997f0849f12b/004 - Recursion/003 Recursion 2/008 Check AB.cpp -------------------------------------------------------------------------------- /004 - Recursion/003 Recursion 2/009 Staircase.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maityamit/Cpp-DSA-Notes-and-Programs/c3039713ff2305a787b11987e539997f0849f12b/004 - Recursion/003 Recursion 2/009 Staircase.cpp -------------------------------------------------------------------------------- /004 - Recursion/003 Recursion 2/010 Binary Searcg Recursive.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maityamit/Cpp-DSA-Notes-and-Programs/c3039713ff2305a787b11987e539997f0849f12b/004 - Recursion/003 Recursion 2/010 Binary Searcg Recursive.cpp -------------------------------------------------------------------------------- /004 - Recursion/003 Recursion 2/011 Return subset of an array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int subset(int input[], int n, int output[][20]) { 5 | 6 | if(n==0){ 7 | output[0][0]=0; 8 | return 1; 9 | } 10 | int ans = subset(input+1,n-1,output); 11 | for(int i=0;i> length; 30 | for(int i=0; i < length; i++){ 31 | cin >> input[i]; 32 | } 33 | 34 | 35 | 36 | 37 | int size = subset(input, length, output); 38 | for( int i = 0; i < size; i++) { 39 | for( int j = 1; j <= output[i][0]; j++) { 40 | cout << output[i][j] << " "; 41 | } 42 | cout << endl; 43 | } 44 | 45 | return 0; 46 | 47 | } 48 | 49 | -------------------------------------------------------------------------------- /004 - Recursion/003 Recursion 2/012 Print Subsets of Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int helper(int input[], int n,int output[][20]){ 5 | if(n==0) { 6 | output[0][0]=0; 7 | return 1; 8 | } 9 | 10 | int smallOutput = helper(input+1,n-1,output); 11 | for(int i=smallOutput;i<2*smallOutput;i++) 12 | { 13 | output[i][0]=output[i-smallOutput][0]+1; 14 | output[i][1]=input[0]; 15 | } 16 | for(int i=smallOutput;i<2*smallOutput;i++) 17 | { 18 | for(int j=2;j<=output[i][0];j++) 19 | { 20 | output[i][j]=output[i-smallOutput][j-1]; 21 | } 22 | } 23 | 24 | 25 | return 2*smallOutput; 26 | 27 | } 28 | void printSubsetsOfArray(int input[], int size) { 29 | // Write your code here 30 | int output[35000][20]; 31 | int z = helper(input,size,output); 32 | for( int i = 0; i < z; i++) { 33 | for( int j = 1; j <= output[i][0]; j++) { 34 | cout << output[i][j] << " "; 35 | } 36 | cout << endl; 37 | } 38 | 39 | } 40 | 41 | int main() { 42 | int input[1000],length; 43 | cin >> length; 44 | for(int i=0; i < length; i++) 45 | cin >> input[i]; 46 | printSubsetsOfArray(input, length); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /004 - Recursion/003 Recursion 2/013 Return subsets sum to K.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maityamit/Cpp-DSA-Notes-and-Programs/c3039713ff2305a787b11987e539997f0849f12b/004 - Recursion/003 Recursion 2/013 Return subsets sum to K.cpp -------------------------------------------------------------------------------- /004 - Recursion/003 Recursion 2/014 Return all codes.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maityamit/Cpp-DSA-Notes-and-Programs/c3039713ff2305a787b11987e539997f0849f12b/004 - Recursion/003 Recursion 2/014 Return all codes.cpp -------------------------------------------------------------------------------- /004 - Recursion/003 Recursion 2/015 Print all codes.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maityamit/Cpp-DSA-Notes-and-Programs/c3039713ff2305a787b11987e539997f0849f12b/004 - Recursion/003 Recursion 2/015 Print all codes.cpp -------------------------------------------------------------------------------- /004 - Recursion/003 Recursion 2/016 Return Permutations.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maityamit/Cpp-DSA-Notes-and-Programs/c3039713ff2305a787b11987e539997f0849f12b/004 - Recursion/003 Recursion 2/016 Return Permutations.cpp -------------------------------------------------------------------------------- /004 - Recursion/Time Complexity/Array Intersection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void intersection(int *arr1, int *arr2, int n, int m) 3 | { 4 | //Write your code here 5 | sort(arr1,arr1+n); 6 | sort(arr2,arr2+m); 7 | int i=0,j=0; 8 | while(i 2 | int arrayRotateCheck(int *input, int size) 3 | { 4 | 5 | 6 | int min=input[0]; 7 | int temp = 0; 8 | 9 | for(int i=1;iinput[i]){ 11 | min = input[i]; 12 | temp = i; 13 | } 14 | } 15 | return temp; 16 | 17 | } 18 | -------------------------------------------------------------------------------- /004 - Recursion/Time Complexity/Duplicate in array.cpp: -------------------------------------------------------------------------------- 1 | int findDuplicate(int *arr, int n) 2 | { 3 | //Write your code here 4 | int sum =0,sum2=0; 5 | for(int i=0;i 2 | using namespace std; 3 | 4 | int findtheDuplicate(int arr[],int n){ 5 | int res = arr[0]; 6 | for(int i=1;i 2 | int pairSum(int *arr, int n, int num) 3 | { 4 | int count = 0; 5 | unordered_map map; 6 | for(int i=0;i 2 | 3 | void rotate(int *input, int d, int n) 4 | { 5 | //Write your code here 6 | reverse(input,input+d); 7 | reverse(input+d,input+n); 8 | reverse(input,input+n); 9 | 10 | } 11 | -------------------------------------------------------------------------------- /004 - Recursion/Time Complexity/Triplet sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int pairSum(int *arr, int start, int end, int num) { 4 | int count = 0; 5 | for(; start < end;) { 6 | if(arr[start] + arr[end] < num) { 7 | start++; 8 | } else if (arr[start] + arr[end] > num) { 9 | end--; 10 | } else { 11 | int start_element = arr[start]; 12 | int end_element = arr[end]; 13 | 14 | //if all the elements are same 15 | if(start_element == end_element) { 16 | int total = (end - start) + 1; 17 | count += ((total ) * (total - 1) ) / 2; // n choose 2; 18 | return count; 19 | } 20 | 21 | int start_temp = start; 22 | int end_temp = end; 23 | 24 | for(;start_temp <= end_temp and arr[start_temp] == start_element;) { 25 | start_temp++; 26 | } 27 | 28 | for(;start_temp <= end_temp and arr[end_temp] == end_element;) { 29 | end_temp--; 30 | } 31 | 32 | count += (start_temp - start) * (end - end_temp); 33 | start = start_temp; 34 | end = end_temp; 35 | } 36 | } 37 | 38 | return count; 39 | } 40 | int tripletSum(int *arr, int n, int num) 41 | { 42 | sort(arr, arr + n); 43 | int numTriplets = 0; 44 | for (int i = 0; i < n; i++) { 45 | int pairSumFor = num - arr[i]; 46 | int numPairs = pairSum(arr, (i + 1), (n - 1), pairSumFor); 47 | numTriplets += numPairs; 48 | } 49 | return numTriplets; 50 | } 51 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 1/001 linkedlist_program.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "node.cpp" 3 | using namespace std; 4 | void print(Node *head){ 5 | //Sure make a Temporary Node Pointer to make sure not to lost head 6 | Node *temp = head; 7 | while(temp != NULL){ 8 | cout<data<<" "; 9 | temp = temp->next; 10 | } 11 | cout< 2 | #include "node.cpp" 3 | using namespace std; 4 | 5 | // Time Complexity of this Fn. is O(n^2)-> 6 | Node *takeInput(){ 7 | int data; 8 | cin>>data; 9 | Node *head=NULL; 10 | while(data != -1){ 11 | //Here we declare dynamic type 12 | //caz if we declare statically we 13 | //can loose our data 14 | Node *n = new Node(data); 15 | if(head==NULL){ 16 | head=n; 17 | }else{ 18 | Node *temp = head; 19 | while(temp->next != NULL){ 20 | temp = temp->next; 21 | } 22 | temp->next = n; 23 | } 24 | cin>>data; 25 | } 26 | return head; 27 | } 28 | void print(Node *head){ 29 | //Sure make a Temporary Node Pointer to make sure not to lost head 30 | Node *temp = head; 31 | while(temp != NULL){ 32 | cout<data<<" "; 33 | temp = temp->next; 34 | } 35 | cout< 2 | #include "node.cpp" 3 | using namespace std; 4 | 5 | // Time Complexity of this Fn. is O(n)-> 6 | Node *takeInput2(){ 7 | int data; 8 | cin>>data; 9 | Node *head=NULL; 10 | Node *tail=NULL; 11 | while(data != -1){ 12 | //Here we declare dynamic type 13 | //caz if we declare statically we 14 | //can loose our data 15 | Node *n = new Node(data); 16 | if(head==NULL){ 17 | head=n; 18 | tail=n; 19 | }else{ 20 | tail->next = n; 21 | tail = tail->next; 22 | } 23 | cin>>data; 24 | } 25 | return head; 26 | } 27 | void print(Node *head){ 28 | //Sure make a Temporary Node Pointer to make sure not to lost head 29 | Node *temp = head; 30 | while(temp != NULL){ 31 | cout<data<<" "; 32 | temp = temp->next; 33 | } 34 | cout< 2 | 3 | class Node 4 | { 5 | public: 6 | int data; 7 | Node *next; 8 | Node(int data) 9 | { 10 | this->data = data; 11 | this->next = NULL; 12 | } 13 | }; 14 | 15 | using namespace std; 16 | 17 | 18 | Node *takeinput() 19 | { 20 | int data; 21 | cin >> data; 22 | Node *head = NULL, *tail = NULL; 23 | while (data != -1) 24 | { 25 | Node *newNode = new Node(data); 26 | if (head == NULL) 27 | { 28 | head = newNode; 29 | tail = newNode; 30 | } 31 | else 32 | { 33 | tail->next = newNode; 34 | tail = newNode; 35 | } 36 | cin >> data; 37 | } 38 | return head; 39 | } 40 | int length(Node *head) 41 | { 42 | //Write your code here 43 | Node *temp = head; 44 | int count=0; 45 | while(temp != NULL){ 46 | count++; 47 | temp = temp->next; 48 | } 49 | return count; 50 | } 51 | 52 | int main() 53 | { 54 | int t; 55 | cin >> t; 56 | while (t--) 57 | { 58 | Node *head = takeinput(); 59 | cout << length(head) << endl; 60 | } 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 1/005 Print ith node.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Node 4 | { 5 | public: 6 | int data; 7 | Node *next; 8 | Node(int data) 9 | { 10 | this->data = data; 11 | this->next = NULL; 12 | } 13 | }; 14 | 15 | using namespace std; 16 | 17 | Node *takeinput() 18 | { 19 | int data; 20 | cin >> data; 21 | Node *head = NULL, *tail = NULL; 22 | while (data != -1) 23 | { 24 | Node *newNode = new Node(data); 25 | if (head == NULL) 26 | { 27 | head = newNode; 28 | tail = newNode; 29 | } 30 | else 31 | { 32 | tail->next = newNode; 33 | tail = newNode; 34 | } 35 | cin >> data; 36 | } 37 | return head; 38 | } 39 | void printIthNode(Node *head, int i) 40 | { 41 | //Write your code here 42 | Node *temp = head; 43 | int m=0; 44 | while(temp != NULL && mnext; 46 | m++; 47 | } 48 | if(temp != NULL){ 49 | cout<data; 50 | } 51 | } 52 | int main() 53 | { 54 | int t; 55 | cin >> t; 56 | while (t--) 57 | { 58 | Node *head = takeinput(); 59 | int pos; 60 | cin >> pos; 61 | printIthNode(head, pos); 62 | cout << endl; 63 | } 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 1/006 - Insert node at ith Position.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "node.cpp" 3 | using namespace std; 4 | 5 | // Time Complexity of this Fn. is O(n)-> 6 | Node *takeInput2(){ 7 | int data; 8 | cin>>data; 9 | Node *head=NULL; 10 | Node *tail=NULL; 11 | while(data != -1){ 12 | //Here we declare dynamic type 13 | //caz if we declare statically we 14 | //can loose our data 15 | Node *n = new Node(data); 16 | if(head==NULL){ 17 | head=n; 18 | tail=n; 19 | }else{ 20 | tail->next = n; 21 | tail = tail->next; 22 | } 23 | cin>>data; 24 | } 25 | return head; 26 | } 27 | Node* insertNode(Node *head,int position , int data){ 28 | Node *n = new Node(data); 29 | if(position==0){ 30 | n->next = head; 31 | head = n; 32 | return head; 33 | } 34 | int count = 0; 35 | Node *temp = head; 36 | while(temp!= NULL && countnext; 38 | count++; 39 | } 40 | if(temp!=NULL){ 41 | n->next = temp->next; 42 | temp->next = n; 43 | } 44 | return head; 45 | 46 | } 47 | void print(Node *head){ 48 | //Sure make a Temporary Node Pointer to make sure not to lost head 49 | Node *temp = head; 50 | while(temp != NULL){ 51 | cout<data<<" "; 52 | temp = temp->next; 53 | } 54 | cout< 2 | #include "node.cpp" 3 | using namespace std; 4 | 5 | // Time Complexity of this Fn. is O(n)-> 6 | Node *takeInput2(){ 7 | int data; 8 | cin>>data; 9 | Node *head=NULL; 10 | Node *tail=NULL; 11 | while(data != -1){ 12 | //Here we declare dynamic type 13 | //caz if we declare statically we 14 | //can loose our data 15 | Node *n = new Node(data); 16 | if(head==NULL){ 17 | head=n; 18 | tail=n; 19 | }else{ 20 | tail->next = n; 21 | tail = tail->next; 22 | } 23 | cin>>data; 24 | } 25 | return head; 26 | } 27 | Node* insertNode(Node *head,int position , int data){ 28 | Node *n = new Node(data); 29 | if(position==0){ 30 | n->next = head; 31 | head = n; 32 | return head; 33 | } 34 | int count = 0; 35 | Node *temp = head; 36 | while(temp!= NULL && countnext; 38 | count++; 39 | } 40 | if(temp!=NULL){ 41 | n->next = temp->next; 42 | temp->next = n; 43 | } 44 | return head; 45 | 46 | } 47 | Node *deleteNode(Node *head,int position){ 48 | if(position==0){ 49 | Node *temp = head; 50 | head = head->next; 51 | delete temp; 52 | return head; 53 | } 54 | int count = 0 ; 55 | Node *temp = head; 56 | while(temp!=NULL && countnext; 58 | count++; 59 | } 60 | if(temp != NULL){ 61 | Node *res = temp->next; 62 | temp->next = res->next; 63 | delete res; 64 | } 65 | return head; 66 | } 67 | void print(Node *head){ 68 | //Sure make a Temporary Node Pointer to make sure not to lost head 69 | Node *temp = head; 70 | while(temp != NULL){ 71 | cout<data<<" "; 72 | temp = temp->next; 73 | } 74 | cout< 2 | #include "node.cpp" 3 | using namespace std; 4 | 5 | 6 | int length(Node *head) { 7 | // Write your code here 8 | if(head==NULL){ 9 | return 0; 10 | } 11 | int ans = length(head->next); 12 | return ans+1; 13 | } 14 | Node *takeinput() { 15 | int data; 16 | cin >> data; 17 | Node *head = NULL, *tail = NULL; 18 | while (data != -1) { 19 | Node *newNode = new Node(data); 20 | if (head == NULL) { 21 | head = newNode; 22 | tail = newNode; 23 | } else { 24 | tail->next = newNode; 25 | tail = newNode; 26 | } 27 | cin >> data; 28 | } 29 | return head; 30 | } 31 | 32 | void print(Node *head) { 33 | Node *temp = head; 34 | 35 | while (temp != NULL) { 36 | cout << temp->data << " "; 37 | temp = temp->next; 38 | } 39 | 40 | cout << endl; 41 | } 42 | 43 | int main() { 44 | int t; 45 | cin >> t; 46 | 47 | while (t--) { 48 | Node *head = takeinput(); 49 | cout << length(head) << "\n"; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 1/009 - Insert Node Recursevely.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "node.cpp" 3 | using namespace std; 4 | 5 | Node* insertNode(Node *head, int i, int data) { 6 | // Write your code here 7 | // Write your code here 8 | if(head==NULL){ 9 | return head; 10 | } 11 | 12 | if(i==0){ 13 | Node *temp = new Node(data); 14 | temp->next = head; 15 | return temp; 16 | } 17 | Node *hello = insertNode(head->next,i-1,data); 18 | head -> next = hello; 19 | return head; 20 | } 21 | Node *takeinput() { 22 | int data; 23 | cin >> data; 24 | Node *head = NULL, *tail = NULL; 25 | while (data != -1) { 26 | Node *newNode = new Node(data); 27 | if (head == NULL) { 28 | head = newNode; 29 | tail = newNode; 30 | } else { 31 | tail->next = newNode; 32 | tail = newNode; 33 | } 34 | cin >> data; 35 | } 36 | return head; 37 | } 38 | 39 | void print(Node *head) { 40 | Node *temp = head; 41 | 42 | while (temp != NULL) { 43 | cout << temp->data << " "; 44 | temp = temp->next; 45 | } 46 | 47 | cout << "\n"; 48 | } 49 | 50 | int main() { 51 | int t; 52 | cin >> t; 53 | 54 | while (t--) { 55 | Node *head = takeinput(); 56 | int pos, data; 57 | cin >> pos >> data; 58 | head = insertNode(head, pos, data); 59 | print(head); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 1/010 - Delete Node Recursevly.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "node.cpp" 3 | using namespace std; 4 | 5 | Node *deleteNodeRec(Node *head, int pos) { 6 | //Write your code here 7 | if(head==NULL){ 8 | return head; 9 | } 10 | if(pos==0){ 11 | Node *temp = head; 12 | head = temp->next; 13 | delete temp; 14 | return head; 15 | } 16 | Node *res = deleteNodeRec(head->next,pos-1); 17 | head->next = res; 18 | return head; 19 | } 20 | 21 | Node *takeinput() 22 | { 23 | int data; 24 | cin >> data; 25 | Node *head = NULL, *tail = NULL; 26 | while (data != -1) 27 | { 28 | Node *newNode = new Node(data); 29 | if (head == NULL) 30 | { 31 | head = newNode; 32 | tail = newNode; 33 | } 34 | else 35 | { 36 | tail->next = newNode; 37 | tail = newNode; 38 | } 39 | cin >> data; 40 | } 41 | return head; 42 | } 43 | 44 | void print(Node *head) 45 | { 46 | Node *temp = head; 47 | while (temp != NULL) 48 | { 49 | cout << temp->data << " "; 50 | temp = temp->next; 51 | } 52 | cout << endl; 53 | } 54 | 55 | int main() 56 | { 57 | int t; 58 | cin >> t; 59 | 60 | while (t--) 61 | { 62 | Node *head = takeinput(); 63 | int pos; 64 | cin >> pos; 65 | head = deleteNodeRec(head, pos); 66 | print(head); 67 | } 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 1/011 - FInd a Node in LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "node.cpp" 3 | using namespace std; 4 | 5 | int findNode(Node *head, int n){ 6 | // Write your code here. 7 | if(head==NULL){ 8 | return -1; 9 | } 10 | int ans = findNode(head->next,n); 11 | if(ans == -1){ 12 | if(head->data == n){ 13 | return ans+1; 14 | }else{ 15 | return ans; 16 | } 17 | }else{ 18 | return ans+1; 19 | } 20 | 21 | } 22 | 23 | Node *takeinput() 24 | { 25 | int data; 26 | cin >> data; 27 | Node *head = NULL, *tail = NULL; 28 | while (data != -1) 29 | { 30 | Node *newNode = new Node(data); 31 | if (head == NULL) 32 | { 33 | head = newNode; 34 | tail = newNode; 35 | } 36 | else 37 | { 38 | tail->next = newNode; 39 | tail = newNode; 40 | } 41 | cin >> data; 42 | } 43 | return head; 44 | } 45 | 46 | int main() 47 | { 48 | int t; 49 | cin >> t; 50 | while (t--) 51 | { 52 | Node *head = takeinput(); 53 | int val; 54 | cin >> val; 55 | cout << findNode(head, val) << endl; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 1/012 - appendLastNToFirst in LL.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include "node.cpp" 4 | using namespace std; 5 | 6 | Node *appendLastNToFirst(Node *head, int n) 7 | { 8 | //Write your code here 9 | // For calculating the length 10 | if(head==NULL || n==0){ 11 | return head; 12 | } 13 | Node *temp_l = head; 14 | int count =0; 15 | while(temp_l->next!=NULL){ 16 | temp_l = temp_l->next; 17 | count++; 18 | } 19 | count++; 20 | Node *temp = head; 21 | for(int i=0;inext; 23 | } 24 | temp_l->next = head; 25 | head = temp->next; 26 | temp->next = NULL; 27 | return head; 28 | 29 | } 30 | 31 | Node *takeinput() 32 | { 33 | int data; 34 | cin >> data; 35 | Node *head = NULL, *tail = NULL; 36 | while (data != -1) 37 | { 38 | Node *newnode = new Node(data); 39 | if (head == NULL) 40 | { 41 | head = newnode; 42 | tail = newnode; 43 | } 44 | else 45 | { 46 | tail->next = newnode; 47 | tail = newnode; 48 | } 49 | cin >> data; 50 | } 51 | return head; 52 | } 53 | 54 | void print(Node *head) 55 | { 56 | Node *temp = head; 57 | while (temp != NULL) 58 | { 59 | cout << temp->data << " "; 60 | temp = temp->next; 61 | } 62 | cout << endl; 63 | } 64 | 65 | int main() 66 | { 67 | int t; 68 | cin >> t; 69 | while (t--) 70 | { 71 | Node *head = takeinput(); 72 | int n; 73 | cin >> n; 74 | head = appendLastNToFirst(head, n); 75 | print(head); 76 | } 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 1/013 - Remove Duplicates element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "node.cpp" 3 | using namespace std; 4 | 5 | Node *removeDuplicates(Node *head) 6 | { 7 | //Write your code here 8 | if(head==NULL || head->next==NULL){ 9 | return head; 10 | } 11 | Node *temp = head; 12 | while(temp->next!=NULL){ 13 | Node *temp1 = temp->next; 14 | if(temp->data == temp1->data){ 15 | temp->next = temp1->next; 16 | }else{ 17 | temp = temp->next; 18 | } 19 | } 20 | return head; 21 | } 22 | Node *takeinput() 23 | { 24 | int data; 25 | cin >> data; 26 | Node *head = NULL, *tail = NULL; 27 | while (data != -1) 28 | { 29 | Node *newnode = new Node(data); 30 | if (head == NULL) 31 | { 32 | head = newnode; 33 | tail = newnode; 34 | } 35 | else 36 | { 37 | tail->next = newnode; 38 | tail = newnode; 39 | } 40 | cin >> data; 41 | } 42 | return head; 43 | } 44 | 45 | void print(Node *head) 46 | { 47 | Node *temp = head; 48 | while (temp != NULL) 49 | { 50 | cout << temp->data << " "; 51 | temp = temp->next; 52 | } 53 | cout << endl; 54 | } 55 | 56 | int main() 57 | { 58 | int t; 59 | cin >> t; 60 | while (t--) 61 | { 62 | Node *head = takeinput(); 63 | head = removeDuplicates(head); 64 | print(head); 65 | } 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 1/014 - Print Reverse LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "node.cpp" 3 | using namespace std; 4 | void printReverse(Node *head) 5 | { 6 | if(head==NULL){ 7 | return; 8 | } 9 | printReverse(head->next); 10 | cout<data<<" "; 11 | } 12 | 13 | Node *takeinput() 14 | { 15 | int data; 16 | cin >> data; 17 | Node *head = NULL, *tail = NULL; 18 | while (data != -1) 19 | { 20 | Node *newnode = new Node(data); 21 | if (head == NULL) 22 | { 23 | head = newnode; 24 | tail = newnode; 25 | } 26 | else 27 | { 28 | tail->next = newnode; 29 | tail = newnode; 30 | } 31 | cin >> data; 32 | } 33 | return head; 34 | } 35 | 36 | int main() 37 | { 38 | int t; 39 | cin >> t; 40 | while (t--) 41 | { 42 | Node *head = takeinput(); 43 | printReverse(head); 44 | cout << endl; 45 | } 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 1/015 - Check Palindrome LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "node.cpp" 3 | using namespace std; 4 | 5 | bool isPalindrome(Node *head) 6 | { 7 | //Write your code here 8 | int count = 0; 9 | Node *temp = head; 10 | while(temp!=NULL){ 11 | temp = temp->next; 12 | count++; 13 | } 14 | int *arr = new int[count]; 15 | int *arr1 = new int[count]; 16 | Node *temp1 = head; 17 | for(int i=count-1;i>=0;i--){ 18 | arr[i]=temp1->data; 19 | arr1[count-1-i] = temp1->data; 20 | temp1 = temp1->next; 21 | } 22 | bool res = true; 23 | for(int i=0;i> data; 35 | Node *head = NULL, *tail = NULL; 36 | while (data != -1) 37 | { 38 | Node *newnode = new Node(data); 39 | if (head == NULL) 40 | { 41 | head = newnode; 42 | tail = newnode; 43 | } 44 | else 45 | { 46 | tail->next = newnode; 47 | tail = newnode; 48 | } 49 | cin >> data; 50 | } 51 | return head; 52 | } 53 | 54 | int main() 55 | { 56 | int t; 57 | cin >> t; 58 | 59 | while (t--) 60 | { 61 | Node *head = takeinput(); 62 | bool ans = isPalindrome(head); 63 | 64 | if (ans) cout << "true"; 65 | else cout << "false"; 66 | 67 | cout << endl; 68 | } 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 1/node.cpp: -------------------------------------------------------------------------------- 1 | class Node{ 2 | public: 3 | int data; 4 | Node *next; 5 | Node(int data){ 6 | this->data = data; 7 | this->next = NULL; 8 | } 9 | 10 | }; 11 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 2/001 Midpoint of LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node *next; 9 | Node(int data) 10 | { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | 17 | Node *midPoint(Node *head) 18 | { 19 | // Write your code here 20 | if(head==NULL){ 21 | return head; 22 | } 23 | Node *slow = head; 24 | Node *fast = head->next; 25 | while(fast!= NULL && fast->next!=NULL){ 26 | slow = slow->next; 27 | fast = fast->next->next; 28 | } 29 | return slow; 30 | 31 | 32 | } 33 | Node *takeinput() 34 | { 35 | int data; 36 | cin >> data; 37 | Node *head = NULL, *tail = NULL; 38 | while (data != -1) 39 | { 40 | Node *newnode = new Node(data); 41 | if (head == NULL) 42 | { 43 | head = newnode; 44 | tail = newnode; 45 | } 46 | else 47 | { 48 | tail->next = newnode; 49 | tail = newnode; 50 | } 51 | cin >> data; 52 | } 53 | return head; 54 | } 55 | 56 | int main() 57 | { 58 | int t; 59 | cin >> t; 60 | while (t--) 61 | { 62 | Node *head = takeinput(); 63 | Node *mid = midPoint(head); 64 | if (mid != NULL) 65 | { 66 | cout << mid->data; 67 | } 68 | cout << endl; 69 | } 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 2/004 Reverse Linked List(Rec).cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | Following is the class structure of the Node class: 4 | 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node *next; 10 | Node(int data) 11 | { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | }; 16 | 17 | *****************************************************************/ 18 | 19 | int findNodeRec(Node *head, int n) 20 | { 21 | //Write your code here 22 | if(head==NULL){ 23 | return -1; 24 | } 25 | int ans = findNodeRec(head->next,n); 26 | if(head->data==n){ 27 | return 0; 28 | }else{ 29 | if(ans==-1){ 30 | return ans; 31 | }else{ 32 | return ans+1; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 2/005 Reverse LL (iterative).cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | Following is the class structure of the Node class: 4 | 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node *next; 10 | Node(int data) 11 | { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | }; 16 | 17 | *****************************************************************/ 18 | 19 | Node *reverseLinkedList(Node *head) { 20 | // Write your code here 21 | // Node *prev = NULL; 22 | // Node *curr = head; 23 | // Node *n = head; 24 | // while(curr!=NULL){ 25 | // n = curr->next; 26 | // curr->next = prev; 27 | // prev=curr; 28 | // curr = n; 29 | // } 30 | // return prev; 31 | 32 | 33 | Node *c = head; 34 | Node *prev = NULL; 35 | Node *n = NULL; 36 | while(c!=NULL){ 37 | n = c->next; 38 | c->next = prev; 39 | prev = c; 40 | c = n; 41 | } 42 | 43 | return prev; 44 | 45 | } 46 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 2/006 Find a Node in LL Recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Node 4 | { 5 | public: 6 | int data; 7 | Node *next; 8 | Node(int data) 9 | { 10 | this->data = data; 11 | this->next = NULL; 12 | } 13 | }; 14 | 15 | using namespace std; 16 | 17 | Node *takeinput() 18 | { 19 | int data; 20 | cin >> data; 21 | Node *head = NULL, *tail = NULL; 22 | while (data != -1) 23 | { 24 | Node *newNode = new Node(data); 25 | if (head == NULL) 26 | { 27 | head = newNode; 28 | tail = newNode; 29 | } 30 | else 31 | { 32 | tail->next = newNode; 33 | tail = newNode; 34 | } 35 | cin >> data; 36 | } 37 | return head; 38 | } 39 | int findNodeRec(Node *head, int n) 40 | { 41 | //Write your code here 42 | if(head==NULL){ 43 | return -1; 44 | } 45 | int ans = findNodeRec(head->next,n); 46 | if(head->data==n){ 47 | return 0; 48 | }else{ 49 | if(ans==-1){ 50 | return ans; 51 | }else{ 52 | return ans+1; 53 | } 54 | } 55 | } 56 | 57 | int main() 58 | { 59 | int t; 60 | cin >> t; 61 | while (t--) 62 | { 63 | Node *head = takeinput(); 64 | int val; 65 | cin >> val; 66 | cout << findNodeRec(head, val) << endl; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 2/008 - Skip M Delete N.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Node 4 | { 5 | public: 6 | int data; 7 | Node *next; 8 | Node(int data) 9 | { 10 | this->data = data; 11 | this->next = NULL; 12 | } 13 | }; 14 | 15 | using namespace std; 16 | 17 | 18 | 19 | Node *skipMdeleteN(Node *head, int M, int N) 20 | { 21 | int c1=1,c2=1; 22 | 23 | Node *t1 = head; 24 | Node *t2 = NULL; 25 | 26 | if(M==0){ 27 | return NULL; 28 | } 29 | 30 | if(N == 0){ 31 | return head; 32 | } 33 | 34 | while(t1!=NULL){ 35 | while(c1next; 37 | c1++; 38 | } 39 | if(t1!=NULL){ 40 | t2 = t1->next; 41 | while(c2next; 43 | c2++; 44 | } 45 | if(t2==NULL){ 46 | t1->next = NULL; 47 | break; 48 | }else{ 49 | t1->next = t2->next; 50 | t1=t2->next; 51 | c1=1; 52 | c2=1; 53 | } 54 | 55 | }else{ 56 | break; 57 | } 58 | 59 | } 60 | 61 | return head; 62 | 63 | 64 | } 65 | 66 | Node *takeinput() 67 | { 68 | int data; 69 | cin >> data; 70 | Node *head = NULL, *tail = NULL; 71 | while (data != -1) 72 | { 73 | Node *newnode = new Node(data); 74 | if (head == NULL) 75 | { 76 | head = newnode; 77 | tail = newnode; 78 | } 79 | else 80 | { 81 | tail->next = newnode; 82 | tail = newnode; 83 | } 84 | cin >> data; 85 | } 86 | return head; 87 | } 88 | 89 | void print(Node *head) 90 | { 91 | Node *temp = head; 92 | while (temp != NULL) 93 | { 94 | cout << temp->data << " "; 95 | temp = temp->next; 96 | } 97 | cout << endl; 98 | } 99 | 100 | int main() 101 | { 102 | int t; 103 | cin >> t; 104 | while (t--) 105 | { 106 | Node *head = takeinput(); 107 | int m, n; 108 | cin >> m >> n; 109 | head = skipMdeleteN(head, m, n); 110 | print(head); 111 | } 112 | return 0; 113 | } 114 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 2/009 Swap two Nodes of LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Node 4 | { 5 | public: 6 | int data; 7 | Node *next; 8 | Node(int data) 9 | { 10 | this->data = data; 11 | this->next = NULL; 12 | } 13 | }; 14 | 15 | using namespace std; 16 | 17 | Node *takeinput() 18 | { 19 | int data; 20 | cin >> data; 21 | Node *head = NULL, *tail = NULL; 22 | while (data != -1) 23 | { 24 | Node *newnode = new Node(data); 25 | if (head == NULL) 26 | { 27 | head = newnode; 28 | tail = newnode; 29 | } 30 | else 31 | { 32 | tail->next = newnode; 33 | tail = newnode; 34 | } 35 | cin >> data; 36 | } 37 | return head; 38 | } 39 | 40 | void print(Node *head) 41 | { 42 | Node *temp = head; 43 | while (temp != NULL) 44 | { 45 | cout << temp->data << " "; 46 | temp = temp->next; 47 | } 48 | cout << endl; 49 | } 50 | Node *swapNodes(Node *head, int i, int j) 51 | { 52 | Node *p=head,*q=head; 53 | 54 | for(int index=0;indexnext; 57 | } 58 | for(int index=0;indexnext; 61 | } 62 | 63 | int x=p->data; 64 | p->data=q->data; 65 | q->data=x; 66 | 67 | return head; 68 | } 69 | int main() 70 | { 71 | int t; 72 | cin >> t; 73 | while (t--) 74 | { 75 | int i, j; 76 | Node *head = takeinput(); 77 | cin >> i; 78 | cin >> j; 79 | head = swapNodes(head, i, j); 80 | print(head); 81 | } 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 2/010 kReverse.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | Following is the class structure of the Node class: 4 | 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node *next; 10 | Node(int data) 11 | { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | }; 16 | 17 | *****************************************************************/ 18 | //reverses the LL 19 | Node *reverse_linked_list_rec(Node *head) 20 | { 21 | //write your recursive code here 22 | if(head==NULL || head ->next ==NULL) 23 | return head; 24 | 25 | Node* smallans=reverse_linked_list_rec(head->next); 26 | Node*tail= head->next; 27 | tail->next=head; 28 | head ->next =NULL; 29 | 30 | return smallans; 31 | 32 | } 33 | 34 | Node *kReverse(Node *head, int k) 35 | { 36 | 37 | 38 | if(head==NULL) return NULL; 39 | 40 | int t = k; 41 | Node* temp = head; 42 | while(t>1 && temp!=NULL){ 43 | temp=temp->next; 44 | t--; 45 | } 46 | 47 | Node* amit; 48 | if(temp!=NULL){ 49 | amit = temp->next; 50 | temp->next=NULL; 51 | }else{ 52 | amit=NULL; 53 | } 54 | Node* ans = reverse_linked_list_rec(head); 55 | Node* smallAns = kReverse(amit,k); 56 | 57 | temp = ans; 58 | while(temp->next!=NULL){ 59 | temp=temp->next; 60 | } 61 | temp->next=smallAns; 62 | return ans; 63 | 64 | 65 | } 66 | -------------------------------------------------------------------------------- /005 - Linked List/Linked List 2/011 Bubble Sort.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | 3 | Following is the class structure of the Node class: 4 | 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node *next; 10 | Node(int data) 11 | { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | }; 16 | 17 | *****************************************************************/ 18 | int len(Node *head){ 19 | Node *temp=head; 20 | int count=1; 21 | while(temp!=NULL){ 22 | temp=temp->next; 23 | count++; 24 | } 25 | return count; 26 | } 27 | Node *bubbleSort(Node *head) 28 | { 29 | if(head==NULL || head->next==NULL) 30 | return head; 31 | 32 | // node *curr=head; 33 | //node *prev=NULL; 34 | //node *n=head->next; 35 | for(int i=0;inext; 39 | 40 | while(curr->next!=NULL) // traverse though LL 41 | { 42 | if(curr->data > curr->next->data) // if nodes are to be swapped 43 | { 44 | if(prev) //check if previous of the current node exist , u cant point on a garbage value 45 | { 46 | // swapping using those 4 steps 47 | Node* t=curr->next->next; 48 | curr->next->next= curr; 49 | prev->next=curr->next; 50 | curr->next=t; 51 | prev=prev->next; 52 | } 53 | else //is it doent exist it would be head node 54 | { 55 | head= curr->next; 56 | curr->next=head->next; 57 | head->next=curr; 58 | prev=head; 59 | } 60 | 61 | } 62 | else // else you simply move to next node 63 | { 64 | prev=curr; 65 | curr=curr->next; 66 | } 67 | } 68 | } 69 | 70 | 71 | return head; 72 | } 73 | -------------------------------------------------------------------------------- /006 - Stack/Balanced Parantheis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | bool isBalanced(string s) 3 | { 4 | stack st; 5 | 6 | for(int i=0;i 2 | bool checkRedundantBrackets(string expression) { 3 | // Write your code here 4 | stack st; 5 | int count=0; 6 | for(int i=0;i 2 | int countBracketReversals(string s) { 3 | // Write your code here 4 | if(s.length()%2!=0){ 5 | return -1; 6 | } 7 | stack st; 8 | for(int i=0;i 2 | #include "PairClass.cpp" 3 | using namespace std; 4 | int main(){ 5 | Pair,int> p1; 6 | Pair p2; 7 | p2.setX(10); 8 | p2.setY(15); 9 | p1.setX(p2); 10 | p1.setY(20); 11 | 12 | cout< 2 | 3 | class Pair{ 4 | T x; 5 | V y; 6 | public: 7 | void setX(T x){ 8 | this->x = x; 9 | } 10 | T getX(){ 11 | return x; 12 | } 13 | void setY(V y){ 14 | this->y = y; 15 | } 16 | V getY(){ 17 | return y; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /006 - Stack/Practice 01/StackUsingArray.h: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | template 4 | 5 | class StackUsingArray{ 6 | public: 7 | T *data; 8 | int nextIndex; 9 | int capacity; 10 | StackUsingArray(int capacity){ 11 | data = new T[capacity]; 12 | nextIndex=0; 13 | this->capacity=capacity; 14 | } 15 | bool isEmpty(){ 16 | if(nextIndex==0){ 17 | return true; 18 | }else{ 19 | return false; 20 | } 21 | } 22 | int getLength(){ 23 | return nextIndex; 24 | } 25 | void pushData(int val){ 26 | if(capacity==nextIndex){ 27 | capacity*=2; 28 | T *temp = new T[capacity]; 29 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int data; 7 | Node* next; 8 | Node(int data){ 9 | this->data=data; 10 | next=NULL; 11 | } 12 | }; 13 | 14 | class StackUsingLL{ 15 | public: 16 | Node* head; 17 | int size; 18 | StackUsingLL(){ 19 | size=0; 20 | head=NULL; 21 | } 22 | int getLength(){ 23 | return size; 24 | } 25 | bool isEmpty(){ 26 | if(head==NULL){ 27 | return true; 28 | }else{ 29 | return false; 30 | } 31 | } 32 | int topData(){ 33 | if(isEmpty()){ 34 | return -1; 35 | }else{ 36 | return head->data; 37 | } 38 | } 39 | void popData(){ 40 | if(isEmpty()){ 41 | cout<<"Not possible"<next; 45 | size--; 46 | } 47 | void pushData(int data){ 48 | Node* temp = new Node(data); 49 | temp->next=head; 50 | head=temp; 51 | size++; 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /006 - Stack/Practice 01/StackUsingLL.h.gch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maityamit/Cpp-DSA-Notes-and-Programs/c3039713ff2305a787b11987e539997f0849f12b/006 - Stack/Practice 01/StackUsingLL.h.gch -------------------------------------------------------------------------------- /006 - Stack/Reverse a Stack.cpp: -------------------------------------------------------------------------------- 1 | void reverseStack(stack &input, stack &extra) { 2 | //Write your code here 3 | stack amit; 4 | while(!input.empty()){ 5 | extra.push(input.top()); 6 | input.pop(); 7 | } 8 | while(!extra.empty()){ 9 | amit.push(extra.top()); 10 | extra.pop(); 11 | } 12 | while(!amit.empty()){ 13 | input.push(amit.top()); 14 | amit.pop(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /006 - Stack/StackBasicExample.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "StackUsingDynamicArray.cpp" 3 | using namespace std; 4 | int main(){ 5 | 6 | StackUsingDynamicArray data; 7 | data.push(10); 8 | data.push(20); 9 | data.push(30); 10 | data.push(40); 11 | data.push(50); 12 | data.push(60); 13 | cout<<"POP: "< 2 | using namespace std; 3 | class StackUsingArray{ 4 | int *data; 5 | int nextIndex; 6 | int capacity; 7 | public: 8 | StackUsingArray(int totalSize){ 9 | data = new int[totalSize]; 10 | nextIndex = 0; 11 | capacity = totalSize; 12 | } 13 | int size(){ 14 | return nextIndex; 15 | } 16 | bool isEmpty(){ 17 | if(nextIndex==0){ 18 | return true; 19 | }else{ 20 | return false; 21 | } 22 | } 23 | int top(){ 24 | if(isEmpty()){ 25 | return -1; 26 | }else{ 27 | return data[nextIndex-1]; 28 | } 29 | } 30 | int pop(){ 31 | if(isEmpty()){ 32 | return -1; 33 | }else{ 34 | nextIndex--; 35 | return data[nextIndex]; 36 | } 37 | } 38 | void push(int val){ 39 | if(nextIndex==capacity){ 40 | cout<<"Stack Full\n"; 41 | }else{ 42 | data[nextIndex] = val; 43 | nextIndex++; 44 | } 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /006 - Stack/StackUsingDynamicArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | template 4 | class StackUsingDynamicArray{ 5 | T *data; 6 | int nextIndex; 7 | int capacity; 8 | public: 9 | StackUsingDynamicArray(){ 10 | nextIndex = 0; 11 | capacity = 5; 12 | data = new T[capacity]; 13 | } 14 | int size(){ 15 | return nextIndex; 16 | } 17 | bool isEmpty(){ 18 | if(nextIndex==0){ 19 | return true; 20 | }else{ 21 | return false; 22 | } 23 | } 24 | int top(){ 25 | if(isEmpty()){ 26 | return -1; 27 | }else{ 28 | return data[nextIndex-1]; 29 | } 30 | } 31 | int pop(){ 32 | if(isEmpty()){ 33 | return -1; 34 | }else{ 35 | nextIndex--; 36 | return data[nextIndex]; 37 | } 38 | } 39 | void push(int val){ 40 | T *newData = new T[capacity*2]; 41 | for(int i=0;i 2 | int* stockSpan(int *price, int size) { 3 | // Write your code here 4 | stack st; 5 | int *ans = new int[size]; 6 | ans[0]=1; 7 | st.push(0); 8 | for(int i=1;iprice[st.top()]){ 10 | st.pop(); 11 | } 12 | if(st.size()==0){ 13 | ans[i]=i+1; 14 | }else{ 15 | ans[i]=i-st.top(); 16 | } 17 | st.push(i); 18 | } 19 | return ans; 20 | } 21 | -------------------------------------------------------------------------------- /007 - Queues/Node.h: -------------------------------------------------------------------------------- 1 | class Node { 2 | public : 3 | int data; 4 | Node *next; 5 | 6 | Node(int data) { 7 | this->data = data; 8 | next = NULL; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /007 - Queues/Practice 01/QueueUsingArray.h: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class QueueUsingArray{ 4 | public: 5 | int* data; 6 | int firstIndex; 7 | int nextIndex; 8 | int capacity; 9 | int size; 10 | QueueUsingArray(int val){ 11 | data=new int[val]; 12 | firstIndex=-1; 13 | nextIndex=0; 14 | size=0; 15 | capacity=val; 16 | } 17 | int getLength(){ 18 | return size; 19 | } 20 | bool isEmpty(){ 21 | if(size==0){ 22 | return true; 23 | }else{ 24 | return false; 25 | } 26 | } 27 | void pushData(int val){ 28 | if(size==capacity){ 29 | int* newData = new int[capacity*2]; 30 | int k=0; 31 | for(int i=firstIndex;i 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int data; 6 | Node* next; 7 | Node(int data){ 8 | this->data=data; 9 | next=NULL; 10 | } 11 | }; 12 | class QueueUsingLL{ 13 | public: 14 | Node* head; 15 | Node* tail; 16 | int size; 17 | QueueUsingLL(){ 18 | head=NULL; 19 | tail=NULL; 20 | size=0; 21 | } 22 | void pushData(int val){ 23 | Node* temp = new Node(val); 24 | if(head==NULL){ 25 | head=temp; 26 | tail=temp; 27 | }else{ 28 | tail->next=temp; 29 | tail=tail->next; 30 | } 31 | size++; 32 | } 33 | int getLength(){ 34 | return size; 35 | } 36 | void popData(){ 37 | if(head==NULL){ 38 | cout<<"Pop Not possible"<next; 42 | size--; 43 | } 44 | int topData(){ 45 | if(isEmpty()){ 46 | return -1; 47 | } 48 | return head->data; 49 | } 50 | bool isEmpty(){ 51 | if(size==0){ 52 | return true; 53 | }else{ 54 | return false; 55 | } 56 | } 57 | }; 58 | -------------------------------------------------------------------------------- /007 - Queues/Practice 01/queue Uses.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include"QueueUsingLL.h" 3 | #include"QueueUsingArray.h" 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | //Reverse a Queue 9 | void reverseQueue(queue &input){ 10 | stack st; 11 | while(input.size()!=0){ 12 | st.push(input.front()); 13 | input.pop(); 14 | } 15 | while(st.size()!=0){ 16 | input.push(st.top()); 17 | st.pop(); 18 | } 19 | } 20 | 21 | int main(){ 22 | 23 | 24 | 25 | cout<<"Queue Using Array"<popData(); 28 | cout<topData()<pushData(10); 30 | st->pushData(20); 31 | st->pushData(30); 32 | st->pushData(40); 33 | cout<topData()<popData(); 35 | st->pushData(50); 36 | st->pushData(60); 37 | st->pushData(70); 38 | st->pushData(80); 39 | st->pushData(90); 40 | st->pushData(100); 41 | cout<topData()<popData(); 43 | cout<getLength()<popData(); 51 | cout<topData()<pushData(10); 53 | stt->pushData(20); 54 | stt->pushData(30); 55 | stt->pushData(40); 56 | cout<topData()<popData(); 58 | stt->pushData(50); 59 | stt->pushData(60); 60 | stt->pushData(70); 61 | stt->pushData(80); 62 | stt->pushData(90); 63 | stt->pushData(100); 64 | cout<topData()<popData(); 66 | cout<getLength()< 2 | #include"QueueUsingArray.h" 3 | using namespace std; 4 | int main(){ 5 | QueueUsingArray s(5); 6 | s.enqueue(10); 7 | s.enqueue(20); 8 | s.enqueue(30); 9 | s.enqueue(40); 10 | s.enqueue(50); 11 | s.enqueue(60); 12 | cout< 2 | using namespace std; 3 | class QueueUsingArray{ 4 | int *data; 5 | int nextIndex; 6 | int firstIndex; 7 | int size; 8 | int capacity; 9 | public: 10 | QueueUsingArray(int s){ 11 | data = new int[s]; 12 | nextIndex = 0; 13 | firstIndex = -1; 14 | size = 0; 15 | capacity = s; 16 | } 17 | int getSize(){ 18 | return size; 19 | } 20 | bool isEmpty(){ 21 | return size==0; 22 | } 23 | int front(){ 24 | if(isEmpty()){ 25 | cout<<"Queue is empty\n"; 26 | return 0; 27 | } 28 | return data[firstIndex]; 29 | } 30 | void enqueue(int value){ 31 | if(size==capacity){ 32 | cout<<"Queue is Full\n"; 33 | return; 34 | } 35 | data[nextIndex] = value; 36 | nextIndex = (nextIndex+1)%capacity; 37 | size++; 38 | if(firstIndex==-1){ 39 | firstIndex = 0; 40 | } 41 | } 42 | int dequeue(){ 43 | if(isEmpty()){ 44 | cout<<"Queue is empty\n"; 45 | return 0; 46 | } 47 | int val = data[firstIndex]; 48 | firstIndex = (firstIndex+1)%capacity; 49 | size--; 50 | if(size==0){ 51 | firstIndex = -1; 52 | nextIndex = 0; 53 | } 54 | return val; 55 | } 56 | }; 57 | -------------------------------------------------------------------------------- /007 - Queues/QueueUsingArray.h.gch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maityamit/Cpp-DSA-Notes-and-Programs/c3039713ff2305a787b11987e539997f0849f12b/007 - Queues/QueueUsingArray.h.gch -------------------------------------------------------------------------------- /007 - Queues/QueueUsingLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include"QueueUsingLinkedList.h" 3 | using namespace std; 4 | int main(){ 5 | QueueUsingLinkedList s; 6 | s.enqueue(10); 7 | s.enqueue(20); 8 | s.enqueue(30); 9 | s.enqueue(40); 10 | s.enqueue(50); 11 | s.enqueue(60); 12 | cout<next = temp; 35 | tail = temp; 36 | } 37 | size++; 38 | } 39 | 40 | int dequeue() { 41 | // Implement the dequeue() function 42 | if(isEmpty()){ 43 | return -1; 44 | } 45 | int val = head->data; 46 | Node *amit = head; 47 | head=head->next; 48 | delete amit; 49 | size--; 50 | return val; 51 | } 52 | 53 | int front() { 54 | // Implement the front() function 55 | if(isEmpty()){ 56 | return -1; 57 | } 58 | return head->data; 59 | } 60 | }; 61 | -------------------------------------------------------------------------------- /007 - Queues/Reveerse a Queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void reverseQueue(queue &input) { 4 | // Write your code here 5 | stack st; 6 | 7 | while(!input.empty()){ 8 | st.push(input.front()); 9 | input.pop(); 10 | } 11 | while(!st.empty()){ 12 | input.push(st.top()); 13 | st.pop(); 14 | } 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /008 - Trees/1. Take Input.cpp: -------------------------------------------------------------------------------- 1 | TreeNode* takeInput(){ 2 | int rootData; 3 | cout<<"Enter Element: "; 4 | cin>>rootData; 5 | TreeNode* root = new TreeNode(rootData); 6 | cout<<"\nEnter no of child: "; 7 | int n; 8 | cin>>n; 9 | for(int i=0;i* temp = takeInput(); 11 | root->children.push_back(temp); 12 | } 13 | return root; 14 | } 15 | -------------------------------------------------------------------------------- /008 - Trees/10. PostOrder Traversal .cpp: -------------------------------------------------------------------------------- 1 | /************************************************************ 2 | 3 | Following is the structure for the TreeNode class 4 | 5 | template 6 | class TreeNode { 7 | public: 8 | T data; 9 | vector*> children; 10 | 11 | TreeNode(T data) { 12 | this->data = data; 13 | } 14 | 15 | ~TreeNode() { 16 | for (int i = 0; i < children.size(); i++) { 17 | delete children[i]; 18 | } 19 | } 20 | }; 21 | 22 | ************************************************************/ 23 | 24 | void printPostOrder(TreeNode* root) { 25 | // Write your code here 26 | for(int i=0;ichildren.size();i++){ 27 | printPostOrder(root->children[i]); 28 | } 29 | cout<data<<" "; 30 | } 31 | -------------------------------------------------------------------------------- /008 - Trees/10. depth at Node d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TreeNode1.h" 3 | #include 4 | using namespace std; 5 | 6 | 7 | TreeNode* takeInputLevelWise(){ 8 | int rootData; 9 | cout<<"Enter Element: "; 10 | cin>>rootData; 11 | TreeNode* root = new TreeNode(rootData); 12 | queue*> pendingNodes; 13 | 14 | pendingNodes.push(root); 15 | while(pendingNodes.size()!=0){ 16 | TreeNode *front = pendingNodes.front(); 17 | pendingNodes.pop(); 18 | cout<<"Enter num of children of: "<data<<" : "; 19 | int n; 20 | cin>>n; 21 | for(int i=0;idata<<" data: "; 24 | cin>>childData; 25 | TreeNode* child = new TreeNode(childData); 26 | front->children.push_back(child); 27 | pendingNodes.push(child); 28 | } 29 | } 30 | return root; 31 | } 32 | void printLevelWise(TreeNode* root) { 33 | queue*> pendingNodes; 34 | pendingNodes.push(root); 35 | while(pendingNodes.size()!=0){ 36 | TreeNode *front = pendingNodes.front(); 37 | pendingNodes.pop(); 38 | cout<data<<":"; 39 | for(int i=0;ichildren.size();i++){ 40 | if(i==front->children.size()-1){ 41 | cout<children[i]->data; 42 | }else{ 43 | cout<children[i]->data<<","; 44 | } 45 | pendingNodes.push(front->children[i]); 46 | } 47 | cout<<"\n"; 48 | } 49 | } 50 | 51 | 52 | void depthOfNodes(TreeNode* root , int k){ 53 | if(k==0){ 54 | cout<data<children.size();i++){ 57 | depthOfNodes(root->children.at(i),k-1); 58 | } 59 | } 60 | 61 | int main(){ 62 | 63 | TreeNode* root = takeInputLevelWise(); 64 | depthOfNodes(root,2); 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /008 - Trees/11. Count leaf Nodes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TreeNode1.h" 3 | #include 4 | using namespace std; 5 | 6 | 7 | TreeNode* takeInputLevelWise(){ 8 | int rootData; 9 | cout<<"Enter Element: "; 10 | cin>>rootData; 11 | TreeNode* root = new TreeNode(rootData); 12 | queue*> pendingNodes; 13 | 14 | pendingNodes.push(root); 15 | while(pendingNodes.size()!=0){ 16 | TreeNode *front = pendingNodes.front(); 17 | pendingNodes.pop(); 18 | cout<<"Enter num of children of: "<data<<" : "; 19 | int n; 20 | cin>>n; 21 | for(int i=0;idata<<" data: "; 24 | cin>>childData; 25 | TreeNode* child = new TreeNode(childData); 26 | front->children.push_back(child); 27 | pendingNodes.push(child); 28 | } 29 | } 30 | return root; 31 | } 32 | void printLevelWise(TreeNode* root) { 33 | queue*> pendingNodes; 34 | pendingNodes.push(root); 35 | while(pendingNodes.size()!=0){ 36 | TreeNode *front = pendingNodes.front(); 37 | pendingNodes.pop(); 38 | cout<data<<":"; 39 | for(int i=0;ichildren.size();i++){ 40 | if(i==front->children.size()-1){ 41 | cout<children[i]->data; 42 | }else{ 43 | cout<children[i]->data<<","; 44 | } 45 | pendingNodes.push(front->children[i]); 46 | } 47 | cout<<"\n"; 48 | } 49 | } 50 | 51 | 52 | 53 | int countLeafNodes(TreeNode* root){ 54 | int count=0; 55 | for(int i=0;ichildren.size();i++){ 56 | int temp = countLeafNodes(root->children.at(i)); 57 | count+=temp; 58 | } 59 | if(root->children.size()==0){ 60 | count++; 61 | } 62 | return count; 63 | } 64 | 65 | int main(){ 66 | 67 | TreeNode* root = takeInputLevelWise(); 68 | cout< 2 | #include "TreeNode1.h" 3 | #include 4 | using namespace std; 5 | 6 | 7 | TreeNode* takeInputLevelWise(){ 8 | int rootData; 9 | cout<<"Enter Element: "; 10 | cin>>rootData; 11 | TreeNode* root = new TreeNode(rootData); 12 | queue*> pendingNodes; 13 | 14 | pendingNodes.push(root); 15 | while(pendingNodes.size()!=0){ 16 | TreeNode *front = pendingNodes.front(); 17 | pendingNodes.pop(); 18 | cout<<"Enter num of children of: "<data<<" : "; 19 | int n; 20 | cin>>n; 21 | for(int i=0;idata<<" data: "; 24 | cin>>childData; 25 | TreeNode* child = new TreeNode(childData); 26 | front->children.push_back(child); 27 | pendingNodes.push(child); 28 | } 29 | } 30 | return root; 31 | } 32 | void printLevelWise(TreeNode* root) { 33 | queue*> pendingNodes; 34 | pendingNodes.push(root); 35 | while(pendingNodes.size()!=0){ 36 | TreeNode *front = pendingNodes.front(); 37 | pendingNodes.pop(); 38 | cout<data<<":"; 39 | for(int i=0;ichildren.size();i++){ 40 | if(i==front->children.size()-1){ 41 | cout<children[i]->data; 42 | }else{ 43 | cout<children[i]->data<<","; 44 | } 45 | pendingNodes.push(front->children[i]); 46 | } 47 | cout<<"\n"; 48 | } 49 | } 50 | 51 | 52 | 53 | void printPostOrder(TreeNode* root) { 54 | // Write your code here 55 | for(int i=0;ichildren.size();i++){ 56 | printPostOrder(root->children[i]); 57 | } 58 | cout<data<<" "; 59 | } 60 | int main(){ 61 | 62 | TreeNode* root = takeInputLevelWise(); 63 | printPostOrder(root); 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /008 - Trees/13. Contains x.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************ 2 | 3 | Following is the structure for the TreeNode class 4 | 5 | template 6 | class TreeNode { 7 | public: 8 | T data; 9 | vector*> children; 10 | 11 | TreeNode(T data) { 12 | this->data = data; 13 | } 14 | 15 | ~TreeNode() { 16 | for (int i = 0; i < children.size(); i++) { 17 | delete children[i]; 18 | } 19 | } 20 | }; 21 | 22 | ************************************************************/ 23 | 24 | bool isPresent(TreeNode* root, int x) { 25 | // Write your code here 26 | bool count = false;; 27 | for(int i=0;ichildren.size();i++){ 28 | bool temp = isPresent(root->children[i],x); 29 | if(temp==true){ 30 | count = true; 31 | } 32 | } 33 | if(root->data == x){ 34 | return true; 35 | }else{ 36 | return count;; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /008 - Trees/14. Count nodes.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************ 2 | 3 | Following is the structure for the TreeNode class 4 | 5 | template 6 | class TreeNode { 7 | public: 8 | T data; 9 | vector*> children; 10 | 11 | TreeNode(T data) { 12 | this->data = data; 13 | } 14 | 15 | ~TreeNode() { 16 | for (int i = 0; i < children.size(); i++) { 17 | delete children[i]; 18 | } 19 | } 20 | }; 21 | 22 | ************************************************************/ 23 | 24 | int getLargeNodeCount(TreeNode* root, int x) { 25 | // Write your code here 26 | int count = 0; 27 | for(int i=0;ichildren.size();i++){ 28 | int temp = getLargeNodeCount(root->children[i],x); 29 | count = count+temp; 30 | } 31 | if(root->data > x){ 32 | count++; 33 | } 34 | return count; 35 | } 36 | -------------------------------------------------------------------------------- /008 - Trees/15. Node with maximum child sum.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************ 2 | 3 | Following is the structure for the TreeNode class 4 | 5 | template 6 | class TreeNode { 7 | public: 8 | T data; 9 | vector*> children; 10 | 11 | TreeNode(T data) { 12 | this->data = data; 13 | } 14 | 15 | ~TreeNode() { 16 | for (int i = 0; i < children.size(); i++) { 17 | delete children[i]; 18 | } 19 | } 20 | }; 21 | 22 | ************************************************************/ 23 | 24 | TreeNode* maxSumNode(TreeNode* root) { 25 | // Write your code here 26 | TreeNode* ans = root; 27 | int sum = root->data; 28 | for(int i=0;ichildren.size();i++){ 29 | sum+=root->children[i]->data; 30 | } 31 | for(int i=0;ichildren.size();i++){ 32 | TreeNode* x = maxSumNode(root->children[i]); 33 | int xsum = x->data; 34 | for(int k=0;kchildren.size();k++){ 35 | xsum+=x->children[k]->data; 36 | } 37 | if(xsum>sum){ 38 | ans = x; 39 | } 40 | } 41 | return ans; 42 | 43 | } 44 | -------------------------------------------------------------------------------- /008 - Trees/16. Structurally identical.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************ 2 | 3 | Following is the structure for the TreeNode class 4 | 5 | template 6 | class TreeNode { 7 | public: 8 | T data; 9 | vector*> children; 10 | 11 | TreeNode(T data) { 12 | this->data = data; 13 | } 14 | 15 | ~TreeNode() { 16 | for (int i = 0; i < children.size(); i++) { 17 | delete children[i]; 18 | } 19 | } 20 | }; 21 | 22 | ************************************************************/ 23 | 24 | bool areIdentical(TreeNode *root1, TreeNode * root2) { 25 | // Write your code here 26 | if(root1->data != root2->data) 27 | return false; 28 | 29 | if(root1->children.size() != root2->children.size()) 30 | return false; 31 | 32 | for(int i=0;ichildren.size();i++) 33 | { 34 | return areIdentical(root1->children[i],root2->children[i]); 35 | 36 | } 37 | return true; 38 | } 39 | -------------------------------------------------------------------------------- /008 - Trees/17. Next larger.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************ 2 | 3 | Following is the structure for the TreeNode class 4 | 5 | template 6 | class TreeNode { 7 | public: 8 | T data; 9 | vector*> children; 10 | 11 | TreeNode(T data) { 12 | this->data = data; 13 | } 14 | 15 | ~TreeNode() { 16 | for (int i = 0; i < children.size(); i++) { 17 | delete children[i]; 18 | } 19 | } 20 | }; 21 | 22 | ************************************************************/ 23 | 24 | TreeNode* getNextLargerElement(TreeNode* root, int n) { 25 | // Write your code here 26 | 27 | if(root==NULL) 28 | return NULL; ///edge case 29 | 30 | TreeNode*max =NULL; 31 | 32 | if(root->data > n) 33 | max=root; 34 | 35 | for(int i=0;ichildren.size();i++) 36 | { 37 | TreeNode*childmax= getNextLargerElement(root->children[i], n); 38 | 39 | if(childmax==NULL) 40 | {continue;} 41 | else 42 | { 43 | if(max==NULL) 44 | max=childmax; 45 | else if(childmax->data > n && childmax->data < max->data) 46 | max= childmax; 47 | } 48 | } 49 | 50 | return max; 51 | } 52 | -------------------------------------------------------------------------------- /008 - Trees/18. Second Largest Element In Tree.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************ 2 | 3 | Following is the structure for the TreeNode class 4 | 5 | template 6 | class TreeNode { 7 | public: 8 | T data; 9 | vector*> children; 10 | 11 | TreeNode(T data) { 12 | this->data = data; 13 | } 14 | 15 | ~TreeNode() { 16 | for (int i = 0; i < children.size(); i++) { 17 | delete children[i]; 18 | } 19 | } 20 | }; 21 | 22 | ************************************************************/ 23 | class helper { 24 | public : 25 | TreeNode* m; 26 | TreeNode* sm; 27 | 28 | helper (TreeNode* m, TreeNode* sm) { 29 | this->m = m; 30 | this->sm = sm; 31 | } 32 | }; 33 | 34 | helper help (TreeNode* root) { 35 | if(root==NULL){ 36 | helper r(NULL,NULL); 37 | return r; 38 | } 39 | helper ans(root, NULL); 40 | for (int i = 0; i < root->children.size(); i++) { 41 | helper child = help (root -> children[i]); 42 | if(child.m->data > ans.m->data){ 43 | if(child.sm==NULL){ 44 | ans.sm=ans.m; 45 | ans.m=child.m; 46 | } 47 | else{ 48 | if(child.sm->data > ans.m->data){ 49 | ans.sm=child.sm; 50 | ans.m=child.m; 51 | } 52 | else{ 53 | ans.sm=ans.m; 54 | ans.m=child.m; 55 | } 56 | } 57 | } 58 | else { 59 | if(ans.m->data!=child.m->data && (ans.sm==NULL || child.m->data > ans.sm->data)){ 60 | ans.sm=child.m; 61 | } 62 | } 63 | } 64 | return ans; 65 | } 66 | 67 | TreeNode* getSecondLargestNode(TreeNode* root) { 68 | // Write your code here 69 | if (root == NULL) return NULL; 70 | helper ans = help (root); 71 | return ans.sm; 72 | } 73 | -------------------------------------------------------------------------------- /008 - Trees/19. Replace with depth.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************ 2 | 3 | Following is the structure for the TreeNode class 4 | 5 | template 6 | class TreeNode { 7 | public: 8 | T data; 9 | vector*> children; 10 | 11 | TreeNode(T data) { 12 | this->data = data; 13 | } 14 | 15 | ~TreeNode() { 16 | for (int i = 0; i < children.size(); i++) { 17 | delete children[i]; 18 | } 19 | } 20 | }; 21 | 22 | ************************************************************/ 23 | void help(TreeNode *root, int depth) 24 | { 25 | root->data=depth; 26 | for(int i=0;ichildren.size();i++) 27 | { 28 | help(root->children[i] ,depth+1); 29 | } 30 | } 31 | void replaceWithDepthValue(TreeNode* root) { 32 | // Write your code here 33 | 34 | int depth=0; 35 | help(root,depth); 36 | } 37 | -------------------------------------------------------------------------------- /008 - Trees/2. Print Output - Copy.cpp: -------------------------------------------------------------------------------- 1 | void printChilds(TreeNode* root){ 2 | cout<data<<" : "; 3 | for(int i=0;ichildren.size();i++){ 4 | cout<children.at(i)->data<<","; 5 | } 6 | cout<<"\n"; 7 | for(int i=0;ichildren.size();i++){ 8 | printChilds(root->children[i]); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /008 - Trees/3. Take Input Level Wise.cpp: -------------------------------------------------------------------------------- 1 | TreeNode* takeInputLevelWise(){ 2 | int rootData; 3 | cout<<"Enter Element: "; 4 | cin>>rootData; 5 | TreeNode* root = new TreeNode(rootData); 6 | queue*> pendingNodes; 7 | 8 | pendingNodes.push(root); 9 | while(pendingNodes.size()!=0){ 10 | TreeNode *front = pendingNodes.front(); 11 | pendingNodes.pop(); 12 | cout<<"Enter num of children of: "<data<<" : "; 13 | int n; 14 | cin>>n; 15 | for(int i=0;idata<<" data: "; 18 | cin>>childData; 19 | TreeNode* child = new TreeNode(childData); 20 | front->children.push_back(child); 21 | pendingNodes.push(child); 22 | } 23 | } 24 | return root; 25 | } 26 | -------------------------------------------------------------------------------- /008 - Trees/4. print_level_wise.cpp: -------------------------------------------------------------------------------- 1 | void printLevelWise(TreeNode* root) { 2 | queue*> pendingNodes; 3 | pendingNodes.push(root); 4 | while(pendingNodes.size()!=0){ 5 | TreeNode *front = pendingNodes.front(); 6 | pendingNodes.pop(); 7 | cout<data<<":"; 8 | for(int i=0;ichildren.size();i++){ 9 | if(i==front->children.size()-1){ 10 | cout<children[i]->data; 11 | }else{ 12 | cout<children[i]->data<<","; 13 | } 14 | pendingNodes.push(front->children[i]); 15 | } 16 | cout<<"\n"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /008 - Trees/5. TreeNodeUses 1,2,3,4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TreeNode1.h" 3 | #include 4 | using namespace std; 5 | 6 | // Take Input Processing 1 7 | TreeNode* takeInput(){ 8 | int rootData; 9 | cout<<"Enter Element: "; 10 | cin>>rootData; 11 | TreeNode* root = new TreeNode(rootData); 12 | cout<<"\nEnter no of child: "; 13 | int n; 14 | cin>>n; 15 | for(int i=0;i* temp = takeInput(); 17 | root->children.push_back(temp); 18 | } 19 | return root; 20 | } 21 | 22 | 23 | 24 | // Take Input Processing 2 25 | TreeNode* takeInputLevelWise(){ 26 | int rootData; 27 | cout<<"Enter Element: "; 28 | cin>>rootData; 29 | TreeNode* root = new TreeNode(rootData); 30 | queue*> pendingNodes; 31 | 32 | pendingNodes.push(root); 33 | while(pendingNodes.size()!=0){ 34 | TreeNode *front = pendingNodes.front(); 35 | pendingNodes.pop(); 36 | cout<<"Enter num of children of: "<data<<" : "; 37 | int n; 38 | cin>>n; 39 | for(int i=0;idata<<" data: "; 42 | cin>>childData; 43 | TreeNode* child = new TreeNode(childData); 44 | front->children.push_back(child); 45 | pendingNodes.push(child); 46 | } 47 | } 48 | return root; 49 | } 50 | 51 | //Print Childs Process 01 52 | void printChilds(TreeNode* root){ 53 | cout<data<<" : "; 54 | for(int i=0;ichildren.size();i++){ 55 | cout<children.at(i)->data<<","; 56 | } 57 | cout<<"\n"; 58 | for(int i=0;ichildren.size();i++){ 59 | printChilds(root->children[i]); 60 | } 61 | } 62 | 63 | //Print Childs Process 02 64 | void printLevelWise(TreeNode* root) { 65 | queue*> pendingNodes; 66 | pendingNodes.push(root); 67 | while(pendingNodes.size()!=0){ 68 | TreeNode *front = pendingNodes.front(); 69 | pendingNodes.pop(); 70 | cout<data<<":"; 71 | for(int i=0;ichildren.size();i++){ 72 | if(i==front->children.size()-1){ 73 | cout<children[i]->data; 74 | }else{ 75 | cout<children[i]->data<<","; 76 | } 77 | pendingNodes.push(front->children[i]); 78 | } 79 | cout<<"\n"; 80 | } 81 | } 82 | 83 | 84 | 85 | int main(){ 86 | 87 | TreeNode* root = takeInput(); 88 | 89 | 90 | printChilds(root); 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /008 - Trees/6. Count Nodes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TreeNode1.h" 3 | #include 4 | using namespace std; 5 | 6 | 7 | TreeNode* takeInputLevelWise(){ 8 | int rootData; 9 | cout<<"Enter Element: "; 10 | cin>>rootData; 11 | TreeNode* root = new TreeNode(rootData); 12 | queue*> pendingNodes; 13 | 14 | pendingNodes.push(root); 15 | while(pendingNodes.size()!=0){ 16 | TreeNode *front = pendingNodes.front(); 17 | pendingNodes.pop(); 18 | cout<<"Enter num of children of: "<data<<" : "; 19 | int n; 20 | cin>>n; 21 | for(int i=0;idata<<" data: "; 24 | cin>>childData; 25 | TreeNode* child = new TreeNode(childData); 26 | front->children.push_back(child); 27 | pendingNodes.push(child); 28 | } 29 | } 30 | return root; 31 | } 32 | void printLevelWise(TreeNode* root) { 33 | queue*> pendingNodes; 34 | pendingNodes.push(root); 35 | while(pendingNodes.size()!=0){ 36 | TreeNode *front = pendingNodes.front(); 37 | pendingNodes.pop(); 38 | cout<data<<":"; 39 | for(int i=0;ichildren.size();i++){ 40 | if(i==front->children.size()-1){ 41 | cout<children[i]->data; 42 | }else{ 43 | cout<children[i]->data<<","; 44 | } 45 | pendingNodes.push(front->children[i]); 46 | } 47 | cout<<"\n"; 48 | } 49 | } 50 | 51 | 52 | 53 | 54 | //Count Nodes Function 55 | int countNodes(TreeNode* root){ 56 | int count =1; 57 | for(int i=0;ichildren.size();i++){ 58 | count+= countNodes(root->children.at(i)); 59 | } 60 | return count; 61 | } 62 | 63 | int main(){ 64 | 65 | TreeNode* root = takeInputLevelWise(); 66 | cout< 2 | #include "TreeNode1.h" 3 | #include 4 | using namespace std; 5 | 6 | 7 | TreeNode* takeInputLevelWise(){ 8 | int rootData; 9 | cout<<"Enter Element: "; 10 | cin>>rootData; 11 | TreeNode* root = new TreeNode(rootData); 12 | queue*> pendingNodes; 13 | 14 | pendingNodes.push(root); 15 | while(pendingNodes.size()!=0){ 16 | TreeNode *front = pendingNodes.front(); 17 | pendingNodes.pop(); 18 | cout<<"Enter num of children of: "<data<<" : "; 19 | int n; 20 | cin>>n; 21 | for(int i=0;idata<<" data: "; 24 | cin>>childData; 25 | TreeNode* child = new TreeNode(childData); 26 | front->children.push_back(child); 27 | pendingNodes.push(child); 28 | } 29 | } 30 | return root; 31 | } 32 | void printLevelWise(TreeNode* root) { 33 | queue*> pendingNodes; 34 | pendingNodes.push(root); 35 | while(pendingNodes.size()!=0){ 36 | TreeNode *front = pendingNodes.front(); 37 | pendingNodes.pop(); 38 | cout<data<<":"; 39 | for(int i=0;ichildren.size();i++){ 40 | if(i==front->children.size()-1){ 41 | cout<children[i]->data; 42 | }else{ 43 | cout<children[i]->data<<","; 44 | } 45 | pendingNodes.push(front->children[i]); 46 | } 47 | cout<<"\n"; 48 | } 49 | } 50 | 51 | //Count Nodes Function 52 | int sumOfNodes(TreeNode* root){ 53 | int count =1; 54 | for(int i=0;ichildren.size();i++){ 55 | int temp = sumOfNodes(root->children.at(i)); 56 | count+=temp; 57 | } 58 | count+=root->data; 59 | return count; 60 | } 61 | 62 | int main(){ 63 | 64 | TreeNode* root = takeInputLevelWise(); 65 | cout< 2 | #include "TreeNode1.h" 3 | #include 4 | using namespace std; 5 | 6 | 7 | TreeNode* takeInputLevelWise(){ 8 | int rootData; 9 | cout<<"Enter Element: "; 10 | cin>>rootData; 11 | TreeNode* root = new TreeNode(rootData); 12 | queue*> pendingNodes; 13 | 14 | pendingNodes.push(root); 15 | while(pendingNodes.size()!=0){ 16 | TreeNode *front = pendingNodes.front(); 17 | pendingNodes.pop(); 18 | cout<<"Enter num of children of: "<data<<" : "; 19 | int n; 20 | cin>>n; 21 | for(int i=0;idata<<" data: "; 24 | cin>>childData; 25 | TreeNode* child = new TreeNode(childData); 26 | front->children.push_back(child); 27 | pendingNodes.push(child); 28 | } 29 | } 30 | return root; 31 | } 32 | void printLevelWise(TreeNode* root) { 33 | queue*> pendingNodes; 34 | pendingNodes.push(root); 35 | while(pendingNodes.size()!=0){ 36 | TreeNode *front = pendingNodes.front(); 37 | pendingNodes.pop(); 38 | cout<data<<":"; 39 | for(int i=0;ichildren.size();i++){ 40 | if(i==front->children.size()-1){ 41 | cout<children[i]->data; 42 | }else{ 43 | cout<children[i]->data<<","; 44 | } 45 | pendingNodes.push(front->children[i]); 46 | } 47 | cout<<"\n"; 48 | } 49 | } 50 | 51 | //Count Nodes Function 52 | TreeNode* maxDataNodes(TreeNode* root){ 53 | int max = 0; 54 | TreeNode* ans = NULL; 55 | for(int i=0;ichildren.size();i++){ 56 | TreeNode* temp = maxDataNodes(root->children.at(i)); 57 | if(temp->data > max){ 58 | ans = temp; 59 | max = temp->data; 60 | } 61 | } 62 | if(root->data > max){ 63 | ans = root; 64 | max = root->data; 65 | } 66 | return ans; 67 | } 68 | 69 | int main(){ 70 | 71 | TreeNode* root = takeInputLevelWise(); 72 | TreeNode* hell = maxDataNodes(root); 73 | cout<<"Max: "<data< 2 | #include "TreeNode1.h" 3 | #include 4 | using namespace std; 5 | 6 | 7 | TreeNode* takeInputLevelWise(){ 8 | int rootData; 9 | cout<<"Enter Element: "; 10 | cin>>rootData; 11 | TreeNode* root = new TreeNode(rootData); 12 | queue*> pendingNodes; 13 | 14 | pendingNodes.push(root); 15 | while(pendingNodes.size()!=0){ 16 | TreeNode *front = pendingNodes.front(); 17 | pendingNodes.pop(); 18 | cout<<"Enter num of children of: "<data<<" : "; 19 | int n; 20 | cin>>n; 21 | for(int i=0;idata<<" data: "; 24 | cin>>childData; 25 | TreeNode* child = new TreeNode(childData); 26 | front->children.push_back(child); 27 | pendingNodes.push(child); 28 | } 29 | } 30 | return root; 31 | } 32 | void printLevelWise(TreeNode* root) { 33 | queue*> pendingNodes; 34 | pendingNodes.push(root); 35 | while(pendingNodes.size()!=0){ 36 | TreeNode *front = pendingNodes.front(); 37 | pendingNodes.pop(); 38 | cout<data<<":"; 39 | for(int i=0;ichildren.size();i++){ 40 | if(i==front->children.size()-1){ 41 | cout<children[i]->data; 42 | }else{ 43 | cout<children[i]->data<<","; 44 | } 45 | pendingNodes.push(front->children[i]); 46 | } 47 | cout<<"\n"; 48 | } 49 | } 50 | 51 | //Count Nodes Function 52 | int heightOfTree(TreeNode* root){ 53 | int count = 0; 54 | for(int i=0;ichildren.size();i++){ 55 | int temp = heightOfTree(root->children.at(i)); 56 | if(count* root = takeInputLevelWise(); 66 | cout<<"Height: "< 2 | using namespace std; 3 | template 4 | class TreeNode{ 5 | public: 6 | T data; 7 | vector children; 8 | TreeNode(int data){ 9 | this->data = data; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /009 - Binary Trees/001 takeInput.cpp: -------------------------------------------------------------------------------- 1 | BinaryTreeNode* takeInput(){ 2 | cout<<"Enter Data: "; 3 | int n; 4 | cin>>n; 5 | if(n==-1){ 6 | return NULL; 7 | } 8 | BinaryTreeNode* root = new BinaryTreeNode(n); 9 | BinaryTreeNode* left = takeInput(); 10 | BinaryTreeNode* right = takeInput(); 11 | root->left = left; 12 | root->right = right; 13 | return root; 14 | } 15 | -------------------------------------------------------------------------------- /009 - Binary Trees/002 printTree.cpp: -------------------------------------------------------------------------------- 1 | void printTree(BinaryTreeNode* root){ 2 | if(root==NULL){ 3 | return; 4 | } 5 | cout<data<<": "; 6 | if(root->left!=NULL){ 7 | cout<<"L: "<left->data<<" "; 8 | } 9 | if(root->right!=NULL){ 10 | cout<<"R: "<right->data<<" "; 11 | } 12 | cout<left); 14 | printTree(root->right); 15 | } 16 | -------------------------------------------------------------------------------- /009 - Binary Trees/003 takeInputLevelWise.cpp: -------------------------------------------------------------------------------- 1 | // Level Wise 2 | BinaryTreeNode* takeInputLevelWise(){ 3 | int rootData; 4 | cout << "Enter root data" << endl; 5 | cin >> rootData; 6 | if (rootData == -1) { 7 | return NULL; 8 | } 9 | BinaryTreeNode* root = new BinaryTreeNode(rootData); 10 | queue*> pendingNodes; 11 | pendingNodes.push(root); 12 | while(pendingNodes.size()!=0){ 13 | BinaryTreeNode* temp = pendingNodes.front(); 14 | pendingNodes.pop(); 15 | int leftData,rightData; 16 | cout<<"Enter leftData of "<data<<": "; 17 | cin>>leftData; 18 | if(leftData!=-1){ 19 | BinaryTreeNode* child = new BinaryTreeNode(leftData); 20 | temp->left=child; 21 | pendingNodes.push(child); 22 | } 23 | 24 | cout<<"Enter rightData of "<data<<": "; 25 | cin>>rightData; 26 | if(rightData!=-1){ 27 | BinaryTreeNode* child = new BinaryTreeNode(rightData); 28 | temp->right=child; 29 | pendingNodes.push(child); 30 | } 31 | } 32 | return root; 33 | } 34 | -------------------------------------------------------------------------------- /009 - Binary Trees/004 printLevelWise.cpp: -------------------------------------------------------------------------------- 1 | void printTreeLevelWise(BinaryTreeNode* root){ 2 | if(root==NULL){ 3 | return; 4 | } 5 | queue*> pendingTree; 6 | pendingTree.push(root); 7 | while(pendingTree.size()!=0){ 8 | BinaryTreeNode* temp = pendingTree.front(); 9 | pendingTree.pop(); 10 | cout<data<<" :"; 11 | if(temp->left!=NULL){ 12 | cout<<" L: "<left->data; 13 | pendingTree.push(temp->left); 14 | } 15 | if(temp->right!=NULL){ 16 | cout<<" R: "<right->data; 17 | pendingTree.push(temp->right); 18 | } 19 | cout<* root){ 2 | if(root==NULL){ 3 | return 0; 4 | } 5 | return countNodes(root->left)+countNodes(root->right)+1; 6 | } 7 | -------------------------------------------------------------------------------- /009 - Binary Trees/007 find a Node.cpp: -------------------------------------------------------------------------------- 1 | bool findaNode(BinaryTreeNode* root,int k){ 2 | bool ans = false; 3 | if(root->data==k){ 4 | ans = true; 5 | } 6 | if(root->left){ 7 | bool hi = findaNode(root->left,k); 8 | if(hi){ 9 | ans=hi; 10 | } 11 | } 12 | if(root->right){ 13 | bool hi = findaNode(root->right,k); 14 | if(hi){ 15 | ans=hi; 16 | } 17 | } 18 | return ans; 19 | } 20 | -------------------------------------------------------------------------------- /009 - Binary Trees/008 height of a Binary tree.cpp: -------------------------------------------------------------------------------- 1 | int heightOFtree(BinaryTreeNode* root){ 2 | int count=0; 3 | if(root==NULL){ 4 | return 0; 5 | } 6 | int l = heightOFtree(root->left); 7 | int r = heightOFtree(root->right); 8 | if(l>r){ 9 | count=l; 10 | }else{ 11 | count=r; 12 | } 13 | return count+1; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /009 - Binary Trees/009 Mirror a Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | void miror(BinaryTreeNode* root){ 2 | if(root==NULL){ 3 | return; 4 | } 5 | BinaryTreeNode* yash1 = root->left; 6 | root->left = root->right; 7 | root->right = yash1; 8 | miror(root->left); 9 | miror(root->right); 10 | } 11 | -------------------------------------------------------------------------------- /009 - Binary Trees/010 Preorder traversal.cpp: -------------------------------------------------------------------------------- 1 | void preOrderTraversal(BinaryTreeNode* root){ 2 | if(root==NULL){ 3 | return; 4 | } 5 | cout<data<<" "; 6 | preOrderTraversal(root->left); 7 | preOrderTraversal(root->right); 8 | } 9 | -------------------------------------------------------------------------------- /009 - Binary Trees/011 post Order Traversal.cpp: -------------------------------------------------------------------------------- 1 | void postOrderTraversal(BinaryTreeNode* root){ 2 | if(root==NULL){ 3 | return; 4 | } 5 | postOrderTraversal(root->left); 6 | postOrderTraversal(root->right); 7 | cout<data<<" "; 8 | } 9 | -------------------------------------------------------------------------------- /009 - Binary Trees/012 in Order Traversal.cpp: -------------------------------------------------------------------------------- 1 | void inOrderTraversal(BinaryTreeNode* root){ 2 | if(root==NULL){ 3 | return; 4 | } 5 | inOrderTraversal(root->left); 6 | cout<data<<" "; 7 | inOrderTraversal(root->right); 8 | } 9 | -------------------------------------------------------------------------------- /009 - Binary Trees/013 Diameter First Approch.cpp: -------------------------------------------------------------------------------- 1 | int heightOFtree(BinaryTreeNode* root){ 2 | int count=0; 3 | if(root==NULL){ 4 | return 0; 5 | } 6 | int l = heightOFtree(root->left); 7 | int r = heightOFtree(root->right); 8 | if(l>r){ 9 | count=l; 10 | }else{ 11 | count=r; 12 | } 13 | return count+1; 14 | } 15 | int DiameterFirstApproch(BinaryTreeNode* root){ 16 | if(root==NULL){ 17 | return 0; 18 | } 19 | int op1 = heightOFtree(root->left)+heightOFtree(root->right); 20 | int op2 = DiameterFirstApproch(root->left); 21 | int op3 = DiameterFirstApproch(root->right); 22 | return max(op1,max(op2,op3)); 23 | } 24 | -------------------------------------------------------------------------------- /009 - Binary Trees/014 Diameter Optimize approch.cpp: -------------------------------------------------------------------------------- 1 | pair DiameterSecondApproch(BinaryTreeNode* root){ 2 | if(root==NULL){ 3 | pair p; 4 | p.first = 0; 5 | p.second = 0; 6 | return p; 7 | } 8 | pair l = DiameterSecondApproch(root->left); 9 | pair r = DiameterSecondApproch(root->right); 10 | int lh = l.first; 11 | int rh = r.first; 12 | int ld = l.second; 13 | int rd = r.second; 14 | int height = 1+max(lh,rh); 15 | int dia = max(lh+rh,max(ld,rd)); 16 | pair pa; 17 | pa.first = height; 18 | pa.second = dia; 19 | return pa; 20 | } 21 | -------------------------------------------------------------------------------- /009 - Binary Trees/015 getMaxandMIN ele.cpp: -------------------------------------------------------------------------------- 1 | pair maxANDmin(BinaryTreeNode* root){ 2 | if(root==NULL){ 3 | pair p; 4 | p.first = INT_MIN; 5 | p.second = INT_MAX; 6 | return p; 7 | } 8 | pair left_ans = maxANDmin(root->left); 9 | pair right_ans = maxANDmin(root->right); 10 | int lmx = left_ans.first; 11 | int rmx = right_ans.first; 12 | int lmn = left_ans.second; 13 | int rmn = right_ans.second; 14 | 15 | pair p; 16 | p.first = max(root->data,max(lmx,rmx)); 17 | p.second = min(root->data,min(lmn,rmn)); 18 | return p; 19 | } 20 | -------------------------------------------------------------------------------- /009 - Binary Trees/016 sumOfNodes.cpp: -------------------------------------------------------------------------------- 1 | int sumOfNodes(BinaryTreeNode* root){ 2 | if(root==NULL){ 3 | return 0; 4 | } 5 | return root->data+sumOfNodes(root->left)+sumOfNodes(root->right); 6 | } 7 | -------------------------------------------------------------------------------- /009 - Binary Trees/017 isBalanced.cpp: -------------------------------------------------------------------------------- 1 | bool isBalanced(BinaryTreeNode* root){ 2 | if (root == NULL) 3 | return true; 4 | int l=heightOFtree(root->left); 5 | int r=heightOFtree(root->right); 6 | 7 | if ( ( abs(l- r) ==0 || abs(l-r) == 1 ) && isBalanced(root->left) && isBalanced(root->right)) 8 | return true; 9 | 10 | return false; 11 | } 12 | -------------------------------------------------------------------------------- /009 - Binary Trees/018 printLevel Wise Differ.cpp: -------------------------------------------------------------------------------- 1 | void printLevelWiseDiffer(BinaryTreeNode* root){ 2 | if(root==NULL){ 3 | return; 4 | } 5 | queue*> qu; 6 | qu.push(root); 7 | qu.push(NULL); 8 | while(qu.size()!=0){ 9 | BinaryTreeNode* temp = qu.front(); 10 | qu.pop(); 11 | if(temp!=NULL){ 12 | cout<data<<" "; 13 | if(temp->left!=NULL){ 14 | qu.push(temp->left); 15 | } 16 | if(temp->right!=NULL){ 17 | qu.push(temp->right); 18 | } 19 | }else{ 20 | if(qu.size()==0){ 21 | break; 22 | } 23 | qu.push(NULL); 24 | cout<* removeLeafNodes(BinaryTreeNode* root){ 2 | if(root->left==NULL && root->right==NULL){ 3 | delete root; 4 | return NULL; 5 | } 6 | if(root->left){ 7 | BinaryTreeNode* left = removeLeafNodes(root->left); 8 | root->left=left; 9 | } 10 | if(root->left){ 11 | BinaryTreeNode* right = removeLeafNodes(root->right); 12 | root->right=right; 13 | } 14 | return root; 15 | } 16 | -------------------------------------------------------------------------------- /009 - Binary Trees/020 Construct Linked List for Each Level.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************** 2 | 3 | Following are the Binary Tree Node class structure and 4 | the Node class structure 5 | 6 | template 7 | class BinaryTreeNode { 8 | public: 9 | T data; 10 | BinaryTreeNode *left; 11 | BinaryTreeNode *right; 12 | 13 | BinaryTreeNode(T data) { 14 | this->data = data; 15 | left = NULL; 16 | right = NULL; 17 | } 18 | }; 19 | 20 | template 21 | class Node { 22 | public: 23 | T data; 24 | Node *next; 25 | Node(T data) { 26 | this->data=data; 27 | this->next=NULL; 28 | } 29 | }; 30 | 31 | ***********************************************************/ 32 | vector*> constructLinkedListForEachLevel(BinaryTreeNode *root) { 33 | 34 | vector*> vt; 35 | if(root==NULL){ 36 | vt.push_back(NULL); 37 | return vt; 38 | } 39 | queue*> qu; 40 | qu.push(root); 41 | qu.push(NULL); 42 | Node* start=NULL; 43 | Node* end=NULL; 44 | while(qu.size()!=0){ 45 | BinaryTreeNode* temp = qu.front(); 46 | qu.pop(); 47 | 48 | if(temp!=NULL){ 49 | Node* amit = new Node(temp->data); 50 | if(start==NULL){ 51 | start=amit; 52 | end=amit; 53 | }else{ 54 | end->next=amit; 55 | end=end->next; 56 | } 57 | 58 | if(temp->left){ 59 | qu.push(temp->left); 60 | } 61 | if(temp->right){ 62 | qu.push(temp->right); 63 | } 64 | 65 | }else{ 66 | vt.push_back(start); 67 | if(qu.size()==0){ 68 | return vt; 69 | } 70 | start=NULL; 71 | end=NULL; 72 | qu.push(NULL); 73 | } 74 | } 75 | return vt; 76 | 77 | 78 | } 79 | -------------------------------------------------------------------------------- /009 - Binary Trees/021. Zig Zag Tree.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************** 2 | Following is the Binary Tree Node class structure 3 | 4 | template 5 | class BinaryTreeNode { 6 | public : 7 | T data; 8 | BinaryTreeNode *left; 9 | BinaryTreeNode *right; 10 | 11 | BinaryTreeNode(T data) { 12 | this -> data = data; 13 | left = NULL; 14 | right = NULL; 15 | } 16 | }; 17 | 18 | ***********************************************************/ 19 | #include 20 | 21 | void zigZagOrder(BinaryTreeNode *root) { 22 | // Write your code here 23 | if(!root) 24 | return; 25 | 26 | stack *> s1; 27 | stack *> s2; 28 | 29 | s1.push(root); 30 | 31 | while(!s1.empty() && !s1.empty()) 32 | { 33 | while(!s1.empty()) 34 | { BinaryTreeNode * top=s1.top(); 35 | s1.pop(); 36 | cout<data<<" "; 37 | 38 | if(top->left) 39 | s2.push(top->left); 40 | if(top->right) 41 | s2.push(top->right); 42 | } 43 | 44 | cout< * top=s2.top(); 49 | s2.pop(); 50 | cout<data<<" "; 51 | 52 | if(top->right) 53 | s1.push(top->right); 54 | if(top->left) 55 | s1.push(top->left); 56 | 57 | } 58 | cout< 6 | class BinaryTreeNode 7 | { 8 | public : 9 | T data; 10 | BinaryTreeNode *left; 11 | BinaryTreeNode *right; 12 | 13 | BinaryTreeNode(T data) 14 | { 15 | this -> data = data; 16 | left = NULL; 17 | right = NULL; 18 | } 19 | }; 20 | 21 | ************************************************************/ 22 | 23 | void printNodesWithoutSibling(BinaryTreeNode *root) { 24 | // Write your code here 25 | // Write your code here 26 | if(root==NULL) 27 | return; 28 | 29 | if(root->left && !root->right) 30 | { cout<left->data<<" "; 31 | 32 | printNodesWithoutSibling(root->left); 33 | } 34 | else if(!root->left && root->right) 35 | { cout<right->data<<" "; 36 | 37 | printNodesWithoutSibling(root->right); 38 | } 39 | else 40 | { 41 | printNodesWithoutSibling(root->left); 42 | printNodesWithoutSibling(root->right); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /009 - Binary Trees/BinaryTreeNode.h: -------------------------------------------------------------------------------- 1 | #include 2 | template 3 | class BinaryTreeNode{ 4 | public: 5 | T data; 6 | BinaryTreeNode* left; 7 | BinaryTreeNode* right; 8 | 9 | BinaryTreeNode(T data){ 10 | this->data = data; 11 | left = NULL; 12 | right = NULL; 13 | } 14 | ~BinaryTreeNode(){ 15 | delete left; 16 | delete right; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /009 - Binary Trees/BinaryTreeNode.h.gch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maityamit/Cpp-DSA-Notes-and-Programs/c3039713ff2305a787b11987e539997f0849f12b/009 - Binary Trees/BinaryTreeNode.h.gch -------------------------------------------------------------------------------- /009 - Binary Trees/Node.h: -------------------------------------------------------------------------------- 1 | template 2 | class Node { 3 | public: 4 | T data; 5 | Node *next; 6 | Node(T data) { 7 | this->data=data; 8 | this->next=NULL; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /009 - Binary Trees/Practice/practice.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maityamit/Cpp-DSA-Notes-and-Programs/c3039713ff2305a787b11987e539997f0849f12b/009 - Binary Trees/Practice/practice.cpp -------------------------------------------------------------------------------- /010 - BST/001 Search in BST.cpp: -------------------------------------------------------------------------------- 1 | bool searchInBST(BinaryTreeNode *root , int k) { 2 | // Write your code here 3 | if(root==NULL){ 4 | return false; 5 | } 6 | bool ans = false; 7 | while(root!=NULL){ 8 | if(root->data==k){ 9 | ans=true; 10 | break; 11 | } 12 | if(root->data > k){ 13 | root=root->left; 14 | }else{ 15 | root=root->right; 16 | } 17 | } 18 | return ans; 19 | } 20 | -------------------------------------------------------------------------------- /010 - BST/002 Print Elements in Range.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************** 2 | 3 | Following is the Binary Tree Node class structure 4 | 5 | template 6 | class BinaryTreeNode { 7 | public : 8 | T data; 9 | BinaryTreeNode *left; 10 | BinaryTreeNode *right; 11 | 12 | BinaryTreeNode(T data) { 13 | this -> data = data; 14 | left = NULL; 15 | right = NULL; 16 | } 17 | }; 18 | 19 | ***********************************************************/ 20 | 21 | void elementsInRangeK1K2(BinaryTreeNode* root, int k1, int k2) { 22 | // Write your code here 23 | if(root==NULL){ 24 | return; 25 | } 26 | if(root->data < k1){ 27 | elementsInRangeK1K2(root->right,k1,k2); 28 | }else if(root->data > k2){ 29 | elementsInRangeK1K2(root->left,k1,k2); 30 | }else{ 31 | elementsInRangeK1K2(root->left,k1,k2); 32 | if(root->data >=k1 && root->data <=k2){ 33 | cout<data<<" "; 34 | } 35 | elementsInRangeK1K2(root->right,k1,k2); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /010 - BST/003 BST from Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | BinaryTreeNode* helper(int *input,int si,int ei){ 2 | if(si>ei){ 3 | return NULL; 4 | } 5 | int mi = (si+ei)/2; 6 | BinaryTreeNode* temp = new BinaryTreeNode(input[mi]); 7 | BinaryTreeNode* l_ans = helper(input,si,mi-1); 8 | BinaryTreeNode* r_ans = helper(input,mi+1,ei); 9 | temp->left = l_ans; 10 | temp->right = r_ans; 11 | return temp; 12 | } 13 | BinaryTreeNode* constructTree(int *input, int n) { 14 | // Write your code here 15 | return helper(input,0,n-1); 16 | } 17 | -------------------------------------------------------------------------------- /010 - BST/004 Check if a Binary Tree is BST.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************** 2 | Following is the Binary Tree Node class structure 3 | 4 | template 5 | class BinaryTreeNode { 6 | public : 7 | T data; 8 | BinaryTreeNode *left; 9 | BinaryTreeNode *right; 10 | 11 | BinaryTreeNode(T data) { 12 | this -> data = data; 13 | left = NULL; 14 | right = NULL; 15 | } 16 | }; 17 | 18 | ***********************************************************/ 19 | #include 20 | int MAX(BinaryTreeNode* root){ 21 | if(root==NULL){ 22 | return INT_MIN; 23 | } 24 | int left_ans = MAX(root->left); 25 | int right_ans = MAX(root->right); 26 | return max(root->data,max(left_ans,right_ans)); 27 | } 28 | int MIN(BinaryTreeNode* root){ 29 | if(root==NULL){ 30 | return INT_MAX; 31 | } 32 | int left_ans = MIN(root->left); 33 | int right_ans = MIN(root->right); 34 | return min(root->data,min(left_ans,right_ans)); 35 | } 36 | bool isBST(BinaryTreeNode *root) { 37 | // Write your code here 38 | if(root==NULL){ 39 | return true; 40 | } 41 | int left_ans = MAX(root->left); 42 | int right_ans = MIN(root->right); 43 | 44 | bool ans = (root->data > left_ans) && (root->data <= right_ans) && isBST(root->left) && isBST(root->right); 45 | return ans; 46 | 47 | 48 | 49 | } 50 | -------------------------------------------------------------------------------- /010 - BST/005 BST to Sorted LL.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************** 2 | 3 | Following are the Binary Tree Node class structure and the 4 | Node class structure 5 | 6 | template 7 | class BinaryTreeNode { 8 | public: 9 | T data; 10 | BinaryTreeNode *left; 11 | BinaryTreeNode *right; 12 | 13 | BinaryTreeNode(T data) { 14 | this->data = data; 15 | left = NULL; 16 | right = NULL; 17 | } 18 | }; 19 | 20 | template 21 | class Node{ 22 | public: 23 | T data; 24 | Node *next; 25 | 26 | Node(T data) { 27 | this->data = data; 28 | this->next = NULL; 29 | } 30 | }; 31 | 32 | ***********************************************************/ 33 | 34 | pair* , Node*> BST(BinaryTreeNode *root) 35 | { 36 | if (root == NULL) 37 | { 38 | pair* , Node*> ans; 39 | ans.first = NULL; 40 | ans.second = NULL; 41 | return ans; 42 | } 43 | 44 | Node *node = new Node(root->data); 45 | 46 | pair* , Node*> leftans = BST(root->left); 47 | pair* , Node*> rightans = BST(root->right); 48 | 49 | pair* , Node*> ans; 50 | 51 | if (leftans.first == NULL && rightans.first == NULL) 52 | { 53 | 54 | ans.first = node; 55 | ans.second = node; 56 | } 57 | 58 | else if (!leftans.first && rightans.first) 59 | { 60 | ans.first = node; 61 | node->next = rightans.first; 62 | 63 | ans.second = rightans.second; 64 | } 65 | else if (leftans.first && rightans.first == NULL) 66 | { 67 | ans.first = leftans.first; 68 | leftans.second->next = node; 69 | ans.second = node; 70 | } 71 | else 72 | { 73 | ans.first = leftans.first; 74 | leftans.second->next = node; 75 | node->next = rightans.first; 76 | ans.second = rightans.second; 77 | } 78 | return ans; 79 | } 80 | Node *constructLinkedList(BinaryTreeNode *root) 81 | { 82 | return BST(root).first; 83 | } 84 | -------------------------------------------------------------------------------- /010 - BST/006 Find Path in BST.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************** 2 | 3 | Following is the Binary Tree Node class structure 4 | 5 | template 6 | class BinaryTreeNode { 7 | public: 8 | T data; 9 | BinaryTreeNode *left; 10 | BinaryTreeNode *right; 11 | 12 | BinaryTreeNode(T data) { 13 | this->data = data; 14 | left = NULL; 15 | right = NULL; 16 | } 17 | }; 18 | 19 | ***********************************************************/ 20 | 21 | vector* getPath(BinaryTreeNode *root , int data) { 22 | 23 | 24 | if(root==NULL){ 25 | return NULL; 26 | } 27 | if(root->data==data){ 28 | vector* vt = new vector; 29 | vt->push_back(root->data); 30 | return vt; 31 | } 32 | 33 | // if(root==NULL) return NULL; 34 | // if(root->data==data){ 35 | // vector* ans = new vector ; 36 | // ans->push_back(data); 37 | // return ans; 38 | // } 39 | 40 | // vector* l = getPath(root->left,data); 41 | // if(l!=NULL){ 42 | // l->push_back(root->data); 43 | // return l; 44 | // } 45 | 46 | // vector* r = getPath(root->left,data); 47 | // if(r!=NULL){ 48 | // r->push_back(root->data); 49 | // return r; 50 | // }else{ 51 | // return NULL; 52 | // } 53 | 54 | 55 | 56 | 57 | // Write your code here 58 | 59 | vector* lef = getPath(root->left,data); 60 | if(lef!=NULL){ 61 | lef->push_back(root->data); 62 | return lef; 63 | } 64 | vector* rig = getPath(root->right,data); 65 | if(rig!=NULL){ 66 | rig->push_back(root->data); 67 | return rig; 68 | }else{ 69 | return NULL; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /010 - BST/008 Create & Insert Duplicate Node.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************ 2 | 3 | Following is the Binary Tree node structure 4 | 5 | template 6 | class BinaryTreeNode 7 | { 8 | public : 9 | T data; 10 | BinaryTreeNode *left; 11 | BinaryTreeNode *right; 12 | 13 | BinaryTreeNode(T data) 14 | { 15 | this -> data = data; 16 | left = NULL; 17 | right = NULL; 18 | } 19 | }; 20 | 21 | ************************************************************/ 22 | 23 | void insertDuplicateNode(BinaryTreeNode *root) { 24 | // Write your code here 25 | if(root==NULL){ 26 | return; 27 | } 28 | insertDuplicateNode(root->left); 29 | BinaryTreeNode* l = root->left; 30 | BinaryTreeNode* temp = new BinaryTreeNode(root->data); 31 | root->left = temp; 32 | temp->left = l; 33 | insertDuplicateNode(root->right); 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /010 - BST/009 LCA of Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************** 2 | Following is the Binary Tree Node class structure 3 | 4 | template 5 | class BinaryTreeNode { 6 | public : 7 | T data; 8 | BinaryTreeNode *left; 9 | BinaryTreeNode *right; 10 | 11 | BinaryTreeNode(T data) { 12 | this -> data = data; 13 | left = NULL; 14 | right = NULL; 15 | } 16 | }; 17 | 18 | ***********************************************************/ 19 | 20 | int getLCA(BinaryTreeNode * root , int a, int b) { 21 | // Write your code here 22 | if(root==NULL){ 23 | return -1; 24 | } 25 | if(root->data==a || root->data==b){ 26 | return root->data; 27 | } 28 | int l = getLCA(root->left,a,b); 29 | int r = getLCA(root->right,a,b); 30 | if(l==-1 && r!=-1){ 31 | return r; 32 | } 33 | else if(l!=-1 && r==-1){ 34 | return l; 35 | } 36 | else{ 37 | return root->data; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /010 - BST/010 LCA of BST.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************** 2 | 3 | Following is the Binary Tree Node class structure 4 | 5 | template 6 | class BinaryTreeNode { 7 | public: 8 | T data; 9 | BinaryTreeNode *left; 10 | BinaryTreeNode *right; 11 | 12 | BinaryTreeNode(T data) { 13 | this->data = data; 14 | left = NULL; 15 | right = NULL; 16 | } 17 | }; 18 | 19 | ***********************************************************/ 20 | 21 | int getLCA(BinaryTreeNode* root , int a , int b){ 22 | // Write your code here 23 | if(root==NULL){ 24 | return -1; 25 | } 26 | if(root->data==a || root->data==b){ 27 | return root->data; 28 | } 29 | if(root->data < a && root->data > b){ 30 | return root->data; 31 | } 32 | if(root->data >a && root->data data; 34 | } 35 | int l = getLCA(root->left,a,b); 36 | int r = getLCA(root->right,a,b); 37 | if(l==-1 && r!=-1){ 38 | return r; 39 | }else if(l!=-1 && r==-1){ 40 | return l; 41 | }else{ 42 | root->data; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /010 - BST/011 Largest BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | /********************************************************** 3 | 4 | Following is the Binary Tree Node class structure 5 | 6 | template 7 | class BinaryTreeNode { 8 | public: 9 | T data; 10 | BinaryTreeNode *left; 11 | BinaryTreeNode *right; 12 | 13 | BinaryTreeNode(T data) { 14 | this->data = data; 15 | left = NULL; 16 | right = NULL; 17 | } 18 | }; 19 | 20 | ***********************************************************/ 21 | #include 22 | #include 23 | class Pair{ 24 | 25 | public: 26 | int minimum; 27 | int maximum; 28 | bool bst; 29 | int height; 30 | }; 31 | 32 | Pair BST(BinaryTreeNode *root) 33 | { 34 | if(root==NULL) 35 | { 36 | Pair obj; 37 | obj.minimum =INT_MAX; 38 | obj.maximum = INT_MIN; 39 | obj.bst = true; 40 | obj.height=0; 41 | return obj; 42 | } 43 | 44 | Pair left= BST(root->left); 45 | Pair right =BST(root->right); 46 | 47 | int minimum=min(root->data,min(left.minimum,right.minimum)); 48 | int maximum=max(root->data,max(left.maximum,right.maximum)); 49 | bool isBSTfinal=(root->data >left.maximum) && (root->data < right.minimum) && left.bst && right.bst; 50 | 51 | Pair obj; 52 | obj.minimum=minimum; 53 | obj.maximum=maximum; 54 | obj.bst=isBSTfinal; 55 | if(isBSTfinal) 56 | { 57 | obj.height= 1+max(left.height,right.height); 58 | } 59 | else obj.height= max(left.height,right.height); 60 | return obj; 61 | 62 | } 63 | 64 | int largestBSTSubtree(BinaryTreeNode *root) { 65 | // Write your code here 66 | return BST(root).height; 67 | } 68 | -------------------------------------------------------------------------------- /010 - BST/012 Replace with Sum of greater nodes.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************** 2 | 3 | Following is the Binary Tree Node class structure 4 | 5 | template 6 | class BinaryTreeNode { 7 | public: 8 | T data; 9 | BinaryTreeNode *left; 10 | BinaryTreeNode *right; 11 | 12 | BinaryTreeNode(T data) { 13 | this->data = data; 14 | left = NULL; 15 | right = NULL; 16 | } 17 | }; 18 | 19 | ***********************************************************/ 20 | int ans = 0; 21 | void replaceWithLargerNodesSum(BinaryTreeNode *root) { 22 | // Write your code here 23 | if(root==NULL){ 24 | return; 25 | } 26 | replaceWithLargerNodesSum(root->right); 27 | int val = root->data; 28 | ans+=val; 29 | root->data=ans; 30 | replaceWithLargerNodesSum(root->left); 31 | } 32 | -------------------------------------------------------------------------------- /010 - BST/013 Path Sum Root to Leaf.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************** 2 | 3 | Following is the Binary Tree Node class structure 4 | 5 | template 6 | class BinaryTreeNode { 7 | public : 8 | T data; 9 | BinaryTreeNode *left; 10 | BinaryTreeNode *right; 11 | 12 | BinaryTreeNode(T data) { 13 | this -> data = data; 14 | left = NULL; 15 | right = NULL; 16 | } 17 | }; 18 | 19 | ***********************************************************/ 20 | 21 | #include 22 | 23 | void rootToLeafPathsSumToK(BinaryTreeNode *root, int k,vector path) 24 | { 25 | if(root==NULL) 26 | return; 27 | 28 | path.push_back(root->data); 29 | k=k- root->data; 30 | 31 | if(!root->left && !root->right) // leaf node 32 | { 33 | if(k==0) 34 | { 35 | for(int i : path) 36 | { 37 | cout<< i<<" "; 38 | } 39 | cout<left,k,path); 45 | rootToLeafPathsSumToK(root->right,k,path); 46 | 47 | } 48 | void rootToLeafPathsSumToK(BinaryTreeNode *root, int k) { 49 | // Write your code here 50 | vector v; 51 | rootToLeafPathsSumToK(root,k,v); 52 | return; 53 | } 54 | -------------------------------------------------------------------------------- /010 - BST/015 Pair sum in a BST.cpp: -------------------------------------------------------------------------------- 1 | /********************************************************** 2 | 3 | Following is the Binary Tree Node class structure 4 | 5 | template 6 | class BinaryTreeNode { 7 | public: 8 | T data; 9 | BinaryTreeNode *left; 10 | BinaryTreeNode *right; 11 | 12 | BinaryTreeNode(T data) { 13 | this->data = data; 14 | left = NULL; 15 | right = NULL; 16 | } 17 | }; 18 | 19 | ***********************************************************/ 20 | #include 21 | vector arrayPreOrder(BinaryTreeNode* root){ 22 | vector temp; 23 | if(root==NULL){ 24 | return temp; 25 | } 26 | vector l = arrayPreOrder(root->left); 27 | temp.insert(temp.end(),l.begin(),l.end()); 28 | temp.push_back(root->data); 29 | vector r = arrayPreOrder(root->right); 30 | temp.insert(temp.end(),r.begin(),r.end()); 31 | return temp; 32 | } 33 | void printData(vector vt,int sum){ 34 | int i=0; 35 | int j=vt.size()-1; 36 | while(i sum){ 42 | j--; 43 | }else{ 44 | i++; 45 | } 46 | } 47 | } 48 | void printNodesSumToS(BinaryTreeNode *root, int s) { 49 | // Write your code here 50 | vector vt = arrayPreOrder(root); 51 | printData(vt,s); 52 | } 53 | -------------------------------------------------------------------------------- /011 - Priority Queues/001 Remove Min.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class PriorityQueue { 4 | vector pq; 5 | 6 | public: 7 | bool isEmpty() { 8 | return pq.size() == 0; 9 | } 10 | 11 | int getSize() { 12 | return pq.size(); 13 | } 14 | 15 | int getMin() { 16 | if (isEmpty()) { 17 | return 0; 18 | } 19 | 20 | return pq[0]; 21 | } 22 | 23 | void insert(int element) { 24 | pq.push_back(element); 25 | 26 | int childIndex = pq.size() - 1; 27 | 28 | while (childIndex > 0) { 29 | int parentIndex = (childIndex - 1) / 2; 30 | 31 | if (pq[childIndex] < pq[parentIndex]) { 32 | int temp = pq[childIndex]; 33 | pq[childIndex] = pq[parentIndex]; 34 | pq[parentIndex] = temp; 35 | } else { 36 | break; 37 | } 38 | 39 | childIndex = parentIndex; 40 | } 41 | } 42 | 43 | int removeMin() { 44 | 45 | 46 | if(isEmpty()){ 47 | return 0; 48 | } 49 | // Write your code here 50 | int ans = pq[0]; 51 | pq[0]=pq[pq.size()-1]; 52 | pq.pop_back(); 53 | 54 | int curr=0; 55 | int LI = curr*2+1; 56 | int RI = curr*2+2; 57 | int mi = curr; 58 | 59 | while(LIpq[LI]){ 62 | mi=LI; 63 | } 64 | if(RIpq[RI]){ 65 | mi=RI; 66 | } 67 | if(curr==mi){ 68 | break; 69 | } 70 | int temp = pq[mi]; 71 | pq[mi]=pq[curr]; 72 | pq[curr]=temp; 73 | 74 | curr=mi; 75 | LI = curr*2+1; 76 | RI = curr*2+2; 77 | 78 | } 79 | 80 | return ans; 81 | 82 | } 83 | }; 84 | -------------------------------------------------------------------------------- /011 - Priority Queues/002 Max Priority Queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | class PriorityQueue { 4 | // Complete this class 5 | vector pq; 6 | 7 | public: 8 | int getSize(){ 9 | return pq.size(); 10 | } 11 | bool isEmpty(){ 12 | return getSize()==0; 13 | } 14 | int getMax() 15 | { 16 | if(pq.size()==0) return INT_MIN; 17 | 18 | return pq[0]; 19 | } 20 | void insert(int element) 21 | { 22 | pq.push_back(element); 23 | int ci=pq.size()-1; 24 | int pi=(ci-1)/2; 25 | 26 | while(pi>=0 && pq[pi]= pq.size()) break; //absence of left child 52 | else 53 | { 54 | if(2*i+2 >= pq.size()) //no right chiid 55 | { 56 | if(pq[i]pq[2*i+2]) //left child is max 68 | { 69 | int temp =pq[i]; 70 | pq[i]=pq[2*i+1]; 71 | pq[2*i+1]=temp; 72 | i=2*i+1; 73 | } 74 | else //right xhild is max 75 | { 76 | int temp =pq[i]; 77 | pq[i]=pq[2*i+2]; 78 | pq[2*i+2]=temp; 79 | i=2*i+2; 80 | } 81 | } 82 | } 83 | } 84 | 85 | 86 | 87 | return ans; 88 | } 89 | }; 90 | -------------------------------------------------------------------------------- /011 - Priority Queues/003 In-place heap sort.cpp: -------------------------------------------------------------------------------- 1 | void heapSort(int pq[], int n) { 2 | // Build the heap in input array 3 | for(int i = 1; i < n; i++) { 4 | 5 | int childIndex = i; 6 | while(childIndex > 0) { 7 | int parentIndex = (childIndex - 1) / 2; 8 | 9 | if(pq[childIndex] < pq[parentIndex]) { 10 | int temp = pq[childIndex]; 11 | pq[childIndex] = pq[parentIndex]; 12 | pq[parentIndex] = temp; 13 | } 14 | else { 15 | break; 16 | } 17 | childIndex = parentIndex; 18 | } 19 | } 20 | 21 | // Remove elements 22 | 23 | int size = n; 24 | 25 | while(size > 1) { 26 | int temp = pq[0]; 27 | pq[0] = pq[size - 1]; 28 | pq[size-1] = temp; 29 | 30 | size--; 31 | 32 | int parentIndex = 0; 33 | int leftChildIndex = 2 * parentIndex + 1; 34 | int rightChildIndx = 2 * parentIndex + 2; 35 | 36 | while(leftChildIndex < size) { 37 | int minIndex = parentIndex; 38 | if(pq[minIndex] > pq[leftChildIndex]) { 39 | minIndex = leftChildIndex; 40 | } 41 | if(rightChildIndx < size && pq[rightChildIndx] < pq[minIndex]) { 42 | minIndex = rightChildIndx; 43 | } 44 | if(minIndex == parentIndex) { 45 | break; 46 | } 47 | int temp = pq[minIndex]; 48 | pq[minIndex] = pq[parentIndex]; 49 | pq[parentIndex] = temp; 50 | 51 | parentIndex = minIndex; 52 | leftChildIndex = 2 * parentIndex + 1; 53 | rightChildIndx = 2 * parentIndex + 2; 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /011 - Priority Queues/004 K smallest Elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | vector kSmallest(int arr[], int n, int k) { 3 | // Write your code here 4 | priority_queue pq; 5 | for(int i=0;iarr[i]){ 10 | pq.pop(); 11 | pq.push(arr[i]); 12 | } 13 | } 14 | vector vt; 15 | while(pq.size()!=0){ 16 | vt.push_back(pq.top()); 17 | pq.pop(); 18 | } 19 | return vt; 20 | } 21 | -------------------------------------------------------------------------------- /011 - Priority Queues/005 K largest elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | vector kLargest(int input[], int n, int k){ 4 | /* Don't write main(). 5 | * Don't read input, it is passed as function argument. 6 | * Return output and don't print it. 7 | * Taking input and printing output is handled automatically. 8 | */ 9 | 10 | priority_queue , greater> pq; 11 | for(int i=0;i vt; 21 | while(pq.size()!=0){ 22 | vt.push_back(pq.top()); 23 | pq.pop(); 24 | } 25 | return vt; 26 | 27 | } 28 | 29 | -------------------------------------------------------------------------------- /011 - Priority Queues/006 Check Max-Heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | bool isMaxHeap(int arr[], int n) { 3 | // Write your code here 4 | int i = 0; 5 | int L=(2*i)+1; 6 | int R=(2*i)+2; 7 | while(Larr[L] && arr[i]>arr[R])){ 9 | return false; 10 | } 11 | i++; 12 | L=(2*i)+1; 13 | R=(2*i)+2; 14 | } 15 | return true; 16 | } 17 | -------------------------------------------------------------------------------- /011 - Priority Queues/007 Kth largest element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int kthLargest(int* arr, int n, int k) { 3 | // Write your code here 4 | priority_queue , greater> pq; 5 | for(int i=0;i 2 | class DATA{ 3 | public: 4 | int data,arrayIndex,elementIndex; 5 | DATA(int data,int arrayIndex,int elementIndex){ 6 | this->data=data; 7 | this->arrayIndex=arrayIndex; 8 | this->elementIndex=elementIndex; 9 | } 10 | }; 11 | struct myComp{ 12 | bool operator()(DATA &d1,DATA &d2){ 13 | return d1.data>d2.data; 14 | } 15 | }; 16 | vector mergeKSortedArrays(vector*> arr) { 17 | // Write your code here 18 | vector ans; 19 | priority_queue,myComp> pq; 20 | for(int i=0;iat(0),i,0); 22 | pq.push(d); 23 | } 24 | while(pq.size()!=0){ 25 | DATA curr = pq.top(); pq.pop(); 26 | ans.push_back(curr.data); 27 | int aP = curr.arrayIndex,eP=curr.elementIndex; 28 | if(eP+1size()){ 29 | DATA d(arr.at(aP)->at(eP+1),aP,eP+1); 30 | pq.push(d); 31 | } 32 | } 33 | return ans; 34 | } 35 | -------------------------------------------------------------------------------- /011 - Priority Queues/009 Running Median.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void findMedian(int *arr, int n) 3 | { 4 | 5 | 6 | if(n==0) return; 7 | 8 | priority_queue max; 9 | priority_queue,greater> min; 10 | max.push(arr[0]); 11 | cout<min.size()){ 15 | if(x>max.top()){ 16 | min.push(x); 17 | }else{ 18 | min.push(max.top()); 19 | max.pop(); 20 | max.push(x); 21 | } 22 | cout<<(max.top()+min.top())/2<<" "; 23 | }else{ 24 | if(x 2 | int buyTicket(int *arr, int n, int k) { 3 | // Write your code here 4 | queue p; //a queue of indices 5 | for(int i=0;i pq; 10 | //build a heap 11 | for(int i=0 ; iarr[p.front()]) 21 | { int x=p.front(); 22 | p.pop(); 23 | p.push(x); 24 | }else if(pq.top()==arr[p.front()]) 25 | { pq.pop(); 26 | p.pop(); 27 | time++; //incres time 28 | } 29 | } 30 | return time+1; //now i will be getting the ticket 31 | } 32 | -------------------------------------------------------------------------------- /012 - Hashmaps/001 Maximum Frequency Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int highestFrequency(int arr[], int n) { 3 | // Write your code here 4 | int max=0,count=0; 5 | unordered_map seen; 6 | for(int i=0;icount){ 9 | count=seen[arr[i]]; 10 | } 11 | } 12 | for(int i=0;i 2 | void printIntersection(int arr1[], int arr2[], int n, int m) { 3 | // Write your code here 4 | unordered_map seen; 5 | for(int i=0;i0){ 10 | cout< 2 | int pairSum(int *arr, int n) { 3 | // Write your code here 4 | // unordered_map seen; 5 | // int count=0; 6 | // for(int i=0;i :: iterator it = seen.begin(); 10 | // while(it!=seen.end()){ 11 | // int f = it->second; 12 | // int s = seen[0-(it->first)]; 13 | // count+=(f*s); 14 | // seen[0-(it->first)]=0; 15 | // it++; 16 | // } 17 | // return count; 18 | unordered_mapmap; 19 | int k=0; 20 | int count=0; 21 | for(int i=0;i 2 | #include 3 | string uniqueChar(string str) { 4 | // Write your code here 5 | string st; 6 | unordered_map seen; 7 | for(int i=0;i 2 | vector helper(int* arr,int n){ 3 | unordered_map map; 4 | for(int i=0;imax){ 17 | start=arr[i]; 18 | end=temp; 19 | max=count; 20 | } 21 | } 22 | return {start,end}; 23 | } 24 | vector longestConsecutiveIncreasingSequence(int *arr, int n) { 25 | 26 | 27 | return helper(arr,n); 28 | 29 | 30 | 31 | // Your Code goes here 32 | // unordered_map seen; 33 | // for(int i=0;imax){ 46 | // start=arr[i]; 47 | // end=temp-1; 48 | // max=count; 49 | // } 50 | // count=0; 51 | // } 52 | // vector vt; 53 | // vt.push_back(start); 54 | // vt.push_back(end); 55 | // return vt; 56 | } 57 | -------------------------------------------------------------------------------- /012 - Hashmaps/006 Pairs with difference K.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int getPairsWithDifferenceK(int *arr, int n, int k) { 5 | 6 | 7 | unordered_map seen; 8 | int count=0; 9 | for(int i=0;i=1){ 13 | count+=seen[a]; 14 | } 15 | else if(seen.count(b)>=1){ 16 | count+=seen[b]; 17 | } 18 | seen[arr[i]]++; 19 | } 20 | return count; 21 | } 22 | -------------------------------------------------------------------------------- /012 - Hashmaps/007 Longest subset zero sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int lengthOfLongestSubsetWithZeroSum(int* arr, int n) { 4 | // Write your code here 5 | int max = INT_MIN; 6 | unordered_map seen; 7 | int sum=0; 8 | for(int i=0;imax){ 12 | max=i+1; 13 | } 14 | } 15 | if(seen.count(sum)>=1){ 16 | int temp = i-seen[sum]; 17 | if(temp>max){ 18 | max=temp; 19 | } 20 | } 21 | seen[sum]=i; 22 | 23 | } 24 | return max; 25 | } 26 | -------------------------------------------------------------------------------- /013 - Tries and Huffman Coding/Trie.h: -------------------------------------------------------------------------------- 1 | class Trie{ 2 | TrieNode *root; 3 | public: 4 | Trie(){ 5 | root = new TrieNode('\0'); 6 | } 7 | void insertWord(string word){ 8 | insertWordHelper(root,word); 9 | } 10 | void insertWordHelper(TrieNode* root,string word){ 11 | //Base Case 12 | if(word.size()==0){ 13 | root->isTerminal=true; 14 | return; 15 | } 16 | 17 | //small calc 18 | int index = word[0]-'a'; 19 | TrieNode *child; 20 | if(root->children[index] !=NULL){ 21 | child = root->children(index); 22 | }else{ 23 | child = new TrieNode(word[0]); 24 | root->children[index] = child; 25 | } 26 | 27 | //Recusrsive call 28 | insertWord(child,word.substr(1)); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /013 - Tries and Huffman Coding/TrieNode.h: -------------------------------------------------------------------------------- 1 | class TrieNode{ 2 | public: 3 | char data; 4 | TrieNode **children; 5 | bool isTerminal; 6 | TrieNode(char data){ 7 | thos->data = data; 8 | children = new TrieNode*[26]; 9 | for(int i=0;i<26;i++) children[i]=NULL; 10 | isTerminal = false; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /013 - Tries and Huffman Coding/trie_practice.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include"TrieNode.h" 3 | using namespace std; 4 | int main(){ 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /015 - DP/001 Fibbonacci.cpp: -------------------------------------------------------------------------------- 1 | int fibbonaci(int n){ 2 | int *arr = new int[n+1]; 3 | int arr[0]=1; 4 | int arr[1]=1; 5 | for(int i=2;i<=n;i++){ 6 | arr[i]=arr[i-1]+arr[i-2]; 7 | } 8 | return arr[n]; 9 | } 10 | -------------------------------------------------------------------------------- /015 - DP/002 Min Steps to 1.cpp: -------------------------------------------------------------------------------- 1 | int countStepsToOne(int n) 2 | { 3 | int *arr = new int[n+1]; 4 | arr[1]=0; 5 | for(int i=1;i<=n;i++){ 6 | int x=INT_MAX,y=INT_MAX; 7 | if(i%3==0) x=arr[i/3]; 8 | if(i%2==0) y=arr[i/2]; 9 | arr[i] = 1+min(arr[i-1],min(x,y)); 10 | } 11 | return arr[n]-1; 12 | } 13 | -------------------------------------------------------------------------------- /015 - DP/003 Staircase using Dp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int m =1e9+7; 4 | long staircase(int n) 5 | { 6 | long int* a = new long int[n+1]; 7 | a[0] = 1; 8 | a[1] = 1; 9 | a[2] = 2; 10 | 11 | for(int i =3;i<=n;i++) 12 | { 13 | a[i] = ((a[i-1]%m+a[i-2]%m)%m+a[i-3]%m)%m; 14 | } 15 | return a[n]; 16 | } 17 | 18 | int main(){ 19 | // write your code here 20 | int t; 21 | cin>>t; 22 | while(t--){ 23 | int n; 24 | cin >> n; 25 | cout << staircase(n)< 2 | int minCount(int n) 3 | { 4 | int *arr = new int[n+1]; 5 | arr[1]=1; 6 | for(int i=2;i<=n;i++){ 7 | int j; 8 | int k=INT_MAX; 9 | for(j=1;j*j<=i;j++){ 10 | k=min(k,1+arr[i-(j*j)]); 11 | } 12 | if(j*j==i) k=1; 13 | arr[i]=k; 14 | } 15 | return arr[n]; 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /015 - DP/005 No. of balanced BTs using DP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int balancedBTs(int n) { 3 | // Write your code here 4 | int* dp = new int[n+1]; 5 | int mod = (int)pow(10,9)+7; 6 | dp[0]=1; 7 | dp[1]=1; 8 | 9 | for(int i=2;i<=n;i++){ 10 | dp[i]=(int)( ( (long)(dp[i-1])*dp[i-1] )%mod + (2*(long)(dp[i-1])*dp[i-2])%mod ) % mod; 11 | 12 | } 13 | 14 | return dp[n]; 15 | } 16 | -------------------------------------------------------------------------------- /015 - DP/006 Min Cost Path(Memorization).cpp: -------------------------------------------------------------------------------- 1 | const int M=1e2; 2 | const int N=1e2; 3 | int dp[M][N]; 4 | int helper(int** input,int m,int n,int i,int j){ 5 | if(i==m-1 && j==n-1){ 6 | return input[i][j]; 7 | } 8 | if(i>=m || j>=n){ 9 | return INT_MAX; 10 | } 11 | 12 | if(dp[i][j]!=-1) return dp[i][j]; 13 | 14 | 15 | int x = helper(input,m,n,i+1,j); 16 | int y = helper(input,m,n,i+1,j+1); 17 | int z = helper(input,m,n,i,j+1); 18 | 19 | 20 | dp[i][j] = input[i][j]+min(x,min(y,z)); 21 | return dp[i][j]; 22 | } 23 | int minCostPath(int **input, int m, int n) 24 | { 25 | //Write your code here 26 | for(int i=0;i=0;j--){ 11 | dp[m-1][j]=input[m-1][j]+dp[m-1][j+1]; 12 | } 13 | 14 | //Last Column fill - j fixed 15 | for(int i=m-2;i>=0;i--){ 16 | dp[i][n-1]=dp[i+1][n-1]+input[i][n-1]; 17 | } 18 | 19 | //fill remaining cells 20 | for(int i=m-2;i>=0;i--){ 21 | for(int j=n-2;j>=0;j--){ 22 | dp[i][j] = min(dp[i][j+1],min(dp[i+1][j+1],dp[i+1][j])) + input[i][j]; 23 | } 24 | } 25 | 26 | return dp[0][0]; 27 | } 28 | -------------------------------------------------------------------------------- /015 - DP/008 Longest Common Subsequence.cpp: -------------------------------------------------------------------------------- 1 | //Recursive Solution: 2 | //Time Complexity: Exponential 3 | 4 | class Solution { 5 | public: 6 | int longestCommonSubsequence(string text1, string text2) { 7 | if(text1.length()==0 || text2.length()==0){ 8 | return 0; 9 | } 10 | if(text1[0]==text2[0]){ 11 | return 1+longestCommonSubsequence(text1.substr(1),text2.substr(1)); 12 | } 13 | int a = longestCommonSubsequence(text1.substr(1),text2); 14 | int b = longestCommonSubsequence(text1.substr(1),text2.substr(1)); 15 | int c = longestCommonSubsequence(text1,text2.substr(1)); 16 | return max(a,max(c,b)); 17 | } 18 | }; 19 | 20 | 21 | //Memorization Approch: 22 | //Time Complexity: O(s1.length() * s2.length()) 23 | 24 | class Solution { 25 | public: 26 | int dp[1000][1000]; 27 | int helper(string text1, string text2,int i,int j){ 28 | if(text1.length()==0 || text2.length()==0){ 29 | return 0; 30 | } 31 | if(dp[i][j]!=-1){ 32 | return dp[i][j]; 33 | } 34 | if(text1[0]==text2[0]){ 35 | dp[i][j] = 1+helper(text1.substr(1),text2.substr(1),i+1,j+1); 36 | return dp[i][j]; 37 | } 38 | int a = helper(text1.substr(1),text2,i+1,j); 39 | int b = helper(text1.substr(1),text2.substr(1),i+1,j+1); 40 | int c = helper(text1,text2.substr(1),i,j+1); 41 | dp[i][j]=max(a,max(b,c)); 42 | return dp[i][j]; 43 | } 44 | int longestCommonSubsequence(string text1, string text2) { 45 | for(int i=0;i<1000;i++){ 46 | for(int j=0;j<1000;j++){ 47 | dp[i][j]=-1; 48 | } 49 | } 50 | return helper(text1,text2,0,0); 51 | } 52 | }; 53 | 54 | 55 | 56 | 57 | //Dynamic Programming: 58 | //Time Complexity: O(s1.length() * s2.length()) 59 | 60 | 61 | class Solution { 62 | public: 63 | int dp[1001][1001]; 64 | int longestCommonSubsequence(string text1, string text2) { 65 | //First Row Fill up 66 | for(int j=text2.length();j>=0;j--) dp[0][j]=0; 67 | //First Col Fill up 68 | for(int i=text1.length();i>=0;i--) dp[i][0]=0; 69 | 70 | //Then fill the all cells row wise 71 | for(int i=1;i<=text1.length();i++){ 72 | for(int j=1;j<=text2.length();j++){ 73 | //If first characeter match or not 74 | if(text1[text1.length()-i]==text2[text2.length()-j]){ 75 | dp[i][j]=1+dp[i-1][j-1]; 76 | }else{ 77 | int a = dp[i-1][j]; 78 | int b = dp[i][j-1]; 79 | int c = dp[i-1][j-1]; 80 | dp[i][j] = max(a,max(b,c)); 81 | } 82 | } 83 | } 84 | return dp[text1.length()][text2.length()]; 85 | } 86 | }; 87 | -------------------------------------------------------------------------------- /015 - DP/009 Edit Distance.cpp: -------------------------------------------------------------------------------- 1 | int editDistance(string s1, string s2) { 2 | // Write your code here 3 | if(s1.length()==0 && s2.length()==0) return 0; 4 | if(s1.length()==0 && s2.length()!=0) return s2.length(); 5 | if(s1.length()!=0 && s2.length()==0) return s1.length(); 6 | 7 | if(s1[0]==s2[0]){ 8 | return editDistance(s1.substr(1),s2.substr(1)); 9 | } 10 | 11 | int a = editDistance(s1.substr(1),s2)+1; 12 | int b = editDistance(s1,s2.substr(1))+1; 13 | int c = editDistance(s1.substr(1),s2.substr(1))+1; 14 | 15 | return min(a,min(b,c)); 16 | } 17 | -------------------------------------------------------------------------------- /015 - DP/010 Edit Distance (Memoization).cpp: -------------------------------------------------------------------------------- 1 | const int N=1001; 2 | int dp[N][N]; 3 | int helper(string s1,string s2,int i,int j){ 4 | if(s1.length()==0 || s2.length()==0){ 5 | return max(s1.length(),s2.length()); 6 | } 7 | 8 | if(dp[i][j]!=-1) return dp[i][j]; 9 | 10 | if(s1[0]==s2[0]){ 11 | dp[i][j] = helper(s1.substr(1),s2.substr(1),i+1,j+1); 12 | return dp[i][j]; 13 | } 14 | 15 | int a = helper(s1.substr(1),s2,i+1,j)+1; 16 | int b = helper(s1,s2.substr(1),i,j+1)+1; 17 | int c = helper(s1.substr(1),s2.substr(1),i+1,j+1)+1; 18 | 19 | dp[i][j]=min(a,min(b,c)); 20 | return dp[i][j]; 21 | } 22 | int editDistance(string s1, string s2) 23 | { 24 | //Write your code here 25 | 26 | for(int i=0;i 2 | using namespace std; 3 | 4 | void print(int **edges,int n,int sv,bool *visited){ 5 | cout<>n>>e; 19 | int **edges = new int*[n]; 20 | for(int i=0;i>fi>>si; 30 | edges[fi][si]=1; 31 | edges[si][fi]=1; 32 | } 33 | 34 | bool *visited = new bool[n]; 35 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | void printBFS(int **edges,int n,int sv,bool *visited){ 6 | queue pendingVertices; 7 | pendingVertices.push(sv); 8 | visited[sv]=true; 9 | while(pendingVertices.size()!=0){ 10 | int temp = pendingVertices.front(); 11 | pendingVertices.pop(); 12 | cout<>n>>e; 26 | int **edges = new int*[n]; 27 | for(int i=0;i>fi>>si; 37 | edges[fi][si]=1; 38 | edges[si][fi]=1; 39 | } 40 | 41 | bool *visited = new bool[n]; 42 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | void printDFS(int **edges,int n,int sv,bool *visited){ 6 | cout< pendingVertices; 19 | pendingVertices.push(sv); 20 | visited[sv]=true; 21 | while(pendingVertices.size()!=0){ 22 | int temp = pendingVertices.front(); 23 | pendingVertices.pop(); 24 | cout<>n>>e; 58 | int **edges = new int*[n]; 59 | for(int i=0;i>fi>>si; 69 | edges[fi][si]=1; 70 | edges[si][fi]=1; 71 | } 72 | 73 | cout<<"DFS"< 2 | #include 3 | using namespace std; 4 | 5 | bool hasPath(int **edges,int n,int sv,bool *visited,int ei){ 6 | queue pendingVertices; 7 | pendingVertices.push(sv); 8 | visited[sv]=true; 9 | while(pendingVertices.size()!=0){ 10 | int temp = pendingVertices.front(); 11 | pendingVertices.pop(); 12 | if(temp==ei){ 13 | return true; 14 | break; 15 | } 16 | for(int i=0;i>n>>e; 30 | int **edges = new int*[n]; 31 | for(int i=0;i>fi>>si; 41 | edges[fi][si]=1; 42 | edges[si][fi]=1; 43 | } 44 | 45 | int si,ei; 46 | cin>>si>>ei; 47 | 48 | bool *visited = new bool[n]; 49 | for(int i=0;i 2 | using namespace std; 3 | class Edge{ 4 | public: 5 | int source; 6 | int destination; 7 | int weight; 8 | }; 9 | int findPar(int v,int* parent){ 10 | if(parent[v]==v) return v; 11 | return findPar(parent[v],parent); 12 | } 13 | int main(){ 14 | int n,e; 15 | cin>>n>>e; 16 | Edge* input = new Edge[e]; 17 | Edge* output = new Edge[n-1]; 18 | int* parent = new int[n]; 19 | for(int i=0;i>input[i].source; 23 | cin>>input[i].destination; 24 | cin>>input[i].weight; 25 | } 26 | //Sort the Input Array 27 | sort(input, input+e, [](const Edge& lhs, const Edge& rhs) { 28 | return lhs.weight < rhs.weight; 29 | }); 30 | 31 | int count=0; 32 | int i=0; 33 | while(countreal = real; 10 | this->imaginary = imaginary; 11 | } 12 | ~ComplexNumbers(){ 13 | 14 | } 15 | void print(){ 16 | cout<real<<" + i"<imaginary<real = this->real+c2.real; 20 | this->imaginary = this->imaginary+c2.imaginary; 21 | } 22 | void multiply(ComplexNumbers const &c2){ 23 | int temp = this->real; 24 | this->real = ((this->real)*(c2.real))-((this->imaginary)*(c2.imaginary)); 25 | this->imaginary = ((this->imaginary)*(c2.real))+((temp)*(c2.imaginary)); 26 | } 27 | 28 | 29 | }; 30 | -------------------------------------------------------------------------------- /017 - Oops/OOPS 1/complex_no_program.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include "complex_no_class.cpp" 4 | int main() { 5 | int real1, imaginary1, real2, imaginary2; 6 | 7 | cin >> real1 >> imaginary1; 8 | cin >> real2 >> imaginary2; 9 | 10 | ComplexNumbers c1(real1, imaginary1); 11 | ComplexNumbers c2(real2, imaginary2); 12 | 13 | 14 | int choice; 15 | cin >> choice; 16 | 17 | if(choice == 1) { 18 | c1.plus(c2); 19 | c1.print(); 20 | } 21 | else if(choice == 2) { 22 | c1.multiply(c2); 23 | c1.print(); 24 | } 25 | else { 26 | return 0; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /017 - Oops/OOPS 1/fraction_class.cpp: -------------------------------------------------------------------------------- 1 | class Fraction{ 2 | private: 3 | int num; 4 | int den; 5 | 6 | public: 7 | //Constructor 8 | Fraction(int num,int den){ 9 | this->num = num; 10 | this->den = den; 11 | } 12 | //Destructor 13 | ~Fraction(){ 14 | 15 | } 16 | //Print Fraction 17 | void print(){ 18 | cout<num<<"/"<den<num,this->den); 24 | for(int i=1;inum%i == 0 && this->den%i == 0){ 26 | gcd=i; 27 | } 28 | } 29 | this->num = this->num / gcd; 30 | this->den = this->den / gcd; 31 | } 32 | //Add two fraction 33 | void add(Fraction const &f2){ 34 | int lcm = (this->den)*(f2.den); 35 | int update_num = ((lcm/this->den)*this->num)+((lcm/f2.den)*f2.num); 36 | this->num = update_num; 37 | this->den = lcm; 38 | simplify(); 39 | } 40 | //Multiply two fraction 41 | void multiply(Fraction const &f2){ 42 | this->num = this->num * f2.num; 43 | this->den = this->den * f2.den; 44 | simplify(); 45 | } 46 | 47 | 48 | }; 49 | -------------------------------------------------------------------------------- /017 - Oops/OOPS 1/fraction_program.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include "fraction_class.cpp" 4 | int main(){ 5 | Fraction f1(12,20); 6 | f1.print(); 7 | Fraction f2(12,20); 8 | f2.print(); 9 | f1.add(f2); 10 | f1.print(); 11 | f1.multiply(f2); 12 | f1.print(); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /017 - Oops/OOPS 1/program_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include "student_class.cpp" 4 | int main(){ 5 | 6 | 7 | /* Uses of Constructor and Parameterzied */ 8 | 9 | // Student s1(12); 10 | // cout<display(); 14 | 15 | 16 | 17 | /* By Default Copy Constructor */ 18 | Student s1(12); 19 | s1.display(); 20 | Student s2(15); 21 | s2.display(); 22 | //Copy Assignment Constructor Operator 23 | s2=s1; 24 | s2.display(); 25 | 26 | Student *s3 = new Student(19); 27 | s3->display(); 28 | delete s3; 29 | 30 | 31 | 32 | return 0; 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /017 - Oops/OOPS 1/student_class.cpp: -------------------------------------------------------------------------------- 1 | class Student{ 2 | //variable use 3 | public: 4 | int rollNumber; 5 | private: 6 | int age; 7 | //Function use 8 | public: 9 | //Constructor 10 | Student(int rollNumber){ 11 | this->rollNumber = rollNumber; 12 | } 13 | //Destructor 14 | ~Student(){ 15 | cout<<"Destructor Called"<0){ 25 | age=a; 26 | } 27 | } 28 | 29 | }; 30 | -------------------------------------------------------------------------------- /017 - Oops/OOPS 2/array_dynamic_class.cpp: -------------------------------------------------------------------------------- 1 | class DynamicArray{ 2 | int *data; 3 | int nextIndex; 4 | int capacity; 5 | public: 6 | DynamicArray(){ 7 | data = new int[5]; 8 | nextIndex = 0; 9 | capacity = 5; 10 | } 11 | DynamicArray(DynamicArray const &d){ 12 | this->data = new int[d.capacity]; 13 | for(int i=0;idata[i] = d.data[i]; 15 | } 16 | this->nextIndex = d.nextIndex; 17 | this->capacity = d.capacity; 18 | } 19 | void add(int ele){ 20 | if(nextIndex == capacity){ 21 | int *newData = new int[2*capacity]; 22 | for(int i=0;i 2 | using namespace std; 3 | #include "array_dynamic_class.cpp" 4 | int main(){ 5 | DynamicArray d1; 6 | d1.add(10); 7 | d1.add(20); 8 | d1.add(30); 9 | d1.add(40); 10 | d1.add(50); 11 | d1.add(60); 12 | d1.print(); 13 | 14 | 15 | //Copy Assignment and Copt Constructor do shallow copy 16 | DynamicArray d2(d1); 17 | d2.add(0,100); 18 | d2.print(); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /017 - Oops/OOPS 2/fraction_class.cpp: -------------------------------------------------------------------------------- 1 | class Fraction{ 2 | private: 3 | int num; 4 | int den; 5 | 6 | public: 7 | //Constructor 8 | Fraction(int num,int den){ 9 | this->num = num; 10 | this->den = den; 11 | } 12 | Fraction(){ 13 | 14 | } 15 | //Destructor 16 | ~Fraction(){ 17 | 18 | } 19 | //Print Fraction 20 | void print(){ 21 | cout<num<<"/"<den<num,this->den); 27 | for(int i=1;inum%i == 0 && this->den%i == 0){ 29 | gcd=i; 30 | } 31 | } 32 | this->num = this->num / gcd; 33 | this->den = this->den / gcd; 34 | } 35 | //Add two fraction 36 | void add(Fraction const &f2){ 37 | int lcm = (this->den)*(f2.den); 38 | int update_num = ((lcm/this->den)*this->num)+((lcm/f2.den)*f2.num); 39 | this->num = update_num; 40 | this->den = lcm; 41 | simplify(); 42 | } 43 | 44 | //Multiply two fraction 45 | void multiply(Fraction const &f2){ 46 | this->num = this->num * f2.num; 47 | this->den = this->den * f2.den; 48 | simplify(); 49 | } 50 | //Add two fraction using operator overloading 51 | Fraction operator+(Fraction const &f2){ 52 | int lcm = (this->den)*(f2.den); 53 | int update_num = ((lcm/this->den)*this->num)+((lcm/f2.den)*f2.num); 54 | Fraction fp; 55 | fp.num = update_num; 56 | fp.den = lcm; 57 | fp.simplify(); 58 | return fp; 59 | } 60 | 61 | //Multiply two fraction using operator overloading 62 | Fraction operator*(Fraction const &f2){ 63 | Fraction fp; 64 | fp.num = this->num * f2.num; 65 | fp.den = this->den * f2.den; 66 | fp.simplify(); 67 | return fp; 68 | } 69 | 70 | 71 | }; 72 | -------------------------------------------------------------------------------- /017 - Oops/OOPS 2/fraction_program.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include "fraction_class.cpp" 4 | int main(){ 5 | Fraction f1(12,20); 6 | f1.print(); 7 | Fraction f2(12,20); 8 | f2.print(); 9 | // Using Normal Function 10 | // f1.add(f2); 11 | // Using Operator Overloading 12 | f1=f1+f2; 13 | f1.print(); 14 | // Using Normal Function 15 | // f1.multiply(f2); 16 | // Using Operator Overloading 17 | f1=f1*f2; 18 | f1.print(); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /017 - Oops/OOPS 2/polynomial_class.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maityamit/Cpp-DSA-Notes-and-Programs/c3039713ff2305a787b11987e539997f0849f12b/017 - Oops/OOPS 2/polynomial_class.cpp -------------------------------------------------------------------------------- /017 - Oops/OOPS 2/polynomial_program.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maityamit/Cpp-DSA-Notes-and-Programs/c3039713ff2305a787b11987e539997f0849f12b/017 - Oops/OOPS 2/polynomial_program.cpp -------------------------------------------------------------------------------- /017 - Oops/OOPS 2/student_class.cpp: -------------------------------------------------------------------------------- 1 | class Student{ 2 | int age; 3 | char *name; 4 | public: 5 | Student(int age,char *name){ 6 | this->age = age; 7 | this->name = name; 8 | } 9 | void display(){ 10 | cout< 2 | using namespace std; 3 | #include "student_class.cpp" 4 | int main(){ 5 | char name[]="amit"; 6 | Student s1(20,name); 7 | s1.display(); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C++ DSA Notes and Programs 2 | C++ Notes & programms by Amit Maity. I am learning C++ now , so i am sharing all of my notes and codes to everyone to help another. 3 |
4 | 5 | (If you really like this, please star this repo) 6 | 7 |
8 |
9 | 10 | cplusplus 11 | 12 | 13 | ## Handwritten Notes 14 | 15 | ||||| 16 | |:----------------------------------------:|:-----------------------------------------:|:-----------------------------------------:|:-----------------------------------------:| 17 | | [1. Basics of C++](https://drive.google.com/file/d/10rqqrdRyJT0-W_rsGDiBb6-K_VYYn7JA/view?usp=share_link) | [2. Recursion 1](https://drive.google.com/file/d/10t45N_GD7deHVeMJ9Fw_oflpTNN9YsUY/view?usp=share_link) | [3. Recursion 2](https://drive.google.com/file/d/17sGwsoeO1wQOZaB7OO4LolUTiYrWecKF/view?usp=share_link) | [4. Recursion 3](https://drive.google.com/file/d/18WM-q3fWCfQtDspkHXOecLyQ2CVUXeh7/view?usp=share_link) | 18 | | [5. Time Complexity](https://drive.google.com/file/d/18avBDRZC8Jkg6WVJ9sKBjj9QaLnTqF92/view?usp=share_link) | [7. LinkedList 1](https://drive.google.com/file/d/13hwi6nfk-Q2sWu3-NLfy9_BD2oaWwY7U/view?usp=share_link) | [8. LinkedList 2](https://drive.google.com/file/d/1NSVR3zGdQGQFyxZhwDczRJYL3DrJ4yqL/view?usp=share_link) | [9. Stack & Queue](https://drive.google.com/file/d/1PSLXDQIzpHMoq_lhEB6bEE03vaMK2PhB/view?usp=share_link) | 19 | | [10. Tree](https://drive.google.com/file/d/1RVmvMjR6U2sfBMY5GTvGwxUJf9KrkmIN/view?usp=share_link) | [11. Binary Tree](https://drive.google.com/file/d/1Vd9YzwlxecKZkzoQP9xLGg6hpOn3N652/view?usp=share_link) | [12. Binary Search Tree](https://drive.google.com/file/d/1PsESvYkHus_PaE_KbGjemrKd3WL_2j_-/view?usp=share_link) | [13. Priority Queue](https://drive.google.com/file/d/1bbSVajasXSVYlkSF0CpgWB5feIvYz01o/view?usp=share_link) | 20 | | [14. Hashmap](https://drive.google.com/file/d/1_m6cfYXi3ec6-s0qHhfrvYku4XX37AqW/view?usp=share_link) | 21 | --------------------------------------------------------------------------------