├── .gitignore ├── README.md ├── Workshop Day 2 ├── Array │ ├── intersection.cpp │ ├── kadane's algo for positive integers.cpp │ ├── kaden for positive.cpp │ ├── maximum length such that no of zeros and ones are equal.cpp │ ├── range updates.cpp │ ├── segregate positive and negative.cpp │ └── when all elemetns of array is negative.cpp └── Binary Search │ ├── Binary Search.txt │ ├── MisiingNumber3.cpp │ ├── MissingNumber1.cpp │ ├── MissingNumber2.cpp │ ├── Monotonic.cpp │ ├── PrefixBinarySearch.cpp │ ├── PrefixLinear.cpp │ ├── SmallestMissing.cpp │ └── TheEnligtenedOnes.cpp ├── Workshop Day 3 ├── infix to post fix conversion.cpp ├── maximum in every window of size k.cpp ├── next greater element.cpp └── postfix evaluation.cpp ├── Workshop Day 4 ├── heap │ ├── Sort a nearly sorted (or K sorted) array.cpp │ ├── heap implementation.cpp │ └── heap sort.cpp └── tree │ ├── CheckSumTree.cpp │ └── LevelSum.cpp ├── Workshop Day 5 ├── Segment Tree │ ├── Prefix.cpp │ ├── RangeMinUpdate.cpp │ ├── RangeSumSegmentTree.cpp │ ├── RangeSumSimple.cpp │ ├── RangeSumWithLazyPropagation.cpp │ ├── SegmentTree.txt │ └── segment_Tree_For_3rd_Year.pdf └── Trie │ └── Trie.cpp ├── Workshop Day 6 ├── bfs.cpp ├── dfs.cpp ├── dijkstra.cpp ├── dsu.cpp └── kruskal.cpp └── Workshop Day1 ├── DistinctElements1.cpp ├── DistinctElements2.cpp ├── Map1.cpp ├── Map2.cpp ├── STL.txt └── SampleCode3rdYear.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data-Structures-and-Algorithms-Workshop 2 | 3 | ## Day 1 : Standard Template library 4 | * Further reading 5 | 1. [http://www.cplusplus.com/reference/](http://www.cplusplus.com/reference/) 6 | 2. [https://www.geeksforgeeks.org/the-c-standard-template-library-stl/](https://www.geeksforgeeks.org/the-c-standard-template-library-stl/) 7 | 3. [Codes used during lectures](https://github.com/pcon-jsr/Data-Structures-and-Algorithms-Workshop/tree/master/Workshop%20Day1) 8 | * Practice problems 9 | 1. [STL Practice](https://www.hackerrank.com/domains/cpp?filters%5Bsubdomains%5D%5B%5D=stl) 10 | 2. [Easy 1](https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/practice-problems/algorithm/frequency-of-students/) 11 | 3. [Easy 2](https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/practice-problems/algorithm/the-electric-type/) 12 | 4. [Easy Medium 1](https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/practice-problems/algorithm/mind-palaces-3/) 13 | 5. [Medium Hard 1](https://www.hackerrank.com/challenges/sherlock-and-anagrams/problem?h_l=interview&playlist_slugs%5B%5D%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D%5B%5D=dictionaries-hashmaps) 14 | 6. [Easy Medium 2](https://practice.geeksforgeeks.org/problems/relative-sorting/0) 15 | 16 | ## Day 2 : Arrays , Binary Search , Linked Lists 17 | * Further Reading 18 | 1. [Detect loop in a linked list](https://www.geeksforgeeks.org/detect-loop-in-a-linked-list/) 19 | 2. [How Does Floyd Cycle Detection Work](https://www.geeksforgeeks.org/how-does-floyds-slow-and-fast-pointers-approach-work/) 20 | 3. [Check if a linked list is a Palindrome](https://www.geeksforgeeks.org/function-to-check-if-a-singly-linked-list-is-palindrome/) 21 | 4. [Binary Search Reference](https://www.topcoder.com/community/data-science/data-science-tutorials/binary-search/) 22 | 5. [Codes used during lectures](https://github.com/pcon-jsr/Data-Structures-and-Algorithms-Workshop/tree/master/Workshop%20Day%202) 23 | * Practice Problems 24 | 1. [Easy 1 Linked List](https://www.hackerrank.com/challenges/reverse-a-doubly-linked-list/problem) 25 | 2. [Easy Medium 1 Linked List](https://www.hackerrank.com/challenges/find-the-merge-point-of-two-joined-linked-lists/problem) 26 | 3. [Easy Medium 2 Linked List](https://www.hackerrank.com/challenges/delete-duplicate-value-nodes-from-a-sorted-linked-list/problem) 27 | 4. [Medium Hard 1 Linked List](https://practice.geeksforgeeks.org/problems/clone-a-linked-list-with-next-and-random-pointer/1) 28 | 5. [Easy 1 Binary Search](https://www.hackerearth.com/practice/algorithms/searching/binary-search/practice-problems/algorithm/monks-encounter-with-polynomial/) 29 | 6. [Easy 2 Binary Search(lower bound implementation)](https://www.hackerearth.com/practice/algorithms/searching/binary-search/practice-problems/algorithm/the-soap-mystery/) 30 | 7. [Easy 3 Binary Search](https://www.hackerearth.com/practice/algorithms/searching/binary-search/practice-problems/algorithm/highest-average-64bdd761/) 31 | 8. [Easy Medium 1 Binary Search](https://www.hackerearth.com/practice/algorithms/searching/binary-search/practice-problems/algorithm/the-enlightened-ones/) 32 | 9. [Easy Medium 2 Binary Search](https://www.spoj.com/problems/AGGRCOW/) 33 | 10. [Medium Hard Binary Search](https://www.hackerrank.com/contests/practice-1-pcon/challenges/little-geometry/problem) 34 | 11. [Easy 1 Array](https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/long-atm-queue-3/) 35 | 12. [Easy 2 Array](https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/can-you-solve-it/) 36 | 13. [Medium Array](http://codeforces.com/problemset/problem/224/B) 37 | 38 | ## Day 3 : Stacks, Queues , Strings 39 | * Further Reading 40 | 1. [Longest Substring with k distinct characters](https://www.geeksforgeeks.org/find-the-longest-substring-with-k-unique-characters-in-a-given-string/) 41 | 2. [Maximum area in a histogram](https://www.geeksforgeeks.org/largest-rectangle-under-histogram/) 42 | 3. [Stock Span Problem](https://www.geeksforgeeks.org/the-stock-span-problem/) 43 | 4. [Stack using queue](https://www.geeksforgeeks.org/implement-stack-using-queue/) 44 | 5. [Minimum Element in a stack](https://www.geeksforgeeks.org/design-a-stack-that-supports-getmin-in-o1-time-and-o1-extra-space/) 45 | 6. [Codes used during lectures](https://github.com/pcon-jsr/Data-Structures-and-Algorithms-Workshop/tree/master/Workshop%20Day%203) 46 | * Practice Problems 47 | 1. [Easy 1 String](https://www.hackerrank.com/challenges/pangrams/problem) 48 | 2. [Easy 2 String](https://www.hackerrank.com/challenges/reduced-string/problem) 49 | 3. [Easy Medium 1 String](https://www.hackerearth.com/practice/algorithms/string-algorithm/basics-of-string-manipulation/practice-problems/algorithm/make-the-cheapest-palindrome-1/) 50 | 4. [Easy 3 String](https://www.hackerearth.com/practice/algorithms/string-algorithm/basics-of-string-manipulation/practice-problems/algorithm/secret-messages/) 51 | 5. [Easy 1 Stack and Queue](https://www.hackerearth.com/practice/data-structures/stacks/basics-of-stacks/practice-problems/algorithm/stakth-1-e6a76632/) 52 | 6. [Easy 2 Stack and Queue](https://www.hackerearth.com/practice/data-structures/stacks/basics-of-stacks/practice-problems/algorithm/sniper-shooting/) 53 | 7. [Easy 3 Stack and Queue](https://www.hackerearth.com/practice/data-structures/stacks/basics-of-stacks/practice-problems/algorithm/staque-1-e790a29f/) 54 | 8. [Easy Medium 1 Stack and Queue](https://www.hackerrank.com/challenges/game-of-two-stacks/problem) 55 | 9. [Easy Medium 2 Stack and Queue](https://www.hackerrank.com/challenges/castle-on-the-grid/problem) 56 | 57 | ## Day 4 : Trees , Heaps 58 | * Further Reading 59 | 1. [Handshaking Lemma](https://www.geeksforgeeks.org/handshaking-lemma-and-interesting-tree-properties/) 60 | 2. [Binary Tree Types](https://www.geeksforgeeks.org/binary-tree-set-3-types-of-binary-tree/) 61 | 3. [Binary Tree Hackerearth](https://www.hackerearth.com/practice/data-structures/trees/binary-and-nary-trees/tutorial/) 62 | 4. [Binary Search Tree Hackerearth](https://www.hackerearth.com/practice/data-structures/trees/binary-search-tree/tutorial/) 63 | 5. [Binary Search Tree Blog](http://sleepincode.blogspot.com/2017/06/all-about-binary-search-trees.html?m=1) 64 | 6. [Distance between two nodes in a Binary Tree](https://www.geeksforgeeks.org/find-distance-between-two-nodes-of-a-binary-tree/) 65 | 7. [Diameter of Binary Tree using DFS](https://www.geeksforgeeks.org/diameter-tree-using-dfs/) 66 | 8. [Check for Sum path with value k](https://www.geeksforgeeks.org/root-to-leaf-path-sum-equal-to-a-given-number/) 67 | 9. [Codes used during Lecture](https://github.com/pcon-jsr/Data-Structures-and-Algorithms-Workshop/tree/master/Workshop%20Day%204) 68 | * Practice Problems 69 | 1. [Easy 1 Tree](https://practice.geeksforgeeks.org/problems/vertical-sum/1) 70 | 2. [Easy 2 Tree](https://www.hackerearth.com/practice/data-structures/trees/binary-and-nary-trees/practice-problems/algorithm/mirror-image-2/) 71 | 3. [Easy Medium 1 Tree](https://www.hackerearth.com/practice/data-structures/trees/binary-and-nary-trees/practice-problems/algorithm/mancunian-and-colored-tree/) 72 | 4. [Easy 1 Priority Queue](https://www.hackerearth.com/practice/data-structures/trees/heapspriority-queues/practice-problems/algorithm/monk-and-multiplication/) 73 | 5. [Easy 2 Priority Queue](https://practice.geeksforgeeks.org/problems/minimum-cost-of-ropes/0) 74 | 6. [Easy Medium 1 Priority Queue](https://practice.geeksforgeeks.org/problems/merge-k-sorted-arrays/1) 75 | 76 | ## Day 5 : Trie , Segment Tree 77 | * Further Reading 78 | 1. [Blog on Tries](https://threads-iiith.quora.com/Tutorial-on-Trie-and-example-problems) 79 | 2. [Codeforces blog on Segment Tree](https://codeforces.com/blog/entry/18051) 80 | 3. [Blog on Segment Trees](https://kartikkukreja.wordpress.com/2014/11/09/a-simple-approach-to-segment-trees/) 81 | 4. [Codes used during lectures](https://github.com/pcon-jsr/Data-Structures-and-Algorithms-Workshop/tree/master/Workshop%20Day%205) 82 | * Practice Problems 83 | 1. [Medium 1 Trie](https://www.hackerearth.com/practice/data-structures/advanced-data-structures/trie-keyword-tree/practice-problems/algorithm/registration-system/) 84 | 2. [Medium 1 Segment Tree](https://www.spoj.com/problems/HORRIBLE/) 85 | 3. [Medium 2 Segment Tree](https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/little-deepu-and-array/) 86 | 87 | ## Day 6 : Graph Theory I 88 | * Further Reading 89 | 1. [Breadth First Search](https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/tutorial/) 90 | 2. [Depth First Search](https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/tutorial/) 91 | 3. [Disjoint Set Union (DSU)](https://www.hackerearth.com/practice/data-structures/disjoint-data-strutures/basics-of-disjoint-data-structures/tutorial/) 92 | 4. [Minimum Spanning Tree](https://www.hackerearth.com/practice/algorithms/graphs/minimum-spanning-tree/tutorial) 93 | 5. [Dijkstra](https://www.hackerearth.com/practice/algorithms/graphs/shortest-path-algorithms/tutorial/) 94 | 6. [Codes used during lectures](https://github.com/pcon-jsr/Data-Structures-and-Algorithms-Workshop/tree/master/Workshop%20Day%206) 95 | * Practice Problems 96 | 1. [Easy 1 BFS](https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/practice-problems/algorithm/monk-and-the-islands/) 97 | 2. [Easy Medium 1 BFS](https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/practice-problems/algorithm/the-circular-jump-9515a45c/) 98 | 3. [Easy Medium 2 BFS](https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/practice-problems/algorithm/the-witches-of-hegwarts-1/) 99 | 4. [Easy Medium 3 BFS](https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/practice-problems/algorithm/dhoom-4/) 100 | 5. [Medium BFS](https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/practice-problems/algorithm/connected-horses-10/) 101 | 6. [Easy 1 DFS](https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/aryas-stunt-63b3da17/) 102 | 7. [Easy 2 DFS](https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/monk-and-graph-problem/) 103 | 8. [Easy Medium DFS](https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/easylife/description/) 104 | 9. [Medium DFS](https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/sg-and-trees/) 105 | 10. [Medium DFS](https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/prison-break-5/) 106 | 11. [Medium DFS](https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/comrades-i-3/) 107 | 12. [Easy MST(Maximum Spanning Tree)](https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/maximum-spanning-tree/) 108 | 13. [Medium DSU(Disjoint Set Union)](https://code.google.com/codejam/contest/3254486/dashboard#s=p1) 109 | 14. [Hard DSU](http://codeforces.com/contest/1027/problem/F) 110 | 15. [Medium Hard](http://codeforces.com/contest/1037/problem/E) 111 | 16. [Easy Medium](https://www.hackerrank.com/challenges/torque-and-development/problem) 112 | ## Day 7 : Graph 2 113 | * Further Reading 114 | 1. [Strongly Connected Components](https://www.hackerearth.com/practice/algorithms/graphs/strongly-connected-components/tutorial/) 115 | 2. [Euler Tour](https://www.geeksforgeeks.org/euler-tour-tree/) 116 | 3. [Hamiltonian Path](https://www.hackerearth.com/practice/algorithms/graphs/hamiltonian-path/tutorial/) 117 | 4. [Articulation Points and Bridges](https://www.hackerearth.com/practice/algorithms/graphs/articulation-points-and-bridges/practice-problems/) 118 | * Practice Problems 119 | 1. [Shortest Path Easy 1](https://www.hackerearth.com/practice/algorithms/graphs/shortest-path-algorithms/practice-problems/algorithm/mittal-wants-to-go-to-play/) 120 | 2. [Shortest Path Easy Medium 1](https://www.hackerearth.com/practice/algorithms/graphs/shortest-path-algorithms/practice-problems/algorithm/irctc/) 121 | 3. [Shortest Path Medium 1](https://www.hackerrank.com/challenges/dijkstrashortreach/submissions) 122 | 4. [Hamiltonian Path Easy 1](https://www.hackerearth.com/practice/algorithms/graphs/hamiltonian-path/practice-problems/algorithm/micro-and-coins/) 123 | 5. [Hamiltonian Path Easy 2](https://www.hackerearth.com/practice/algorithms/graphs/hamiltonian-path/practice-problems/algorithm/micro-and-permutations/) 124 | 6. [SCC Easy 1](https://www.hackerearth.com/practice/algorithms/graphs/strongly-connected-components/practice-problems/algorithm/a-walk-to-remember-qualifier2/) 125 | 7. [Shortest Path Easy Medium 2](https://www.hackerearth.com/practice/algorithms/graphs/shortest-path-algorithms/practice-problems/algorithm/booze-first-76e979dd/) 126 | 8. [Bridges and AP Easy 1](https://www.hackerearth.com/practice/algorithms/graphs/articulation-points-and-bridges/practice-problems/algorithm/rhezo-and-destructive-mind/) 127 | 9. [Bridges and AP Easy Medium 2](https://www.hackerearth.com/practice/algorithms/graphs/articulation-points-and-bridges/practice-problems/algorithm/sg-and-graphs/) 128 | ## Day 8 : Dynamic Programming I 129 | * Further Reading 130 | 1. [Introduction to Dynamic Programming I](https://www.youtube.com/watch?v=OQ5jsbhAv_M&feature=youtu.be&list=PLfMspJ0TLR5HRFu2kLh3U4mvStMO8QURm) 131 | 2. [Introduction to Dynamic Programming II](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/MIT6_006F11_lec19.pdf) 132 | 3. [LCA - Lowest Common Ancestor and Sparse Table](https://www.topcoder.com/community/data-science/data-science-tutorials/range-minimum-query-and-lowest-common-ancestor/#Lowest%20Common%20Ancestor%20(LCA)) 133 | 4. [Unbounded Knapsack](https://www.geeksforgeeks.org/unbounded-knapsack-repetition-items-allowed/) 134 | 5. [Fractional Knapsack](https://www.geeksforgeeks.org/fractional-knapsack-problem/) 135 | * Practice Problems 136 | 1. [Maximim Sum such that no two elements are adjacent](https://www.geeksforgeeks.org/maximum-sum-such-that-no-two-elements-are-adjacent/) 137 | 2. [Maximum Sum sub array in an array(Kadane’s Algorithm)](https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/) 138 | 3. [Longest Increasing Subsequence](https://www.geeksforgeeks.org/longest-increasing-subsequence-dp-3/) 139 | 4. [Longest Common Subsequence in given two Strings](https://www.geeksforgeeks.org/longest-common-subsequence-dp-4/) 140 | 5. [0/1 Knapsach Bounded](https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/) 141 | 6. [Easy Medium 1](https://www.hackerearth.com/practice/algorithms/dynamic-programming/introduction-to-dynamic-programming-1/practice-problems/algorithm/coin-problem-2/) 142 | 7. [Easy Medium 2](https://www.hackerearth.com/practice/algorithms/dynamic-programming/2-dimensional/practice-problems/algorithm/largest-subsequence-c554fb8c/) 143 | -------------------------------------------------------------------------------- /Workshop Day 2/Array/intersection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //this function will print the intersection of two array 5 | void findIntersection(int ar1[],int ar2[],int n1,int n2) 6 | { 7 | 8 | //take an unordered map to store the count of each array element 9 | unordered_map mp; 10 | for(int i=0;i0 16 | that means this element is also present in first array 17 | print it and reduce the count of corresponding element by 1 18 | and propagate further 19 | */ 20 | for(int i=0;i>n1>>n2; 34 | int ar1[n1+2],ar2[n2+2]; 35 | for(int i=0;i>ar1[i]; 37 | for(int i=0;i>ar2[i]; 39 | findIntersection(ar1,ar2,n1,n2); 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Workshop Day 2/Array/kadane's algo for positive integers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int kaden(int ar[],int n) 4 | { 5 | int max_so_far=0,max_ending_here=0; 6 | for(int i=0;i>n; 20 | int ar[n+2]; 21 | for(int i=0;i>ar[i]; 23 | int mxSum=kaden(ar,n); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Workshop Day 2/Array/kaden for positive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int kaden(int ar[],int n) 4 | { 5 | int max_so_far=0,max_ending_here=0; 6 | for(int i=0;i>n; 20 | int ar[n+2]; 21 | for(int i=0;i>ar[i]; 23 | int mxSum=kaden(ar,n); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Workshop Day 2/Array/maximum length such that no of zeros and ones are equal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //this funtion will return the maximum length of sub array 5 | //that contains equal no of zeros and ones 6 | int maxLen(int ar[],int n) 7 | { 8 | 9 | //take the map which will store the first occurrences of each 10 | //sum sum in cumulative array if that sum is not zero 11 | map mp; 12 | 13 | //it will take the cumulative sum of all elements of array taking 1 as 1 and 0 as -1 14 | int cumSum[n+2]; 15 | for(int i=0;i<=n;i++) 16 | cumSum[i]=0; 17 | 18 | //0 will contribute -1 quantity and 1 will contribute 1 quantity to total sum 19 | cumSum[1]=(ar[1]==1)?1:-1; 20 | 21 | //taking cumulative sum 22 | for(int i=2;i<=n;i++) 23 | cumSum[i]=cumSum[i-1]+((ar[i]==1)?1:-1); 24 | //now the question becomes finding the maximum length of sub array in which total 25 | //sum is zero 26 | 27 | //maxLen will store the maximum length 28 | int mxLen=0; 29 | for(int i=1;i<=n;i++) 30 | { 31 | 32 | /* 33 | if cumulative sum up to this point is zero from the initiation then 34 | no of zeros and ones are equal in no from the starting of array 35 | */ 36 | if(cumSum[i]==0) 37 | mxLen=i; 38 | 39 | /* 40 | if this cumulative sum is not visited previously then 41 | store the first index at which this sum occur 42 | */ 43 | else if(mp[cumSum[i]]==0) 44 | mp[cumSum[i]]=i; 45 | 46 | /* 47 | if this sum is previously visited then from that point up to this 48 | current point i the sum of that sub array has contributed a total of zero value 49 | as the value remains constant so update the max length accordingly 50 | */ 51 | else if(mp[cumSum[i]]) 52 | mxLen=max(mxLen,i-mp[cumSum[i]]); 53 | } 54 | 55 | //return the maximum length 56 | return mxLen; 57 | } 58 | int main() 59 | { 60 | int n; 61 | cin>>n; 62 | int ar[n+2]; 63 | for(int i=1;i<=n;i++) 64 | { 65 | cin>>ar[i]; 66 | } 67 | int len=maxLen(ar,n); 68 | cout<<"Maximum length subarray is:"< 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin>>n; 7 | int ar[n+2]; 8 | for(int i=0;i>ar[i]; 10 | int q; 11 | 12 | //no of queries 10^5 13 | cin>>q; 14 | while(q--) 15 | { 16 | 17 | //left range, right range and value to be summed in given range 18 | int l,r,val; 19 | cin>>l>>r>>val; 20 | 21 | //l should be less than number of elements in array 22 | if(l>n) 23 | continue; 24 | //Generally, we would add the values to all the elements in the given range 25 | //but here we just mark the range that's given in the query 26 | 27 | //Add value to the first element of given range so that in taking cumulative sum after all queries 28 | //the val quantity should be propagate from L to R 29 | ar[l]+=val; 30 | 31 | //after range ends,i.e., after r the value(val) should not be propagated any further 32 | if(r+1>q1; 43 | while(q1--) 44 | { 45 | int index; 46 | cin>>index; 47 | cout< 2 | using namespace std; 3 | 4 | //this function will segregate negattive in left side and positive in right side 5 | void segregate(int ar[],int n) 6 | { 7 | 8 | //initialize i to the left of array and j to the right of array 9 | int i=0,j=n-1; 10 | while(i=0) 21 | j--; 22 | 23 | //if elements are out of order then swap them 24 | swap(ar[i],ar[j]); 25 | i++; 26 | j--; 27 | } 28 | } 29 | int main() 30 | { 31 | int n; 32 | cin>>n; 33 | int ar[n+2]; 34 | for(int i=0;i>ar[i]; 36 | segregate(ar,n); 37 | cout<<"Array after segregation\n"; 38 | for(int i=0;i 2 | using namespace std; 3 | int maxSubArraySum(int a[], int size) 4 | { 5 | int max_so_far = a[0]; 6 | int curr_max = a[0]; 7 | for (int i = 1; i < size; i++) 8 | { 9 | curr_max = max(a[i], curr_max+a[i]); 10 | max_so_far = max(max_so_far, curr_max); 11 | } 12 | return max_so_far; 13 | } 14 | int main() 15 | { 16 | int a[] = {-2, -3, 4, -1, -2, 1, 5, -3}; 17 | int n = sizeof(a)/sizeof(a[0]); 18 | int max_sum = maxSubArraySum(a, n); 19 | cout << "Maximum contiguous sum is " << max_sum; 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Workshop Day 2/Binary Search/Binary Search.txt: -------------------------------------------------------------------------------- 1 | Linear Search 2 | Question 3 | 1.Find the missing number in an array 4 | ->Linear Approach 5 | ->Sum Approach 6 | ->XOR approach 7 | 8 | 9 | What is search space? 10 | The area of search under consideration. 11 | 12 | What is Binary Search? 13 | -> Divide and Conquer Approach 14 | -> O(log2 n) time complexity? 15 | Even if we had 1 billion elements binary search will still be able to find our key in at most 30 steps. 16 | At each step we divide the search space into two parts hence the name binary. 17 | 18 | For the given array 19 | 11 13 17 19 20 25 89 20 | 0 1 2 3 4 5 6 21 | What is the search space here? 22 | Ans-> 23 | 24 | Where do we see binary search in general? 25 | 1.Searching in dictionary 26 | 2.Newton Raphson Method(Engineering Mathematics) 27 | 28 | 29 | 1.Monotonic function Invariants. 30 | ->Graph with min value k(increasing) 31 | ->graph with max value k(Decreasing) 32 | 33 | 34 | 2.Find the smallest positive missing number 35 | ->Binary Search 36 | 37 | 38 | 3.Search on prefix sum array 39 | ->linear approach 40 | ->Binary search(for queries) 41 | 42 | 43 | 4.Search in a sorted and Rotated Array 44 | ->linear approach 45 | ->binary Search -------------------------------------------------------------------------------- /Workshop Day 2/Binary Search/MisiingNumber3.cpp: -------------------------------------------------------------------------------- 1 | //Using XOR 2 | 3 | //Find missing number in an array of the form 1 to n 4 | /* 5 | Sample Input 6 | 5 7 | 1 2 3 5 8 | Sample Output 9 | 4 10 | */ 11 | #include 12 | using namespace std; 13 | #define fastio ios_base::sync_with_stdio(false) 14 | #define testcase int tc;cin>>tc;while(tc--) 15 | typedef vector vi; 16 | int main() 17 | { 18 | testcase 19 | { 20 | int i,n; 21 | cin>>n; 22 | int x1=0; 23 | vi v(n-1); 24 | for(i=0;i>v[i]; 27 | x1=x1^v[i]; //Xor of the input numbers 28 | } 29 | for(i=1;i<=n;i++) 30 | { 31 | x1=x1^i; //Xor with numbers from 1 to n 32 | } 33 | cout< 12 | using namespace std; 13 | #define fastio ios_base::sync_with_stdio(false) 14 | #define testcase int tc;cin>>tc;while(tc--) 15 | typedef vector vi; 16 | int main() 17 | { 18 | testcase 19 | { 20 | int i,n; 21 | cin>>n; 22 | vi v(n-1); 23 | for(i=0;i>v[i]; 26 | } 27 | for(i=0;i 13 | using namespace std; 14 | #define fastio ios_base::sync_with_stdio(false) 15 | #define testcase int tc;cin>>tc;while(tc--) 16 | typedef vector vi; 17 | int main() 18 | { 19 | testcase 20 | { 21 | int i,n; 22 | cin>>n; 23 | vi v(n-1); 24 | int sum=0; 25 | for(i=0;i>v[i]; 28 | sum+=v[i]; //Sum of numbers of array 29 | } 30 | int sum2=(n*(n+1))/2; //Sum of numbers from 1 to n 31 | cout< 2 | using namespace std; 3 | #define faster cin.tie(0);ios_base::sync_with_stdio(0) 4 | #define testcase int tc;cin>>tc;while(tc--) 5 | 6 | //Function to calculate f(x) 7 | ll val(ll a,ll b,ll c,ll x) 8 | { 9 | return ((a*x*x)+(b*x)+c); 10 | } 11 | //Binary Search Function 12 | ll bs(ll a,ll b,ll c,ll k) 13 | { 14 | if(val(a,b,c,0)>=k)return 0; 15 | ll low=1; 16 | ll high=100000; 17 | while(low>1; 20 | if(val(a,b,c,mid)=k) 25 | { 26 | high=mid; 27 | } 28 | } 29 | return low; 30 | } 31 | 32 | int main() 33 | { 34 | faster; 35 | testcase 36 | { 37 | ll a,b,c,k; 38 | cin>>a>>b>>c>>k; 39 | cout< 17 | using namespace std; 18 | #define fastio ios_base::sync_with_stdio(false) 19 | int main() 20 | { 21 | int i,n,s; 22 | cin>>n; 23 | vector v(n); 24 | for(i = 0 ; i < n ; i++) 25 | { 26 | cin>>v[i]; 27 | } 28 | vector pref(n,0); 29 | pref[0]=v[0]; 30 | for(int i=1;i>q; 36 | while(q--) 37 | { 38 | cin>>s; 39 | auto it=lower_bound(pref.begin(),pref.end(),s); 40 | int ans=distance(pref.begin(),it); 41 | if(ans==n)cout<<-1<<"\n"; 42 | else cout< 2 | using namespace std; 3 | #define fastio ios_base::sync_with_stdio(false) 4 | int main() 5 | { 6 | int i,n; 7 | cin>>n; 8 | vector v(n); 9 | for(i = 0 ; i < n ; i++) 10 | { 11 | cin>>v[i]; 12 | } 13 | int q; 14 | cin>>q; 15 | while(q--) 16 | { 17 | int ts=0; 18 | for(i = 0 ; i < n ; i++) 19 | { 20 | ts+=v[i]; 21 | if(ts>=s) 22 | { 23 | cout< 17 | using namespace std; 18 | #define fastio ios_base::sync_with_stdio(false) 19 | #define testcase int tc;cin>>tc;while(tc--) 20 | typedef vector vi; 21 | int findFirstMissing(vi &array,int start,int end) 22 | { 23 | if (start > end) 24 | return end + 1; 25 | if (start != array[start]) 26 | return start; 27 | int mid = (start + end) / 2; 28 | // Left half has all elements from 0 to mid 29 | if (array[mid] == mid) 30 | return findFirstMissing(array, mid+1, end); 31 | return findFirstMissing(array, start, mid); 32 | } 33 | int main() 34 | { 35 | testcase 36 | { 37 | int n; 38 | cin>>n; 39 | vi v(n); 40 | for(int i=0;i>v[i]; 41 | cout< 2 | using namespace std; 3 | #define faster cin.tie(0);ios_base::sync_with_stdio(0) 4 | #define testcase int tc;cin>>tc;while(tc--) 5 | #define PB push_back 6 | #define MP make_pair 7 | typedef long long ll; 8 | typedef vector vl; 9 | typedef pair pl; 10 | const ll mod=1000000007; 11 | #define all(x) x.begin(),x.end() 12 | #define allR(x) x.rbegin(),x.rend() 13 | /*----------------------------*/ 14 | template 15 | T Max(T a,T b) 16 | { 17 | return a>b?a:b; 18 | } 19 | template 20 | T Min(T a,T b) 21 | { 22 | return a &v) 26 | { 27 | ll fi=v[0]+mid; 28 | k--; 29 | ll n=v.size(); 30 | for(ll i=1;i=v[i])continue; 33 | if(k==0)return true; 34 | fi=v[i]+mid; 35 | k--; 36 | } 37 | return false; 38 | } 39 | int main() 40 | { 41 | faster; 42 | ll n,k; 43 | cin>>n>>k; 44 | vector v(n); 45 | for(ll i=0;i>v[i]; 46 | sort(all(v)); 47 | ll low=0,high=1e9; 48 | while(low<=high) 49 | { 50 | ll mid=(low+high)>>1; 51 | if(check(mid,k,v)) 52 | { 53 | low=mid+1; 54 | } 55 | else 56 | { 57 | high=mid-1; 58 | } 59 | } 60 | cout< 2 | using namespace std; 3 | int prec(char c) 4 | { 5 | switch(c) 6 | { 7 | case '+': 8 | case '-': 9 | return 1; 10 | case '*': 11 | case '/': 12 | return 2; 13 | case '^': 14 | return 3; 15 | } 16 | return -1; 17 | } 18 | int main() 19 | { 20 | string s; 21 | cin>>s; 22 | int n=s.size(); 23 | stack s1; 24 | string o=""; 25 | for(int i=0;i='a'&& s[i]<='z' || s[i]>='A' && s[i]<='Z') 28 | o+=s[i]; 29 | else if(s[i]=='(') 30 | s1.push(s[i]); 31 | else if(s[i]==')') 32 | { 33 | while(!s1.empty() && s1.top()!='(') 34 | { 35 | o+=s1.top(); 36 | s1.pop(); 37 | } 38 | s1.pop(); 39 | } 40 | else 41 | { 42 | while(!s1.empty() && prec(s[i])<=prec(s1.top())) 43 | { 44 | o+=s1.top(); 45 | s1.pop(); 46 | } 47 | s1.push(s[i]); 48 | } 49 | } 50 | while(!s1.empty()) 51 | { 52 | o+=s1.top(); 53 | s1.pop(); 54 | } 55 | cout< 2 | using namespace std; 3 | 4 | //this funtion will print maximum element in every sub array of size k 5 | void printKMax(int ar[],int n,int k) 6 | { 7 | 8 | //taking a dequeue of size k 9 | deque q(k); 10 | int i; 11 | 12 | /* 13 | process 1st k element in dequeue.We will push only those elements 14 | which are useful. 15 | Here useful elements means it should be greater than current array 16 | element and it should be in given sub array range. 17 | We will make dequeue in such a way that dequeue will be sorted in 18 | descending order and new element is inserted at back side. 19 | That means front of dequeue will always contain the maximum element of 20 | that sub array and front of dequeue will be oldest element. 21 | 22 | */ 23 | for(i=0;iar[q.back()]) 28 | q.pop_back(); 29 | 30 | //push current element to the back of dequeue 31 | q.push_back(i); 32 | } 33 | for(;iar[q.back()]) 45 | q.pop_back(); 46 | 47 | //push current element to the back of dequeue 48 | q.push_back(i); 49 | } 50 | cout<>n>>k; 56 | int ar[n+2]; 57 | for(int i=0;i>ar[i]; 59 | printKMax(ar,n,k); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Workshop Day 3/next greater element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin>>n; 7 | int ar[n+2]; 8 | for(int i=0;i>ar[i]; 10 | stack s; 11 | 12 | //push the first element to the stack 13 | s.push(ar[0]); 14 | for(int i=1;i 2 | using namespace std; 3 | int main() 4 | { 5 | string s; 6 | cin>>s; 7 | int n=s.size(); 8 | int i=0; 9 | stack v; 10 | while(i 3 | using namespace std; 4 | 5 | void heapify(int ar[],int n,int i) 6 | { 7 | int l=2*i+1,r=2*i+2; 8 | int largest=i; 9 | if(lar[i]) 10 | largest=l; 11 | if(rar[largest]) 12 | largest=r; 13 | if(i!=largest) 14 | { 15 | swap(ar[i],ar[largest]); 16 | heapify(ar,n,largest); 17 | } 18 | } 19 | void buildHeap(int ar[],int n) 20 | { 21 | for(int i=(n-1)/2;i>=0;i--) 22 | heapify(ar,n,i); 23 | } 24 | int main() 25 | { 26 | int n; 27 | cin>>n; 28 | int ar[n+3]; 29 | for(int i=0;i>ar[i]; 31 | buildHeap(ar,n); 32 | cout<<"Array element after heap construction:\n"; 33 | for(int i=0;i 2 | using namespace std; 3 | void heapify(int ar[],int n,int i) 4 | { 5 | int l=2*i+1,r=2*i+2; 6 | int largest=i; 7 | if(lar[i]) 8 | largest=l; 9 | if(rar[largest]) 10 | largest=r; 11 | if(i!=largest) 12 | { 13 | swap(ar[i],ar[largest]); 14 | heapify(ar,n,largest); 15 | } 16 | } 17 | void heapsort(int ar[],int n) 18 | { 19 | for(int i=(n-1)/2;i>=0;i--) 20 | heapify(ar,n,i); 21 | for(int i=n-1;i>0;i--) 22 | { 23 | swap(ar[0],ar[i]); 24 | heapify(ar,i,0); 25 | } 26 | } 27 | int main() 28 | { 29 | int n; 30 | cin>>n; 31 | int ar[n+3]; 32 | for(int i=0;i>ar[i]; 34 | heapsort(ar,n); 35 | for(int i=0;i 2 | #include 3 | 4 | /* A binary tree node has data, left child and right child */ 5 | struct node 6 | { 7 | int data; 8 | struct node* left; 9 | struct node* right; 10 | }; 11 | 12 | /* Utillity function to check if the given node is leaf or not */ 13 | int isLeaf(struct node *node) 14 | { 15 | if(node == NULL) 16 | return 0; 17 | if(node->left == NULL && node->right == NULL) 18 | return 1; 19 | return 0; 20 | } 21 | 22 | /* returns 1 if SumTree property holds for the given 23 | tree */ 24 | int isSumTree(struct node* node) 25 | { 26 | int ls; // for sum of nodes in left subtree 27 | int rs; // for sum of nodes in right subtree 28 | 29 | /* If node is NULL or it's a leaf node then 30 | return true */ 31 | if(node == NULL || isLeaf(node)) 32 | return 1; 33 | 34 | if( isSumTree(node->left) && isSumTree(node->right)) 35 | { 36 | // Get the sum of nodes in left subtree 37 | if(node->left == NULL) 38 | ls = 0; 39 | else if(isLeaf(node->left)) 40 | ls = node->left->data; 41 | else 42 | ls = 2*(node->left->data); 43 | 44 | // Get the sum of nodes in right subtree 45 | if(node->right == NULL) 46 | rs = 0; 47 | else if(isLeaf(node->right)) 48 | rs = node->right->data; 49 | else 50 | rs = 2*(node->right->data); 51 | 52 | /* If root's data is equal to sum of nodes in left 53 | and right subtrees then return 1 else return 0*/ 54 | return(node->data == ls + rs); 55 | } 56 | 57 | return 0; 58 | } 59 | 60 | /* Helper function that allocates a new node 61 | with the given data and NULL left and right 62 | pointers. 63 | */ 64 | struct node* newNode(int data) 65 | { 66 | struct node* node = 67 | (struct node*)malloc(sizeof(struct node)); 68 | node->data = data; 69 | node->left = NULL; 70 | node->right = NULL; 71 | return(node); 72 | } 73 | 74 | /* Driver program to test above function */ 75 | int main() 76 | { 77 | struct node *root = newNode(26); 78 | root->left = newNode(10); 79 | root->right = newNode(3); 80 | root->left->left = newNode(4); 81 | root->left->right = newNode(6); 82 | root->right->right = newNode(3); 83 | if(isSumTree(root)) 84 | printf("The given tree is a SumTree "); 85 | else 86 | printf("The given tree is not a SumTree "); 87 | 88 | getchar(); 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /Workshop Day 4/tree/LevelSum.cpp: -------------------------------------------------------------------------------- 1 | // A queue based C++ program to find sum of all levels in a Binary Tree 2 | #include 3 | using namespace std ; 4 | 5 | /* A binary tree node has data, pointer to left child 6 | and a pointer to right child */ 7 | struct Node 8 | { 9 | int data ; 10 | struct Node * left, * right ; 11 | }; 12 | 13 | // Function to find the sum of each level 14 | void levelSum(struct Node * root) 15 | { 16 | // Base case 17 | if (root == NULL) 18 | return; 19 | 20 | // vector to store the result 21 | vector levSum; 22 | 23 | // Do Level order traversal keeping track of number 24 | // of nodes at every level. 25 | queue q; 26 | q.push(root); 27 | while (!q.empty()) 28 | { 29 | // Get the size of queue when the level order 30 | // traversal for one level finishes 31 | int count = q.size() ; 32 | 33 | // Iterate for all the nodes in the queue currently 34 | int sum = 0; 35 | while (count--) 36 | { 37 | // Dequeue an node from queue 38 | Node *temp = q.front(); 39 | q.pop(); 40 | 41 | // Add this node's value to current sum. 42 | sum = sum + temp->data; 43 | 44 | // Enqueue left and right children of 45 | // dequeued node 46 | if (temp->left != NULL) 47 | q.push(temp->left); 48 | if (temp->right != NULL) 49 | q.push(temp->right); 50 | } 51 | 52 | // Update the level Sum 53 | v.push_back(sum); 54 | } 55 | int n=v.size(); 56 | for(int i=0;idata = data; 68 | node->left = node->right = NULL; 69 | return (node); 70 | } 71 | 72 | int main() 73 | { 74 | struct Node *root = newNode(1); 75 | root->left = newNode(2); 76 | root->right = newNode(3); 77 | root->left->left = newNode(4); 78 | root->left->right = newNode(5); 79 | root->right->right = newNode(8); 80 | root->right->right->left = newNode(6); 81 | root->right->right->right = newNode(7); 82 | 83 | /* Constructed Binary tree is: 84 | 1 85 | / \ 86 | 2 3 87 | / \ \ 88 | 4 5 8 89 | / \ 90 | 6 7 */ 91 | levelSum(root); 92 | return 0; 93 | } -------------------------------------------------------------------------------- /Workshop Day 5/Segment Tree/Prefix.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Program for Computation of Range Sum with Prefix sum and no updates 3 | Sample input 4 | 5 5 | 2 1 4 3 4 6 | 3 7 | 1 2 8 | 0 3 9 | 3 4 10 | Sample Output 11 | 5 12 | 10 13 | 7 14 | */ 15 | #include 16 | using namespace std; 17 | int main() 18 | { 19 | cin.tie(NULL); 20 | ios_base::sync_with_stdio(false); 21 | int n; //Size of Array 22 | cin>>n; 23 | vector v(n); 24 | for(int i=0;i>v[i]; 25 | int q; 26 | cin>>q; 27 | int l,r; 28 | /*--------Prefix Sum PreComputation---------*/ 29 | vector pref(n,0); 30 | pref[0]=a[0]; 31 | for(int i=1;i>l>>r; 39 | if(l==0) 40 | { 41 | cout< 21 | using namespace std; 22 | const int MAX = 1000000; 23 | #define ll long long int 24 | /*----------*/ 25 | template 26 | T Max(T a,T b) 27 | { 28 | return a>b?a:b; 29 | } 30 | template 31 | T Min(T a,T b) 32 | { 33 | return a seg(2*MAX+2,0); 37 | vector in(MAX); 38 | void construct_tree(int low,int high,int pos) 39 | { 40 | if(low==high) 41 | { 42 | seg[pos]=in[low]; 43 | return; 44 | } 45 | int mid=(low+high)/2; 46 | construct_tree(low,mid,2*pos+1); 47 | construct_tree(mid+1,high,2*pos+2); 48 | seg[pos]=min(seg[2*pos+1],seg[2*pos+2]); 49 | } 50 | void update_tree(int low,int high,int val,int idx,int pos) 51 | { 52 | if(low==high) 53 | { 54 | in[idx]=val; 55 | seg[pos]=val; 56 | } 57 | else 58 | { 59 | int mid=(low+high)/2; 60 | if(low<=idx&&idx<=mid) 61 | { 62 | update_tree(low,mid,val,idx,2*pos+1); 63 | } 64 | else 65 | { 66 | update_tree(mid+1,high,val,idx,2*pos+2); 67 | } 68 | seg[pos]=Min(seg[2*pos+1],seg[2*pos+2]); 69 | } 70 | } 71 | int query(int low,int high,int l,int r,int pos) 72 | { 73 | if(rhigh) 74 | return INT_MAX; 75 | if(l<=low and r>=high) 76 | return seg[pos]; 77 | int mid=(low+high)/2; 78 | return Min(query(low,mid,l,r,2*pos+1),query(mid+1,high,l,r,2*pos+2)); 79 | } 80 | int main() 81 | { 82 | int l,r,x,y,n,q,t; 83 | cin>>n; 84 | for(int i=0;i>in[i]; 85 | construct_tree(0,n-1,0); 86 | cin>>q; 87 | while(q--) 88 | { 89 | cin>>t; 90 | if(t==1) 91 | { 92 | cin>>l>>r; 93 | cout<>x>>y; 98 | update_tree(0,n-1,y,x,0); 99 | } 100 | } 101 | return 0; 102 | } -------------------------------------------------------------------------------- /Workshop Day 5/Segment Tree/RangeSumSegmentTree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Program for Computation of Range Sum Brute Force and no updates 3 | Sample input 4 | 5 5 | 2 1 4 3 4 6 | 3 7 | 1 2 8 | 0 3 9 | 3 4 10 | Sample Output 11 | 5 12 | 10 13 | 7 14 | */ 15 | #include 16 | using namespace std; 17 | #define MAX 1000000 18 | #define ll long long int 19 | ll seg[2*MAX-1]; 20 | ll in[MAX]; 21 | void construct_tree(int low,int high,int pos) 22 | { 23 | if(low==high) //Reached leaf node 24 | { 25 | seg[pos]=in[low]; 26 | return; 27 | } 28 | int mid=(low+high)/2; 29 | construct_tree(low,mid,2*pos+1); //recursively go to the left subtree 30 | construct_tree(mid+1,high,2*pos+2); //recursively go to the right subtree 31 | seg[pos]=(seg[2*pos+1]+seg[2*pos+2]); //Update the value 32 | } 33 | int query(int low,int high,int l,int r,int pos) 34 | { 35 | if(rhigh) //If current range is not considerable 36 | return INT_MAX; 37 | if(l<=low&&r>=high) //If current range is the required range 38 | return seg[pos]; 39 | int mid=(low+high)/2; 40 | return (query(low,mid,l,r,2*pos+1) + query(mid+1,high,l,r,2*pos+2)); //Return the result 41 | } 42 | int main() 43 | { 44 | int l,r,n,q; 45 | cin>>n; 46 | for(int i=0;i>in[i]; 47 | cin>>q; 48 | construct_tree(0,n-1,0); //Build the segment tree 49 | while(q--) 50 | { 51 | cin>>l>>r; 52 | int sum=query(0,n-1,l,r,0); //Query the segment tree 53 | cout< 16 | using namespace std; 17 | int main() 18 | { 19 | cin.tie(NULL); 20 | ios_base::sync_with_stdio(false); 21 | int n; //Size of Array 22 | cin>>n; 23 | vector v(n); 24 | for(int i=0;i>v[i]; 25 | int q; 26 | cin>>q; 27 | int l,r; 28 | //Handle query without precomputation 29 | while(q--) 30 | { 31 | cin>>l>>r; 32 | int sum=0; 33 | for(int i=l;i<=r;i++) 34 | { 35 | sum+=v[i]; 36 | } 37 | cout< 6 | using namespace std; 7 | typedef long long ll; 8 | vectorv; 9 | vector seg; 10 | vector lazy; 11 | void build(int pos,int start,int end) 12 | { 13 | if(start==end) 14 | { 15 | seg[pos]=v[start]; 16 | } 17 | else 18 | { 19 | int mid=(start+end)>>1; 20 | build(2*pos+1,start,mid); 21 | build(2*pos+2,mid+1,end); 22 | seg[pos]=seg[2*pos+1]+seg[2*pos+2]; 23 | } 24 | } 25 | /*---updateRange using lazy propagation---*/ 26 | void updateRange(int pos,int start,int end,ll l,ll r,ll val) 27 | { 28 | if(lazy[pos]!=0) 29 | { 30 | seg[pos] += (end - start + 1) * lazy[pos]; 31 | if(start != end) 32 | { 33 | lazy[(pos<<1)+1] += lazy[pos]; 34 | lazy[(pos<<1)+2] += lazy[pos]; 35 | } 36 | lazy[pos] = 0; 37 | } 38 | if(start > end || start > r || end < l)return; 39 | if(start >= l and end <= r) 40 | { 41 | seg[pos] += (end - start + 1) * val; 42 | if(start != end) 43 | { 44 | lazy[(pos<<1)+1] += val; 45 | lazy[(pos<<1)+2] += val; 46 | } 47 | return; 48 | } 49 | int mid = (start + end)>>1; 50 | updateRange((pos<<1)+1, start, mid, l, r, val); 51 | updateRange((pos<<1)+2, mid + 1, end, l, r, val); 52 | seg[pos] = seg[(pos<<1)+1] + seg[(pos<<1)+2]; 53 | } 54 | /*---Query for Value---*/ 55 | ll query(int pos, int start, int end, int l, int r) 56 | { 57 | if(start > end or start > r or end < l) 58 | return 0; 59 | if(lazy[pos]!=0) 60 | { 61 | seg[pos]+=(end-start+1)*lazy[pos]; 62 | if(start != end) 63 | { 64 | lazy[(pos<<1)+1] += lazy[pos]; 65 | lazy[(pos<<1)+2] += lazy[pos]; 66 | } 67 | lazy[pos] = 0; 68 | } 69 | if(start >= l and end <= r) 70 | return seg[pos]; 71 | ll mid = (start + end)>>1; 72 | return (query((pos<<1)+1, start, mid, l, r)+query((pos<<1)+2, mid + 1, end, l, r)); 73 | } 74 | int main() 75 | { 76 | int tc; 77 | cin>>tc; 78 | while(tc--) 79 | { 80 | int n,q; 81 | cin>>n>>q; 82 | ll l,r,val; 83 | v.resize(n,0); 84 | seg.resize(2e6+5,0); 85 | lazy.resize(2e6+5,0); 86 | build(0,0,n-1); 87 | for(int i=0;i>qt; 91 | if(qt==0) 92 | { 93 | cin>>l>>r>>val; 94 | updateRange(0,0,n-1,l-1,r-1,val); 95 | } 96 | else 97 | { 98 | cin>>l>>r; 99 | cout< 4 | using namespace std; 5 | 6 | const int ALPHABET_SIZE = 26; 7 | 8 | // trie node 9 | struct TrieNode 10 | { 11 | struct TrieNode *children[ALPHABET_SIZE]; 12 | 13 | // isEndOfWord is true if the node represents 14 | // end of a word 15 | bool isEndOfWord; 16 | }; 17 | 18 | // Returns new trie node (initialized to NULLs) 19 | struct TrieNode *getNode(void) 20 | { 21 | struct TrieNode *pNode = new TrieNode; 22 | 23 | pNode->isEndOfWord = false; 24 | 25 | for (int i = 0; i < ALPHABET_SIZE; i++) 26 | pNode->children[i] = NULL; 27 | 28 | return pNode; 29 | } 30 | 31 | // If not present, inserts key into trie 32 | // If the key is prefix of trie node, just 33 | // marks leaf node 34 | void insert(struct TrieNode *root, string key) 35 | { 36 | struct TrieNode *pCrawl = root; 37 | 38 | for (int i = 0; i < key.length(); i++) 39 | { 40 | int index = key[i] - 'a'; 41 | if (!pCrawl->children[index]) 42 | pCrawl->children[index] = getNode(); 43 | 44 | pCrawl = pCrawl->children[index]; 45 | } 46 | 47 | // mark last node as leaf 48 | pCrawl->isEndOfWord = true; 49 | } 50 | 51 | // Returns true if key presents in trie, else 52 | // false 53 | bool search(struct TrieNode *root, string key) 54 | { 55 | struct TrieNode *pCrawl = root; 56 | 57 | for (int i = 0; i < key.length(); i++) 58 | { 59 | int index = key[i] - 'a'; 60 | if (!pCrawl->children[index]) 61 | return false; 62 | 63 | pCrawl = pCrawl->children[index]; 64 | } 65 | 66 | return (pCrawl != NULL && pCrawl->isEndOfWord); 67 | } 68 | 69 | // Driver 70 | int main() 71 | { 72 | // Input keys (use only 'a' through 'z' 73 | // and lower case) 74 | string keys[] = {"the", "a", "there", 75 | "answer", "any", "by", 76 | "bye", "their" }; 77 | int n = sizeof(keys)/sizeof(keys[0]); 78 | 79 | struct TrieNode *root = getNode(); 80 | 81 | // Construct trie 82 | for (int i = 0; i < n; i++) 83 | insert(root, keys[i]); 84 | 85 | // Search for different keys 86 | search(root, "the")? cout << "Yes\n" : 87 | cout << "No\n"; 88 | search(root, "these")? cout << "Yes\n" : 89 | cout << "No\n"; 90 | return 0; 91 | } -------------------------------------------------------------------------------- /Workshop Day 6/bfs.cpp: -------------------------------------------------------------------------------- 1 | //BFS traversal, level and number of connected components of a graph. 2 | 3 | #include 4 | using namespace std; 5 | const int N = 1e5; 6 | vector adj[N], level(N); 7 | vector vis(N); 8 | vector v; 9 | 10 | void bfs(int s) 11 | { 12 | queue q; 13 | q.push(s); 14 | vis[s] = true; 15 | level[s] = 0; 16 | while (!q.empty()) 17 | { 18 | int p = q.front(); 19 | q.pop(); 20 | v.push_back(p); 21 | for (int i = 0; i < adj[p].size(); i++) 22 | { 23 | int x = adj[p][i]; 24 | if (vis[x] == true) continue; 25 | level[x] = level[p] + 1; 26 | q.push(x); 27 | vis[x] = true; 28 | } 29 | } 30 | } 31 | 32 | 33 | int main(int argc, char const *argv[]) { 34 | 35 | int n, m; 36 | cin >> n >> m; //n and m are number of nodes and edges resp. 37 | while (m--) 38 | { 39 | int a, b; 40 | cin >> a >> b; //(a, b) denotes a bidirectional edge between nodes a and b. 41 | adj[a].push_back(b); 42 | adj[b].push_back(a); 43 | } 44 | 45 | int num_connnected_comp = 0; 46 | for (int i = 1; i <= n; i++) 47 | { 48 | if (vis[i] == 1) continue; 49 | bfs(i); 50 | num_connnected_comp++; 51 | } 52 | 53 | cout << num_connnected_comp << endl; //Number of connected component 54 | 55 | for (int i = 0; i < v.size(); i++) //BFS traversal 56 | cout << v[i] << " "; cout << endl; 57 | 58 | for (int i = 1; i <= n; i++) //Level of each node 59 | cout << level[i] << " "; cout << endl; 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Workshop Day 6/dfs.cpp: -------------------------------------------------------------------------------- 1 | //DFS traversal of a graph 2 | 3 | #include 4 | using namespace std; 5 | const int N = 1e5; 6 | vector adj[N], v; 7 | vector vis(N); 8 | 9 | 10 | void dfs(int s) 11 | { 12 | vis[s] = true; 13 | v.push_back(s); 14 | for (int i = 0; i < adj[s].size(); i++) 15 | { 16 | int x = adj[s][i]; 17 | if (vis[x] == true) continue; 18 | dfs(x); 19 | } 20 | } 21 | 22 | 23 | void dfs_iterative(int s) 24 | { 25 | stack S; 26 | S.push(s); 27 | vis[s] = true; 28 | while (!S.empty()) 29 | { 30 | int p = S.top(); 31 | S.pop(); 32 | for (int i = 0; i < adj[p].size(); i++) 33 | { 34 | int x = adj[p][i]; 35 | if (vis[x] == true) continue; 36 | S.push(x); 37 | vis[x] = true; 38 | } 39 | } 40 | } 41 | 42 | 43 | int main(int argc, char const *argv[]) { 44 | 45 | int n, m; 46 | cin >> n >> m; //n and m are number of nodes and edges resp. 47 | while (m--) 48 | { 49 | int a, b; 50 | cin >> a >> b; //(a, b) denotes a bidirectional edge between nodes a and b. 51 | adj[a].push_back(b); 52 | adj[b].push_back(a); 53 | } 54 | 55 | for (int i = 1; i <= n; i++) 56 | { 57 | if (vis[i] == true) continue; 58 | dfs(i); 59 | } 60 | 61 | for (int i = 0; i < v.size(); i++) //DFS traversal 62 | cout << v[i] << " "; cout << endl; 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Workshop Day 6/dijkstra.cpp: -------------------------------------------------------------------------------- 1 | //Shortest distance from a single node to every other nodes in the graph. 2 | 3 | #include 4 | using namespace std; 5 | const int N = 1e5, inf = 1e9; 6 | vector > v[N]; 7 | vector dist(N, inf); 8 | vector vis(N); 9 | 10 | void dj(int n) 11 | { 12 | priority_queue, vector >, greater > > q; 13 | q.push(make_pair(0, n)); 14 | dist[n] = 0; 15 | while (!q.empty()) 16 | { 17 | pair p = q.top(); 18 | q.pop(); 19 | int x = p.second; 20 | if (vis[x] == true) continue; 21 | vis[x] = true; 22 | for (int i = 0; i < v[x].size(); i++) 23 | { 24 | int a = v[x][i].first, b = v[x][i].second; 25 | if ( dist[b] > a + dist[x] ) 26 | { 27 | dist[b] = a + dist[x]; 28 | q.push(make_pair( dist[b], b )); 29 | } 30 | } 31 | } 32 | } 33 | 34 | int main() { 35 | 36 | int n, m; 37 | cin >> n >> m; 38 | for (int i = 0; i < m; i++) 39 | { 40 | int a, b, c; 41 | cin >> a >> b >> c; 42 | v[a].push_back(make_pair(c, b)); 43 | v[b].push_back(make_pair(c, a)); 44 | } 45 | 46 | dj(1); 47 | for (int i = 2; i <= n; i++) //Shortest distance from 1 to all other nodes. 48 | cout << dist[i] << " "; cout << endl; 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Workshop Day 6/dsu.cpp: -------------------------------------------------------------------------------- 1 | //Weighted Disjoint Set Union with path compression. 2 | 3 | 4 | #include 5 | using namespace std; 6 | const int N = 1e5; 7 | vector parent(N), cnt(N, 0); 8 | 9 | int get_parent(int n) 10 | { 11 | while (n != parent[n]) 12 | { 13 | parent[n] = parent[parent[n]]; 14 | n = parent[n]; 15 | } 16 | return n; 17 | } 18 | 19 | int main(int argc, char const *argv[]) { 20 | 21 | int n, m; 22 | cin >> n >> m; 23 | for (int i = 1; i <= n; i++) 24 | parent[i] = i; 25 | 26 | while (m--) 27 | { 28 | int a, b; 29 | cin >> a >> b; 30 | int x = get_parent(a), y = get_parent(b); 31 | if (x == y) continue; 32 | 33 | if (cnt[x] < cnt[y]) 34 | { 35 | parent[x] = parent[y]; 36 | cnt[y] += cnt[x]; 37 | } 38 | else 39 | { 40 | parent[y] = parent[x]; 41 | cnt[x] += cnt[y]; 42 | } 43 | } 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Workshop Day 6/kruskal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 1e5; 4 | vector > > adj; 5 | vector p(N); 6 | 7 | int get_parent(int n) 8 | { 9 | while (n != p[n]) 10 | { 11 | p[n] = p[p[n]]; 12 | n = p[n]; 13 | } 14 | return n; 15 | } 16 | 17 | int main() 18 | { 19 | int n, m; 20 | cin >> n >> m; 21 | for (int i = 1; i <= n; i++) 22 | p[i] = i; 23 | 24 | for (int i = 0; i < m; i++) 25 | { 26 | int a, b, c; //edge between nodes a and b with weight c 27 | cin >> a >> b >> c; 28 | adj.push_back({c, {a, b}}); 29 | } 30 | sort(adj.begin(), adj.end()); 31 | 32 | int cnt = 0; 33 | for (int i = 0; i < m; i++) 34 | { 35 | int a = adj[i].second.second, b = adj[i].second.first; 36 | int x = get_parent(a), y = get_parent(b); 37 | if (x != y) 38 | { 39 | p[x] = y; 40 | cnt += adj[i].first; 41 | } 42 | } 43 | 44 | cout << cnt << endl; 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Workshop Day1/DistinctElements1.cpp: -------------------------------------------------------------------------------- 1 | //Find no of distinct elements in an array using set 2 | #include 3 | using namespace std; 4 | #define fastio ios_base::sync_with_stdio(false) //FastInputoutput 5 | #define testcase int tc;cin>>tc;while(tc--) //Testcases 6 | typedef vector vi; 7 | int main() 8 | { 9 | fastio; 10 | testcase 11 | { 12 | int n; 13 | cin>>n; 14 | if(n==0) 15 | { 16 | cout<<0< s; 20 | vi v(n); 21 | for(int i=0;i>v[i]; 24 | s.insert(v[i]); //Insertion in O(1) time 25 | } 26 | cout< 3 | using namespace std; 4 | #define fastio ios_base::sync_with_stdio(false) 5 | #define testcase int tc;cin>>tc;while(tc--) 6 | int main() 7 | { 8 | fastio; 9 | testcase 10 | { 11 | int n; 12 | cin>>n; 13 | if(n==0) 14 | { 15 | cout<<"Invalid\n"; 16 | continue; 17 | } 18 | set s; //Set used instead of unordered_set for sorted order 19 | vector v(n); //Try to make the program without using this vector 20 | for(int i=0;i>v[i]; 23 | s.insert(v[i]); //Insertion in set takes O(log n) time 24 | } 25 | for(auto it=s.begin();it!=s.end();it++) // set::iterator it and auto it are equivalent 26 | { 27 | cout<<*it<<" "; //Traverse the set using iterator and print values 28 | } 29 | cout<<"\n"; 30 | } 31 | } 32 | 33 | 34 | /* 35 | Further Taks; 36 | 1.Try to make a set which stores values in descending order. 37 | If the ans was (1,2,3) it should be (3,2,1) now. 38 | 2.Try to traverse the set in opposite order 39 | If the ans was (1,2,3) it should be (3,2,1) now but use the set s itself. 40 | */ -------------------------------------------------------------------------------- /Workshop Day1/Map1.cpp: -------------------------------------------------------------------------------- 1 | //Print the attendance of students attending the workshop 2 | #include 3 | using namespace std; 4 | #define fastio ios_base::sync_with_stdio(false) 5 | #define testcase int tc;cin>>tc;while(tc--) 6 | int main() 7 | { 8 | fastio; 9 | int n; 10 | cin>>n; 11 | string s; //To input roll no. 12 | map m; //Key value "pair" in a map 13 | while(n--) 14 | { 15 | cin>>s; 16 | m[s]++; //Increment the attendance 17 | } 18 | for(auto it=m.begin();it!=m.end();it++) 19 | { 20 | cout<first<<" "<second<<"\n"; //Why did we use first and second here?? 21 | } 22 | } 23 | 24 | 25 | /* 26 | Further tasks 27 | Write a program that answers queries of the following type- 28 | 1.Mark the attendance of the student 29 | 2.Delete the student entry 30 | 3.Decrement the attendance of the student 31 | 4.How many days was the student present 32 | 33 | Sample Input 34 | 11 35 | 1 2017UGMM012 36 | 1 2017UGMM012 37 | 2 2017UGMM001 38 | 3 2017UGMM012 39 | 4 2017UGMM012 40 | 1 2017UGMM012 41 | 1 2017UGMM011 42 | 1 2017UGMM012 43 | 1 2017UGMM011 44 | 4 2017UGMM011 45 | 4 2017UGMM012 46 | 47 | Sample Output 48 | 1 49 | 2 50 | 3 51 | */ -------------------------------------------------------------------------------- /Workshop Day1/Map2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define fastio ios_base::sync_with_stdio(false) 4 | #define testcase int tc;cin>>tc;while(tc--) 5 | int main() 6 | { 7 | fastio; 8 | int q; 9 | cin>>q; 10 | string s; //To input roll no. 11 | map m; //Key value "pair" in a map 12 | while(q--) 13 | { 14 | int t; 15 | cin>>t; 16 | cin>>s; 17 | if(t==1) 18 | { 19 | m[s]++; 20 | } 21 | else if(t==2) 22 | { 23 | m.erase(s); //erase by value,key not found but no error generated 24 | } 25 | else if(t==3) 26 | { 27 | if(m.find(s)!=m.end()) 28 | m[s]--; 29 | } 30 | else 31 | { 32 | cout<s; //Creates a set of integers. 9 | 10 | 11 | Size: 12 | int length=s.size(); //Gives the size of the set. 13 | 14 | 15 | Insert: 16 | s.insert(x); //Inserts an integer x into the set s. 17 | 18 | 19 | Erasing an element: 20 | s.erase(val); //Erases an integer val from the set s. 21 | 22 | 23 | Finding an element: 24 | set::iterator itr=s.find(val); //Gives the iterator to the element val if it is found otherwise returns s.end() . 25 | 26 | Ex: set::iterator itr=s.find(100); //If 100 is not present then it==s.end(). 27 | 28 | 29 | 30 | 31 | /------------------Map-----------------/ 32 | 33 | Maps are a part of the C++ STL.Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order.The mainly used member functions of maps are: 34 | 35 | Map Template: 36 | map 37 | 38 | Declaration: 39 | mapm; //Creates a map m where key_type is of type string and data_type is of type int. 40 | 41 | 42 | Size: 43 | int length=m.size(); //Gives the size of the map. 44 | 45 | 46 | 47 | Insert: 48 | m.insert(make_pair("hello",9)); //Here the pair is inserted into the map where the key is "hello" and the value associated with it is 9. 49 | 50 | 51 | 52 | Erasing an element: 53 | m.erase(val); //Erases the pair from the map where the key_type is val. 54 | 55 | 56 | 57 | Finding an element: 58 | map::iterator itr=m.find(val); 59 | //Gives the iterator to the element val if it is found otherwise returns m.end() . 60 | 61 | Ex: map::iterator itr=m.find("Maps"); //If Maps is not present as the key value then itr==m.end(). 62 | 63 | Accessing the value stored in the key: 64 | To get the value stored of the key "MAPS" we can do m["MAPS"] or we can get the iterator using the find function and then by itr->second we can access the value. 65 | Ex- 66 | map::iterator itr=m.find(val); 67 | cout<second<<"\n"; 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /Workshop Day1/SampleCode3rdYear.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | template 4 | bool cmp(T a, T b) 5 | { 6 | return a>b; 7 | } 8 | 9 | template 10 | struct triplet 11 | { 12 | T first, second, third; 13 | 14 | triplet(T a, T b, T c) 15 | { 16 | first = a; 17 | second = b; 18 | third = c; 19 | } 20 | T get_max() 21 | { 22 | 23 | return max(first,max(second,third)); 24 | } 25 | }; 26 | class cmp_length{ 27 | public : bool operator() (string s1, string s2) 28 | { 29 | return s1.length() > s2.length(); 30 | } 31 | }; 32 | 33 | class cmp_mp{ 34 | public : bool operator() (int a, int b) 35 | { 36 | return a>b; 37 | } 38 | }; 39 | 40 | bool is_volwel(char c) 41 | { 42 | if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u') 43 | return true; 44 | return false; 45 | } 46 | 47 | class cmp_vowels{ 48 | public : bool operator() (string s1, string s2) 49 | { 50 | int c1 = 0, c2 = 0; 51 | for(int i=0; i 58 | class my_vec 59 | { 60 | vector v; 61 | map m; 62 | 63 | public: 64 | void push(T x) 65 | { 66 | v.push_back(x); 67 | m[x]++; 68 | } 69 | 70 | int size() 71 | { 72 | return v.size(); 73 | } 74 | 75 | T pop() 76 | { 77 | T x = v.back(); 78 | v.pop_back(); 79 | m[x]--; 80 | return x; 81 | } 82 | 83 | T operator[] (int i) 84 | { 85 | return v[i]; 86 | } 87 | 88 | int get_freq(int i) 89 | { 90 | return m[v[i]]; 91 | } 92 | 93 | }; 94 | 95 | 96 | 97 | int main() { 98 | ios_base::sync_with_stdio(false); 99 | 100 | std::vector< pair > v(5); 101 | for(int i=1; i<=5; i++) v[i-1] = make_pair(i,i*2); 102 | 103 | for(vector< pair >::iterator it = v.begin(); it!=v.end(); it++) cout<<(*it).first<<" "; 104 | 105 | cout< > v1(4, std::vector(5,1)); 112 | 113 | for(int i=0; i<4; i++) 114 | { 115 | for(int j=0; j<5; j++) 116 | v1[i][j] = i*j; 117 | } 118 | 119 | 120 | 121 | for(auto it1 : v1) 122 | { 123 | for(auto it2 : it1) 124 | cout< > > v2( 3, vector< vector >(4, vector(5,1) ) ); 142 | 143 | for(auto it1 : v2) 144 | { 145 | for(auto it2 : it1) 146 | { 147 | for(auto it3 : it2) 148 | { 149 | cout< v3(5); 157 | std::vector v4(5); 158 | 159 | for(int i=0; i<5; i++) 160 | { 161 | v3[i] = i*2 - 3; 162 | v4[i] = i/3.0; 163 | } 164 | 165 | cout<); 168 | 169 | sort(v4.begin(), v4.end(), cmp); 170 | 171 | for(int i=0; i<5; i++) cout< t1(3,1,2); 180 | cout< t2(0.3,11.52,2.42); 183 | cout< st; 186 | 187 | pair< set::iterator , bool > ret = st.insert("a"); 188 | cout<< ret.second< , greater > min_heap; 206 | 207 | min_heap.push(4); 208 | min_heap.push(1); 209 | min_heap.push(5); 210 | min_heap.push(7); 211 | 212 | while(!min_heap.empty()) 213 | { 214 | cout< , cmp_vowels > max_volwels; 221 | 222 | max_volwels.push("abcedf"); 223 | max_volwels.push("aaaee"); 224 | max_volwels.push("erfgt"); 225 | max_volwels.push("aei"); 226 | 227 | while(!max_volwels.empty()) 228 | { 229 | cout< mv; 235 | mv.push(1); 236 | mv.push(2); 237 | mv.push(3); 238 | mv.push(4); 239 | mv.push(2); 240 | mv.push(2); 241 | mv.push(3); 242 | mv.push(5); 243 | 244 | for(int i=0; i mabc; 261 | mabc[2] = 3; 262 | mabc[3] = 5; 263 | mabc[4] = 1; 264 | cout<<"done"; 265 | return 0; 266 | } --------------------------------------------------------------------------------