├── .gitignore ├── 01. Time and Space Analysis ├── 01 Need of Asymptotic Analysis │ └── code.cpp ├── 02 Asymptotic Analysis │ └── code.cpp ├── 03 Order of Growth │ └── code.cpp ├── 04 Time and Space Complexity Order │ └── code.cpp ├── 05 Best, Average, Worst Case │ └── code.cpp ├── 06 Asymptotic Notations │ └── code.cpp ├── 07 Analysis of Iterative Loops │ └── code.cpp └── 08 Analysis of Recursion │ └── code.cpp ├── 02. Mathematics ├── 01 Count Digits │ └── 01.cpp ├── 02 Palindrome Numbers │ └── 02.cpp ├── 03 Factorial of a Number │ ├── 03a.cpp │ └── 03b.cpp ├── 04 Trailing Zeroes in Factorial │ ├── 04a.cpp │ └── 04b.cpp ├── 05 GCD or HCF of two Numbers │ ├── 05a.cpp │ ├── 05b.cpp │ └── 05c.cpp ├── 06 LCM of two Numbers │ ├── 06a.cpp │ └── 06b.cpp ├── 07 Check for Prime │ ├── 07a.cpp │ ├── 07b.cpp │ └── 07c.cpp ├── 08 Prime Factors │ ├── 08a.cpp │ ├── 08b.cpp │ └── 08c.cpp ├── 09 Divisors of a Number │ ├── 09a.cpp │ ├── 09b.cpp │ └── 09c.cpp ├── 10 Sieve of Eratosthenes │ ├── 10a.cpp │ ├── 10b.cpp │ ├── 10c.cpp │ └── 10d.cpp ├── 11 Computing Power │ ├── 11a.cpp │ └── 11b.cpp └── 12 Iterative Power │ ├── 12a.cpp │ ├── 12b.cpp │ └── 12c.cpp ├── 03. Bit Manipulation ├── 01 Bitwise Operators │ └── 01 bitwise operators.cpp ├── 02 Check Kth Bit │ ├── 02a check kth bit.cpp │ └── 02b check kth bit.cpp ├── 03 Count Set Bits │ ├── 03a count set bits.cpp │ └── 03b count set bits.cpp ├── 04 Power of Two │ ├── 04a check power of two.cpp │ └── 04b check power of two.cpp ├── 05 One Odd Occurring │ ├── 05 one odd occurring.cpp │ └── Bonus Question.cpp ├── 06 Two Odd Occurring │ ├── 06a two unique numbers.cpp │ └── 06b two unique numbers.cpp └── 07 Power Set using Bitwise Operators │ └── 07 generate power set.cpp ├── 04. Recursion ├── 01 Introduction │ ├── 01 Function Calls.cpp │ └── 02 Basic Recursion.cpp ├── 02 Guess Output │ ├── 02a Guess Output.cpp │ ├── 02b Guess Output.cpp │ ├── 02c Guess Output.cpp │ └── 02d Guess Output.cpp ├── 03 Print N to 1 │ └── 03 Print N to 1.cpp ├── 04 Print 1 to N │ └── 04 Print 1 to N.cpp ├── 05 Tail Recursion │ └── 05 Tail Recursion.cpp ├── 06 Factorial │ └── 06 Factorial of Number.cpp ├── 07 Nth Fibonacci │ └── 07 Nth Fibonacci.cpp ├── 08 Sum of N Natural Numbers │ └── 08 Sum of N Natural Numbers.cpp ├── 09 Check Palindrome │ └── 09 Palindrome Check using Recursion.cpp ├── 10 Sum of Digits │ └── 10 Sum of Digits using Recursion.cpp ├── 11 Rope Cutting Problem │ └── 11 Rope Cutting Problem.cpp ├── 12 Generate Subsets │ └── 12 Generate Subsets.cpp ├── 13 Tower of Hanoi │ └── 13 Tower of Hanoi.cpp ├── 14 Josephus Problem │ └── 14 Josephus Problem.cpp ├── 15 Subset Sum Problem │ └── 15 Subset Sum Problem.cpp └── 16 Printing All Permutations │ └── 16 Printing All Permutations.cpp ├── 05. Arrays ├── 01 Linear Search │ └── 01 Linear Search.cpp ├── 02 Insert │ └── 02 Insert.cpp ├── 03 Delete │ └── 03 Delete.cpp ├── 04 Largest Element │ └── 04 Largest Element.cpp ├── 05 Second Largest Element │ ├── 05a Second Largest Element.cpp │ └── 05b Second Largest Element.cpp ├── 06 Check Array Sorted │ └── 06 Check Array Sorted.cpp ├── 07 Reverse Array │ └── 07 Reverse Array.cpp ├── 08 Remove Duplicates from Sorted Array │ └── 08 Remove Duplicates from Sorted Array.cpp ├── 09 Move Zeros to End │ ├── 09a Move Zero to End.cpp │ └── 09b Move Zero to End.cpp ├── 10 Left Rotate Array by One │ └── 10 Left Rotate Array by One.cpp ├── 11 Left Rotate Array by D Spaces │ ├── 11a Left Rotate Array by D Spaces.cpp │ ├── 11b Left Rotate Array by D Spaces.cpp │ └── 11c Left Rotate Array by D Spaces.cpp ├── 12 Leaders in Array │ ├── 12a Leaders in Array.cpp │ └── 12b Leaders in Array.cpp ├── 13 Maximum Difference │ ├── 13a Maximum Difference.cpp │ └── 13b Maximum Difference.cpp ├── 14 Frequencies in Sorted Array │ ├── 14a Frequencies in Sorted Array.cpp │ └── 14b Frequencies in Sorted Array.cpp ├── 15 Stock Buy and Sell │ └── 15 Stock Buy and Sell.cpp ├── 16 Trapping Rain Water │ ├── 16a Trapping Rain Water.cpp │ ├── 16b Trapping Rain Water.cpp │ └── 16c Trapping Rain Water.cpp ├── 17 Maximum Consecutive 1s │ └── 17 Maximum Consecutive 1.cpp ├── 18 Maximum Subarray Sum │ ├── 18a Maximum Sub Array Sum.cpp │ └── 18b Maximum Sub Array Sum.cpp ├── 19 Max Length Even Odd Subarray │ └── 19 Max Length Even Odd SubArray.cpp ├── 20 Max Circular Subarray Sum │ ├── 20a Max Circular Subarray Sum.cpp │ └── 20b Max Circular Subarray Sum.cpp ├── 21 Majority Element │ ├── 21a Majority Element.cpp │ └── 21b Majority Element.cpp ├── 22 Minimum Consecutive Flips │ ├── 22a Minimum Flips to make Array same.cpp │ └── 22b Minimum Flips to make Array same.cpp ├── 23 Max Sum of K Consecutive Element │ ├── 23a Max Sum of K Consecutive elements.cpp │ └── 23b Max Sum of K Consecutive elements.cpp ├── 24 Find Subarray with Given Sum │ ├── 24a Check Subarray with Given Sum.cpp │ └── 24b Check Subarray with Given Sum.cpp ├── 25 N-Bonnaci Number │ └── 25 N-Bonnaci Number.cpp ├── 26 Find Query Sum │ ├── 26a Find Query Sum.cpp │ └── 26b Find Query Sum.cpp ├── 27 Find Equlibrium Point │ ├── 27a Find Equlibrium Point.cpp │ └── 27b Find Equlibrium Point.cpp ├── 28 Find Maximum Occurring Element in Ranges │ ├── 28a Find Maximum Occurring Element in Ranges.cpp │ └── 28b Find Maximum Occurring Element in Ranges.cpp ├── 29 Split Array in 3 Equal Subarrays │ └── 29 Split Array in 3 Equal Subarrays.cpp ├── 30 Check if Subarray with Zero Sum │ └── 30 Check if Subarray with Zero sum.cpp ├── 31 Max Length of Subarray with Equal 0s and 1s │ └── 31 Max Length of Subarray with Equal 0s and 1s.cpp └── Introduction.cpp ├── 06. Searching ├── 01 Binary Search │ ├── 01a.cpp │ └── 01b.cpp ├── 02 Index of First Occurrence │ ├── 02a.cpp │ ├── 02b.cpp │ └── 02c.cpp ├── 03 Index of Last Occurrence │ ├── 03a index of last occurrence.cpp │ ├── 03b index of last occurrence.cpp │ └── 03c index of last occurrence.cpp ├── 04 Count Occurrences │ ├── 04a.cpp │ └── 04b.cpp ├── 05 Count 1s in Sorted Binary Array │ ├── 05a.cpp │ └── 05b.cpp ├── 06 Square Root │ ├── 06a square root.cpp │ └── 06b square root.cpp ├── 07 Search in Infinite Sized Array │ ├── 07a search in infinite sorted array.cpp │ └── 07b search in infinite sorted array.cpp ├── 08 Search in Sorted Rotated Array │ ├── 08a search in sorted rotated array.cpp │ └── 08b search in sorted rotated array.cpp ├── 09 Peak Element │ ├── 09a peak element.cpp │ └── 09b peak element.cpp ├── 10 Pair Sum │ ├── 10a pair sum.cpp │ └── 10b pair sum.cpp ├── 11 Triplet Sum │ ├── 11a triplet sum.cpp │ └── 11b triplet sum.cpp ├── 12 Count Pairs with Given Sum │ ├── 12a count pairs with given sum.cpp │ └── 12b count pairs with given sum.cpp ├── 13 Count Triplet with Given Sum │ ├── 13a count triplets with given sum.cpp │ └── 13b count triplets with given sum.cpp ├── 14 Check if there exists a Triplet with Given Condition │ ├── 14a.cpp │ └── 14b.cpp ├── 15 Median of Two Sorted Arrays │ ├── 15a.cpp │ └── 15b.cpp ├── 16 Repeating Element │ ├── 16a.cpp │ ├── 16b.cpp │ ├── 16c.cpp │ ├── 16d.cpp │ └── 16e.cpp └── 17 Allocate Minimum Pages │ └── 17.cpp ├── 07. Sorting ├── 01 Sort STL │ ├── 01 STL Introduction.cpp │ ├── 01a STL Arrays.cpp │ ├── 01b STL Vectors.cpp │ └── 01c STL Class.cpp ├── 02 Stability in Sorting Algorithm │ ├── 02a Stability of Algorithms Concept.cpp │ └── 02b Stability of Algorithms Examples.cpp ├── 03 Bubble Sort │ ├── 03a Bubble Sort.cpp │ └── 03b Bubble Sort Optimized.cpp ├── 04 Selection Sort │ └── 04 Selection Sort.cpp ├── 05 Insertion Sort │ └── 05 Insertion Sort.cpp ├── 06 Merge Two Sorted Arrays │ ├── 06a Merge Two Sorted Arrays.cpp │ └── 06b Merge Two Sorted Arrays.cpp ├── 07 Merge Sort │ ├── 07a Merge Sort Introduction.cpp │ ├── 07b Merge Function of Merge Sort.cpp │ └── 07c Merge Sort.cpp ├── 08 Intersection of Two Sorted Arrays │ ├── 08a Intersection of two Sorted Arrays.cpp │ └── 08b Intersection of two Sorted Arrays.cpp ├── 09 Union of Two Sorted Arrays │ ├── 09a Union of Two Sorted Arrays.cpp │ └── 09b Union of Two Sorted Arrays.cpp ├── 10 Count Inversions in Array │ ├── 10a count inversions in array.cpp │ └── 10b count inversions in array.cpp ├── 11 Partition Algorithms Introduction │ ├── 11a Partitioning Introduction.cpp │ └── 11b Partitioning Algorithms.cpp ├── 12 Naive Partition │ └── 12 Naive Partitioning.cpp ├── 13 Lomuto Partition │ ├── 13a Lomuto Partitioning.cpp │ └── 13b Lomuto Partitioning.cpp ├── 14 Hoare Partition │ ├── 14a Hoare Partition.cpp │ └── 14b Hoare Partition.cpp ├── 15 Quick Sort │ ├── 15a Quick Sort Introduction.cpp │ ├── 15b Quick Sort using Lomuto Partition.cpp │ └── 15c Quick Sort using Hoare Partition.cpp └── 20 Minimum Difference in Array │ ├── 20a Minimum Difference in Array.cpp │ └── 20b Minimum Difference in Array.cpp ├── 08. Matrix ├── 01 Matrix in C++ │ ├── 01a Fixed Size Arrays.cpp │ ├── 01b Variable Size Arrays.cpp │ ├── 01c Double Pointer.cpp │ ├── 01d Array of Pointers.cpp │ ├── 01e Array of Vectors.cpp │ └── 01f Vector of Vectors.cpp ├── 02 Snake Pattern │ └── 02 Snake Pattern.cpp ├── 03 Boundary Traversal │ └── 03 Boundary Traversal.cpp ├── 04 Transpose │ ├── 04a Transpose.cpp │ └── 04b Transpose.cpp ├── 05 Rotate │ └── 05 Rotate.cpp ├── 06 Spiral Traversal │ └── 06 Spiral Traversal.cpp ├── 07 Search in Row and Column wise Sorted Matrix │ └── 07 Search in Row and Column wise Sorted Matrix.cpp └── 08 Median of Row wise Sorted Matrix │ ├── 08a Median of Row Wise Sorted Matrix.cpp │ └── 08b Median of Row Wise Sorted Matrix.cpp ├── 09. Hashing ├── 01 Unordered Set │ └── 01 Unordered Set.cpp ├── 02 Unordered Map │ └── 02 Unordered Map.cpp ├── 03 Count Distinct │ ├── 03a Count Distinct.cpp │ └── 03b Count Distinct.cpp ├── 04 Frequencies of Elements │ ├── 04a Frequencies of Elements.cpp │ └── 04b Frequencies of Elements.cpp ├── 05 Intersection of Arrays │ ├── 05a Intersection of Arrays.cpp │ ├── 05b Intersection of Arrays.cpp │ └── 05c Intersection of Arrays.cpp ├── 06 Union of Arrays │ └── 06 Union of Arrays.cpp ├── 07 Pair Sum │ ├── 07a Pair with given Sum.cpp │ └── 07b Pair with given Sum.cpp ├── 08 Subarray with Zero Sum │ ├── 08a SubArray with Zero Sum.cpp │ └── 08b SubArray with Zero Sum.cpp ├── 09 Subarray with Given Sum │ ├── 09a SubArray with given Sum.cpp │ └── 09b SubArray with given Sum.cpp ├── 10 Longest Subarray with Given Sum │ ├── 10a Longest SubArray with given Sum.cpp │ └── 10b Longest SubArray with given Sum.cpp ├── 11 Longest Subarray with Equal 0s and 1s │ ├── 11a Longest SubArray with equal 0s and 1s.cpp │ └── 11b Longest SubArray with equal 0s and 1s.cpp ├── 12 Longest Common Span with Same Sum in Binary Array │ └── 12a Longest Common Span with Same Sum in Binary Array.cpp ├── 13 Longest Common Subsequence │ └── 13a Longest Consecutive SubSequence.cpp ├── 14 Count Distinct Elements in a Window of size K │ ├── 14a Count Distinct Elements in Window of size K.cpp │ ├── 14b Count Distinct Elements in Window of size K.cpp │ └── 14c Count Distinct Elements in Window of size K.cpp ├── 15 More than N-K Occurrences │ ├── 15a More Than N k Occurences.cpp │ └── 15b More Than N k Occurences.cpp └── Introduction.cpp ├── 10. Strings ├── 01 ASCII Values │ └── 01 ASCII Values.cpp ├── 02 Printing ASCII Values │ ├── 02a Printing ASCII Values.cpp │ ├── 02b Printing ASCII Values.cpp │ └── 02c Printing ASCII Values.cpp ├── 03 Print Frequencies of Character │ └── 03 Print Frequencies of Character in String.cpp ├── 04 Strings in C │ ├── 04a Strings in C.cpp │ └── 04b Strings in C.cpp ├── 05 Strings in CPP │ └── 05 Strings in CPP.cpp ├── 06 Taking Input │ ├── 06a Taking Input.cpp │ ├── 06b Taking Input.cpp │ └── 06c Taking Input.cpp ├── 07 Iterating Strings │ └── 07 Iterating Strings.cpp ├── 08 Palindrome Check │ ├── 08a Palindrome Check.cpp │ └── 08b Palindrome Check.cpp ├── 09 Validate Subsequence │ ├── 09a Validate Subsequence.cpp │ └── 09b Validate Subsequence.cpp ├── 10 Check for Anagram │ ├── 10a Check for Anagram.cpp │ └── 10b Check for Anagram.cpp ├── 11 Leftmost Repeating Character │ ├── 11a Leftmost Repeating Character.cpp │ └── 11b Leftmost Repeating Character.cpp ├── 12 Leftmost Non Repeating Character │ ├── 12a Leftmost Non Repeating Character.cpp │ └── 12b Leftmost Non Repeating Character.cpp ├── 13 Reverse Words in String │ ├── 13a Reverse Words in String.cpp │ └── 13b Reverse Words in String.cpp ├── 14 Check Rotation │ ├── 14a Check if Strings are Rotation.cpp │ └── 14b Check if Strings are Rotation.cpp ├── 15 Anagram Search │ ├── 15a Anagram Search.cpp │ └── 15b Anagram Search.cpp ├── 16 Pattern Searching │ ├── 16a Naive Pattern Searching.cpp │ └── 16b Naive Pattern Searching.cpp ├── 17 Lexiographic Rank of a String │ └── 17a Lexiographic Rank of a String.cpp └── 18 Longest Substring with Distinct Characters │ ├── 18a Longest Substring with Distinct Characters.cpp │ ├── 18b Longest Substring with Distinct Characters.cpp │ └── 18c Longest Substring with Distinct Characters.cpp ├── 11. Linked List ├── 01 Linked List Class │ └── 01 Linked List Class.cpp ├── 02 Iterative Traversal │ └── 02 Iterative Traversal.cpp ├── 03 Recursive Traversal │ ├── 03 Recursive Traversal.cpp │ └── 04 Insert at Head.cpp ├── 05 Insert At Tail │ └── 05 Insert At Tail.cpp ├── 06 Delete At Head │ └── 06 Delete At Head.cpp ├── 07 Delete At Tail │ └── 07 Delete At Tail.cpp ├── 08 Insert At Given Position │ └── 08 Insert At Given Position.cpp ├── 09 Iterative Search │ └── 09 Iterative Search.cpp ├── 10 Recursive Search │ └── 10 Recursive Search.cpp ├── 11 Doubly Linked List Class │ └── 11 Doubly Linked List Class.cpp ├── 12 Insert At Head Doubly LL │ └── 12 Insert At Head Doubly LL.cpp ├── 13 Insert At Tail Doubly LL │ └── 13 Insert At Tail Doubly LL.cpp ├── 14 Reverse Doubly LL │ └── 14 Reverse Doubly LL.cpp ├── 15 Delete At Head Doubly LL │ └── 15 Delete At Head Doubly LL.cpp ├── 16 Delete At Tail Double LL │ └── 16 Delete At Tail Doubly LL.cpp ├── 17 Circular Linked List Class │ └── 17 Circular Linked List Class.cpp ├── 18 Traversing Circular LL │ └── 18 Traversing Ciruclar LL.cpp ├── 19 Insert At Head Circular LL │ ├── 19a Insert At Head Circular LL.cpp │ └── 19b Insert At Head Circular LL.cpp ├── 20 Insert At Tail Circular LL │ └── 20 Insert At Tail Circular LL.cpp ├── 21 Delete at Head Circular LL │ └── 21 Delete At Head Circular LL.cpp ├── 22 Delete Kth Node in Circular LL │ └── 22 Delete Kth Node in Circular LL.cpp ├── 23 Circular Doubly Linked List │ └── 23 Circular Doubly Linked List.cpp ├── 24 Sorted Insert in LL │ └── 24 Sorted Insert in LL.cpp ├── 25 Middle Of Linked List │ ├── 25a Middle Of Linked List.cpp │ └── 25b Middle Of Linked List.cpp ├── 26 Find Nth Node From End Of LL │ ├── 26a Find Nth Node From End Of LL.cpp │ └── 26b Find Nth Node From End Of LL.cpp ├── 27 Iterative Reverse of LL │ ├── 27a Iterative Reverse of LL.cpp │ └── 27b Iterative Reverse of LL.cpp ├── 28 Recursive Reverse of LL │ └── 28 Recursive Reverse of LL.cpp ├── 29 Remove Duplicates from Sorted LL │ └── 29 Remove Duplicates from Sorted LL.cpp ├── 30 Reverse LL in group size of K │ ├── 30a Reverse LL in group size of K.cpp │ └── 30b Reverse LL in group size of K.cpp ├── 31 Detect Loop │ ├── 31a Detect Loop.cpp │ └── 31b Detect Loop.cpp ├── 32 Detect Loop Using Floyd Cycle Detection │ └── 32 Detect Loop Using Floyd Cycle Detection.cpp ├── 33 Detect and Remove Loop │ └── 33 Detect and Remove Loop.cpp ├── 34 Delete Node With Only Pointer Given │ └── 34 Delete Node With Only Pointer Given.cpp ├── 35 Seagregate Even Odd Nodes of LL │ └── 35 Segregate Even Odd Nodes of LL.cpp ├── 36 Intersection Point of Two Linked Lists │ ├── 36a Intersection Point of Two Linked Lists.cpp │ └── 36b Intersection Point of Two Linked Lists.cpp ├── 37 Pairwise Node Swap of LL │ └── 37 Pairwise Node Swap of LL.cpp ├── 38 Clone a LL with Random Pointer │ └── 38a Clone a LL with Random Pointer.cpp ├── 40 Merge Two Sorted LL │ └── 40 Merge Two Sorted LL.cpp └── 41 Palindrome Linked List │ └── 41 Palindrome Linked List.cpp ├── 12. Stacks ├── 01 Introduction │ └── 01 Introduction.cpp ├── 02 Implementation │ ├── 02a Implementation using Arrays.cpp │ ├── 02b Implementation using Vectors.cpp │ └── 02c Implementation using Linked List.cpp ├── 03 STL │ └── 03 STL.cpp ├── 04 Balanced Parentheses │ └── 04 Balanced Parentheses.cpp ├── 05 Two Stacks in an Array │ └── 05 Two Stacks in an Array.cpp ├── 06 K Stacks in an Array │ └── 06 K Stacks in an Array.cpp ├── 07 Stock Span Problem │ ├── 07a Stock Span Problem.cpp │ └── 07b Stock Span Problem.cpp ├── 08 Previous Greater Element │ ├── 08a Previous Greater Element.cpp │ └── 08b Previous Greater Element.cpp ├── 09 Next Greater Element │ ├── 09a Next Greater Element.cpp │ └── 09b Next Greater Element.cpp ├── 10 Largest Rectangular Area │ ├── 10a Largest Rectangluar Area.cpp │ └── 10b Largest Rectangluar Area.cpp └── 12 Design Stack with getMin in O(1) │ ├── 12a Design Stack with getMin in O(1).cpp │ └── 12b Design Stack with getMin in O(1).cpp ├── 13. Queue ├── 01_Introduction.cpp ├── 02_Queue_STL.cpp ├── 03_Implement_Stack_using_Queue.cpp ├── 04_Reversing_Queue_Iterative.cpp ├── 05_Reversing_Queue_Recursive.cpp └── 06_Generate_Number_with_Given_Digits.cpp ├── 14. Deque ├── 01 Introduction │ └── 01.cpp ├── 02 Array Implementation of Deque │ └── 02.cpp ├── 03 Deque STL │ └── 03.cpp ├── 04 Data Structure with Min Max Operation │ └── 04.cpp ├── 05 Maximum of all Subarrays of size K │ ├── 05a.cpp │ └── 05b.cpp └── 06 First Circular Tour │ ├── 06a.cpp │ └── 06b.cpp ├── 15. Binary Tree ├── 01 Introduction │ └── 01.cpp ├── 02 Tree Class │ └── 02.cpp ├── 03 Types of Tree Traversals │ └── 03.cpp ├── 04 Inorder Traversal │ └── 04.cpp ├── 05 Preorder Traversal │ └── 05.cpp ├── 06 Postorder Traversal │ └── 06.cpp ├── 07 Height of Tree │ └── 07.cpp ├── 08 Print Nodes at K Distance │ └── 08.cpp ├── 09 Level Order Traversal │ ├── 09a.cpp │ └── 09b.cpp ├── 10 Level Order Traversal Line by Line │ ├── 10a.cpp │ ├── 10b.cpp │ └── 10c.cpp ├── 11 Size of Binary Tree │ ├── 11a.cpp │ └── 11b.cpp ├── 12 Maximum in Binary Tree │ ├── 12a.cpp │ └── 12b.cpp ├── 13 Left View of Binary Tree │ ├── 13a.cpp │ └── 13b.cpp ├── 14 Children Sum Property │ └── 14.cpp ├── 15 Check for Balanced Binary Tree │ ├── 15a.cpp │ └── 15b.cpp ├── 16 Maximum Width of Binary Tree │ └── 16.cpp ├── 17 Convert Binary Tree to Doubly Linked List │ └── 17.cpp ├── 19 Tree Traversal in Spiral Form │ ├── 19a.cpp │ └── 19b.cpp ├── 20 Diameter of Binary Tree │ └── 20.cpp ├── 21 LCA of Binary Tree │ ├── 21a.cpp │ └── 21b.cpp ├── 22 Burn Binary Tree from Leaf │ └── 22.cpp ├── 23 Count Nodes in Complete Binary Tree │ ├── 23a.cpp │ └── 23b.cpp ├── 24 Serialize and Deserialize Binary Tree │ └── 24.cpp └── 25 Iterative Inorder Traversal │ └── 24.cpp ├── 16. Binary Search Tree ├── 01 Search in BST │ ├── 01a Search in BST.cpp │ └── 01b Search in BST.cpp ├── 02 Insert in BST │ ├── 02a Insert in BST.cpp │ └── 02b Insert in BST.cpp ├── 03 Delete in BST │ └── 03 Delete in BST.cpp ├── 04 Floor in BST │ └── 04 Floor in BST.cpp ├── 05 Ceil in BST │ └── 05 Ceil in BST.cpp ├── 06 Self Balancing BST │ └── 06 Self Balancing BST.cpp ├── 07 AVL Tree │ └── 07 AVL Tree.cpp ├── 08 Red Black Tree │ └── 08 Red Black Tree.cpp ├── 09 Set STL │ ├── 09a Set STL.cpp │ ├── 09b Set STL.cpp │ └── 09c Set STL.cpp └── 10 Map STL │ ├── 10a Map STL.cpp │ ├── 10b Map STL.cpp │ └── 10c Map STL.cpp ├── 17. Heaps ├── 01 Introduction │ └── 01 Introduction.cpp ├── 02 Implementation │ └── 02 Implementation.cpp ├── 03 Insert │ └── 03 Insert.cpp ├── 04 Heapify │ └── 04 Heapify.cpp ├── 05 Extract Min │ └── 05 Extract Min.cpp ├── 06 Decrease Key │ └── 06 Decrease Key.cpp ├── 07 Delete │ └── 07 Delete.cpp ├── 08 Build Heap │ └── 08 Build Heap.cpp ├── 09 Heap Sort │ └── 09 Heap Sort.cpp ├── 10 Priority Queue STL │ ├── 10a Priority Queue STL.cpp │ ├── 10b Priority Queue STL.cpp │ ├── 10c Priority Queue STL.cpp │ └── 10d Priority Queue STL.cpp ├── 11 Sort K Sorted Array │ ├── 11a Sort K-Sorted Array.cpp │ └── 11b Sort K-Sorted Array.cpp ├── 12 Purchasing Maximum Items │ ├── 12a Purchasing Maximum Items.cpp │ └── 12b Purchasing Maximum Items.cpp ├── 13 K Largest Element │ ├── 13a K-Largest Element.cpp │ └── 13b K-Largest Element.cpp ├── 14 K Closest Elements │ ├── 14a K-Closest Elements.cpp │ └── 14b K-Closest Elements.cpp ├── 15 Merge K Sorted Arrays │ ├── 15a Merge K Sorted Arrays.cpp │ └── 15b Merge K Sorted Arrays.cpp └── 16 Median in Stream │ ├── 16a Median in Stream.cpp │ └── 16b Median in Stream.cpp ├── 18. Graphs ├── 01 Graphs Introduction │ └── 01 Graphs Introduction.cpp ├── 02 Graphs Representation │ ├── 02a Graphs Representation.cpp │ └── 02b Graphs Representation.cpp ├── 03 Graphs Implementation │ ├── 03a Graphs Implementation.cpp │ └── 03b Graphs Implementation.cpp ├── 04 Breadth First Search │ ├── 04a Breadth First Search.cpp │ ├── 04b Breadth First Search.cpp │ └── 04c Breadth First Search.cpp ├── 05 Depth First Search │ ├── 05a Depth First Search.cpp │ ├── 05b Depth First Search.cpp │ └── 05c Depth First Search.cpp ├── 06 Shortest Path in Unweighted Graph │ └── 06 Shortest Path in Unweighted Graph.cpp └── 08 Topological Sorting │ └── 08 Topological Sorting.cpp ├── 19. Greedy Algorithms ├── 01 Minimum Coins │ └── 01 Minimum Coins.cpp ├── 02 Activity Selection │ └── 02 Activity Selection.cpp ├── 03 Fractional Knapsack │ └── 03 Fractional Knapsack.cpp ├── 04 Job Sequencing │ └── 04 Job Sequencing.cpp └── 05 Huffman Coding │ └── 05 Huffman Coding.cpp ├── 20. Backtracking ├── 01 Print Permutations of Strings with Given Condition │ ├── 01a Print Permutations of Strings with Given Condition.cpp │ └── 01b Print Permutations of Strings with Given Condition.cpp └── 02 Rat in a Maze │ └── 02 Rat in a Maze.cpp ├── 21. Dynamic Programming ├── 01 Introduction │ └── 01 Introduction.cpp ├── 02 Nth Fibonacci │ ├── 02a Nth Fibonacci.cpp │ ├── 02b Nth Fibonacci.cpp │ └── 02c Nth Fibonacci.cpp ├── 03 Longest Common Subsequence │ ├── 03a Longest Common Subsequence.cpp │ ├── 03b Longest Common Subsequence.cpp │ └── 03c Longest Common Subsequence.cpp ├── 04 Coin Change │ ├── 04a Coin Change.cpp │ ├── 04b Coin Change.cpp │ └── 04c Coin Change.cpp ├── 05 Edit Distance │ ├── 05a Edit Distance.cpp │ ├── 05b Edit Distance.cpp │ └── 05c Edit Distance.cpp └── 06 Maximum Sum with no 2 Consecutive │ ├── 06a Maximum Sum with no 2 Consecutive.cpp │ ├── 06b Maximum Sum with no 2 Consecutive.cpp │ └── 06c Maximum Sum with no 2 Consecutive.cpp ├── 22. Trie ├── 01 Introduction.cpp ├── 02 Trie Representation.cpp └── 03 Trie Delete.cpp ├── LICENSE ├── README.md └── logo.png /.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 | -------------------------------------------------------------------------------- /01. Time and Space Analysis/01 Need of Asymptotic Analysis/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sum1(int n) 5 | { 6 | int res=0; 7 | 8 | for(int i=1;i<=n;i++) 9 | { 10 | for(int j=1;j<=i;j++) 11 | { 12 | res++; 13 | } 14 | } 15 | 16 | return res; 17 | } 18 | 19 | int sum2(int n) 20 | { 21 | int res=0; 22 | for(int i=1;i<=n;i++) 23 | { 24 | res+=i; 25 | } 26 | 27 | return res; 28 | } 29 | 30 | int sum3(int n) 31 | { 32 | return (n*(n+1))/2; 33 | } 34 | 35 | int main() 36 | { 37 | int n=4; 38 | 39 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Asymtotic Analysis: 9 | 10 | 1. Computes running time of any algorithm 11 | 2. Independent of machine, programming language etc- 12 | 3. Goal is to calculate: best case, average case, worst case 13 | 14 | Example: 15 | 16 | asymptotic analysis: 17 | we are considering sum of n natural numbers example 18 | (code in previous folder) 19 | 20 | sum1 analysis is: c1*n*n + c2*n + c3 21 | sum2 analysis is: c4*n +c5 22 | sum3 analysis id: c6 23 | 24 | now ignore lower order terms and constants. 25 | so, analysis becomes: 26 | -->n*n 27 | -->n 28 | -->1 29 | 30 | so, order of growth is 1 < n < n*n or sum3 < sum2 < sum1 31 | 32 | note: we consider n very large while doing asymtotic analysis 33 | 34 | */ 35 | return 0; 36 | } -------------------------------------------------------------------------------- /01. Time and Space Analysis/03 Order of Growth/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Order of Growth: 9 | 10 | 1.Used to compare which function is growing faster than other. 11 | 2.lim n->INF g(n)/f(n) gives 0. 12 | this means f(n) is growing faster than g(n), n,f(n),g(n)>=0 13 | 14 | Example: 15 | 16 | f(n)=n*n 17 | g(n)=n 18 | 19 | so lim n->INF g(n)/f(n) = 1/n = 0 20 | 21 | so, f(n) grows faster than g(n) 22 | 23 | */ 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /01. Time and Space Analysis/04 Time and Space Complexity Order/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Time and Space Complexity Order: 9 | 10 | c < log(logn) < logn < n^(1/3) < n^(1/2) < n < n(logn) < n^2 < n^3 < 2^n < n! < n^n 11 | 12 | */ 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /01. Time and Space Analysis/05 Best, Average, Worst Case/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | --> Best case is the function which performs the minimum number 9 | of steps on input data of n elements. 10 | 11 | --> Worst case is the function which performs the maximum number 12 | of steps on input data of size n. 13 | 14 | --> Average case is the function which performs an average number 15 | of steps on input data of n elements. 16 | 17 | */ 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /01. Time and Space Analysis/06 Asymptotic Notations/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Asymptotic Notations: 9 | 10 | 1. Big O: Exact or Upper 11 | 2. Theta: Exact 12 | 3. Omega: Exact or Lower 13 | 14 | */ 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /01. Time and Space Analysis/08 Analysis of Recursion/code.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Q1: 9 | n=6 10 | void fun(int n) 11 | { 12 | if(n<=0) 13 | { 14 | return ; 15 | } 16 | 17 | cout<<"parth"<<; 18 | fun(n/2); 19 | fun(n/2); 20 | } 21 | 22 | Q2: 23 | n=5 24 | void fun(int n) 25 | { 26 | if(n<=0) 27 | { 28 | return ; 29 | } 30 | 31 | for(int i=0;i 2 | using namespace std; 3 | 4 | /* 5 | count digit of integer number x. 6 | */ 7 | 8 | //time: O(log10(n)) 9 | //space: O(1) 10 | int countDigits(int n) 11 | { 12 | if(n==0) 13 | { 14 | return 1; 15 | } 16 | 17 | int res=0; 18 | while(n) 19 | { 20 | n=n/10; 21 | res++; 22 | } 23 | 24 | return res; 25 | } 26 | 27 | int main() 28 | { 29 | int n=1313; 30 | 31 | cout< 2 | using namespace std; 3 | 4 | /* 5 | check whether given integer number 6 | palindrome or not. 7 | */ 8 | 9 | //time: O(log10(n)) 10 | //space: O(1) 11 | bool isPalindrome(int n) 12 | { 13 | int org_no=n; 14 | 15 | //reverse the number 16 | int rev=0; 17 | while(n) 18 | { 19 | int d=n%10; 20 | rev=rev*10+d; 21 | n=n/10; 22 | } 23 | 24 | return (org_no==rev); 25 | } 26 | 27 | int main() 28 | { 29 | int n=12321; 30 | 31 | cout< 2 | using namespace std; 3 | 4 | /* 5 | find factorial of integer number n. 6 | */ 7 | 8 | //time: O(n) 9 | //space: O(1) 10 | int factorial(int n) 11 | { 12 | int res=1; 13 | for(int i=2;i<=n;i++) 14 | { 15 | res=res*i; 16 | } 17 | 18 | return res; 19 | } 20 | 21 | int main() 22 | { 23 | int n=5; 24 | 25 | cout< 2 | using namespace std; 3 | 4 | /* 5 | find factorial of integer number n. 6 | */ 7 | 8 | //time: O(n) 9 | //space: O(n) 10 | int factorial(int n) 11 | { 12 | if(n==0) 13 | { 14 | return 1; 15 | } 16 | 17 | return (n*factorial(n-1)); 18 | } 19 | 20 | int main() 21 | { 22 | int n=5; 23 | 24 | cout< 2 | using namespace std; 3 | 4 | /* 5 | find number of zeroes in factorial 6 | of given number. 7 | */ 8 | 9 | int factorial(int n) 10 | { 11 | int res=1; 12 | for(int i=2;i<=n;i++) 13 | { 14 | res=res*i; 15 | } 16 | 17 | return res; 18 | } 19 | 20 | //time: O(n) 21 | //space: O(1) 22 | //not optimised to find zeros in factorial 23 | //of large numbers 24 | int trailingZeroes(int n) 25 | { 26 | //first find factorial of the number 27 | int factorial_n=factorial(n); 28 | 29 | //count zeros in factorial 30 | int res=0; 31 | while(factorial_n%10==0) 32 | { 33 | res++; 34 | factorial_n=factorial_n/10; 35 | } 36 | 37 | return res; 38 | } 39 | 40 | int main() 41 | { 42 | int n=10; 43 | 44 | cout< 2 | using namespace std; 3 | 4 | /* 5 | find number of zeroes in factorial 6 | of given number. 7 | */ 8 | 9 | //time: O(log5(n)) 10 | //space: O(1) 11 | //optimised 12 | int trailingZeroes(int n) 13 | { 14 | //we need to count number of zeroes 15 | //one zero is made of one 5 and one 2. 16 | //we also know that there are less number 17 | //of 5's in a factorial. 18 | //so, count only number of 5's in factorial. 19 | 20 | int res=0; 21 | int p=5; 22 | 23 | while(p<=n) 24 | { 25 | res+=(n/p); 26 | p=p*5; 27 | } 28 | 29 | return res; 30 | } 31 | 32 | int main() 33 | { 34 | int n1=10,n2=100; 35 | 36 | cout< 2 | using namespace std; 3 | 4 | /* 5 | find gcd or hcf of two numbers. 6 | 7 | HCF or GCD: 8 | The highest common factor (HCF) is found by 9 | finding all common factors of two numbers and 10 | selecting the largest one. 11 | */ 12 | 13 | /* 14 | This question also gives answer to the problem 15 | of largest square tile to fill whole rectangle 16 | of size 'axb'. 17 | */ 18 | 19 | //time: O(min(a,b)) 20 | //space: O(1) 21 | int hcf(int a,int b) 22 | { 23 | int res=min(a,b); 24 | while(res) 25 | { 26 | if(a%res==0 and b%res==0) 27 | { 28 | return res; 29 | } 30 | res--; 31 | } 32 | 33 | return 1; 34 | } 35 | 36 | int main() 37 | { 38 | int a1=40,b1=60,a2=7,b2=13; 39 | 40 | cout< 2 | using namespace std; 3 | 4 | /* 5 | find gcd or hcf of two numbers. 6 | 7 | HCF or GCD: 8 | The highest common factor (HCF) is found by 9 | finding all common factors of two numbers and 10 | selecting the largest one. 11 | */ 12 | 13 | /* 14 | This question also gives answer to the problem 15 | of largest square tile to fill whole rectangle 16 | of size 'axb'. 17 | */ 18 | 19 | /* 20 | Euclid's Theorem: 21 | if a==b: 22 | return a 23 | if a>b: 24 | hcf(a,b)=hcf(a-b,b) 25 | else: 26 | hcf(a,b)=hcf(b,a) 27 | */ 28 | 29 | //time: O(log(min(a,b))) 30 | //space: O(log(min(a,b))) 31 | //optimised Euclid's Algo 32 | int hcf(int a,int b) 33 | { 34 | return (b==0)?a:hcf(b,a%b); 35 | } 36 | 37 | int main() 38 | { 39 | int a1=40,b1=60,a2=7,b2=13; 40 | 41 | cout< 2 | using namespace std; 3 | 4 | /* 5 | find lcm of two numbers. 6 | 7 | LCM: 8 | the least common multiple of two integers a and b, 9 | usually denoted by lcm, is the smallest positive 10 | integer that is divisible by both a and b. 11 | */ 12 | 13 | //time: O(a*b) 14 | //space: O(1) 15 | int lcm(int a,int b) 16 | { 17 | int res=max(a,b); 18 | 19 | while(true) 20 | { 21 | if(res%a==0 and res%b==0) 22 | { 23 | break; 24 | } 25 | res++; 26 | } 27 | 28 | return res; 29 | } 30 | 31 | int main() 32 | { 33 | int a1=4,b1=6,a2=7,b2=13; 34 | 35 | cout< 2 | using namespace std; 3 | 4 | /* 5 | find lcm of two numbers. 6 | 7 | LCM: 8 | the least common multiple of two integers a and b, 9 | usually denoted by lcm, is the smallest positive 10 | integer that is divisible by both a and b. 11 | */ 12 | 13 | int hcf(int a,int b) 14 | { 15 | return (b==0)?a:hcf(b,a%b); 16 | } 17 | 18 | //time: O(log(min(a,b))) 19 | //space: O(log(min(a,b))) 20 | int lcm(int a,int b) 21 | { 22 | //using the formula: 23 | //LCM*GCD=a*b 24 | 25 | return ((a*b)/hcf(a,b)); 26 | } 27 | 28 | int main() 29 | { 30 | int a1=4,b1=6,a2=7,b2=13; 31 | 32 | cout< 2 | using namespace std; 3 | 4 | /* 5 | check if given number prime 6 | or not. 7 | */ 8 | 9 | //time: O(n) 10 | //space: O(1) 11 | bool isPrime(int n) 12 | { 13 | if(n==1) 14 | { 15 | return false; 16 | } 17 | 18 | for(int i=2;i 2 | using namespace std; 3 | 4 | /* 5 | check if given number prime 6 | or not. 7 | */ 8 | 9 | //time: O(sqrt(n)) 10 | //space: O(1) 11 | bool isPrime(int n) 12 | { 13 | /* 14 | divisors always appear in pairs. 15 | eg: 30=(1,30);(2,15);(3,10);(5,6) 16 | 17 | from above: 18 | x*y=n 19 | also, 20 | x<=y 21 | x*x<=x*y 22 | x*x<=n 23 | therefore: 24 | x<=sqrt(n) 25 | */ 26 | 27 | if(n==1) 28 | { 29 | return false; 30 | } 31 | 32 | for(int i=2;i*i<=n;i++) 33 | { 34 | if(n%i==0) 35 | { 36 | return false; 37 | } 38 | } 39 | 40 | return true; 41 | } 42 | 43 | int main() 44 | { 45 | int n=13; 46 | 47 | cout< 2 | using namespace std; 3 | 4 | /* 5 | check if given number prime 6 | or not. 7 | */ 8 | 9 | //time: O(sqrt(n)) 10 | //space: O(1) 11 | //efficient for large values of n 12 | bool isPrime(int n) 13 | { 14 | /* 15 | check for divisiblity of 16 | 2 and 3 before hand to 17 | save iterations. 18 | */ 19 | 20 | if(n==1) 21 | { 22 | return false; 23 | } 24 | 25 | if(n==2 or n==3) 26 | { 27 | return true; 28 | } 29 | 30 | if(n%2==0 or n%3==0) 31 | { 32 | return false; 33 | } 34 | 35 | for(int i=5;i*i<=n;i+=6) 36 | { 37 | if(n%i==0 or n%(i+2)==0) 38 | { 39 | return false; 40 | } 41 | } 42 | 43 | return true; 44 | } 45 | 46 | int main() 47 | { 48 | int n=13; 49 | 50 | cout< 2 | using namespace std; 3 | 4 | /* 5 | print all the prime 6 | factors of given number. 7 | */ 8 | 9 | //time: O(sqrt(n)) 10 | //space: O(1) 11 | void primeFactors(int n) 12 | { 13 | /* 14 | divisors always appear in pairs. 15 | eg: 30=(1,30);(2,15);(3,10);(5,6) 16 | 17 | from above: 18 | x*y=n 19 | also, 20 | x<=y 21 | x*x<=x*y 22 | x*x<=n 23 | therefore: 24 | x<=sqrt(n) 25 | */ 26 | 27 | int i=2; 28 | while(i*i<=n) 29 | { 30 | if(n%i==0) 31 | { 32 | cout<1) 42 | { 43 | cout< 2 | using namespace std; 3 | 4 | /* 5 | print all the prime 6 | factors of given number. 7 | */ 8 | 9 | //time: O(sqrt(n)) 10 | //space: O(1) 11 | //efficient for large values of n 12 | void primeFactors(int n) 13 | { 14 | if(n<=1) 15 | { 16 | return ; 17 | } 18 | 19 | while(n%2==0) 20 | { 21 | cout<<2<<" "; 22 | n=n/2; 23 | } 24 | 25 | while(n%3==0) 26 | { 27 | cout<<3<<" "; 28 | n=n/3; 29 | } 30 | 31 | for(int i=5;i*i<=n;i+=6) 32 | { 33 | while(n%i==0) 34 | { 35 | cout<1) 47 | { 48 | cout< 2 | using namespace std; 3 | 4 | /* 5 | print all divisors of 6 | a number. 7 | */ 8 | 9 | //time: O(n) 10 | //space: O(1) 11 | void divisors(int n) 12 | { 13 | for(int i=1;i<=n;i++) 14 | { 15 | if(n%i==0) 16 | { 17 | cout< 2 | using namespace std; 3 | 4 | /* 5 | print all divisors of 6 | a number. 7 | */ 8 | 9 | //time: O(sqrt(n)) 10 | //space: O(1) 11 | //order is disturbed 12 | void divisors(int n) 13 | { 14 | /* 15 | divisors always appear in pairs. 16 | eg: 30=(1,30);(2,15);(3,10);(5,6) 17 | 18 | from above: 19 | x*y=n 20 | also, 21 | x<=y 22 | x*x<=x*y 23 | x*x<=n 24 | therefore: 25 | x<=sqrt(n) 26 | */ 27 | 28 | for(int i=1;i*i<=n;i++) 29 | { 30 | if(n%i==0) 31 | { 32 | cout< 2 | using namespace std; 3 | 4 | /* 5 | print all divisors of 6 | a number. 7 | */ 8 | 9 | //time: O(sqrt(n)) 10 | //space: O(1) 11 | //order is not disturbed 12 | void divisors(int n) 13 | { 14 | //print from 1 to sqrt(n) (exclusive) 15 | int i; 16 | for(i=1;i*i=1;i--) 26 | { 27 | if(n%i==0) 28 | { 29 | cout< 2 | using namespace std; 3 | 4 | /* 5 | print prime numbers less than 6 | equal to given number 7 | */ 8 | 9 | bool isPrime(int n) 10 | { 11 | if(n==1) 12 | { 13 | return false; 14 | } 15 | 16 | if(n==2 or n==3) 17 | { 18 | return true; 19 | } 20 | 21 | if(n%2==0 or n%3==0) 22 | { 23 | return false; 24 | } 25 | 26 | for(int i=5;i*i<=n;i+=6) 27 | { 28 | if(n%i==0 or n%(i+2)==0) 29 | { 30 | return false; 31 | } 32 | } 33 | 34 | return true; 35 | } 36 | 37 | //time: O(n*sqrt(n)) 38 | //space: O(1) 39 | void printPrimes(int n) 40 | { 41 | for(int i=2;i<=n;i++) 42 | { 43 | if(isPrime(i)) 44 | { 45 | cout< 2 | using namespace std; 3 | 4 | /* 5 | compute x^n. 6 | */ 7 | 8 | //time: O(n) 9 | //space: O(1) 10 | int power(int x,int n) 11 | { 12 | int res=1; 13 | 14 | while(n--) 15 | { 16 | res=res*x; 17 | } 18 | 19 | return res; 20 | } 21 | 22 | int main() 23 | { 24 | int x1=2,n1=3; 25 | cout< 2 | using namespace std; 3 | 4 | /* 5 | compute x^n. 6 | */ 7 | 8 | //time: O(log(n)) 9 | //space: O(logn(n)) 10 | int power(int x,int n) 11 | { 12 | if(n==0) 13 | { 14 | return 1; 15 | } 16 | 17 | int res=1; 18 | int temp=power(x,n/2); 19 | temp=temp*temp; 20 | 21 | if(n%2==0) 22 | { 23 | return temp; 24 | } 25 | else 26 | { 27 | return temp*x; 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | int x1=2,n1=3; 34 | cout< 2 | using namespace std; 3 | 4 | /* 5 | compute x^n. 6 | */ 7 | 8 | //time: O(log(n)) 9 | //space: O(1) 10 | int power(int x,int n) 11 | { 12 | /* 13 | Binary Exponentiation: 14 | -->every number can be written 15 | as power of 2. 16 | -->we can traverse through all 17 | bits of a number(from lsb to msb) 18 | in log(n) time. 19 | */ 20 | 21 | int res=1; 22 | while(n) 23 | { 24 | if(n%2==0) 25 | { 26 | x=x*x; 27 | n=n/2; 28 | } 29 | else 30 | { 31 | res=res*x; 32 | n--; 33 | } 34 | } 35 | 36 | return res; 37 | } 38 | 39 | int main() 40 | { 41 | int x1=2,n1=3; 42 | cout< 2 | using namespace std; 3 | 4 | /* 5 | compute x^n. 6 | */ 7 | 8 | //time: O(log(n)) 9 | //space: O(1) 10 | //optimised solution using bitwise operators 11 | int power(int x,int n) 12 | { 13 | /* 14 | Binary Exponentiation: 15 | -->every number can be written 16 | as power of 2. 17 | -->we can traverse through all 18 | bits of a number(from lsb to msb) 19 | in log(n) time. 20 | */ 21 | 22 | int res=1; 23 | while(n) 24 | { 25 | if(n&1!=1) 26 | { 27 | x=x*x; 28 | n=n>>1; 29 | } 30 | else 31 | { 32 | res=res*x; 33 | n--; 34 | } 35 | } 36 | 37 | return res; 38 | } 39 | 40 | int main() 41 | { 42 | int x1=2,n1=3; 43 | cout< 2 | using namespace std; 3 | 4 | /* 5 | compute x^n. 6 | */ 7 | 8 | //time: O(log(n)) 9 | //space: O(1) 10 | //optimised solution using bitwise operators 11 | //avoiding overflow using modulo operator 12 | int m=1e9; 13 | int power(int x,int n) 14 | { 15 | /* 16 | Binary Exponentiation: 17 | -->every number can be written 18 | as power of 2. 19 | -->we can traverse through all 20 | bits of a number(from lsb to msb) 21 | in log(n) time. 22 | */ 23 | 24 | int res=1; 25 | while(n) 26 | { 27 | if(n&1!=1) 28 | { 29 | x=(x*x)%m; 30 | n=n>>1; 31 | } 32 | else 33 | { 34 | res=(res*x)%m; 35 | n--; 36 | } 37 | } 38 | 39 | return res; 40 | } 41 | 42 | int main() 43 | { 44 | int x1=2,n1=3; 45 | cout< 2 | using namespace std; 3 | 4 | /* 5 | All operations are O(1) 6 | */ 7 | 8 | int main() 9 | { 10 | int x = 2, y = 3; 11 | 12 | //and 13 | cout << (x & y) << endl; 14 | 15 | //or 16 | cout << (x | y) << endl; 17 | 18 | //xor 19 | cout << (x ^ y) << endl; 20 | 21 | //left shift operator : x<>y = x/(2^y) 25 | cout << (x >> y) << endl; 26 | 27 | //not operator : inverts all bits : unary operator (~x) 28 | cout << (~x) << endl; 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /03. Bit Manipulation/02 Check Kth Bit/02a check kth bit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Using Left Shift Operator 6 | */ 7 | 8 | //Time: O(1) 9 | bool kthBit(int n, int k) 10 | { 11 | int mask = 1 << (k - 1); 12 | return (n & mask); 13 | } 14 | 15 | int main() 16 | { 17 | //6 in binary is: 110 18 | int n = 6; 19 | 20 | //k range is: [1,32] 21 | int k1 = 1; 22 | int k2 = 2; 23 | int k3 = 3; 24 | 25 | cout << kthBit(n, k1) << endl; 26 | cout << kthBit(n, k2) << endl; 27 | cout << kthBit(n, k3) << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /03. Bit Manipulation/02 Check Kth Bit/02b check kth bit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Using Right Shift Operator 6 | */ 7 | 8 | //Time: O(1) 9 | bool kthBit(int n, int k) 10 | { 11 | n = n >> (k - 1); 12 | return (n & 1); 13 | } 14 | 15 | int main() 16 | { 17 | //6 in binary is: 110 18 | int n = 6; 19 | 20 | //k range is: [1,32] 21 | int k1 = 1; 22 | int k2 = 2; 23 | int k3 = 3; 24 | 25 | cout << kthBit(n, k1) << endl; 26 | cout << kthBit(n, k2) << endl; 27 | cout << kthBit(n, k3) << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /03. Bit Manipulation/03 Count Set Bits/03a count set bits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(logn) 5 | int setBits(int n) 6 | { 7 | int cnt = 0; 8 | 9 | while (n) 10 | { 11 | cnt += n & 1; 12 | n = n >> 1; 13 | } 14 | 15 | return cnt; 16 | } 17 | 18 | int main() 19 | { 20 | //5 binary is: 101 21 | int n = 5; 22 | 23 | cout << setBits(n) << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /03. Bit Manipulation/03 Count Set Bits/03b count set bits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Brian Kerningham's Algorithm 5 | //optimised for large values of n 6 | 7 | //Time: O(k) ; k:total set bits 8 | int setBits(int n) 9 | { 10 | int cnt = 0; 11 | 12 | while (n) 13 | { 14 | n = n & (n - 1); 15 | cout << n << " "; 16 | cnt++; 17 | } 18 | 19 | return cnt; 20 | } 21 | 22 | int main() 23 | { 24 | //5 binary is: 101 25 | int n = 7; 26 | setBits(n); 27 | // cout << setBits(n) << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /03. Bit Manipulation/04 Power of Two/04a check power of two.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int setBits(int n) 5 | { 6 | int cnt = 0; 7 | 8 | while (n) 9 | { 10 | if (n & 1) 11 | { 12 | cnt++; 13 | } 14 | n = n >> 1; 15 | } 16 | 17 | return cnt; 18 | } 19 | 20 | bool isPower(int n)//time comp. O(logn) 21 | { 22 | //if total set bit count is 1 then power of 2 23 | int total_set_bit = setBits(n); 24 | 25 | return total_set_bit == 1 ? 1 : 0; 26 | } 27 | 28 | int main() 29 | { 30 | int n = 5;//binary representation : 101 31 | 32 | cout << isPower(n) << endl; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /03. Bit Manipulation/04 Power of Two/04b check power of two.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Brian Kerningham's Algorithm 5 | bool isPower(int n)//time comp. O(1) 6 | { 7 | if (n == 0) 8 | { 9 | return false; 10 | } 11 | 12 | return (n & (n - 1) == 0); 13 | } 14 | 15 | int main() 16 | { 17 | int n = 5;//binary representation : 101 18 | 19 | cout << isPower(n) << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /03. Bit Manipulation/05 One Odd Occurring/05 one odd occurring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | All numbers repeat except one number. 6 | Find that number. 7 | */ 8 | 9 | //Time: O(n) 10 | int oneOddOccuring(vector nums) 11 | { 12 | int n = nums.size(); 13 | 14 | //xor variable 15 | int x = 0; 16 | 17 | //take xor of whole array with x 18 | for (int i = 0; i < n; i++) 19 | { 20 | x = x ^ nums[i]; 21 | } 22 | 23 | return x; 24 | } 25 | 26 | int main() 27 | { 28 | vector nums = {1, 9, 2, 8, 3, 1, 9, 2, 8}; 29 | 30 | cout << oneOddOccuring(nums) << endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /03. Bit Manipulation/05 One Odd Occurring/Bonus Question.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //find the element which is missing from the range 1 to n 5 | 6 | //trick 1 : xor of same numbers gives zero 7 | //trick 2 : xor of zero with any number returns original number 8 | int missing(int a[], int n)//time comp. O(n) 9 | { 10 | int X = 0; 11 | for (int i = 1; i <= n; i++) 12 | { 13 | X = X ^ i; 14 | } 15 | 16 | for (int i = 0; i < n - 1; i++) 17 | { 18 | X = X ^ a[i]; 19 | } 20 | 21 | return X; 22 | } 23 | 24 | int main() 25 | { 26 | int a[] = {1, 2, 3, 5, 6, 7, 8}; 27 | int n = 8; 28 | 29 | cout << missing(a, n) << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /03. Bit Manipulation/06 Two Odd Occurring/06a two unique numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void twoUnique(int a[], int n)//time comp. O(n^2) 5 | { 6 | for (int i = 0; i < n; i++) 7 | { 8 | int count = 0; 9 | for (int j = 0; j < n; j++) 10 | { 11 | if (a[i] == a[j]) 12 | { 13 | count++; 14 | } 15 | } 16 | 17 | if (count % 2 != 0) 18 | { 19 | cout << a[i] << " "; 20 | } 21 | } 22 | cout << endl; 23 | } 24 | 25 | int main() 26 | { 27 | int a[] = {1, 3, 2, 1, 3, 4, 2, 5, 4, 6}; 28 | int n = sizeof(a) / sizeof(int); 29 | 30 | twoUnique(a, n); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /03. Bit Manipulation/07 Power Set using Bitwise Operators/07 generate power set.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void powerSet(string s)//time comp. O(n*(2^n)) 5 | { 6 | int n = s.length(); 7 | int powSize = pow(2, n); 8 | 9 | for (int i = 0; i < powSize; i++) 10 | { 11 | for (int j = 0; j < n; j++) 12 | { 13 | if ((i & (1 << j)) != 0) 14 | { 15 | cout << s[j]; 16 | } 17 | } 18 | cout << endl; 19 | } 20 | } 21 | 22 | int main() 23 | { 24 | string s = "abc"; 25 | 26 | powerSet(s); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /04. Recursion/01 Introduction/01 Function Calls.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void fun1() 5 | { 6 | cout << "fun1()" << endl; 7 | } 8 | 9 | void fun2() 10 | { 11 | cout << "Before fun1()" << endl; 12 | fun1(); 13 | cout << "After fun1()" << endl; 14 | } 15 | 16 | int main() 17 | { 18 | cout << "Before fun2()" << endl; 19 | fun2(); 20 | cout << "After fun2()" << endl; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /04. Recursion/01 Introduction/02 Basic Recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void fun(int n) 5 | { 6 | //base case 7 | if (n == 0) 8 | { 9 | return ; 10 | } 11 | 12 | //recursive case 13 | cout << "Hello "; 14 | fun(n - 1); 15 | } 16 | 17 | int main() 18 | { 19 | int n; 20 | cin >> n; 21 | 22 | fun(n); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /04. Recursion/02 Guess Output/02a Guess Output.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void fun(int n) 5 | { 6 | if(n==0) 7 | { 8 | return ; 9 | } 10 | 11 | cout< 2 | using namespace std; 3 | 4 | void fun(int n) 5 | { 6 | if(n==0) 7 | { 8 | return ; 9 | } 10 | 11 | fun(n-1); 12 | cout< 2 | using namespace std; 3 | 4 | /* 5 | this function return floor(log2(n)) 6 | for floor(log3(n)) change base condition to n<3 and return 1+fun(n/3) 7 | */ 8 | 9 | int fun(int n) 10 | { 11 | if (n == 1) 12 | { 13 | return 0; 14 | } 15 | else 16 | { 17 | return 1 + fun(n / 2); 18 | } 19 | } 20 | 21 | int main() 22 | { 23 | cout << fun(16) << endl; 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /04. Recursion/02 Guess Output/02d Guess Output.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //converts decimal to binary 5 | void fun(int n) 6 | { 7 | if (n == 0) 8 | { 9 | return ; 10 | } 11 | 12 | fun(n / 2); 13 | cout << n % 2; 14 | } 15 | 16 | int main() 17 | { 18 | fun(7); 19 | 20 | cout << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /04. Recursion/03 Print N to 1/03 Print N to 1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | //Space: O(n) 6 | 7 | void print(int n) 8 | { 9 | if (n == 0) 10 | { 11 | return ; 12 | } 13 | 14 | cout << n << " "; 15 | print(n - 1); 16 | } 17 | 18 | int main() 19 | { 20 | int n; 21 | cin >> n; 22 | 23 | print(n); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /04. Recursion/04 Print 1 to N/04 Print 1 to N.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | //Space: O(n) 6 | 7 | void print(int n) 8 | { 9 | if (n == 0) 10 | { 11 | return ; 12 | } 13 | 14 | print(n - 1); 15 | cout << n << " "; 16 | } 17 | 18 | int main() 19 | { 20 | int n; 21 | cin >> n; 22 | 23 | print(n); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /04. Recursion/05 Tail Recursion/05 Tail Recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Tail Recursion: 9 | when the last thing that happens in function 10 | is a recursive call. 11 | 12 | Eg: print n to 1 is a tail recursive code 13 | 14 | Tail Recursive codes are faster, efficient and takes 15 | less space than non-tail recursive codes. 16 | 17 | In execution of tail recursive codes, compiler does 18 | not need to save the state of previous function calls. 19 | 20 | Non-tail recursive codes can be converted to tail recursive code 21 | by passing an extra parameter. 22 | 23 | */ 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /04. Recursion/06 Factorial/06 Factorial of Number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | //Space: O(n) 6 | 7 | int factorial(int n) 8 | { 9 | if (n == 0) 10 | { 11 | return 1; 12 | } 13 | 14 | return n * factorial(n - 1); 15 | } 16 | 17 | int main() 18 | { 19 | int n; 20 | cin >> n; 21 | 22 | cout << factorial(n); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /04. Recursion/07 Nth Fibonacci/07 Nth Fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(2^n) 5 | //Space: O(2^n) 6 | 7 | int fibonacci(int n) 8 | { 9 | if (n == 0 or n == 1) 10 | { 11 | return n; 12 | } 13 | 14 | return fibonacci(n - 1) + fibonacci(n - 2); 15 | } 16 | 17 | int main() 18 | { 19 | int n; 20 | cin >> n; 21 | 22 | cout << fibonacci(n); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /04. Recursion/08 Sum of N Natural Numbers/08 Sum of N Natural Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | //Space: O(n) 6 | 7 | int sum(int n) 8 | { 9 | if (n == 1) 10 | { 11 | return n; 12 | } 13 | 14 | return n + sum(n - 1); 15 | } 16 | 17 | int main() 18 | { 19 | int n; 20 | cin >> n; 21 | 22 | cout << sum(n); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /04. Recursion/09 Check Palindrome/09 Palindrome Check using Recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | //Space: O(n) 6 | 7 | bool palindrome(string str, int s, int e) 8 | { 9 | if (s >= e) 10 | { 11 | return true; 12 | } 13 | 14 | return (str[s] == str[e]) and palindrome(str, s + 1, e - 1); 15 | } 16 | 17 | int main() 18 | { 19 | string str; 20 | cin >> str; 21 | 22 | int s = 0; 23 | int e = str.length() - 1; 24 | 25 | cout << palindrome(str, s, e); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /04. Recursion/10 Sum of Digits/10 Sum of Digits using Recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(logn) 5 | //Space: O(logn) 6 | 7 | int sumDigits(int n) 8 | { 9 | if (n == 0) 10 | { 11 | return 0; 12 | } 13 | 14 | return (n % 10) + (sumDigits(n / 10)); 15 | } 16 | 17 | int main() 18 | { 19 | int n; 20 | cin >> n; 21 | 22 | cout << sumDigits(n); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /04. Recursion/11 Rope Cutting Problem/11 Rope Cutting Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(3^n) 5 | //Space: O(3^n) 6 | 7 | int maxPieces(int n, int a, int b, int c) 8 | { 9 | if (n == 0) 10 | { 11 | return 0; 12 | } 13 | 14 | if (n < 0) 15 | { 16 | return -1; 17 | } 18 | 19 | int res = max(maxPieces(n - a, a, b, c), max(maxPieces(n - b, a, b, c), maxPieces(n - c, a, b, c))); 20 | 21 | if (res == (-1)) 22 | { 23 | return -1; 24 | } 25 | 26 | return (res + 1); 27 | } 28 | 29 | int main() 30 | { 31 | int n; 32 | int a, b, c; 33 | 34 | cin >> n >> a >> b >> c; 35 | 36 | cout << maxPieces(n, a, b, c); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /04. Recursion/12 Generate Subsets/12 Generate Subsets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | for a string of length 'n' 6 | there exists '2^n' subsets(subsequences) 7 | */ 8 | 9 | //Time: O(2^n) 10 | //Space: O(2^n) 11 | 12 | void subsets(string s, string curr, int i) 13 | { 14 | if (i == s.length()) 15 | { 16 | cout << curr << " "; 17 | return ; 18 | } 19 | 20 | subsets(s, curr, i + 1); 21 | subsets(s, curr + s[i], i + 1); 22 | } 23 | 24 | int main() 25 | { 26 | string str; 27 | getline(cin, str); 28 | 29 | subsets(str, "", 0); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /04. Recursion/13 Tower of Hanoi/13 Tower of Hanoi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(2^n) 5 | //Space: O(2^n) 6 | 7 | void towerOfHanoi(int n, char A, char B, char C) 8 | { 9 | if (n == 1) 10 | { 11 | cout << "Move 1 from " << A << " to " << C << endl; 12 | return ; 13 | } 14 | 15 | towerOfHanoi(n - 1, A, C, B); 16 | cout << "Move " << n << " from " << A << " to " << C << endl; 17 | towerOfHanoi(n - 1, B, A, C); 18 | } 19 | 20 | int main() 21 | { 22 | int n; 23 | cin >> n; 24 | 25 | towerOfHanoi(n, 'A', 'B', 'C'); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /04. Recursion/14 Josephus Problem/14 Josephus Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int josephus(int n,int k) 5 | { 6 | if(n==1) 7 | { 8 | return 0; 9 | } 10 | 11 | return (josephus(n-1,k)+k)%n;//here counting from 0 , if counting starts from 1 - add 1 to the final result 12 | } 13 | 14 | int main() 15 | { 16 | int n,k; 17 | cin>>n>>k; 18 | 19 | cout< 2 | using namespace std; 3 | 4 | int countSubset(int arr[],int n,int sum) 5 | { 6 | if(n==0) 7 | { 8 | return (sum==0)?1:0; 9 | } 10 | 11 | return countSubset(arr,n-1,sum)+countSubset(arr,n-1,sum-arr[n-1]); 12 | } 13 | 14 | int main() 15 | { 16 | int n; 17 | cin>>n; 18 | 19 | int arr[n]; 20 | for(int i=0;i>arr[i]; 23 | } 24 | 25 | int sum; 26 | cin>>sum; 27 | 28 | cout< 2 | using namespace std; 3 | 4 | void permutations(string s,int i=0) 5 | { 6 | if(i==(s.length()-1)) 7 | { 8 | cout< 2 | using namespace std; 3 | 4 | //Time: O(n) for unsorted array 5 | //Time: O(logn) for sorted array (Binary Search) 6 | int linearSearch(int arr[], int n, int key) 7 | { 8 | for (int i = 0; i < n; i++) 9 | { 10 | if (arr[i] == key) 11 | { 12 | return i; 13 | } 14 | } 15 | 16 | return -1; 17 | } 18 | 19 | int main() 20 | { 21 | int arr[] = {3, 2, 4, 1, 5}; 22 | int n = sizeof(arr) / sizeof(int); 23 | int key = 1; 24 | 25 | cout << linearSearch(arr, n, key) << endl; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /05. Arrays/02 Insert/02 Insert.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) insert at begin 5 | //Time: O(1) insert at end 6 | int insert(int arr[], int n, int key, int pos) 7 | { 8 | for (int i = n - 1; i >= pos - 1; i--) 9 | { 10 | arr[i + 1] = arr[i]; 11 | } 12 | arr[pos - 1] = key; 13 | n++; 14 | 15 | return n; 16 | } 17 | 18 | void display(int arr[], int n) 19 | { 20 | for (int i = 0; i < n; i++) 21 | { 22 | cout << arr[i] << " "; 23 | } 24 | cout << endl; 25 | 26 | return ; 27 | } 28 | 29 | int main() 30 | { 31 | int arr[] = {3, 2, 4, 1, 5}; 32 | int n = sizeof(arr) / sizeof(int); 33 | int key = 6; 34 | int pos = 3; 35 | 36 | display(arr, n); 37 | n = insert(arr, n, key, pos); 38 | display(arr, n); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /05. Arrays/04 Largest Element/04 Largest Element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | int largest(int a[], int n) 6 | { 7 | int max_element = INT_MIN; 8 | for (int i = 0; i < n; i++) 9 | { 10 | if (a[i] > max_element) 11 | { 12 | max_element = a[i]; 13 | } 14 | } 15 | 16 | return max_element; 17 | } 18 | 19 | int main() 20 | { 21 | int arr[] = {3, 2, 4, 5, 1}; 22 | int n = sizeof(arr) / sizeof(int); 23 | 24 | cout << largest(arr, n) << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /05. Arrays/05 Second Largest Element/05b Second Largest Element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) (one traversal) 5 | int secondLargest(int arr[], int n) 6 | { 7 | int greatest, secondGreatest; 8 | greatest = secondGreatest = -1; 9 | 10 | for (int i = 0; i < n; i++) 11 | { 12 | if (arr[i] > greatest) 13 | { 14 | secondGreatest = greatest; 15 | greatest = arr[i]; 16 | } 17 | if (arr[i]secondGreatest) 18 | { 19 | secondGreatest = arr[i]; 20 | } 21 | } 22 | 23 | return secondGreatest; 24 | } 25 | 26 | int main() 27 | { 28 | int arr[] = {3, 2, 4, 1, 5}; 29 | int n = sizeof(arr) / sizeof(int); 30 | 31 | cout << secondLargest(arr, n) << endl; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /05. Arrays/06 Check Array Sorted/06 Check Array Sorted.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | bool sorted(int a[], int n) 6 | { 7 | if (n <= 1) 8 | { 9 | return true; 10 | } 11 | 12 | for (int i = 0; i < n - 1; i++) 13 | { 14 | if (a[i] > a[i + 1]) 15 | { 16 | return false; 17 | } 18 | } 19 | 20 | return true; 21 | } 22 | 23 | int main() 24 | { 25 | int arr[] = {3, 4, 5, 1, 2}; 26 | int n = sizeof(arr) / sizeof(int); 27 | 28 | cout << sorted(arr, n) << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /05. Arrays/07 Reverse Array/07 Reverse Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | void reverse(int a[], int n) 6 | { 7 | if (n <= 1) 8 | { 9 | return ; 10 | } 11 | 12 | int i = 0; 13 | int j = n - 1; 14 | while (i < j) 15 | { 16 | swap(a[i], a[j]); 17 | i++; 18 | j--; 19 | } 20 | } 21 | 22 | void display(int a[], int n) 23 | { 24 | for (int i = 0; i < n; i++) 25 | { 26 | cout << a[i] << " "; 27 | } 28 | cout << endl; 29 | } 30 | 31 | int main() 32 | { 33 | int arr[] = {3, 2, 4, 1, 5}; 34 | int n = sizeof(arr) / sizeof(int); 35 | 36 | display(arr, n); 37 | reverse(arr, n); 38 | display(arr, n); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /05. Arrays/08 Remove Duplicates from Sorted Array/08 Remove Duplicates from Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | int removeDuplicate(int a[], int n) 6 | { 7 | int res=1; 8 | for(int i=1;i 2 | using namespace std; 3 | 4 | //Time: O(n^2) 5 | void moveZeros(int a[], int n) 6 | { 7 | for (int i = 0; i < n; i++) 8 | { 9 | if (a[i] == 0) 10 | { 11 | for (int j = i + 1; j < n; j++) 12 | { 13 | if (a[j] != 0) 14 | { 15 | swap(a[i], a[j]); 16 | break; 17 | } 18 | } 19 | } 20 | } 21 | } 22 | 23 | void display(int a[], int n) 24 | { 25 | for (int i = 0; i < n; i++) 26 | { 27 | cout << a[i] << " "; 28 | } 29 | cout << endl; 30 | } 31 | 32 | int main() 33 | { 34 | int arr[] = {0, 3, 2, 1, 0, 4, 7, 0, 2}; 35 | int n = sizeof(arr) / sizeof(int); 36 | 37 | display(arr, n); 38 | moveZeros(arr, n); 39 | display(arr, n); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /05. Arrays/09 Move Zeros to End/09b Move Zero to End.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | void moveZeros(int a[], int n) 6 | { 7 | int count = 0; 8 | for (int i = 0; i < n; i++) 9 | { 10 | if (a[i] != 0) 11 | { 12 | swap(a[i], a[count]); 13 | count++; 14 | } 15 | } 16 | } 17 | 18 | void display(int a[], int n) 19 | { 20 | for (int i = 0; i < n; i++) 21 | { 22 | cout << a[i] << " "; 23 | } 24 | cout << endl; 25 | } 26 | 27 | int main() 28 | { 29 | int arr[] = {0, 3, 2, 1, 0, 4, 7, 0, 2}; 30 | int n = sizeof(arr) / sizeof(int); 31 | 32 | display(arr, n); 33 | moveZeros(arr, n); 34 | display(arr, n); 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /05. Arrays/10 Left Rotate Array by One/10 Left Rotate Array by One.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | void rotate(int a[], int n) 6 | { 7 | int temp = a[0]; 8 | for (int i = 1; i < n; i++) 9 | { 10 | a[i - 1] = a[i]; 11 | } 12 | a[n - 1] = temp; 13 | } 14 | 15 | void display(int a[], int n) 16 | { 17 | for (int i = 0; i < n; i++) 18 | { 19 | cout << a[i] << " "; 20 | } 21 | cout << endl; 22 | } 23 | 24 | int main() 25 | { 26 | int arr[] = {1, 2, 3, 4, 5}; 27 | int n = sizeof(arr) / sizeof(int); 28 | 29 | display(arr, n); 30 | rotate(arr, n); 31 | display(arr, n); 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /05. Arrays/11 Left Rotate Array by D Spaces/11a Left Rotate Array by D Spaces.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n*d) 5 | void rotateDspaces(int a[], int n, int d) 6 | { 7 | d = d % n; //to eliminate extra repeated iterations 8 | for (int k = 0; k < d; k++) 9 | { 10 | int temp = a[0]; 11 | for (int i = 1; i < n; i++) 12 | { 13 | a[i - 1] = a[i]; 14 | } 15 | a[n - 1] = temp; 16 | } 17 | } 18 | 19 | void display(int a[], int n) 20 | { 21 | for (int i = 0; i < n; i++) 22 | { 23 | cout << a[i] << " "; 24 | } 25 | cout << endl; 26 | } 27 | 28 | int main() 29 | { 30 | int arr[] = {1, 2, 3, 4, 5}; 31 | int n = sizeof(arr) / sizeof(int); 32 | int d = 3; 33 | 34 | display(arr, n); 35 | rotateDspaces(arr, n, d); 36 | display(arr, n); 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /05. Arrays/12 Leaders in Array/12a Leaders in Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | leader - nothing greater in right of it (equals are not allowed) 6 | */ 7 | 8 | //Time: O(n^2) 9 | void leaders(int *a, int n) 10 | { 11 | for (int i = 0; i < n; i++) 12 | { 13 | bool flag = true; 14 | for (int j = i + 1; j < n; j++) 15 | { 16 | if (a[j] >= a[i]) 17 | { 18 | flag = false; 19 | break; 20 | } 21 | } 22 | if (flag == true) 23 | { 24 | cout << a[i] << " "; 25 | } 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | int arr[]={7,10,4,3,6,5,2}; 32 | int n=sizeof(arr)/sizeof(int); 33 | 34 | leaders(arr, n); 35 | cout< 2 | using namespace std; 3 | 4 | /* 5 | leader - nothing greater in right of it (equals are not allowed) 6 | this solution prints from right to left (for printing left to right store in a array and print reverse of it) 7 | */ 8 | 9 | //Time: O(n) 10 | void leaders(int *a, int n) 11 | { 12 | int curr_leader = a[n - 1]; 13 | cout << curr_leader << " "; 14 | for (int i = n - 2; i >= 0; i--) 15 | { 16 | if (curr_leader < a[i]) 17 | { 18 | curr_leader = a[i]; 19 | cout << curr_leader << " "; 20 | } 21 | } 22 | 23 | return ; 24 | } 25 | 26 | int main() 27 | { 28 | int arr[]={7,10,4,3,6,5,2}; 29 | int n=sizeof(arr)/sizeof(int); 30 | 31 | leaders(arr, n); 32 | cout< 2 | using namespace std; 3 | 4 | //time: O(n*n) 5 | int maxDiff(int *a,int n) 6 | { 7 | int max_diff=INT_MIN; 8 | for(int i=0;i 2 | using namespace std; 3 | 4 | //time: O(n) 5 | int maxDiff(int *a,int n) 6 | { 7 | int res=a[1]-a[0]; 8 | int minVal=a[0]; 9 | 10 | for(int i=1;i 2 | using namespace std; 3 | 4 | //time: O(n*n) 5 | void printFrequencies(int *a,int n) 6 | { 7 | for(int i=0;i 2 | using namespace std; 3 | 4 | //time: O(n) (i incremented in both loops) 5 | void printFrequencies(int *a,int n) 6 | { 7 | int freq=1,i=1; 8 | 9 | while(i>n; 32 | 33 | int arr[n]; 34 | for(int i=0;i>arr[i]; 37 | } 38 | 39 | printFrequencies(arr,n); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /05. Arrays/15 Stock Buy and Sell/15 Stock Buy and Sell.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //time: O(n) 5 | int stockBuy(int *a,int n) 6 | { 7 | int profit=0; 8 | for(int i=1;ia[i-1]) 11 | { 12 | profit+=a[i]-a[i-1]; 13 | } 14 | } 15 | 16 | return profit; 17 | } 18 | 19 | int main() 20 | { 21 | int arr[]={1,5,3,8,12}; 22 | int n=sizeof(arr)/sizeof(int); 23 | 24 | cout< 2 | using namespace std; 3 | 4 | //time: O(n*n) 5 | //space: O(1) 6 | int trapRainWater(int *arr,int n) 7 | { 8 | int res=0; 9 | 10 | for(int i=1;i 2 | using namespace std; 3 | 4 | //time: O(n) 5 | //space: O(n) 6 | int trapRainWater(int a[],int n) 7 | { 8 | int res=0; 9 | int lmax[n],rmax[n]; 10 | 11 | lmax[0]=0; 12 | rmax[n-1]=0; 13 | 14 | for(int i=1;i=0;i--) 20 | { 21 | rmax[i]=max(a[i+1],rmax[i+1]); 22 | } 23 | 24 | for(int i=1;ia[i]) 28 | { 29 | res+=h-a[i]; 30 | } 31 | } 32 | 33 | return res; 34 | } 35 | 36 | int main() 37 | { 38 | int arr[]={3,0,1,2,5}; 39 | int n=sizeof(arr)/sizeof(int); 40 | 41 | cout< 2 | using namespace std; 3 | 4 | //time: O(n) 5 | int maxConsecutive1(int *a, int n) 6 | { 7 | int res = 0, curr = 0; 8 | 9 | for (int i = 0; i < n; i++) 10 | { 11 | if (a[i] == 0) 12 | { 13 | curr = 0; 14 | } 15 | else 16 | { 17 | curr++; 18 | res = max(res, curr); 19 | } 20 | } 21 | 22 | return res; 23 | } 24 | 25 | int main() 26 | { 27 | int arr[]={1,0,1,1,1,1,0,1,1}; 28 | int n=sizeof(arr)/sizeof(int); 29 | 30 | cout << maxConsecutive1(arr, n) << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /05. Arrays/18 Maximum Subarray Sum/18a Maximum Sub Array Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //time: O(n*n) 5 | int maxSubArraySum(int a[], int n) 6 | { 7 | int res = INT_MIN; 8 | 9 | for (int i = 0; i < n; i++) 10 | { 11 | int curr = 0; 12 | for (int j = i; j < n; j++) 13 | { 14 | curr = curr + a[j]; 15 | res = max(res, curr); 16 | } 17 | } 18 | 19 | return res; 20 | } 21 | 22 | int main() 23 | { 24 | int arr[] = {2, 3, -8, 7, -1, 2, 3}; 25 | int n = sizeof(arr) / sizeof(int); 26 | 27 | cout << maxSubArraySum(arr, n); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /05. Arrays/18 Maximum Subarray Sum/18b Maximum Sub Array Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Kadane's Algo 5 | //time: O(n) 6 | int maxSubarraySum(int nums[],int n) 7 | { 8 | int curr_sum=0; 9 | int max_sum=INT_MIN; 10 | 11 | for(int i=0;i 2 | using namespace std; 3 | 4 | //time: O(n) 5 | int maxLengthEvenOddSubArray(int *a,int n) 6 | { 7 | /* 8 | concept : sum of two even no.s is even 9 | also for two odd numbers sum is even 10 | but for one even and one odd sum is odd. 11 | */ 12 | 13 | int len=1; 14 | int maxlen=1; 15 | 16 | for(int i=0;i 2 | using namespace std; 3 | 4 | //time: O(n*n) 5 | int majorityElement(int arr[],int n) 6 | { 7 | for(int i=0;i(n/2)) 18 | { 19 | return arr[i]; 20 | } 21 | } 22 | 23 | return -1; 24 | } 25 | 26 | int main() 27 | { 28 | int arr[]={1,2,1,3,1}; 29 | int n=sizeof(arr)/sizeof(int); 30 | 31 | cout< 2 | using namespace std; 3 | 4 | //Sliding Window: sliding a window of size k 5 | //time: O(n) 6 | int maxKSum(vector nums,int k) 7 | { 8 | int n=nums.size(); 9 | int window_sum=0; 10 | int max_sum=0; 11 | 12 | for(int i=0;i nums={1,8,30,-5,20,7}; 36 | int k=3; 37 | 38 | cout< 2 | using namespace std; 3 | 4 | //time: O(m) 5 | void bonacciseries(long n, int m) 6 | { 7 | int a[m] = { 0 }; 8 | a[n - 1] = 1; 9 | a[n] = 1; 10 | 11 | for (int i = n + 1; i < m; i++) 12 | a[i] = 2 * a[i - 1] - a[i - n - 1]; 13 | 14 | for (int i = 0; i < m; i++) 15 | cout << a[i] << " "; 16 | } 17 | 18 | int main() 19 | { 20 | int N = 5, M = 15; 21 | 22 | bonacciseries(N, M); 23 | cout< 2 | using namespace std; 3 | 4 | //time: O(n*q) q: number of queries 5 | int getSum(int a[],int n,int l,int r) 6 | { 7 | int sum=0; 8 | for(int i=l;i<=r;i++) 9 | { 10 | sum=sum+a[i]; 11 | } 12 | 13 | return sum; 14 | } 15 | 16 | int main() 17 | { 18 | int arr[]={2,8,3,9,6,5,4}; 19 | int n=sizeof(arr)/sizeof(int); 20 | 21 | cout< 2 | using namespace std; 3 | 4 | //pre-computing prefix array sum 5 | int prefix[1000] {0}; 6 | void prefixSum(int a[], int n) 7 | { 8 | prefix[0] = a[0]; 9 | 10 | for (int i = 1; i < n; i++) 11 | { 12 | prefix[i] = prefix[i - 1] + a[i]; 13 | } 14 | } 15 | 16 | //time: O(1) (only time is taken by pre computation which is O(n)) 17 | int getSum(int l, int r) 18 | { 19 | if (l == 0) 20 | { 21 | return prefix[r]; 22 | } 23 | 24 | return (prefix[r] - prefix[l - 1]); 25 | } 26 | 27 | int main() 28 | { 29 | int arr[]={2,8,3,9,6,5,4}; 30 | int n=sizeof(arr)/sizeof(int); 31 | 32 | prefixSum(arr,n); 33 | 34 | cout< 2 | using namespace std; 3 | 4 | /* 5 | Equilibrium Point : 6 | A point such that sum of elements before it 7 | equals sum of element after it. 8 | */ 9 | 10 | //time: O(n*n) 11 | bool isEquilibrium(int a[], int n) 12 | { 13 | for (int i = 0; i < n; i++) 14 | { 15 | int left_sum = 0; 16 | for (int j = 0; j < i; j++) 17 | { 18 | left_sum += a[j]; 19 | } 20 | 21 | int right_sum = 0; 22 | for (int j = i + 1; j < n; j++) 23 | { 24 | right_sum += a[j]; 25 | } 26 | 27 | if (left_sum == right_sum) 28 | { 29 | return true; 30 | } 31 | } 32 | 33 | return false; 34 | } 35 | 36 | int main() 37 | { 38 | int arr[] = {3, 4, 8, -9, 20, 6}; 39 | int n = sizeof(arr) / sizeof(int); 40 | 41 | cout << isEquilibrium(arr, n) << endl; 42 | return 0; 43 | } -------------------------------------------------------------------------------- /05. Arrays/27 Find Equlibrium Point/27b Find Equlibrium Point.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //time: O(n) 5 | bool isEquilibrium(int a[], int n) 6 | { 7 | int sum = 0; 8 | for (int i = 0; i < n; i++) 9 | { 10 | sum += a[i]; 11 | } 12 | 13 | int left_sum = 0; 14 | for (int i = 0; i < n; i++) 15 | { 16 | if (left_sum == sum - a[i]) 17 | { 18 | return true; 19 | } 20 | left_sum += a[i]; 21 | sum -= a[i]; 22 | } 23 | 24 | return false; 25 | } 26 | 27 | int main() 28 | { 29 | int a[] = {3, 4, 8, -9, 20, 6}; 30 | int n = sizeof(a) / sizeof(int); 31 | 32 | cout << isEquilibrium(a, n); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /05. Arrays/28 Find Maximum Occurring Element in Ranges/28a Find Maximum Occurring Element in Ranges.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | given n ranges find maximum 6 | occurring element 7 | */ 8 | 9 | //time: O(n*n) 10 | //space: O(n) 11 | int maxAppearing(int l[],int r[],int n) 12 | { 13 | unordered_map m; 14 | 15 | for(int i=0;imax_count) 29 | { 30 | max_count=x.second; 31 | max_appearing=x.first; 32 | } 33 | } 34 | 35 | return max_appearing; 36 | } 37 | 38 | int main() 39 | { 40 | int l[]={1,2,5,15}; 41 | int r[]={5,8,7,18}; 42 | int n=sizeof(l)/sizeof(int); 43 | 44 | cout< 2 | using namespace std; 3 | 4 | //time: O(n) 5 | //space: O(n) 6 | int checkSubarray0Sum(int nums[],int n) 7 | { 8 | int presum=0; 9 | unordered_set s; 10 | 11 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | /* 6 | 7 | Arrays Advantages: 8 | 1.random access 9 | 2.cache friendly 10 | 11 | Types of Arrays: 12 | 1.fixed size 13 | eg: 14 | int arr[100] stack allocated 15 | int arr[n] stack allocated 16 | int arr[]={10,20,30} stack allocated 17 | int *arr=new int[n] heap allocated 18 | 19 | 2.dynamic size 20 | eg: 21 | vector v heap allocated 22 | 23 | */ 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /06. Searching/01 Binary Search/01a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //time: O(logn) 5 | int binarySearch(vector nums,int key) 6 | { 7 | int n=nums.size(); 8 | int low = 0; 9 | int high = n - 1; 10 | 11 | while (low <= high) 12 | { 13 | int mid = (low + high) / 2; 14 | 15 | if (nums[mid] == key) 16 | { 17 | return mid; 18 | } 19 | else if (nums[mid] > key) 20 | { 21 | high = mid - 1; 22 | } 23 | else 24 | { 25 | low = mid + 1; 26 | } 27 | } 28 | 29 | return -1; 30 | } 31 | 32 | int main() 33 | { 34 | vector nums={1,2,3,4,5,6}; 35 | 36 | cout< 2 | using namespace std; 3 | 4 | //time: O(logn) 5 | //space: O(logn) 6 | int binarySearch(vector nums, int low, int high, int key) 7 | { 8 | //base case 9 | if (low > high) 10 | { 11 | return -1; 12 | } 13 | 14 | //recursive case 15 | int mid = (low + high) / 2; 16 | if (nums[mid] == key) 17 | { 18 | return mid; 19 | } 20 | else if (nums[mid] > key) 21 | { 22 | return binarySearch(nums, low, mid - 1, key); 23 | } 24 | else 25 | { 26 | return binarySearch(nums, mid + 1, high, key); 27 | } 28 | } 29 | 30 | int main() 31 | { 32 | vector nums={1,2,3,4,5,6}; 33 | 34 | cout< 2 | using namespace std; 3 | 4 | //time: O(n) 5 | int firstOccurrence(vector nums,int key) 6 | { 7 | int n=nums.size(); 8 | for (int i = 0; i < n; i++) 9 | { 10 | if (nums[i] == key) 11 | { 12 | return i; 13 | } 14 | } 15 | 16 | return -1; 17 | } 18 | 19 | int main() 20 | { 21 | vector nums={1,2,3,3,3,4,5,6}; 22 | 23 | cout< 2 | using namespace std; 3 | 4 | //time: O(n) 5 | int lastOccurrence(vector nums,int key) 6 | { 7 | int n=nums.size(); 8 | for (int i = n-1; i>=0; i--) 9 | { 10 | if (nums[i] == key) 11 | { 12 | return i; 13 | } 14 | } 15 | 16 | return -1; 17 | } 18 | 19 | int main() 20 | { 21 | vector nums={1,2,3,3,3,4,5,6}; 22 | 23 | cout< 2 | using namespace std; 3 | 4 | //time: O(n) 5 | int countOccurrence(vector nums,int key) 6 | { 7 | int n=nums.size(); 8 | int cnt=0; 9 | 10 | for (int i = 0; i < n; i++) 11 | { 12 | if (nums[i] == key) 13 | { 14 | cnt++; 15 | } 16 | } 17 | 18 | return cnt; 19 | } 20 | 21 | int main() 22 | { 23 | vector nums={1,2,3,3,3,4,5,6}; 24 | 25 | cout< 2 | using namespace std; 3 | 4 | //time: O(n) 5 | int count1s(vector nums) 6 | { 7 | int n=nums.size(); 8 | int cnt = 0; 9 | 10 | for (int i = 0; i < n; i++) 11 | { 12 | if (nums[i] == 1) 13 | { 14 | cnt++; 15 | } 16 | } 17 | 18 | return cnt; 19 | } 20 | 21 | int main() 22 | { 23 | vector nums={0, 0, 0, 0, 1, 1, 1}; 24 | 25 | cout << count1s(a, n)< 2 | using namespace std; 3 | 4 | /* 5 | problem is similar as finding first occurrence 6 | because array is binary and sorted so, 7 | if we find first occurrence of 1, then subtract it from 8 | size of array to find count. 9 | */ 10 | 11 | int count1s(int a[], int n)//time comp. O(logn) 12 | { 13 | int low = 0; 14 | int high = n - 1; 15 | 16 | while (low <= high) 17 | { 18 | int mid = (low + high) / 2; 19 | if (a[mid] == 0) 20 | { 21 | low = mid + 1; 22 | } 23 | else 24 | { 25 | if (mid == 0 or a[mid - 1] != a[mid]) 26 | { 27 | return (n - mid); 28 | } 29 | else 30 | { 31 | high = mid - 1; 32 | } 33 | } 34 | } 35 | 36 | return 0; 37 | } 38 | 39 | int main() 40 | { 41 | int a[] = {0, 0, 0, 0, 1, 1, 1}; 42 | int n = sizeof(a) / sizeof(int); 43 | 44 | cout << count1s(a, n); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /06. Searching/06 Square Root/06a square root.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sqRoot(int n)//time comp. O(sqrt(n)) 5 | { 6 | int i = 1; 7 | 8 | while (i * i <= n) 9 | { 10 | i++; 11 | } 12 | 13 | return (i - 1); 14 | } 15 | 16 | int main() 17 | { 18 | int n = 225; 19 | 20 | cout << sqRoot(n); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /06. Searching/06 Square Root/06b square root.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sqRoot(int n)//time comp. O(logn) 5 | { 6 | int low = 0; 7 | int high = n; 8 | 9 | int ans = -1; 10 | while (low <= high) 11 | { 12 | int mid = (low + high) / 2; 13 | int mid_sq = mid * mid; 14 | 15 | if (mid_sq == n) 16 | { 17 | return mid; 18 | } 19 | else if (mid_sq > n) 20 | { 21 | high = mid - 1; 22 | } 23 | else 24 | { 25 | low = mid + 1; 26 | ans = mid; 27 | } 28 | } 29 | 30 | return ans; 31 | } 32 | 33 | int main() 34 | { 35 | int n = 225; 36 | 37 | cout << sqRoot(n); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /06. Searching/07 Search in Infinite Sized Array/07a search in infinite sorted array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int searchInfinite(int a[], int key)//time comp. O(p) ; p:position or index in array 5 | { 6 | int i = 0; 7 | 8 | while (true) 9 | { 10 | if (a[i] == key) 11 | { 12 | return i; 13 | } 14 | if (a[i] > key) 15 | { 16 | return -1; 17 | } 18 | i++; 19 | } 20 | } 21 | 22 | int main() 23 | { 24 | int n; 25 | cin >> n; 26 | 27 | int a[n]; 28 | for (int i = 0; i < n; i++) 29 | { 30 | cin >> a[i]; 31 | } 32 | 33 | int key; 34 | cin >> key; 35 | 36 | cout << searchInfinite(a, key); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /06. Searching/08 Search in Sorted Rotated Array/08a search in sorted rotated array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | suppose original sorted array is : 1 2 3 5 8 6 | 7 | now rotate it (any number of times clockwise or anticlockwise) 8 | eg: 2 3 5 8 1 9 | 10 | so above array is sorted rotated array 11 | 12 | we need to search in this kind of array 13 | */ 14 | 15 | //linear search 16 | int searchRotated(int a[], int n, int key)//time comp. O(n) 17 | { 18 | for (int i = 0; i < n; i++) 19 | { 20 | if (a[i] == key) 21 | { 22 | return i; 23 | } 24 | } 25 | 26 | return -1; 27 | } 28 | 29 | int main() 30 | { 31 | int a[] = {10, 20, 30, 40, 50, 8, 9}; 32 | int n = sizeof(a) / sizeof(int); 33 | int key = 30; 34 | 35 | cout << searchRotated(a, n, key); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /06. Searching/09 Peak Element/09b peak element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int peak(int a[], int n)//time comp. O(logn) 5 | { 6 | //base cases 7 | if (n <= 1) 8 | { 9 | return a[0]; 10 | } 11 | 12 | if (a[0] > a[1]) 13 | { 14 | return a[0]; 15 | } 16 | 17 | if (a[n - 1] > a[n - 2]) 18 | { 19 | return a[n - 1]; 20 | } 21 | 22 | //main code 23 | int low = 1; 24 | int high = n - 2; 25 | 26 | while (low <= high) 27 | { 28 | int mid = low + (high - low) / 2; 29 | 30 | if (a[mid] > a[mid - 1] and a[mid] > a[mid + 1]) 31 | { 32 | return a[mid]; 33 | } 34 | else if (a[mid] < a[mid - 1]) 35 | { 36 | high = mid - 1; 37 | } 38 | else 39 | { 40 | low = mid + 1; 41 | } 42 | } 43 | 44 | return -1; 45 | } 46 | 47 | int main() 48 | { 49 | int a[] = {5, 10, 20, 15, 7}; 50 | int n = sizeof(a) / sizeof(int); 51 | 52 | cout << peak(a, n); 53 | return 0; 54 | } -------------------------------------------------------------------------------- /06. Searching/10 Pair Sum/10a pair sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | in an unsorted array check if a 6 | pair exists with given sum 7 | */ 8 | 9 | bool twoSum(int a[], int n, int sum)//time comp. O(n^2) 10 | { 11 | for (int i = 0; i < n; i++) 12 | { 13 | for (int j = i + 1; j < n; j++) 14 | { 15 | if (a[i] + a[j] == sum) 16 | { 17 | return true; 18 | } 19 | } 20 | } 21 | 22 | return false; 23 | } 24 | 25 | int main() 26 | { 27 | int a[] = {3, 5, 9, 2, 8, 10, 11}; 28 | int n = sizeof(a) / sizeof(int); 29 | int sum = 17; 30 | 31 | cout << twoSum(a, n, sum); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /06. Searching/10 Pair Sum/10b pair sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | in an sorted array check if a 6 | pair exists with given sum 7 | */ 8 | 9 | //two pointer approach - only applicable on sorted arrays 10 | bool twoSum(int a[], int n, int sum)//time comp. O(n) 11 | { 12 | int i = 0; 13 | int j = n - 1; 14 | 15 | while (i < j) 16 | { 17 | int curr_sum = a[i] + a[j]; 18 | if (curr_sum == sum) 19 | { 20 | return true; 21 | } 22 | else if (curr_sum > sum) 23 | { 24 | j--; 25 | } 26 | else 27 | { 28 | i++; 29 | } 30 | } 31 | 32 | return false; 33 | } 34 | 35 | int main() 36 | { 37 | int a[] = {2, 3, 5, 8, 9, 10, 11}; 38 | int n = sizeof(a) / sizeof(int); 39 | int sum = 17; 40 | 41 | cout << twoSum(a, n, sum); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /06. Searching/11 Triplet Sum/11a triplet sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | in an unsorted array check if a 6 | triplet exists with given sum 7 | */ 8 | 9 | bool threeSum(int a[], int n, int sum)//time comp. O(n^3) 10 | { 11 | for (int i = 0; i < n; i++) 12 | { 13 | for (int j = i + 1; j < n; j++) 14 | { 15 | for (int k = j + 1; k < n; k++) 16 | { 17 | if (a[i] + a[j] + a[k] == sum) 18 | { 19 | return true; 20 | } 21 | } 22 | } 23 | } 24 | 25 | return false; 26 | } 27 | 28 | int main() 29 | { 30 | int a[] = {3, 2, 8, 4, 20, 9, 40}; 31 | int n = sizeof(a) / sizeof(int); 32 | int sum = 32; 33 | 34 | cout << threeSum(a, n, sum); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /06. Searching/11 Triplet Sum/11b triplet sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | in an unsorted array check if a 6 | triplet exists with given sum 7 | */ 8 | 9 | //two pointer approach - only applicable on sorted arrays 10 | bool threeSum(int a[], int n, int sum)//time comp. O(n^2) 11 | { 12 | for (int i = 0; i < n; i++) 13 | { 14 | int p1 = i + 1; 15 | int p2 = n - 1; 16 | 17 | while (p1 < p2) 18 | { 19 | int curr_sum = a[i] + a[p1] + a[p2]; 20 | if (curr_sum == sum) 21 | { 22 | return true; 23 | } 24 | else if (curr_sum > sum) 25 | { 26 | p2--; 27 | } 28 | else 29 | { 30 | p1++; 31 | } 32 | } 33 | } 34 | 35 | return false; 36 | } 37 | 38 | int main() 39 | { 40 | int a[] = {2, 3, 4, 8, 9, 20, 40}; 41 | int n = sizeof(a) / sizeof(int); 42 | int sum = 32; 43 | 44 | cout << threeSum(a, n, sum); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /06. Searching/12 Count Pairs with Given Sum/12a count pairs with given sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int twoSum(int a[], int n, int sum)//time comp. O(n^2) 5 | { 6 | int cnt = 0; 7 | 8 | for (int i = 0; i < n; i++) 9 | { 10 | for (int j = i + 1; j < n; j++) 11 | { 12 | if (a[i] + a[j] == sum) 13 | { 14 | cnt++; 15 | } 16 | } 17 | } 18 | 19 | return cnt; 20 | } 21 | 22 | int main() 23 | { 24 | int a[] = {1, 1, 1, 1}; 25 | int n = sizeof(a) / sizeof(int); 26 | int sum = 2; 27 | 28 | cout << twoSum(a, n, sum); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /06. Searching/12 Count Pairs with Given Sum/12b count pairs with given sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int twoSum(int a[], int n, int sum)//time comp. O(n) ; space comp. O(n) 5 | { 6 | int cnt = 0; 7 | 8 | unordered_map m; 9 | for (int i = 0; i < n; i++) 10 | { 11 | if (m.find(sum - a[i]) != m.end()) 12 | { 13 | cnt += m[sum - a[i]]; 14 | } 15 | m[a[i]]++; 16 | } 17 | 18 | return cnt; 19 | } 20 | 21 | int main() 22 | { 23 | int a[] = {1, 1, 1, 1}; 24 | int n = sizeof(a) / sizeof(int); 25 | int sum = 2; 26 | 27 | cout << twoSum(a, n, sum); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /06. Searching/13 Count Triplet with Given Sum/13a count triplets with given sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int threeSum(int a[], int n, int sum) //time comp. O(n^3) 5 | { 6 | int cnt = 0; 7 | 8 | for (int i = 0; i < n; i++) 9 | { 10 | for (int j = i + 1; j < n; j++) 11 | { 12 | for (int k = j + 1; k < n; k++) 13 | { 14 | if (a[i] + a[j] + a[k] == sum) 15 | { 16 | cnt++; 17 | } 18 | } 19 | } 20 | } 21 | 22 | return cnt; 23 | } 24 | 25 | int main() 26 | { 27 | int a[] = {0, -1, 2, -3, 1}; 28 | int n = sizeof(a) / sizeof(int); 29 | int sum = -2; 30 | 31 | cout << threeSum(a, n, sum); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /06. Searching/13 Count Triplet with Given Sum/13b count triplets with given sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //two pointer approach - only applicable on sorted arrays 5 | int threeSum(int a[], int n, int sum)//time comp. O(n^2) ; space comp. O(n) 6 | { 7 | int cnt = 0; 8 | 9 | for (int i = 0; i < n; i++) 10 | { 11 | unordered_set s; 12 | for (int j = i + 1; j < n; j++) 13 | { 14 | int x = sum - (a[i] + a[j]); 15 | 16 | if (s.find(x) != s.end()) 17 | { 18 | cnt++; 19 | } 20 | else 21 | { 22 | s.insert(a[j]); 23 | } 24 | } 25 | } 26 | 27 | return cnt; 28 | } 29 | 30 | int main() 31 | { 32 | int a[] = {0, -1, 2, -3, 1}; 33 | int n = sizeof(a) / sizeof(int); 34 | int sum = -2; 35 | 36 | cout << threeSum(a, n, sum); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /06. Searching/14 Check if there exists a Triplet with Given Condition/14a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Check if triplet with condition 6 | a^2+b^2=c^2 exists in array 7 | */ 8 | 9 | //time: O(n*n*n) 10 | bool isTriplet(vector nums) 11 | { 12 | int n=nums.size(); 13 | 14 | for(int i=0;i nums={3,1,4,6,5}; 38 | 39 | cout< 2 | using namespace std; 3 | 4 | //time: O(n*n) 5 | bool isTriplet(vector nums) 6 | { 7 | int n=nums.size(); 8 | 9 | for(int i=0;i=0;i--) 17 | { 18 | int l=0; 19 | int h=i-1; 20 | 21 | while(lnums[i]) 30 | { 31 | h--; 32 | } 33 | else 34 | { 35 | l++; 36 | } 37 | } 38 | } 39 | 40 | return false; 41 | } 42 | 43 | int main() 44 | { 45 | vector nums={3,1,4,6,5}; 46 | 47 | cout< 2 | using namespace std; 3 | 4 | double median(vector v1, vector v2)//time comp. O((m+n)*log(m+n)) ; space comp. O(m+n) 5 | { 6 | vector v; 7 | 8 | for (auto x : v1) 9 | { 10 | v.push_back(x); 11 | } 12 | 13 | for (auto x : v2) 14 | { 15 | v.push_back(x); 16 | } 17 | 18 | sort(v.begin(), v.end()); 19 | 20 | int sz = v.size(); 21 | 22 | if (sz % 2 == 0) 23 | { 24 | int idx1 = sz / 2; 25 | int idx2 = idx1 - 1; 26 | 27 | return ((v[idx1] + v[idx2]) / 2.0); 28 | } 29 | else 30 | { 31 | return v[sz / 2]; 32 | } 33 | } 34 | 35 | int main() 36 | { 37 | vector v1{10, 20, 30, 40, 50}; 38 | vector v2{5, 15, 25, 35, 45}; 39 | 40 | int m = v1.size(); 41 | int n = v2.size(); 42 | 43 | cout << median(v1, v2)< 2 | using namespace std; 3 | 4 | /* 5 | Given an array, only one element is repeating in it. 6 | Find that element such that: 7 | --> n>=2 8 | --> only one element repeats any number of times 9 | --> all the elements from 1 to max(arr) are present 10 | */ 11 | 12 | //super naive 13 | int repeatingElement(int a[], int n)//time comp. O(n^2) ; space comp. O(1) 14 | { 15 | for (int i = 0; i < n; i++) 16 | { 17 | for (int j = i + 1; j < n; j++) 18 | { 19 | if (a[i] == a[j]) 20 | { 21 | return a[i]; 22 | } 23 | } 24 | } 25 | 26 | return -1; 27 | } 28 | 29 | int main() 30 | { 31 | int arr[] = {3, 2, 1, 3, 4, 5}; 32 | int n = sizeof(arr) / sizeof(int); 33 | 34 | cout << repeatingElement(arr, n); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /06. Searching/16 Repeating Element/16b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int repeatingElement(int a[], int n)//time comp. O(n*logn) ; space comp. O(1) 5 | { 6 | sort(a, a + n); 7 | 8 | for (int i = 0; i < n - 1; i++) 9 | { 10 | if (a[i] == a[i + 1]) 11 | { 12 | return a[i]; 13 | } 14 | } 15 | 16 | return -1; 17 | } 18 | 19 | int main() 20 | { 21 | int arr[] = {3, 2, 1, 3, 4, 5}; 22 | int n = sizeof(arr) / sizeof(int); 23 | 24 | cout << repeatingElement(arr, n); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /06. Searching/16 Repeating Element/16c.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int repeatingElement(int a[], int n)//time comp. O(n) ; space comp. O(n) 5 | { 6 | unordered_map m; 7 | 8 | for (int i = 0; i < n; i++) 9 | { 10 | m[a[i]]++; 11 | if (m[a[i]] == 2) 12 | { 13 | return a[i]; 14 | } 15 | } 16 | 17 | return -1; 18 | } 19 | 20 | int main() 21 | { 22 | int arr[] = {3, 2, 1, 3, 4, 5}; 23 | int n = sizeof(arr) / sizeof(int); 24 | 25 | cout << repeatingElement(arr, n); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /06. Searching/16 Repeating Element/16d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int repeatingElement(int nums[], int n)//time comp. O(n) ; space comp. O(1) 5 | { 6 | int slow=nums[0]; 7 | int fast=nums[nums[0]]; 8 | 9 | while(slow!=fast) 10 | { 11 | slow=nums[slow]; 12 | fast=nums[nums[fast]]; 13 | } 14 | 15 | fast=0; 16 | while(slow!=fast) 17 | { 18 | slow=nums[slow]; 19 | fast=nums[fast]; 20 | } 21 | 22 | return slow; 23 | } 24 | 25 | int main() 26 | { 27 | int arr[] = {3, 2, 1, 3, 4, 5}; 28 | int n = sizeof(arr) / sizeof(int); 29 | 30 | cout << repeatingElement(arr, n); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /06. Searching/16 Repeating Element/16e.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int repeatingElement(int a[], int n)//time comp. O(n) ; space comp. O(1) 5 | { 6 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Inbuilt sort function features : 9 | 10 | 1. worst and average time comp. O(nlogn) 11 | 2. uses Intro-Sort (hybrid of heapsort , quicksort and insertion sort) 12 | 13 | */ 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /07. Sorting/01 Sort STL/01a STL Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a[] = {5, 4, 3, 2, 1}; 7 | int n = sizeof(a) / sizeof(int); 8 | 9 | sort(a, a + n); //sorting increasing form 10 | for (int x : a) 11 | { 12 | cout << x << " "; 13 | } 14 | cout << endl; 15 | 16 | sort(a, a + n, greater()); //sorting decreasing form 17 | for (int x : a) 18 | { 19 | cout << x << " "; 20 | } 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /07. Sorting/01 Sort STL/01b STL Vectors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | vector v = {5, 4, 3, 2, 1}; 7 | 8 | sort(v.begin(), v.end()); 9 | for (int x : v) 10 | { 11 | cout << x << " "; 12 | } 13 | cout << endl; 14 | 15 | sort(v.begin(), v.end(), greater()); 16 | for (int x : v) 17 | { 18 | cout << x << " "; 19 | } 20 | cout << endl; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /07. Sorting/01 Sort STL/01c STL Class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class point 5 | { 6 | public: 7 | int x, y; 8 | }; 9 | 10 | bool compare1(point p1, point p2) 11 | { 12 | return p1.x < p2.x; 13 | } 14 | 15 | bool compare2(point p1, point p2) 16 | { 17 | return p1.x > p2.x; 18 | } 19 | 20 | int main() 21 | { 22 | point a[] = {{12, 23}, {8, 9}, {5, 6}}; 23 | int n = sizeof(a) / sizeof(a[0]); 24 | 25 | sort(a, a + n, compare1); 26 | for (auto k : a) 27 | { 28 | cout << k.x << " " << k.y << endl; 29 | } 30 | cout << "----" << endl; 31 | 32 | sort(a, a + n, compare2); 33 | for (auto k : a) 34 | { 35 | cout << k.x << " " << k.y << endl; 36 | } 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /07. Sorting/02 Stability in Sorting Algorithm/02b Stability of Algorithms Examples.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Stable Algorithms : 9 | 10 | 1.Bubble Sort 11 | 2.Insertion Sort 12 | 3.Merge Sort 13 | 14 | Unstable Algorithms : 15 | 16 | 1.Selection 17 | 2.Quick Sort 18 | 3.Heap Sort 19 | 20 | */ 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /07. Sorting/03 Bubble Sort/03a Bubble Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Bubble Sort: Stable Algo 6 | swap the consecutive elements 7 | if they are in the wrong order. 8 | */ 9 | 10 | //time: O(n*n) 11 | //space: O(1) 12 | void bubbleSort(int a[], int n) 13 | { 14 | for (int i = 0; i < n - 1; i++) 15 | { 16 | for (int j = 0; j < n - i - 1; j++) 17 | { 18 | if (a[j] > a[j + 1]) 19 | { 20 | swap(a[j], a[j + 1]); 21 | } 22 | } 23 | } 24 | } 25 | 26 | void display(int a[], int n) 27 | { 28 | for (int i = 0; i < n; i++) 29 | { 30 | cout << a[i] << " "; 31 | } 32 | cout << endl; 33 | } 34 | 35 | int main() 36 | { 37 | int a[] = {3, 2, 4, 1, 5}; 38 | int n = sizeof(a) / sizeof(int); 39 | 40 | display(a, n); 41 | bubbleSort(a, n); 42 | display(a, n); 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /07. Sorting/03 Bubble Sort/03b Bubble Sort Optimized.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //time: O(n*n) 5 | //space: O(1) 6 | void bubbleSort(int a[], int n) 7 | { 8 | for (int i = 0; i < n - 1; i++) 9 | { 10 | bool flag = false; 11 | for (int j = 0; j < n - i - 1; j++) 12 | { 13 | if (a[j] > a[j + 1]) 14 | { 15 | swap(a[j], a[j + 1]); 16 | flag = true; 17 | } 18 | } 19 | if (flag == false) 20 | { 21 | break; 22 | } 23 | } 24 | } 25 | 26 | void display(int a[], int n) 27 | { 28 | for (int i = 0; i < n; i++) 29 | { 30 | cout << a[i] << " "; 31 | } 32 | cout << endl; 33 | } 34 | 35 | int main() 36 | { 37 | int a[] = {3, 2, 4, 1, 5}; 38 | int n = sizeof(a) / sizeof(int); 39 | 40 | display(a, n); 41 | bubbleSort(a, n); 42 | display(a, n); 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /07. Sorting/05 Insertion Sort/05 Insertion Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Insertion Sort: Stable Algo 6 | it is like sorting playing cards. 7 | insert the value in presorted array 8 | to sort the value in array. 9 | */ 10 | 11 | //time: O(n*n) 12 | //space: O(1) 13 | void insertionSort(int a[], int n) 14 | { 15 | for (int i = 1; i < n; i++) 16 | { 17 | int key = a[i]; 18 | int j = i - 1; 19 | while (j >= 0 and a[j] > key) 20 | { 21 | a[j + 1] = a[j]; 22 | j--; 23 | } 24 | a[j + 1] = key; 25 | } 26 | } 27 | 28 | void display(int a[], int n) 29 | { 30 | for (int i = 0; i < n; i++) 31 | { 32 | cout << a[i] << " "; 33 | } 34 | cout << endl; 35 | } 36 | 37 | int main() 38 | { 39 | int a[] = {3, 2, 4, 1, 5}; 40 | int n = sizeof(a) / sizeof(int); 41 | 42 | display(a, n); 43 | insertionSort(a, n); 44 | display(a, n); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /07. Sorting/06 Merge Two Sorted Arrays/06a Merge Two Sorted Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void merge(int a[], int b[], int m, int n) //time comp. O((m+n)*log(m+n)) ; space comp. O(m+n) 5 | { 6 | int c[m + n]; 7 | 8 | for (int i = 0; i < m; i++) 9 | { 10 | c[i] = a[i]; 11 | } 12 | 13 | for (int i = 0; i < n; i++) 14 | { 15 | c[m + i] = b[i]; 16 | } 17 | 18 | sort(c, c + m + n); 19 | 20 | for (int i = 0; i < (m + n); i++) 21 | { 22 | cout << c[i] << " "; 23 | } 24 | } 25 | 26 | int main() 27 | { 28 | int a1[] = {1, 3, 5, 7, 9}; 29 | int a2[] = {2, 4, 6, 8}; 30 | 31 | int n1 = sizeof(a1) / sizeof(int); 32 | int n2 = sizeof(a2) / sizeof(int); 33 | 34 | merge(a1, a2, n1, n2); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /07. Sorting/06 Merge Two Sorted Arrays/06b Merge Two Sorted Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void merge(int a[], int b[], int m, int n) //time comp. O(m+n) ; space comp. O(1) 5 | { 6 | int i = 0; 7 | int j = 0; 8 | 9 | while (i < m and j < n) 10 | { 11 | if (a[i] <= b[j]) 12 | { 13 | cout << a[i] << " "; 14 | i++; 15 | } 16 | else 17 | { 18 | cout << b[j] << " "; 19 | j++; 20 | } 21 | } 22 | 23 | //print remaining of array a and b 24 | while (i < m) 25 | { 26 | cout << a[i] << " "; 27 | i++; 28 | } 29 | 30 | while (i < n) 31 | { 32 | cout << a[j] << " "; 33 | j++; 34 | } 35 | } 36 | 37 | int main() 38 | { 39 | int a1[] = {1, 3, 5, 7, 9}; 40 | int a2[] = {2, 4, 6, 8}; 41 | 42 | int n1 = sizeof(a1) / sizeof(int); 43 | int n2 = sizeof(a2) / sizeof(int); 44 | 45 | merge(a1, a2, n1, n2); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /07. Sorting/07 Merge Sort/07a Merge Sort Introduction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | /* 6 | 7 | Features : 8 | 9 | 1.divide and conquer algorithm 10 | 11 | 2.3steps : 12 | a.divide 13 | b.sort 14 | c.merge 15 | 16 | 3.stable algorithm 17 | 18 | 4.Time comp. O(n*logn) 19 | 20 | 5.Space comp. O(n) 21 | 22 | 6.For arrays : Quicksort performs better 23 | 24 | 7.For linked list : Merge Sort performs better 25 | O(1) aux space 26 | 27 | */ 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /07. Sorting/08 Intersection of Two Sorted Arrays/08a Intersection of two Sorted Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void intersection(int a[], int b[], int m, int n) //time comp. O(m*n) ; space comp. O(1) 5 | { 6 | for (int i = 0; i < m; i++) 7 | { 8 | if (i > 0 and a[i] == a[i - 1]) 9 | { 10 | continue; 11 | } 12 | 13 | for (int j = 0; j < n; j++) 14 | { 15 | if (a[i] == b[j]) 16 | { 17 | cout << a[i] << " "; 18 | break; 19 | } 20 | } 21 | } 22 | } 23 | 24 | int main() 25 | { 26 | int a[] = {3, 5, 10, 10, 10, 15, 15, 20}; 27 | int b[] = {5, 10, 10, 15, 30}; 28 | 29 | int m = sizeof(a) / sizeof(int); 30 | int n = sizeof(b) / sizeof(int); 31 | 32 | intersection(a, b, m, n); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /07. Sorting/08 Intersection of Two Sorted Arrays/08b Intersection of two Sorted Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void intersection(int a[], int b[], int m, int n) //time comp. O(m+n) ; space comp. O(1) 5 | { 6 | int i = 0, j = 0; 7 | 8 | while (i < m and j < n) 9 | { 10 | if (i > 0 and a[i] == a[i - 1]) 11 | { 12 | i++; 13 | continue; 14 | } 15 | 16 | if (a[i] < b[j]) 17 | { 18 | i++; 19 | } 20 | else if (a[i] > b[j]) 21 | { 22 | j++; 23 | } 24 | else 25 | { 26 | cout << a[i] << " "; 27 | i++; 28 | j++; 29 | } 30 | } 31 | } 32 | 33 | int main() 34 | { 35 | int a[] = {3, 5, 10, 10, 10, 15, 15, 20}; 36 | int b[] = {5, 10, 10, 15, 30}; 37 | 38 | int m = sizeof(a) / sizeof(int); 39 | int n = sizeof(b) / sizeof(int); 40 | 41 | intersection(a, b, m, n); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /07. Sorting/09 Union of Two Sorted Arrays/09a Union of Two Sorted Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void unionArrays(int a[], int b[], int m, int n) //time comp. O((m+n)*log(m+n)) ; space comp. O(m+n) 5 | { 6 | int c[m + n]; 7 | 8 | for (int i = 0; i < m; i++) 9 | { 10 | c[i] = a[i]; 11 | } 12 | 13 | for (int i = 0; i < n; i++) 14 | { 15 | c[m + i] = b[i]; 16 | } 17 | 18 | sort(c, c + m + n); 19 | 20 | for (int i = 0; i < (m + n); i++) 21 | { 22 | if (i == 0 or (i > 0 and c[i] != c[i - 1])) 23 | { 24 | cout << c[i] << " "; 25 | } 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | int a[] = {2, 5, 8}; 32 | int b[] = {2, 4, 9, 10, 15}; 33 | 34 | int m = sizeof(a) / sizeof(int); 35 | int n = sizeof(b) / sizeof(int); 36 | 37 | unionArrays(a, b, m, n); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /07. Sorting/10 Count Inversions in Array/10a count inversions in array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Inversion : ia[j] 5 | 6 | int countInversions(int a[], int n)//time comp. O(n^2) ; spoce comp. O(1) 7 | { 8 | int cnt = 0; 9 | 10 | for (int i = 0; i < n; i++) 11 | { 12 | for (int j = i + 1; j < n; j++) 13 | { 14 | if (a[i] > a[j]) 15 | { 16 | cnt++; 17 | } 18 | } 19 | } 20 | 21 | return cnt; 22 | } 23 | 24 | int main() 25 | { 26 | int a[] = {2, 4, 1, 3, 5}; 27 | int n = sizeof(a) / sizeof(int); 28 | 29 | cout << countInversions(a, n); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /07. Sorting/11 Partition Algorithms Introduction/11a Partitioning Introduction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | What is partitoning ? 9 | 10 | arr[]={3,8,6,12,10,7}; 11 | p=5; 12 | 13 | p is the index of element which is taken as pivot element 14 | 15 | so, 7 is the pivot element 16 | 17 | output : 2 18 | 19 | explaination: 20 | 21 | arr[]={3,6,7,8,12,10} 22 | 23 | after setting 7 to its correct position we return index of 7 in new array 24 | 25 | */ 26 | } -------------------------------------------------------------------------------- /07. Sorting/11 Partition Algorithms Introduction/11b Partitioning Algorithms.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Stable Algorithms : 9 | 10 | Naive 11 | 12 | Unstable Algorithms : 13 | 14 | a.Lomuto 15 | b.Hoare (more efficient) 16 | 17 | */ 18 | } -------------------------------------------------------------------------------- /07. Sorting/15 Quick Sort/15a Quick Sort Introduction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Features : 9 | 10 | 1.divide and conquer algorithm 11 | 12 | 2.worst case time : O(n^2) 13 | 14 | 3.faster because of : 15 | 16 | a.in-place 17 | b.cache friendly 18 | c.average case is O(n*logn) 19 | d.tail recursive 20 | 21 | 4.partition is key funtion(naive,lomuto,hoare) 22 | 23 | */ 24 | } -------------------------------------------------------------------------------- /07. Sorting/20 Minimum Difference in Array/20a Minimum Difference in Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int minDif(vector& nums)//time comp. O(n^2) ; space comp. O(1) 5 | { 6 | int n = nums.size(); 7 | int res = INT_MAX; 8 | 9 | for (int i = 0; i < n; i++) 10 | { 11 | for (int j = i + 1; j < n; j++) 12 | { 13 | res = min(res, abs(nums[i] - nums[j])); 14 | } 15 | } 16 | 17 | return res; 18 | } 19 | 20 | int main() 21 | { 22 | vector nums{3, 10, 8, 6}; 23 | 24 | cout << minDif(nums) << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /07. Sorting/20 Minimum Difference in Array/20b Minimum Difference in Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int minDif(vector& nums)//time comp. O(n*logn) ; space comp. O(1) 5 | { 6 | int n = nums.size(); 7 | 8 | sort(nums.begin(), nums.end()); 9 | 10 | int res = INT_MAX; 11 | 12 | for (int i = 0; i < n - 1; i++) 13 | { 14 | res = min(res, abs(nums[i] - nums[i + 1])); 15 | } 16 | 17 | return res; 18 | } 19 | 20 | int main() 21 | { 22 | vector nums{3, 10, 8, 6}; 23 | 24 | cout << minDif(nums) << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /08. Matrix/01 Matrix in C++/01a Fixed Size Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int arr[3][2]={ 6 | {10,20}, 7 | {30,40}, 8 | {50,60} 9 | }; 10 | 11 | for(int i=0;i<3;i++) 12 | { 13 | for(int j=0;j<2;j++) 14 | { 15 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int m=3,n=2; 6 | int arr[m][n]; 7 | 8 | int val=1; 9 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int m=3,n=2; 6 | int **arr;//double pointer : stores address of pointer 7 | 8 | arr=new int*[m]; 9 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int m=3,n=2; 6 | int *arr[m]; 7 | 8 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int m=3,n=2; 6 | vector arr[m]; 7 | 8 | int val=1; 9 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int m=3,n=2; 6 | vector> arr; 7 | 8 | for(int i=0;i v; 11 | 12 | for(int j=0;j 2 | using namespace std; 3 | const int r=4,c=4; 4 | 5 | void printSnake(int arr[r][c]) 6 | { 7 | for(int i=0;i=0;j--) 19 | { 20 | cout< 2 | using namespace std; 3 | 4 | bool search(vector> v, int key)//time comp. O(n) 5 | { 6 | int i = 0; 7 | int j = v[0].size() - 1; 8 | 9 | while (i= 0) 10 | { 11 | int x = v[i][j]; 12 | 13 | if (x == key) 14 | { 15 | return true; 16 | } 17 | else if (x > key) 18 | { 19 | j--; 20 | } 21 | else 22 | { 23 | i++; 24 | } 25 | } 26 | 27 | return false; 28 | } 29 | 30 | int main() 31 | { 32 | vector> v = { 33 | {10, 20, 30, 40}, 34 | {15, 25, 35, 45}, 35 | {27, 29, 37, 48}, 36 | {32, 33, 39, 50} 37 | }; 38 | 39 | int key = 37; 40 | 41 | cout << search(v, key); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /08. Matrix/08 Median of Row wise Sorted Matrix/08a Median of Row Wise Sorted Matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Assumptions : 6 | 1.odd sized arrays 7 | 2.all distinct elements 8 | */ 9 | int median(vector>& v)//time comp. O(r*c*log(r*c)) 10 | { 11 | vector arr; 12 | 13 | for (int i = 0; i < v.size(); i++) 14 | { 15 | for (int j = 0; j < v[i].size(); j++) 16 | { 17 | arr.push_back(v[i][j]); 18 | } 19 | } 20 | 21 | sort(arr.begin(), arr.end()); 22 | 23 | return arr[arr.size() / 2]; 24 | } 25 | 26 | int main() 27 | { 28 | vector> v = { 29 | {2, 4, 6, 8, 10}, 30 | {1, 3, 5, 7, 9}, 31 | {100, 200, 400, 500, 800} 32 | }; 33 | 34 | cout << median(v); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /09. Hashing/03 Count Distinct/03a Count Distinct.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int countDistinct(int arr[], int n) //time comp. O(n^2) ; space comp. O(1) 5 | { 6 | int cnt = 0; 7 | 8 | for (int i = 0; i < n; i++) 9 | { 10 | bool flag = false; 11 | for (int j = 0; j < i; j++) 12 | { 13 | if (arr[i] == arr[j]) 14 | { 15 | flag = true; 16 | break; 17 | } 18 | } 19 | if (flag == false) 20 | { 21 | cnt++; 22 | } 23 | } 24 | 25 | return cnt; 26 | } 27 | 28 | int main() 29 | { 30 | int arr[] = {15, 12, 13, 12, 13, 13, 18}; 31 | int n = sizeof(arr) / sizeof(int); 32 | 33 | cout << countDistinct(arr, n); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /09. Hashing/03 Count Distinct/03b Count Distinct.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int countDistinct(int arr[], int n) //time comp. O(n) ; space comp. O(n) 5 | { 6 | unordered_set s; 7 | 8 | for (int i = 0; i < n; i++) 9 | { 10 | s.insert(arr[i]); 11 | } 12 | 13 | return s.size(); 14 | } 15 | 16 | int countDistinct_Oneliner(int arr[], int n) //above implementation made short 17 | { 18 | unordered_set s(arr, arr + n); 19 | return s.size(); 20 | } 21 | 22 | int main() 23 | { 24 | int arr[] = {15, 12, 13, 12, 13, 13, 18}; 25 | int n = sizeof(arr) / sizeof(int); 26 | 27 | cout << countDistinct(arr, n) << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /09. Hashing/04 Frequencies of Elements/04a Frequencies of Elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void frequencies(int arr[], int n) //time comp. O(n^2) ; space comp. O(1) 5 | { 6 | for (int i = 0; i < n; i++) 7 | { 8 | //check if seen before 9 | bool flag = false; 10 | for (int j = 0; j < i; j++) 11 | { 12 | if (arr[i] == arr[j]) 13 | { 14 | flag = true; 15 | break; 16 | } 17 | } 18 | 19 | if (flag) 20 | { 21 | continue; 22 | } 23 | 24 | //if not seen before, count frequencies 25 | int cnt = 1; 26 | for (int j = i + 1; j < n; j++) 27 | { 28 | if (arr[i] == arr[j]) 29 | { 30 | cnt++; 31 | } 32 | } 33 | cout << arr[i] << " " << cnt << endl; 34 | } 35 | } 36 | 37 | int main() 38 | { 39 | int arr[] = {10, 12, 10, 15, 10, 20, 12, 12}; 40 | int n = sizeof(arr) / sizeof(int); 41 | 42 | frequencies(arr, n); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /09. Hashing/04 Frequencies of Elements/04b Frequencies of Elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void frequencies(int arr[], int n) //time comp. O(n) ; space comp. O(n) 5 | { 6 | unordered_map m; 7 | 8 | for (int i = 0; i < n; i++) 9 | { 10 | m[arr[i]]++; 11 | } 12 | 13 | for (auto x : m) 14 | { 15 | cout << x.first << " " << x.second << endl; 16 | } 17 | } 18 | 19 | int main() 20 | { 21 | int arr[] = {10, 12, 10, 15, 10, 20, 12, 12}; 22 | int n = sizeof(arr) / sizeof(int); 23 | 24 | frequencies(arr, n); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /09. Hashing/05 Intersection of Arrays/05b Intersection of Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int intersection(int a[], int b[], int m, int n) //time comp. O(max(m,n)) ; space comp. O(m+n) 5 | { 6 | unordered_set sa(a, a + m); 7 | unordered_set sb(b, b + n); 8 | 9 | int cnt = 0; 10 | for (auto x : sa) 11 | { 12 | if (sb.count(x) > 0) 13 | { 14 | cnt++; 15 | } 16 | } 17 | 18 | return cnt; 19 | } 20 | 21 | int main() 22 | { 23 | int a[] = {10, 15, 20, 5, 30}; 24 | int b[] = {30, 5, 30, 80}; 25 | 26 | int m = sizeof(a) / sizeof(int); 27 | int n = sizeof(b) / sizeof(int); 28 | 29 | cout << intersection(a, b, m, n); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /09. Hashing/05 Intersection of Arrays/05c Intersection of Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int intersection(int a[], int b[], int m, int n) //time comp. O(n) ; space comp. O(n) 5 | { 6 | int cnt = 0; 7 | unordered_set s(a, a + m); 8 | 9 | for (int i = 0; i < n; i++) 10 | { 11 | if (s.find(b[i]) != s.end()) 12 | { 13 | cnt++; 14 | s.erase(b[i]);//important step 15 | } 16 | } 17 | 18 | return cnt; 19 | } 20 | 21 | int main() 22 | { 23 | int a[] = {10, 15, 20, 5, 30}; 24 | int b[] = {30, 5, 30, 80}; 25 | 26 | int m = sizeof(a) / sizeof(int); 27 | int n = sizeof(b) / sizeof(int); 28 | 29 | cout << intersection(a, b, m, n); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /09. Hashing/06 Union of Arrays/06 Union of Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int unionArray(int a[], int b[], int m, int n) //time comp. O(n) ; space comp. O(m+n) 5 | { 6 | unordered_set s(a, a + m); 7 | 8 | for (int i = 0; i < n; i++) 9 | { 10 | s.insert(b[i]); 11 | } 12 | 13 | return s.size(); 14 | } 15 | 16 | int main() 17 | { 18 | int a[] = {15, 20, 5, 15}; 19 | int b[] = {15, 15, 15, 20, 10}; 20 | 21 | int m = sizeof(a) / sizeof(int); 22 | int n = sizeof(b) / sizeof(int); 23 | 24 | cout << unionArray(a, b, m, n); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /09. Hashing/07 Pair Sum/07a Pair with given Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool pairSum(int a[], int n, int sum) //time comp. O(n^2) ; space comp. O(1) 5 | { 6 | for (int i = 0; i < n; i++) 7 | { 8 | for (int j = i + 1; j < n; j++) 9 | { 10 | if (a[i] + a[j] == sum) 11 | { 12 | return true; 13 | } 14 | } 15 | } 16 | return false; 17 | } 18 | 19 | int main() 20 | { 21 | int arr[] = {3, 2, 8, 15, -8}; 22 | int n = sizeof(arr) / sizeof(n); 23 | int sum = 17; 24 | 25 | cout << pairSum(arr, n, sum); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /09. Hashing/07 Pair Sum/07b Pair with given Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool pairSum(int a[], int n, int sum) //time comp. O(n) ; space comp. O(n) 5 | { 6 | unordered_set m; 7 | 8 | for (int i = 0; i < n; i++) 9 | { 10 | if (m.find(sum - a[i]) != m.end()) 11 | { 12 | return true; 13 | } 14 | 15 | m.insert(a[i]); 16 | } 17 | 18 | return false; 19 | } 20 | 21 | int main() 22 | { 23 | int arr[] = {3, 2, 8, 15, -8}; 24 | int n = sizeof(arr) / sizeof(n); 25 | int sum = 17; 26 | 27 | cout << pairSum(arr, n, sum); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /09. Hashing/08 Subarray with Zero Sum/08a SubArray with Zero Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //subarray means contigous elements 5 | bool isZero(int a[], int n) //time comp. O(n^2) ; space comp. O(1) 6 | { 7 | for (int i = 0; i < n; i++) 8 | { 9 | int curr_sum = 0; 10 | for (int j = i; j < n; j++) 11 | { 12 | curr_sum = curr_sum + a[j]; 13 | if (curr_sum == 0) 14 | { 15 | return true; 16 | } 17 | } 18 | } 19 | 20 | return false; 21 | } 22 | 23 | int main() 24 | { 25 | int arr[] = {1, 4, 13, -3, -10, 5}; 26 | int n = sizeof(arr) / sizeof(int); 27 | 28 | cout << isZero(arr, n); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /09. Hashing/08 Subarray with Zero Sum/08b SubArray with Zero Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //trick : using prefix sum + hashing 5 | bool isZero(int a[], int n) //time comp. O(n) ; space comp. O(n) 6 | { 7 | unordered_set s; 8 | int prefix_sum = 0; 9 | 10 | for (int i = 0; i < n; i++) 11 | { 12 | prefix_sum += a[i]; 13 | if (s.find(prefix_sum) != s.end()) 14 | { 15 | return true; 16 | } 17 | if (prefix_sum == 0) 18 | { 19 | return true; 20 | } 21 | s.insert(prefix_sum); 22 | } 23 | 24 | return false; 25 | } 26 | 27 | int main() 28 | { 29 | int arr[] = {1, 4, 13, -3, -10, 5}; 30 | int n = sizeof(arr) / sizeof(int); 31 | 32 | cout << isZero(arr, n); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /09. Hashing/09 Subarray with Given Sum/09a SubArray with given Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isSum(int a[], int n, int sum) //time comp. O(n^2) ; space comp. O(1) 5 | { 6 | for (int i = 0; i < n; i++) 7 | { 8 | int curr_sum = 0; 9 | for (int j = i; j < n; j++) 10 | { 11 | curr_sum = curr_sum + a[j]; 12 | if (curr_sum == sum) 13 | { 14 | return true; 15 | } 16 | } 17 | } 18 | 19 | return false; 20 | } 21 | 22 | int main() 23 | { 24 | int arr[] = {5, 8, 6, 13, 3, -1}; 25 | int n = sizeof(arr) / sizeof(int); 26 | int sum = 22; 27 | 28 | cout << isSum(arr, n, sum); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /09. Hashing/09 Subarray with Given Sum/09b SubArray with given Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isSum(int a[], int n, int sum) //time comp. O(n) ; space comp. O(n) 5 | { 6 | unordered_set s; 7 | 8 | int pre_sum = 0; 9 | 10 | for (int i = 0; i < n; i++) 11 | { 12 | pre_sum = pre_sum + a[i]; 13 | if (s.find(pre_sum - sum) != s.end()) 14 | { 15 | return true; 16 | } 17 | if (pre_sum == sum) 18 | { 19 | return true; 20 | } 21 | s.insert(pre_sum); 22 | } 23 | 24 | return false; 25 | } 26 | 27 | int main() 28 | { 29 | int arr[] = {5, 8, 6, 13, 3, -1}; 30 | int n = sizeof(arr) / sizeof(int); 31 | int sum = 22; 32 | 33 | cout << isSum(arr, n, sum); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /09. Hashing/10 Longest Subarray with Given Sum/10a Longest SubArray with given Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int longestSubArray(int a[], int n, int sum) //time comp. O(n^2) ; space comp. O(1) 5 | { 6 | int res = 0; 7 | 8 | for (int i = 0; i < n; i++) 9 | { 10 | int curr_sum = 0; 11 | for (int j = i; j < n; j++) 12 | { 13 | curr_sum = curr_sum + a[j]; 14 | if (curr_sum == sum) 15 | { 16 | res = max(res, j - i + 1); 17 | } 18 | } 19 | } 20 | 21 | return res; 22 | } 23 | 24 | int main() 25 | { 26 | int arr[] = {3, 1, 0, 1, 8, 2, 3, 6}; 27 | int n = sizeof(arr) / sizeof(int); 28 | int sum = 5; 29 | 30 | cout << longestSubArray(arr, n, sum); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /09. Hashing/10 Longest Subarray with Given Sum/10b Longest SubArray with given Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int longestSubArray(int a[], int n, int sum) //time comp. O(n) ; space comp. O(n) 5 | { 6 | unordered_map m; 7 | int pre_sum = 0, res = 0; 8 | 9 | for (int i = 0; i < n; i++) 10 | { 11 | pre_sum += a[i]; 12 | if (pre_sum == sum) 13 | { 14 | res = i + 1; 15 | } 16 | 17 | if (m.find(pre_sum) == m.end()) 18 | { 19 | m.insert({pre_sum, i}); 20 | } 21 | 22 | if (m.find(pre_sum - sum) != m.end()) 23 | { 24 | res = max(res, i - m[pre_sum - sum]); 25 | } 26 | } 27 | 28 | return res; 29 | } 30 | 31 | int main() 32 | { 33 | int arr[] = {3, 1, 0, 1, 8, 2, 3, 6}; 34 | int n = sizeof(arr) / sizeof(int); 35 | int sum = 5; 36 | 37 | cout << longestSubArray(arr, n, sum); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /09. Hashing/11 Longest Subarray with Equal 0s and 1s/11a Longest SubArray with equal 0s and 1s.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int longest01(int a[], int n) //time comp. O(n^2) ; space comp. O(1) 5 | { 6 | int res = 0; 7 | 8 | for (int i = 0; i < n; i++) 9 | { 10 | int c0 = 0, c1 = 0; 11 | for (int j = i; j < n; j++) 12 | { 13 | if (a[j] == 0) 14 | { 15 | c0++; 16 | } 17 | else 18 | { 19 | c1++; 20 | } 21 | 22 | if (c0 == c1) 23 | { 24 | res = max(res, c0 + c1); 25 | } 26 | } 27 | } 28 | 29 | return res; 30 | } 31 | 32 | int main() 33 | { 34 | int arr[] = {1, 0, 1, 1, 1, 0, 0}; 35 | int n = sizeof(arr) / sizeof(int); 36 | 37 | cout << longest01(arr, n); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /09. Hashing/12 Longest Common Span with Same Sum in Binary Array/12a Longest Common Span with Same Sum in Binary Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int longestCommonSpan(int a[], int b[], int n) //time comp. O(n^2) ; space comp. O(1) 5 | { 6 | int res = 0; 7 | 8 | for (int i = 0; i < n; i++) 9 | { 10 | int sum1 = 0, sum2 = 0; 11 | for (int j = i; j < n; j++) 12 | { 13 | sum1 += a[j]; 14 | sum2 += b[j]; 15 | if (sum1 == sum2) 16 | { 17 | res = max(res, j - i + 1); 18 | } 19 | } 20 | } 21 | 22 | return res; 23 | } 24 | 25 | int main() 26 | { 27 | int a[] = {0, 1, 0, 0, 0, 0}; 28 | int b[] = {1, 0, 1, 0, 0, 1}; 29 | int n = sizeof(a) / sizeof(int); 30 | 31 | cout << longestCommonSpan(a, b, n); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /09. Hashing/13 Longest Common Subsequence/13a Longest Consecutive SubSequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int longestConsecutiveSubsequence(int a[], int n)//time comp. O(n*logn) ; space comp. O(1) 5 | { 6 | sort(a, a + n); 7 | int res = 1, curr = 1; 8 | 9 | for (int i = 1; i < n; i++) 10 | { 11 | if (a[i] == a[i - 1] + 1) 12 | { 13 | curr++; 14 | } 15 | else 16 | { 17 | res = max(res, curr); 18 | curr = 1; 19 | } 20 | } 21 | 22 | return max(res, curr); 23 | } 24 | 25 | int main() 26 | { 27 | int arr[] = {1, 9, 3, 4, 2, 20}; 28 | int n = sizeof(arr) / sizeof(n); 29 | 30 | cout << longestConsecutiveSubsequence(arr, n); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /09. Hashing/14 Count Distinct Elements in a Window of size K/14a Count Distinct Elements in Window of size K.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void distinctInKSizeWindow(int a[], int n, int k)//time comp. O((n-k)*k*k) ; space comp. O(1) 5 | { 6 | for (int i = 0; i <= n - k; i++) 7 | { 8 | int cnt = 0; 9 | for (int j = 0; j < k; j++) 10 | { 11 | bool flag = false; 12 | for (int p = 0; p < j; p++) 13 | { 14 | if (a[i + j] == a[i + p]) 15 | { 16 | flag = true; 17 | break; 18 | } 19 | } 20 | if (flag == false) 21 | { 22 | cnt++; 23 | } 24 | } 25 | cout << cnt << " "; 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | int arr[] = {10, 10, 5, 3, 20, 5}; 32 | int n = sizeof(arr) / sizeof(int); 33 | int k = 4; 34 | 35 | distinctInKSizeWindow(arr, n, k); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /09. Hashing/14 Count Distinct Elements in a Window of size K/14b Count Distinct Elements in Window of size K.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void distinctInKSizeWindow(int *a, int n, int k)//time comp. O((n-k)*k) ; space comp. O(k) 5 | { 6 | for (int i = 0; i <= n - k; i++) 7 | { 8 | set s; 9 | for (int j = i; j < i + k; j++) 10 | { 11 | s.insert(a[j]); 12 | } 13 | cout << s.size() << " "; 14 | } 15 | } 16 | 17 | int main() 18 | { 19 | int arr[] = {10, 10, 5, 3, 20, 5}; 20 | int n = sizeof(arr) / sizeof(int); 21 | int k = 4; 22 | 23 | distinctInKSizeWindow(arr, n, k); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /09. Hashing/14 Count Distinct Elements in a Window of size K/14c Count Distinct Elements in Window of size K.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void distinctInKSizeWindow(int a[], int n, int k)//time comp. O(n-k) ; space comp. O(k) 5 | { 6 | unordered_map m; 7 | 8 | for (int i = 0; i < k; i++) 9 | { 10 | m[a[i]]++; 11 | } 12 | 13 | cout << m.size() << " "; 14 | 15 | for (int i = k; i < n; i++) 16 | { 17 | m[a[i - k]]--; 18 | 19 | if (m[a[i - k]] == 0) 20 | { 21 | m.erase(a[i - k]); 22 | } 23 | 24 | m[a[i]]++; 25 | 26 | cout << m.size() << " "; 27 | } 28 | } 29 | 30 | int main() 31 | { 32 | int arr[] = {10, 10, 5, 3, 20, 5}; 33 | int n = sizeof(arr) / sizeof(int); 34 | int k = 4; 35 | 36 | distinctInKSizeWindow(arr, n, k); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /09. Hashing/15 More than N-K Occurrences/15a More Than N k Occurences.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | print elements occuring more than 6 | (n/k) times in array 7 | */ 8 | 9 | void nkOcc(vector v, int k)//time comp. O(n*logn) ; space comp. O(1) 10 | { 11 | int n = v.size(); 12 | sort(v.begin(), v.end()); 13 | 14 | int i = 1, cnt = 1; 15 | while (i < n) 16 | { 17 | while (i < n and v[i] == v[i - 1]) 18 | { 19 | cnt++; 20 | i++; 21 | } 22 | if (cnt > (n / k)) 23 | { 24 | cout << v[i - 1] << " "; 25 | } 26 | cnt = 1; 27 | i++; 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | vector v = {10, 10, 20, 30, 20, 10, 10}; 34 | int k = 2; 35 | 36 | nkOcc(v, k); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /09. Hashing/15 More than N-K Occurrences/15b More Than N k Occurences.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | print elements occuring more than 6 | (n/k) times in array 7 | */ 8 | 9 | void nkOcc(vector v, int k)//time comp. O(n) ; space comp. O(n) 10 | { 11 | int n = v.size(); 12 | unordered_map mp; 13 | 14 | for (int i = 0; i < n; i++) 15 | { 16 | mp[v[i]]++; 17 | } 18 | 19 | for (auto x : mp) 20 | { 21 | if (x.second > (n / k)) 22 | { 23 | cout << x.first << " "; 24 | } 25 | } 26 | } 27 | 28 | int main() 29 | { 30 | vector v = {10, 10, 20, 30, 20, 10, 10}; 31 | int k = 4; 32 | 33 | nkOcc(v, k); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /10. Strings/01 ASCII Values/01 ASCII Values.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | ASCII Values : 9 | 10 | 'a' to 'z' : 97 - 122 11 | 12 | 'A' to 'Z' : 65 - 90 13 | 14 | '0' to '9' : 48 - 57 15 | 16 | */ 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /10. Strings/02 Printing ASCII Values/02a Printing ASCII Values.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | char ch = 'a'; 7 | 8 | for (int i = 0; i < 26; i++) 9 | { 10 | cout << ch << " " << int(ch) << endl; 11 | ch++; 12 | } 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /10. Strings/02 Printing ASCII Values/02b Printing ASCII Values.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | char ch = 'A'; 7 | 8 | for (int i = 0; i < 26; i++) 9 | { 10 | cout << ch << " " << int(ch) << endl; 11 | ch++; 12 | } 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /10. Strings/02 Printing ASCII Values/02c Printing ASCII Values.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | char ch = '0'; 7 | 8 | for (int i = 0; i < 10; i++) 9 | { 10 | cout << ch << " " << int(ch) << endl; 11 | ch++; 12 | } 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /10. Strings/03 Print Frequencies of Character/03 Print Frequencies of Character in String.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | //Space: O(1) 6 | void freq(string s) 7 | { 8 | int n = s.length(); 9 | int count[26] = {0}; 10 | 11 | //fill count array 12 | for (int i = 0; i < n; i++) 13 | { 14 | count[s[i] - 'a']++; 15 | } 16 | 17 | //print output 18 | for (int i = 0; i < 26; i++) 19 | { 20 | if (count[i] != 0) 21 | { 22 | cout << char('a' + i) << " " << count[i] << endl; 23 | } 24 | } 25 | } 26 | 27 | int main() 28 | { 29 | string s = "parthgarg"; 30 | 31 | freq(s); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /10. Strings/04 Strings in C/04a Strings in C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | //1.c-style strings 7 | char s[] = "parth"; //null character ('\0') at end of string by default 8 | 9 | cout << s << endl; 10 | cout << sizeof(s) << endl << "---" << endl; //size is 6 not 5 because extra character '\0' (Null character) is stored 11 | 12 | //2.c-style strings 13 | char a[] = {'g', 'a', 'r', 'g'}; //in this format null character not stored by default 14 | 15 | cout << a << endl; //since null character not present , so garbage value appears at end 16 | cout << sizeof(a) << endl << "---" << endl; 17 | 18 | //3.c-style strings 19 | char b[] = {'a', 'm', 'a', 'n', 'd', 'a', '\0'}; 20 | 21 | cout << b << endl; 22 | cout << sizeof(b) << endl << "---" << endl; 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /10. Strings/06 Taking Input/06a Taking Input.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | string s; 7 | cin >> s; 8 | 9 | /* 10 | if you give input : "parth" then it will print "parth" 11 | if you give input : "parth garg" then only "parth" will be printed 12 | 13 | because cin when sees space it stops reading the character 14 | */ 15 | 16 | cout << s; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /10. Strings/06 Taking Input/06b Taking Input.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | string s; 7 | getline(cin, s); //getline function reads full string "parth garg" 8 | 9 | cout << s; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /10. Strings/06 Taking Input/06c Taking Input.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | string s; 7 | getline(cin, s, '$'); 8 | 9 | /* 10 | 11 | getline function also has a third parameter 12 | which is optional , you can replace '$' with any 13 | other character . This parameter is used to stop 14 | getline function to take input . 15 | 16 | */ 17 | 18 | cout << s; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /10. Strings/07 Iterating Strings/07 Iterating Strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | string s = "parth garg"; 7 | 8 | //method 1 : simple for loop 9 | for (int i = 0; i < s.length(); i++) 10 | { 11 | cout << s[i]; 12 | } 13 | cout << endl; 14 | 15 | //method 2 : for each loop 16 | for (char x : s) 17 | { 18 | cout << x; 19 | } 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /10. Strings/08 Palindrome Check/08a Palindrome Check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Palindrome : 6 | A palindrome is a word, number, phrase, or other sequence of characters 7 | which reads the same backward as forward, such as madam or racecar. 8 | */ 9 | 10 | //Time: O(n) 11 | //Space: O(n) 12 | 13 | bool isPalindrome(string s) 14 | { 15 | string rev = s; 16 | 17 | //reverse function 18 | reverse(rev.begin(), rev.end()); 19 | 20 | return (rev == s); 21 | } 22 | 23 | int main() 24 | { 25 | string s = "madam"; 26 | 27 | cout << isPalindrome(s); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /10. Strings/08 Palindrome Check/08b Palindrome Check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | //Space: O(1) 6 | 7 | bool isPalindrome(string s) 8 | { 9 | int i = 0; 10 | int j = s.length() - 1; 11 | 12 | while (i < j) 13 | { 14 | if (s[i] == s[j]) 15 | { 16 | i++; 17 | j--; 18 | } 19 | else 20 | { 21 | return false; 22 | } 23 | } 24 | 25 | return true; 26 | } 27 | 28 | int main() 29 | { 30 | string s = "madam"; 31 | cin >> s; 32 | 33 | cout << isPalindrome(s); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /10. Strings/09 Validate Subsequence/09a Validate Subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Subsequence : 6 | a subsequence is a sequence that can be derived from another sequence 7 | by deleting some or no elements without changing the order of the remaining 8 | elements. 9 | 10 | eg :s1="ABCDE" 11 | s2="ACE" 12 | 13 | s2 is a subsequence of s1 14 | */ 15 | 16 | //Time: O(m+n) 17 | //Space: O(1) 18 | 19 | bool isSubsequence(string s1, string s2) 20 | { 21 | int m = s1.length(); 22 | int n = s2.length(); 23 | 24 | int i = 0, j = 0; 25 | 26 | while (i < m and j < n) 27 | { 28 | if (s1[i] == s2[j]) 29 | { 30 | j++; 31 | } 32 | i++; 33 | } 34 | 35 | return (j == n); 36 | } 37 | 38 | int main() 39 | { 40 | string s1 = "parth"; 41 | string s2 = "path"; 42 | 43 | cout << isSubsequence(s1, s2); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /10. Strings/09 Validate Subsequence/09b Validate Subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(m+n) 5 | //Space: O(m+n) 6 | 7 | bool isSubsequence(string s1, string s2, int m, int n) 8 | { 9 | //base case 10 | if (n == 0) 11 | { 12 | return true; 13 | } 14 | 15 | if (m == 0) 16 | { 17 | return false; 18 | } 19 | 20 | //recursive case 21 | if (s1[m - 1] == s2[n - 1]) 22 | { 23 | return isSubsequence(s1, s2, m - 1, n - 1); 24 | } 25 | else 26 | { 27 | return isSubsequence(s1, s2, m - 1, n); 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | string s1 = "parth"; 34 | string s2 = "path"; 35 | 36 | int m = s1.length(); 37 | int n = s2.length(); 38 | 39 | cout << isSubsequence(s1, s2, m, n); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /10. Strings/10 Check for Anagram/10a Check for Anagram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Anagram : 6 | a word or phrase that is made by arranging the letters of 7 | another word or phrase in a different order 8 | 9 | eg : "silent" and "listen" are anagrams 10 | */ 11 | 12 | //Time: O(m*logm +n*logn) 13 | //Space: O(1) 14 | 15 | bool isAnagram(string s1, string s2) 16 | { 17 | if (s1.length() != s2.length()) 18 | { 19 | return false; 20 | } 21 | 22 | sort(s1.begin(), s1.end()); 23 | sort(s2.begin(), s2.end()); 24 | 25 | return (s1 == s2); 26 | } 27 | 28 | int main() 29 | { 30 | string s1 = "silent"; 31 | string s2 = "listen"; 32 | 33 | cout << isAnagram(s1, s2); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /10. Strings/10 Check for Anagram/10b Check for Anagram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | //Space: O(1) 6 | 7 | bool isAnagram(string s1, string s2) 8 | { 9 | if (s1.length() != s2.length()) 10 | { 11 | return false; 12 | } 13 | 14 | int count[256] = {0}; 15 | int n = s1.length(); 16 | 17 | for (int i = 0; i < n; i++) 18 | { 19 | count[s1[i]]++; 20 | count[s2[i]]--; 21 | } 22 | 23 | for (int i = 0; i < 256; i++) 24 | { 25 | if (count[i] != 0) 26 | { 27 | return false; 28 | } 29 | } 30 | 31 | return true; 32 | } 33 | 34 | int main() 35 | { 36 | string s1 = "silent"; 37 | string s2 = "listen"; 38 | 39 | cout << isAnagram(s1, s2); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /10. Strings/11 Leftmost Repeating Character/11a Leftmost Repeating Character.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n^2) 5 | //Space: O(1) 6 | 7 | int leftMostRepeating(string s) 8 | { 9 | int n = s.length(); 10 | for (int i = 0; i < n; i++) 11 | { 12 | for (int j = i + 1; j < n; j++) 13 | { 14 | if (s[i] == s[j]) 15 | { 16 | return i; 17 | } 18 | } 19 | } 20 | 21 | return -1; 22 | } 23 | 24 | int main() 25 | { 26 | string s = "parthgarg"; 27 | 28 | cout << leftMostRepeating(s); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /10. Strings/11 Leftmost Repeating Character/11b Leftmost Repeating Character.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | //Space: O(1) 6 | 7 | int leftMostRepeating(string s) 8 | { 9 | int count[256] = {0}; 10 | int n = s.length(); 11 | 12 | for (int i = 0; i < n; i++) 13 | { 14 | count[s[i]]++; 15 | } 16 | 17 | for (int i = 0; i < n; i++) 18 | { 19 | if (count[s[i]] > 1) 20 | { 21 | return i; 22 | } 23 | } 24 | 25 | return -1; 26 | } 27 | 28 | int main() 29 | { 30 | string s = "parthgarg"; 31 | 32 | cout << leftMostRepeating(s); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /10. Strings/12 Leftmost Non Repeating Character/12a Leftmost Non Repeating Character.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //naive approach 5 | int leftMostNonRepeating(string s)//time comp. O(n^2) ; space comp. O(1) 6 | { 7 | int n = s.length(); 8 | 9 | for (int i = 0; i < n; i++) 10 | { 11 | int j; 12 | for (j = 0; j < n; j++) 13 | { 14 | if (i != j and s[i] == s[j]) 15 | { 16 | break; 17 | } 18 | } 19 | if (j == n) 20 | { 21 | return i; 22 | } 23 | } 24 | 25 | return -1; 26 | } 27 | 28 | int main() 29 | { 30 | string s = "parthgargp"; 31 | 32 | cout << leftMostNonRepeating(s); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /10. Strings/12 Leftmost Non Repeating Character/12b Leftmost Non Repeating Character.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //efficient approach 5 | int leftMostNonRepeating(string s)//time comp. O(n) ; space comp. O(1) 6 | { 7 | int count[256] = {0}; 8 | int n = s.length(); 9 | 10 | for (int i = 0; i < n; i++) 11 | { 12 | count[s[i]]++; 13 | } 14 | 15 | for (int i = 0; i < n; i++) 16 | { 17 | if (count[s[i]] == 1) 18 | { 19 | return i; 20 | } 21 | } 22 | 23 | return -1; 24 | } 25 | 26 | int main() 27 | { 28 | string s = "parthgargp"; 29 | 30 | cout << leftMostNonRepeating(s); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /10. Strings/13 Reverse Words in String/13a Reverse Words in String.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | //Space: O(n) 6 | 7 | void reverseWords(string s) 8 | { 9 | stack st; 10 | 11 | string temp = ""; 12 | int n = s.length(); 13 | 14 | for (int i = 0; i < n; i++) 15 | { 16 | if (s[i] == ' ') 17 | { 18 | st.push(temp); 19 | temp = ""; 20 | } 21 | else 22 | { 23 | temp = temp + s[i]; 24 | } 25 | } 26 | 27 | st.push(temp); 28 | 29 | while (!st.empty()) 30 | { 31 | cout << st.top() << " "; 32 | st.pop(); 33 | } 34 | } 35 | 36 | int main() 37 | { 38 | string s = "parth is the best"; 39 | 40 | reverseWords(s); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /10. Strings/13 Reverse Words in String/13b Reverse Words in String.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void reverse(string &s, int low, int high) //helper function 5 | { 6 | while (low < high) 7 | { 8 | swap(s[low], s[high]); 9 | low++; 10 | high--; 11 | } 12 | } 13 | 14 | //Time: O(n) 15 | //Space: O(1) 16 | 17 | void reverseWords(string &s) 18 | { 19 | int n = s.length(); 20 | int start = 0; 21 | 22 | for (int end = 0; end < n; end++) 23 | { 24 | if (s[end] == ' ') 25 | { 26 | reverse(s, start, end - 1); 27 | start = end + 1; 28 | } 29 | } 30 | 31 | reverse(s, start, n - 1); 32 | reverse(s, 0, n - 1); 33 | } 34 | 35 | int main() 36 | { 37 | string s = "parth is the best"; 38 | 39 | reverseWords(s); 40 | cout << s; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /10. Strings/14 Check Rotation/14a Check if Strings are Rotation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void rotate(string &s) 5 | { 6 | char ch = s[0]; 7 | for (int i = 1; i < s.length(); i++) 8 | { 9 | s[i - 1] = s[i]; 10 | } 11 | s[s.length() - 1] = ch; 12 | } 13 | 14 | //Time: O(n^2) 15 | //Space: O(1) 16 | bool isRotated(string s1, string s2) 17 | { 18 | if (s1 == s2) 19 | { 20 | return true; 21 | } 22 | if (s1.length() != s2.length()) 23 | { 24 | return false; 25 | } 26 | 27 | int n = s1.length(); 28 | for (int i = 0; i < n; i++) 29 | { 30 | if (s1 == s2) 31 | { 32 | return true; 33 | } 34 | else 35 | { 36 | //rotate s2 37 | rotate(s2); 38 | } 39 | } 40 | 41 | return false; 42 | } 43 | 44 | int main() 45 | { 46 | string s1 = "ABCD"; 47 | string s2 = "CDAB"; 48 | 49 | cout << isRotated(s1, s2); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /10. Strings/14 Check Rotation/14b Check if Strings are Rotation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | //Space: O(m+n) 6 | 7 | bool isRotated(string s1, string s2) 8 | { 9 | if (s1 == s2) 10 | { 11 | return true; 12 | } 13 | if (s1.length() != s2.length()) 14 | { 15 | return false; 16 | } 17 | 18 | string temp = s1 + s1; 19 | 20 | return temp.find(s2) != string::npos; 21 | } 22 | 23 | int main() 24 | { 25 | string s1 = "ABCD"; 26 | string s2 = "CDAB"; 27 | 28 | cout << isRotated(s1, s2); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /10. Strings/16 Pattern Searching/16a Naive Pattern Searching.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n^2) 5 | //Space: O(1) 6 | void patternSearch(string s, string txt) 7 | { 8 | for (int i = 0; i < s.length(); i++) 9 | { 10 | if (s.substr(i, txt.length()) == txt) 11 | { 12 | cout << i << " "; 13 | } 14 | } 15 | } 16 | 17 | int main() 18 | { 19 | string s = "AAAAAA"; 20 | string txt = "AAA"; 21 | 22 | patternSearch(s, txt); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /10. Strings/16 Pattern Searching/16b Naive Pattern Searching.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //naive method 5 | void patternSearch(string txt, string pat)//time comp. O(n^2) ; space comp. O(1) 6 | { 7 | int n = txt.length(); 8 | int m = pat.length(); 9 | 10 | for (int i = 0; i <= n - m; i++) 11 | { 12 | bool flag = true; 13 | for (int j = 0; j < m; j++) 14 | { 15 | if (pat[j] != txt[i + j]) 16 | { 17 | flag = false; 18 | break; 19 | } 20 | } 21 | if (flag) 22 | { 23 | cout << i << " "; 24 | } 25 | } 26 | } 27 | 28 | int main() 29 | { 30 | string txt = "AAAAAA"; 31 | string pat = "AAA"; 32 | 33 | patternSearch(txt, pat); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /10. Strings/17 Lexiographic Rank of a String/17a Lexiographic Rank of a String.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n!) 5 | //Space: O(n) 6 | int lexiographicRank(string s) 7 | { 8 | string sorted_s = s; 9 | sort(sorted_s.begin(), sorted_s.end()); 10 | 11 | int rank = 0; 12 | while (true) 13 | { 14 | rank++; 15 | if (s == sorted_s) 16 | { 17 | return rank; 18 | } 19 | 20 | next_permutation(sorted_s.begin(), sorted_s.end()); 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | int main() 27 | { 28 | string s = "STRING"; 29 | 30 | cout << lexiographicRank(s) << endl; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /10. Strings/18 Longest Substring with Distinct Characters/18a Longest Substring with Distinct Characters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isDistinct(string s) 5 | { 6 | set set(s.begin(), s.end()); 7 | 8 | return (s.size() == set.size()); 9 | } 10 | 11 | //Time: O(n^3) 12 | //Space: O(n) 13 | int longestSubstring(string s) 14 | { 15 | int n = s.size(); 16 | int maxL = 0; 17 | 18 | for (int i = 0; i < n; i++) 19 | { 20 | for (int j = i; j < n; j++) 21 | { 22 | string temp = s.substr(i, j - i + 1); 23 | if (isDistinct(temp)) 24 | { 25 | maxL = max(maxL, j - i + 1); 26 | } 27 | } 28 | } 29 | 30 | return maxL; 31 | } 32 | 33 | int main() 34 | { 35 | string s = "abcabcdbb"; 36 | 37 | cout << longestSubstring(s) << endl; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /10. Strings/18 Longest Substring with Distinct Characters/18b Longest Substring with Distinct Characters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n^2) 5 | //Space: O(n) 6 | int longestSubstring(string s) 7 | { 8 | int n = s.size(); 9 | int maxL = 0; 10 | 11 | for (int i = 0; i < n; i++) 12 | { 13 | set set; 14 | for (int j = i; j < n; j++) 15 | { 16 | if (set.find(s[j]) != set.end()) 17 | { 18 | break; 19 | } 20 | else 21 | { 22 | set.insert(s[j]); 23 | maxL = max(maxL, j - i + 1); 24 | } 25 | } 26 | } 27 | 28 | return maxL; 29 | } 30 | 31 | int main() 32 | { 33 | string s = "abcabcdbb"; 34 | 35 | cout << longestSubstring(s) << endl; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /10. Strings/18 Longest Substring with Distinct Characters/18c Longest Substring with Distinct Characters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Time: O(n) 5 | //Space: O(n) 6 | int longestSubstring(string s) 7 | { 8 | int n = s.length(); 9 | int maxL = 0; 10 | int i, j = 0; 11 | 12 | unordered_set set; 13 | 14 | while (i < n and j < n) 15 | { 16 | if (set.find(s[j]) == set.end()) 17 | { 18 | set.insert(s[j]); 19 | maxL = max(maxL, j - i + 1); 20 | j++; 21 | } 22 | else 23 | { 24 | set.erase(s[i++]); 25 | } 26 | } 27 | 28 | return maxL; 29 | } 30 | 31 | int main() 32 | { 33 | string s = "abcabcdbb"; 34 | 35 | cout << longestSubstring(s) << endl; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /11. Linked List/01 Linked List Class/01 Linked List Class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* next; 9 | 10 | Node(int data_) 11 | { 12 | data=data_; 13 | next=NULL; 14 | } 15 | }; 16 | 17 | int main() 18 | { 19 | Node* head=new Node(10); 20 | head->next=new Node(20); 21 | head->next->next=new Node(30); 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /11. Linked List/02 Iterative Traversal/02 Iterative Traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* next; 9 | 10 | Node(int data_) 11 | { 12 | data=data_; 13 | next=NULL; 14 | } 15 | }; 16 | 17 | //Time: O(N) 18 | //Space: O(1) 19 | void print(Node* head) 20 | { 21 | while(head) 22 | { 23 | cout<data<<" "; 24 | head=head->next; 25 | } 26 | cout<next=new Node(20); 33 | head->next->next=new Node(30); 34 | 35 | print(head); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /11. Linked List/03 Recursive Traversal/03 Recursive Traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* next; 9 | 10 | Node(int data_) 11 | { 12 | data=data_; 13 | next=NULL; 14 | } 15 | }; 16 | 17 | //Time: O(N) 18 | //Space: O(N) 19 | void print(Node* head) 20 | { 21 | if(head==NULL) 22 | { 23 | return ; 24 | } 25 | 26 | cout<data<<" "; 27 | print(head->next); 28 | } 29 | 30 | int main() 31 | { 32 | Node* head=new Node(10); 33 | head->next=new Node(20); 34 | head->next->next=new Node(30); 35 | 36 | print(head); 37 | cout< 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* next; 9 | 10 | Node(int data_) 11 | { 12 | data=data_; 13 | next=NULL; 14 | } 15 | }; 16 | 17 | //Time: O(1) 18 | //Space: O(1) 19 | Node* insertAtHead(Node* &head,int key) 20 | { 21 | Node* n=new Node(key); 22 | n->next=head; 23 | 24 | return n; 25 | } 26 | 27 | void print(Node* head) 28 | { 29 | while(head) 30 | { 31 | cout<data<<" "; 32 | head=head->next; 33 | } 34 | cout<next=new Node(20); 41 | head->next->next=new Node(30); 42 | 43 | print(head); 44 | head=insertAtHead(head,40); 45 | print(head); 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /11. Linked List/06 Delete At Head/06 Delete At Head.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* next; 9 | 10 | Node(int data_) 11 | { 12 | data=data_; 13 | next=NULL; 14 | } 15 | }; 16 | 17 | //Time: O(1) 18 | //Space: O(1) 19 | Node* deleteAtHead(Node* &head) 20 | { 21 | if(head==NULL) 22 | { 23 | return NULL; 24 | } 25 | 26 | Node* curr=head; 27 | head=head->next; 28 | delete curr; 29 | 30 | return head; 31 | } 32 | 33 | void print(Node* head) 34 | { 35 | while(head) 36 | { 37 | cout<data<<" "; 38 | head=head->next; 39 | } 40 | cout<next=new Node(20); 47 | head->next->next=new Node(30); 48 | 49 | print(head); 50 | head=deleteAtHead(head); 51 | print(head); 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /11. Linked List/09 Iterative Search/09 Iterative Search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* next; 9 | 10 | Node(int data_) 11 | { 12 | data=data_; 13 | next=NULL; 14 | } 15 | }; 16 | 17 | //Time: O(N) 18 | //Space: O(1) 19 | int search(Node* head,int key) 20 | { 21 | int pos=1; 22 | 23 | while(head) 24 | { 25 | if(head->data==key) 26 | { 27 | return pos; 28 | } 29 | head=head->next; 30 | pos++; 31 | } 32 | 33 | return -1; 34 | } 35 | 36 | void print(Node* head) 37 | { 38 | while(head) 39 | { 40 | cout<data<<" "; 41 | head=head->next; 42 | } 43 | cout<next=new Node(20); 50 | head->next->next=new Node(30); 51 | 52 | print(head); 53 | 54 | cout< 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* prev; 9 | Node* next; 10 | 11 | Node(int data_) 12 | { 13 | data=data_; 14 | prev=NULL; 15 | next=NULL; 16 | } 17 | }; 18 | 19 | //Time: O(N) 20 | //Space: O(1) 21 | void print(Node* head) 22 | { 23 | while(head) 24 | { 25 | cout<data<<" "; 26 | head=head->next; 27 | } 28 | cout<next=node2; 38 | node2->next=node3; 39 | 40 | node2->prev=node1; 41 | node3->prev=node2; 42 | 43 | print(node1); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /11. Linked List/17 Circular Linked List Class/17 Circular Linked List Class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* next; 9 | 10 | Node(int data_) 11 | { 12 | data=data_; 13 | next=NULL; 14 | } 15 | }; 16 | 17 | int main() 18 | { 19 | Node* node1=new Node(10); 20 | Node* node2=new Node(20); 21 | Node* node3=new Node(30); 22 | 23 | node1->next=node2; 24 | node2->next=node3; 25 | node3->next=node1; 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /11. Linked List/18 Traversing Circular LL/18 Traversing Ciruclar LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* next; 9 | 10 | Node(int data_) 11 | { 12 | data=data_; 13 | next=NULL; 14 | } 15 | }; 16 | 17 | //Time: O(N) 18 | //Space: O(1) 19 | void print(Node* head) 20 | { 21 | if(head==NULL) 22 | { 23 | return ; 24 | } 25 | 26 | //firstly print head the traverse with loop 27 | Node* curr=head; 28 | 29 | cout<data<<" "; 30 | curr=curr->next; 31 | 32 | while(curr!=head) 33 | { 34 | cout<data<<" "; 35 | curr=curr->next; 36 | } 37 | cout<next=node2; 47 | node2->next=node3; 48 | node3->next=node1; 49 | 50 | print(node1); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /11. Linked List/27 Iterative Reverse of LL/27b Iterative Reverse of LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* next; 9 | 10 | Node(int data_) 11 | { 12 | data=data_; 13 | next=NULL; 14 | } 15 | }; 16 | 17 | //Time: O(N) 18 | //Space: O(1) 19 | Node* reverse(Node* &head) 20 | { 21 | Node* curr=head; 22 | Node* prev=NULL; 23 | 24 | while(curr) 25 | { 26 | Node* nextNode=curr->next; 27 | curr->next=prev; 28 | prev=curr; 29 | curr=nextNode; 30 | } 31 | 32 | return prev; 33 | } 34 | 35 | void print(Node* head) 36 | { 37 | while(head) 38 | { 39 | cout<data<<" "; 40 | head=head->next; 41 | } 42 | cout<next=new Node(20); 49 | head->next->next=new Node(30); 50 | 51 | print(head); 52 | head=reverse(head); 53 | print(head); 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /11. Linked List/28 Recursive Reverse of LL/28 Recursive Reverse of LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* next; 9 | 10 | Node(int data_) 11 | { 12 | data=data_; 13 | next=NULL; 14 | } 15 | }; 16 | 17 | //Time: O(N) 18 | //Space: O(N) 19 | Node* reverse(Node* &head) 20 | { 21 | if(head==NULL or head->next==NULL) 22 | { 23 | return head; 24 | } 25 | 26 | Node* n=reverse(head->next); 27 | head->next->next=head; 28 | head->next=NULL; 29 | return n; 30 | } 31 | 32 | void print(Node* head) 33 | { 34 | while(head) 35 | { 36 | cout<data<<" "; 37 | head=head->next; 38 | } 39 | cout<next=new Node(20); 46 | head->next->next=new Node(30); 47 | 48 | print(head); 49 | head=reverse(head); 50 | print(head); 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /11. Linked List/31 Detect Loop/31a Detect Loop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* next; 9 | 10 | Node(int data_) 11 | { 12 | data=data_; 13 | next=NULL; 14 | } 15 | }; 16 | 17 | //Time: O(N) 18 | //Space: O(N) 19 | bool detectLoop(Node* &head) 20 | { 21 | unordered_set s; 22 | 23 | Node* curr=head; 24 | while(curr) 25 | { 26 | if(s.find(curr)!=s.end()) 27 | { 28 | return true; 29 | } 30 | s.insert(curr); 31 | curr=curr->next; 32 | } 33 | 34 | return false; 35 | } 36 | 37 | int main() 38 | { 39 | Node* node1=new Node(10); 40 | Node* node2=new Node(20); 41 | Node* node3=new Node(30); 42 | Node* node4=new Node(40); 43 | 44 | node1->next=node2; 45 | node2->next=node3; 46 | node3->next=node4; 47 | node4->next=node2; 48 | 49 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | >> Stack 9 | Stack is a data structure designed to operate in LIFO (Last in First out) context. 10 | In stack elements are inserted as well as get removed from only one end. 11 | 12 | >> Stack Operations 13 | 1.push(x): push `x` into stack 14 | 2.pop(): remove element on top of the stack 15 | 3.size(): returns size of stack 16 | 4.empty(): returns true if stack empty 17 | 5.top(): returns top element of the stack 18 | 19 | note: all above operations take O(1) time. 20 | 21 | >> Stack Errors (when memory not dynamically allocated) 22 | 1.underflow: when top() or pop() called on empty stack 23 | 2.overflow: when push() called on full stack 24 | 25 | */ 26 | return 0; 27 | } -------------------------------------------------------------------------------- /12. Stacks/03 STL/03 STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | All stack functions having time complexties 6 | O(1) 7 | */ 8 | 9 | int main() 10 | { 11 | stack s; 12 | 13 | //push elements 14 | s.push(10); 15 | s.push(20); 16 | s.push(30); 17 | s.push(40); 18 | s.push(50); 19 | 20 | //size of stack 21 | cout << s.size() << endl; 22 | 23 | //top element 24 | cout << s.top() << endl; 25 | 26 | //pop elements from top of stack 27 | s.pop(); 28 | s.pop(); 29 | s.pop(); 30 | s.pop(); 31 | s.pop(); 32 | 33 | //check if stack empty, returns 1 if stack empty 34 | cout << s.empty() << endl; 35 | 36 | //stack traversal 37 | s.push(10); 38 | s.push(20); 39 | s.push(15); 40 | 41 | while (s.empty() == false) 42 | { 43 | cout << s.top() << " "; 44 | s.pop(); 45 | } 46 | cout << endl; 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /12. Stacks/07 Stock Span Problem/07a Stock Span Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //time: O(n*n) 5 | //space: O(1) 6 | void printSpan(int arr[],int n) 7 | { 8 | for(int i=0;i 2 | using namespace std; 3 | 4 | //time: O(n) 5 | //space: O(n) 6 | //span=idx_curr_ele - idx_previous_greater_ele 7 | //(in case of no previous greater than add 1 in curr_ele idx) 8 | void printSpan(int arr[],int n) 9 | { 10 | stack s; 11 | s.push(0); 12 | cout<<1<<" "; 13 | 14 | for(int i=1;i 2 | using namespace std; 3 | 4 | //time: O(n*n) 5 | //space: O(1) 6 | void printPrevGreater(int arr[],int n) 7 | { 8 | for(int i=0;i=0;j--) 12 | { 13 | if(arr[j]>arr[i]) 14 | { 15 | cout< 2 | using namespace std; 3 | 4 | //time: O(n) 5 | //space: O(n) 6 | void printPrevGreater(int arr[],int n) 7 | { 8 | stack s; 9 | s.push(0); 10 | cout<<-1<<" "; 11 | 12 | for(int i=1;i 2 | using namespace std; 3 | 4 | //time: O(n*n) 5 | //space: O(1) 6 | void nextGreater(int arr[],int n) 7 | { 8 | for(int i=0;iarr[i]) 14 | { 15 | cout< 2 | using namespace std; 3 | 4 | //time: O(n) 5 | //space: O(n) 6 | void nextGreater(int arr[],int n) 7 | { 8 | stack s; 9 | vector v; 10 | 11 | s.push(arr[n-1]); 12 | v.push_back(-1); 13 | 14 | for(int i=n-2;i>=0;i--) 15 | { 16 | while(s.size() and s.top() 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Queue is FIFO (First In First Out) data structure 9 | 10 | ------------------------ 11 | dequeue() <--- 10 | 20 | 30 | 40 | 50 | <--- enqueue(x) 12 | ------------------------ 13 | 14 | ^ ^ 15 | | | 16 | | | 17 | 18 | front rear 19 | 20 | Operations : 21 | 22 | a.enque(x) : insert 23 | b.deque() : remove 24 | c.getfront() : get front element 25 | d.getrear() : get last element 26 | e.size() : get current size of queue 27 | f.isempty() : check if queue empty 28 | 29 | all operations have time comp. O(1) 30 | 31 | */ 32 | } -------------------------------------------------------------------------------- /13. Queue/02_Queue_STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | queue q; 7 | 8 | //insert 9 | q.push(10); 10 | q.push(20); 11 | q.push(30); 12 | q.push(40); 13 | 14 | cout< 2 | using namespace std; 3 | 4 | void print(queue q) 5 | { 6 | while(q.empty()==false) 7 | { 8 | cout< &q)//time comp. O(n) ; space comp. O(n) 15 | { 16 | stack s; 17 | 18 | while(q.empty()==false) 19 | { 20 | s.push(q.front()); 21 | q.pop(); 22 | } 23 | 24 | while(s.empty()==false) 25 | { 26 | q.push(s.top()); 27 | s.pop(); 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | queue q; 34 | 35 | q.push(10); 36 | q.push(20); 37 | q.push(30); 38 | q.push(40); 39 | q.push(50); 40 | 41 | print(q); 42 | reverse(q); 43 | print(q); 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /13. Queue/05_Reversing_Queue_Recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void print(queue q) 5 | { 6 | while(q.empty()==false) 7 | { 8 | cout< &q)//time comp. O(n) ; space comp. O(n) 15 | { 16 | if(q.empty()) 17 | { 18 | return ; 19 | } 20 | 21 | int x=q.front(); 22 | q.pop(); 23 | reverse(q); 24 | q.push(x); 25 | } 26 | 27 | int main() 28 | { 29 | queue q; 30 | 31 | q.push(10); 32 | q.push(20); 33 | q.push(30); 34 | q.push(40); 35 | q.push(50); 36 | 37 | print(q); 38 | reverse(q); 39 | print(q); 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /13. Queue/06_Generate_Number_with_Given_Digits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | print first n numbers containing 6 | only 5 and 6 as digits 7 | */ 8 | 9 | void printNumbers(int n)//time comp. O(n) ; space comp. O(n) 10 | { 11 | queue q; 12 | 13 | q.push("5"); 14 | q.push("6"); 15 | 16 | for(int i=0;i>n; 30 | 31 | printNumbers(n); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /14. Deque/01 Introduction/01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Introduction: 9 | deque : doubly ended queue 10 | meaning that it has two ends : front and rear 11 | we can perform insert/delete on both ends. 12 | 13 | Basic Operations: 14 | 1.push_front() 15 | 2.push_back() 16 | 3.pop_front() 17 | 4.pop_back() 18 | 19 | More Operations: 20 | 1.front() 21 | 2.back() 22 | 3.size() 23 | 4.max_size() 24 | 5.empty() 25 | 26 | note: 27 | 1. All operations mentioned above are O(1) operations 28 | 2. In C++ STL, Deque allows random access 29 | eg: dq[0],dq[3] 30 | 31 | Implementation : 32 | 1.Linked List (doubly) 33 | 2.Array (circular) 34 | 35 | */ 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /14. Deque/05 Maximum of all Subarrays of size K/05a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Print all maximum in size of subarray k 5 | void maxOfSubarrays(vector v, int k)//time comp. O(n^2) ; space comp. O(1) 6 | { 7 | int n = v.size(); 8 | for (int i = 0; i < n - k + 1; i++) 9 | { 10 | int max_no = INT_MIN; 11 | for (int j = i; j < i + k; j++) 12 | { 13 | max_no = max(max_no, v[j]); 14 | } 15 | cout << max_no << " "; 16 | } 17 | } 18 | 19 | int main() 20 | { 21 | vector v = {10, 8, 5, 12, 15, 7, 6}; 22 | int k = 3; 23 | 24 | maxOfSubarrays(v, k); 25 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | -->Tree stores data in the form of 9 | hierarchical form. 10 | 11 | -->Non-linear data structure. 12 | 13 | Applications of Tree Data Structure: 14 | 1.used to represent hierarchical data. 15 | -->organisation structure 16 | -->folder structure 17 | -->XML/HTML content 18 | -->In OOPs(inheritance) 19 | 2.binary search trees 20 | 3.binary heap 21 | 4.B and B+ trees in DBMS 22 | 5.Spanning and shortest path trees 23 | 6.Parse trees, Comparison Trees in Compilers. 24 | 25 | */ 26 | } -------------------------------------------------------------------------------- /15. Binary Tree/02 Tree Class/02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Binary Tree: 6 | Every node has almost two 7 | children. 8 | */ 9 | 10 | class Node 11 | { 12 | public: 13 | int data; 14 | Node* left; 15 | Node* right; 16 | 17 | Node(int d) 18 | { 19 | data=d; 20 | left=NULL; 21 | right=NULL; 22 | } 23 | }; 24 | 25 | int main() 26 | { 27 | Node* root=new Node(10); 28 | 29 | root->left=new Node(20); 30 | root->right=new Node(30); 31 | 32 | root->left->left=new Node(40); 33 | root->left->right=new Node(50); 34 | 35 | root->right->left=new Node(60); 36 | root->right->right=new Node(70); 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /15. Binary Tree/03 Types of Tree Traversals/03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Tree Traversals: 9 | It means printing every key of tree exactly ones. 10 | 11 | 1. Breadth First Traversal / level Order Traversal 12 | 13 | 2. Depth First Traversal 14 | 2.1 Inorder Traversal (Left Root Right) 15 | 2.2 Preorder Traversal (Root Left Right) 16 | 2.3 Postorder Traversal (Left Right Root) 17 | 18 | */ 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /15. Binary Tree/04 Inorder Traversal/04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* left; 9 | Node* right; 10 | 11 | Node(int d) 12 | { 13 | data=d; 14 | left=NULL; 15 | right=NULL; 16 | } 17 | }; 18 | 19 | //Time: O(N) 20 | //Space: O(H) 21 | //Inorder: left Root Right 22 | void inorder(Node* root) 23 | { 24 | if(root==NULL) 25 | { 26 | return ; 27 | } 28 | 29 | inorder(root->left); 30 | cout<data<<" "; 31 | inorder(root->right); 32 | } 33 | 34 | int main() 35 | { 36 | Node* root=new Node(10); 37 | 38 | root->left=new Node(20); 39 | root->right=new Node(30); 40 | 41 | root->left->left=new Node(40); 42 | root->left->right=new Node(50); 43 | 44 | root->right->left=new Node(60); 45 | root->right->right=new Node(70); 46 | 47 | inorder(root); 48 | cout< 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* left; 9 | Node* right; 10 | 11 | Node(int d) 12 | { 13 | data=d; 14 | left=NULL; 15 | right=NULL; 16 | } 17 | }; 18 | 19 | //Time: O(N) 20 | //Space: O(H) 21 | //Preorder: Root Left Right 22 | void preorder(Node* root) 23 | { 24 | if(root==NULL) 25 | { 26 | return ; 27 | } 28 | 29 | cout<data<<" "; 30 | preorder(root->left); 31 | preorder(root->right); 32 | } 33 | 34 | int main() 35 | { 36 | Node* root=new Node(10); 37 | 38 | root->left=new Node(20); 39 | root->right=new Node(30); 40 | 41 | root->left->left=new Node(40); 42 | root->left->right=new Node(50); 43 | 44 | root->right->left=new Node(60); 45 | root->right->right=new Node(70); 46 | 47 | preorder(root); 48 | cout< 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* left; 9 | Node* right; 10 | 11 | Node(int d) 12 | { 13 | data=d; 14 | left=NULL; 15 | right=NULL; 16 | } 17 | }; 18 | 19 | //Time: O(N) 20 | //Space: O(H) 21 | //Postorder: Left Right Root 22 | void postorder(Node* root) 23 | { 24 | if(root==NULL) 25 | { 26 | return ; 27 | } 28 | 29 | postorder(root->left); 30 | postorder(root->right); 31 | cout<data<<" "; 32 | } 33 | 34 | int main() 35 | { 36 | Node* root=new Node(10); 37 | 38 | root->left=new Node(20); 39 | root->right=new Node(30); 40 | 41 | root->left->left=new Node(40); 42 | root->left->right=new Node(50); 43 | 44 | root->right->left=new Node(60); 45 | root->right->right=new Node(70); 46 | 47 | postorder(root); 48 | cout< 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* left; 9 | Node* right; 10 | 11 | Node(int d) 12 | { 13 | data=d; 14 | left=NULL; 15 | right=NULL; 16 | } 17 | }; 18 | 19 | //Time: O(N) 20 | //Space: O(H) 21 | int height(Node* root) 22 | { 23 | if(root==NULL) 24 | { 25 | return 0; 26 | } 27 | 28 | return (1+max(height(root->left),height(root->right))); 29 | } 30 | 31 | int main() 32 | { 33 | Node* root=new Node(10); 34 | 35 | root->left=new Node(20); 36 | root->right=new Node(30); 37 | 38 | root->left->left=new Node(40); 39 | root->left->right=new Node(50); 40 | 41 | root->right->left=new Node(60); 42 | root->right->right=new Node(70); 43 | 44 | cout< 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* left; 9 | Node* right; 10 | 11 | Node(int d) 12 | { 13 | data=d; 14 | left=NULL; 15 | right=NULL; 16 | } 17 | }; 18 | 19 | //Time: O(N) 20 | //Space: O(H); 21 | int size(Node* root) 22 | { 23 | if(root==NULL) 24 | { 25 | return 0; 26 | } 27 | 28 | return (1+size(root->left)+size(root->right)); 29 | } 30 | 31 | int main() 32 | { 33 | Node* root=new Node(10); 34 | 35 | root->left=new Node(20); 36 | root->right=new Node(30); 37 | 38 | root->left->left=new Node(40); 39 | root->left->right=new Node(50); 40 | 41 | root->right->left=new Node(60); 42 | root->right->right=new Node(70); 43 | 44 | cout< 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* left; 9 | Node* right; 10 | 11 | Node(int d) 12 | { 13 | data=d; 14 | left=NULL; 15 | right=NULL; 16 | } 17 | }; 18 | 19 | //Time: O(N) 20 | //Space: O(H); 21 | int maximum(Node* root) 22 | { 23 | if(root==NULL) 24 | { 25 | return INT_MIN; 26 | } 27 | 28 | return max({root->data,maximum(root->left),maximum(root->right)}); 29 | } 30 | 31 | int main() 32 | { 33 | Node* root=new Node(10); 34 | 35 | root->left=new Node(20); 36 | root->right=new Node(30); 37 | 38 | root->left->left=new Node(40); 39 | root->left->right=new Node(50); 40 | 41 | root->right->left=new Node(60); 42 | root->right->right=new Node(70); 43 | 44 | cout< 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node* left; 9 | Node* right; 10 | 11 | Node(int d) 12 | { 13 | data=d; 14 | left=NULL; 15 | right=NULL; 16 | } 17 | }; 18 | 19 | //Time: O(N) 20 | //Space: O(H) 21 | int res=0; 22 | int diameter(Node* root) 23 | { 24 | if(root==NULL) 25 | { 26 | return 0; 27 | } 28 | 29 | int lh=diameter(root->left); 30 | int rh=diameter(root->right); 31 | res=max(res,1+lh+rh); 32 | 33 | return 1+max(lh,rh); 34 | } 35 | 36 | int main() 37 | { 38 | Node* root=new Node(10); 39 | 40 | root->left=new Node(20); 41 | root->right=new Node(30); 42 | 43 | root->left->left=new Node(40); 44 | root->left->right=new Node(50); 45 | 46 | root->right->left=new Node(60); 47 | root->right->right=new Node(70); 48 | 49 | diameter(root); 50 | 51 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Self Balancing BST: 9 | 10 | --> Keep the height O(logN) 11 | 12 | --> As operations in BST depend on 13 | height. 14 | 15 | --> If height if logN then all operations 16 | will be in O(logN) time. 17 | 18 | --> These tree restructure(re-balancing or rotation) themselves upon 19 | insertion/deletion. 20 | 21 | --> If we know keys in advance then sort the keys 22 | and make middle element as root and repeat it 23 | for further nodes. 24 | 25 | --> Types of rotations: 26 | a. left rotation 27 | b. right rotation 28 | 29 | --> Examples of BST: 30 | a. AVL Tree - strict in making self BST 31 | b. Red Black Tree - slight loose in making BST 32 | 33 | --> C++ libraries (set and map) use Red Black Tree 34 | 35 | */ 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /16. Binary Search Tree/07 AVL Tree/07 AVL Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | AVL Tree: 9 | 10 | --> It is a BST. 11 | 12 | --> It is balanced. (diff. between left and right subtree 13 | doesn't exceed 1). 14 | 15 | --> Balance Factor: abs(LH-RH) 16 | 17 | --> Balance Factor <= 1 18 | 19 | --> Height of AVL Tree: h < c*log2(n+2) + b 20 | where, c =~ 1.4405 ; b =~ -1.3277 21 | 22 | */ 23 | return 0; 24 | } -------------------------------------------------------------------------------- /16. Binary Search Tree/08 Red Black Tree/08 Red Black Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Red Black Tree: 9 | 10 | --> Every node is either black or red 11 | 12 | --> Root node is always black 13 | 14 | --> No two consecutive Red nodes 15 | 16 | --> Number of black nodes from every 17 | node to all of its descedants leaves 18 | should be same. 19 | 20 | --> No very strict as AVL tree so, 21 | less number of rotations 22 | 23 | --> AVL Tree has faster search operations 24 | compared to Red Black Tree. 25 | 26 | --> When having a mix of operations (insertions, 27 | deletions,search) prefer Red Black Tree. 28 | 29 | */ 30 | return 0; 31 | } -------------------------------------------------------------------------------- /16. Binary Search Tree/09 Set STL/09b Set STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Internal Working and Time Complexities: 9 | 10 | --> set is build on self balancing BSTs 11 | 12 | --> particularly Red Black Tree 13 | 14 | --> inorder traversal of BST is sorted 15 | 16 | --> begin(),end(),rbegin(),rend(),size(),empty() : O(1) 17 | 18 | --> insert(),find(),count(),lower_bound(),upper_bound(),erase(val) : O(logN) 19 | 20 | --> erase(it) : Amortized O(1) operation 21 | 22 | */ 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /16. Binary Search Tree/09 Set STL/09c Set STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | set on user defined data-type 6 | */ 7 | 8 | class Test 9 | { 10 | public: 11 | int id; 12 | 13 | bool operator<(const Test& t) const 14 | { 15 | return (this->id < t.id); 16 | } 17 | }; 18 | 19 | int main() 20 | { 21 | Test t1 = { 110 }, t2 = { 102 }, 22 | t3 = { 101 }, t4 = { 115 }; 23 | 24 | set s; 25 | 26 | // insert structure in set 27 | s.insert(t1); 28 | s.insert(t2); 29 | s.insert(t3); 30 | s.insert(t4); 31 | 32 | //iterating 33 | for(auto x:s) 34 | { 35 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Internal Working and Time Complexities: 9 | 10 | --> it is a Red Black Tree 11 | 12 | --> all time complexities same as set 13 | 14 | --> begin(),end(),rbegin(),rend(),size(),empty() : O(1) 15 | 16 | --> count(),find(),erase(),insert(),lower_bound(),upper_bound() : O(logN) 17 | 18 | */ 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /17. Heaps/01 Introduction/01 Introduction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Binary Heap used in implementations of: 9 | 10 | a.Heap Sort 11 | 12 | b.Priority Queue 13 | 14 | Two Types: 15 | 16 | a.Min Heap - Min element always at top / Every node is smaller than its descendants 17 | 18 | b.Max Heap - Max element always at top / Every node is greater than its descendants 19 | 20 | Properties: 21 | 22 | Binary Heap is a complete Binary Tree(stored as an array) 23 | 24 | Accessing Element: 25 | 26 | left[i]=2*i+1 27 | right[i]=2*i+2 28 | parent[i]=floor((i-1)/2) 29 | 30 | Advantages: 31 | 32 | a.Contigous Location 33 | 34 | b.Random Access 35 | 36 | c.Cache Friendly 37 | 38 | d.Complete Binary Tree - minimum height of tree is there(space efficient) 39 | 40 | */ 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /17. Heaps/02 Implementation/02 Implementation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class MinHeap 5 | { 6 | int *arr; 7 | int size; 8 | int capacity; 9 | 10 | public: 11 | 12 | MinHeap(int c) 13 | { 14 | arr = new int[c]; 15 | size = 0; 16 | capacity = c; 17 | } 18 | 19 | int left(int i)//time comp. O(1) 20 | { 21 | return 2 * i + 1; 22 | } 23 | 24 | int right(int i)//time comp. O(1) 25 | { 26 | return 2 * i + 2; 27 | } 28 | 29 | int parent(int i)//time comp. O(1) 30 | { 31 | return (i - 1) / 2; 32 | } 33 | }; 34 | 35 | int main() 36 | { 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /17. Heaps/03 Insert/03 Insert.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class MinHeap 5 | { 6 | int *arr; 7 | int size; 8 | int capacity; 9 | 10 | MinHeap(int c) 11 | { 12 | arr = new int[c]; 13 | size = 0; 14 | capacity = c; 15 | } 16 | 17 | int left(int i) 18 | { 19 | return (2 * i + 1); 20 | } 21 | 22 | int right(int i) 23 | { 24 | return (2 * i + 2); 25 | } 26 | 27 | int parent(int i) 28 | { 29 | return (i - 1) / 2; 30 | } 31 | 32 | void insert(int x)//time comp. O(logn) 33 | { 34 | if (size == capacity) 35 | { 36 | return ; 37 | } 38 | 39 | size++; 40 | arr[size - 1] = x; 41 | 42 | int i = size - 1; 43 | while (i > 0 and arr[parent(i)] > arr[i]) 44 | { 45 | swap(arr[parent(i)], arr[i]); 46 | i = parent(i); 47 | } 48 | } 49 | }; 50 | 51 | int main() 52 | { 53 | 54 | return 0; 55 | } -------------------------------------------------------------------------------- /17. Heaps/10 Priority Queue STL/10a Priority Queue STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | //declaration (max heap) 7 | priority_queue pq; 8 | 9 | //insert O(logn) 10 | pq.push(10); 11 | pq.push(15); 12 | pq.push(5); 13 | 14 | //top (maximum element will be at top) O(1) 15 | cout << pq.top() << endl; 16 | 17 | //size O(1) 18 | cout << pq.size() << endl; 19 | 20 | //iterating 21 | while (!pq.empty())//empty : O(1) 22 | { 23 | cout << pq.top() << " "; 24 | pq.pop();//O(logn) 25 | } 26 | cout << endl; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /17. Heaps/10 Priority Queue STL/10b Priority Queue STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | //declaration (min heap) 7 | priority_queue, greater> pq; 8 | 9 | //insert 10 | pq.push(10); 11 | pq.push(15); 12 | pq.push(5); 13 | 14 | //top (smallest element will be at top) 15 | cout << pq.top() << endl; 16 | 17 | //size 18 | cout << pq.size() << endl; 19 | 20 | //iterating 21 | while (!pq.empty()) 22 | { 23 | cout << pq.top() << " "; 24 | pq.pop(); 25 | } 26 | cout << endl; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /17. Heaps/10 Priority Queue STL/10c Priority Queue STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a[] = {3, 2, 4}; 7 | int n = sizeof(a) / sizeof(int); 8 | 9 | //make priority queue using an existing array/vector 10 | priority_queue pq(a, a + n);//O(n) (iterating and pushing will take more time) 11 | 12 | //iterating 13 | while (!pq.empty()) 14 | { 15 | cout << pq.top() << " "; 16 | pq.pop(); 17 | } 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /17. Heaps/10 Priority Queue STL/10d Priority Queue STL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Priority Queue on Objects 5 | struct Person 6 | { 7 | int age; 8 | float height; 9 | 10 | Person(int a, int h) 11 | { 12 | age = a; 13 | height = h; 14 | } 15 | }; 16 | 17 | struct compare 18 | { 19 | bool operator() (Person const &p1, Person const &p2) 20 | { 21 | p1.ht < p2.ht; 22 | } 23 | }; 24 | 25 | int main() 26 | { 27 | //min priority queue 28 | priority_queue, compare> pq; 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /17. Heaps/11 Sort K Sorted Array/11a Sort K-Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Given an array of n elements, where each element is 6 | at most k (index -> [i-k,i+k]) away from its target position. 7 | Sort the array in minimum time complexity 8 | */ 9 | 10 | void kSorted(int a[], int n, int k)//time comp. O(n*logn) ; space comp. O(1) 11 | { 12 | sort(a, a + n); 13 | } 14 | 15 | void display(int a[], int n) 16 | { 17 | for (int i = 0; i < n; i++) 18 | { 19 | cout << a[i] << " "; 20 | } 21 | cout << endl; 22 | } 23 | 24 | int main() 25 | { 26 | int a[] = {10, 9, 8, 7, 4, 70, 50, 60}; 27 | int n = sizeof(a) / sizeof(int); 28 | int k = 4; 29 | 30 | kSorted(a, n, k); 31 | display(a, n); 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /17. Heaps/12 Purchasing Maximum Items/12a Purchasing Maximum Items.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int maxItems(int a[], int n, int sum)//time comp. O(n*logn) ; space comp. O(1) 5 | { 6 | int cnt = 0; 7 | 8 | sort(a, a + n); 9 | 10 | for (int i = 0; i < n; i++) 11 | { 12 | if (sum >= a[i]) 13 | { 14 | sum = sum - a[i]; 15 | cnt++; 16 | } 17 | else 18 | { 19 | break; 20 | } 21 | } 22 | 23 | return cnt; 24 | } 25 | 26 | int main() 27 | { 28 | int a[] = {1, 12, 5, 111, 200}; 29 | int n = sizeof(a) / sizeof(int); 30 | int sum = 10; 31 | 32 | cout << maxItems(a, n, sum); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /17. Heaps/12 Purchasing Maximum Items/12b Purchasing Maximum Items.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Efficient Solution (better soln. than previous one in most of the cases) 5 | int maxItems(int a[], int n, int sum)//time comp. O(n*logn) ; space comp. O(n) 6 | { 7 | int cnt = 0; 8 | 9 | priority_queue, greater> pq(a, a + n); 10 | 11 | for (int i = 0; i < n; i++) 12 | { 13 | if (sum >= pq.top()) 14 | { 15 | sum = sum - pq.top(); 16 | pq.pop(); 17 | cnt++; 18 | } 19 | else 20 | { 21 | break; 22 | } 23 | } 24 | 25 | return cnt; 26 | } 27 | 28 | int main() 29 | { 30 | int a[] = {1, 12, 5, 111, 200}; 31 | int n = sizeof(a) / sizeof(int); 32 | int sum = 10; 33 | 34 | cout << maxItems(a, n, sum); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /17. Heaps/13 K Largest Element/13a K-Largest Element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void kLargest(int a[], int n, int k)//time comp. O(n*logn) ; space comp. O(1) 5 | { 6 | sort(a, a + n); 7 | 8 | int i = n - 1; 9 | while (k--) 10 | { 11 | cout << a[i] << " "; 12 | i--; 13 | } 14 | } 15 | 16 | int main() 17 | { 18 | int a[] = {5, 15, 10, 20, 8}; 19 | int n = sizeof(a) / sizeof(int); 20 | int k = 3; 21 | 22 | kLargest(a, n, k); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /17. Heaps/13 K Largest Element/13b K-Largest Element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void kLargest(int a[], int n, int k)//time comp. O((n-k)*logk) ; space comp. O(k) 5 | { 6 | priority_queue, greater> pq; 7 | 8 | for (int i = 0; i < k; i++) 9 | { 10 | pq.push(a[i]); 11 | } 12 | 13 | for (int i = k; i < n; i++) 14 | { 15 | if (pq.top() < a[i]) 16 | { 17 | pq.pop(); 18 | pq.push(a[i]); 19 | } 20 | } 21 | 22 | while (!pq.empty()) 23 | { 24 | cout << pq.top() << " "; 25 | pq.pop(); 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | int a[] = {5, 15, 10, 20, 8}; 32 | int n = sizeof(a) / sizeof(int); 33 | int k = 3; 34 | 35 | kLargest(a, n, k); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /17. Heaps/14 K Closest Elements/14a K-Closest Elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool compare(pair p1, pair p2) 5 | { 6 | return p1.second < p2.second; 7 | } 8 | 9 | void kClosest(int a[], int n, int x, int k)//time comp. O(n*logn) ; space comp. O(n) 10 | { 11 | vector> v; 12 | 13 | for (int i = 0; i < n; i++) 14 | { 15 | v.push_back({a[i], abs(a[i] - x)}); 16 | } 17 | 18 | sort(v.begin(), v.end(), compare); 19 | 20 | for (int i = 0; i < k; i++) 21 | { 22 | cout << v[i].first << " "; 23 | } 24 | } 25 | 26 | int main() 27 | { 28 | int a[] = {10, 30, 5, 40, 38, 80, 70}; 29 | int n = sizeof(a) / sizeof(int); 30 | int x = 35; 31 | int k = 3; 32 | 33 | kClosest(a, n, x, k); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /17. Heaps/14 K Closest Elements/14b K-Closest Elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void kClosest(int a[], int n, int x, int k)//time comp. O(n*logk) ; space comp. O(k) 5 | { 6 | priority_queue> pq; 7 | 8 | for (int i = 0; i < k; i++) 9 | { 10 | pq.push({abs(a[i] - x), a[i]}); 11 | } 12 | 13 | for (int i = k; i < n; i++) 14 | { 15 | int diff = abs(a[i] - x); 16 | if (pq.top().first > diff) 17 | { 18 | pq.pop(); 19 | pq.push({diff, a[i]}); 20 | } 21 | } 22 | 23 | while (!pq.empty()) 24 | { 25 | cout << pq.top().second << " "; 26 | pq.pop(); 27 | } 28 | } 29 | 30 | int main() 31 | { 32 | int a[] = {10, 30, 5, 40, 38, 80, 70}; 33 | int n = sizeof(a) / sizeof(int); 34 | int x = 35; 35 | int k = 3; 36 | 37 | kClosest(a, n, x, k); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /17. Heaps/15 Merge K Sorted Arrays/15a Merge K Sorted Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector mergeKSorted(vector> v)//time comp. O(n*k*log(nk)) ; space comp. O(n*k) 5 | { 6 | vector res; 7 | 8 | for (int i = 0; i < v.size(); i++) 9 | { 10 | for (int j = 0; j < v[i].size(); j++) 11 | { 12 | res.push_back(v[i][j]); 13 | } 14 | } 15 | 16 | sort(res.begin(), res.end()); 17 | return res; 18 | } 19 | 20 | int main() 21 | { 22 | vector> v; 23 | 24 | v.push_back({10, 20, 30}); 25 | v.push_back({5, 15}); 26 | v.push_back({1, 9, 11, 18}); 27 | 28 | vector res = mergeKSorted(v); 29 | 30 | for (auto x : res) 31 | { 32 | cout << x << " "; 33 | } 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /17. Heaps/15 Merge K Sorted Arrays/15b Merge K Sorted Arrays.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devparthgarg/Data-Structures-and-Algorithms/8064e301ad4ebf8dc4c6a1c3eb83e1c48db8c544/17. Heaps/15 Merge K Sorted Arrays/15b Merge K Sorted Arrays.cpp -------------------------------------------------------------------------------- /17. Heaps/16 Median in Stream/16a Median in Stream.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | vector nums 7 | } -------------------------------------------------------------------------------- /17. Heaps/16 Median in Stream/16b Median in Stream.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devparthgarg/Data-Structures-and-Algorithms/8064e301ad4ebf8dc4c6a1c3eb83e1c48db8c544/17. Heaps/16 Median in Stream/16b Median in Stream.cpp -------------------------------------------------------------------------------- /18. Graphs/01 Graphs Introduction/01 Graphs Introduction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | V : vertices, E : edges 9 | 10 | 1.Directed Graph : 11 | 12 | sum of indegrees = E 13 | sum of outdegrees = E 14 | maximum number of edges = V * ( V - 1 ) 15 | 16 | 2.Undirected Graph : 17 | 18 | sum of degrees = 2*E 19 | maximum number of edges = ( V * ( V - 1 ) ) / 2 20 | 21 | 3.Some common graph terms : 22 | 23 | a. walk : A walk is a sequence of vertices and edges of a graph i.e. 24 | if we traverse a graph then we get a walk. 25 | Vertex can be repeated 26 | Edges can’t be repeated 27 | 28 | b. path : A special walk in which Vertex and Edges can't be repeated. 29 | 30 | c. cyclic : A special walk which begins and ends at same vertex. 31 | 32 | d. acyclic : A graph which doesn't contain a cycle. 33 | 34 | */ 35 | } -------------------------------------------------------------------------------- /18. Graphs/02 Graphs Representation/02a Graphs Representation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Adjacency Matrix : 9 | 10 | A B C 11 | ----------------- 12 | A 0 1 1 13 | 14 | B 1 0 1 15 | 16 | C 1 1 0 17 | 18 | 1. size of matrix : (V * V) 19 | 20 | 2. for undirected graph : matrix is symmetric 21 | 22 | 3. 1 represents there is edge 23 | 0 represents there is no edge 24 | 25 | 4. handle arbitrary vertices names with hash table eg: m["ABC"] = 2 26 | 27 | 5. Properties : 28 | 29 | a. space required O(V*V) 30 | 31 | b. check u and v adjacent O(V) 32 | 33 | c. find all vertices adjacent to u O(V) 34 | 35 | d. find degree of u O(V) 36 | 37 | e. add/remove edge O(1) 38 | 39 | f. add/remove vertex O(V*V) 40 | 41 | */ 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /18. Graphs/02 Graphs Representation/02b Graphs Representation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | Adjacency List : 9 | 10 | 11 | 0 - 1-->2 12 | 1 - 0-->2 13 | 2 - 0-->1-->3 14 | 3 - 2 15 | 16 | Properties : 17 | 18 | a. space required O(V+E) 19 | 20 | 1. undirected O(V + 2 * E) 21 | 2. directed O(V + E) 22 | 23 | b. check u and v adjacent O(V) 24 | 25 | c. find all vertices adjacent to u O(degree(u)) 26 | 27 | d. find degree of u O(1) 28 | 29 | e. add edge O(1) 30 | 31 | f. remove edge O(V) 32 | 33 | */ 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /18. Graphs/03 Graphs Implementation/03b Graphs Implementation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Adjacency List 5 | void addEdge(vector adj[], int u, int v) 6 | { 7 | adj[u].push_back(v); 8 | adj[v].push_back(u); 9 | } 10 | 11 | void print(vector adj[], int v) 12 | { 13 | for (int i = 0; i < v; i++) 14 | { 15 | cout << i << " : "; 16 | for (auto x : adj[i]) 17 | { 18 | cout << x << " "; 19 | } 20 | cout << endl; 21 | } 22 | } 23 | 24 | int main() 25 | { 26 | int v = 4; 27 | vector adj[v]; 28 | 29 | addEdge(adj, 0, 1); 30 | addEdge(adj, 0, 2); 31 | addEdge(adj, 1, 2); 32 | addEdge(adj, 1, 3); 33 | 34 | print(adj, v); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /19. Greedy Algorithms/01 Minimum Coins/01 Minimum Coins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //time: O(n*logn) 5 | //space: O(1) 6 | int minCoins(int coins[],int n,int amount) 7 | { 8 | sort(coins,coins+n); 9 | 10 | int res=0; 11 | for(int i=n-1;i>=0;i--) 12 | { 13 | if(coins[i]<=amount) 14 | { 15 | int number_of_coins=floor(amount/coins[i]); 16 | res+=number_of_coins; 17 | amount=amount-number_of_coins*coins[i]; 18 | } 19 | } 20 | 21 | return res; 22 | } 23 | 24 | int main() 25 | { 26 | int coins[]={5,10,2,1}; 27 | int n=4; 28 | int amount=57; 29 | 30 | cout< 2 | using namespace std; 3 | 4 | //time: O(n*logn) 5 | //space: O(1) 6 | int activitySelection(vector> intervals,int n) 7 | { 8 | sort(intervals.begin(),intervals.end()); 9 | 10 | int res=1; 11 | int endTime=intervals[0][1]; 12 | for(int i=1;i=endTime) 15 | { 16 | res++; 17 | endTime=intervals[i][1]; 18 | } 19 | } 20 | 21 | return res; 22 | } 23 | 24 | int main() 25 | { 26 | vector> intervals={{3,8},{2,4},{1,3},{10,11}}; 27 | int n=4; 28 | 29 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | 6 | return 0; 7 | } -------------------------------------------------------------------------------- /19. Greedy Algorithms/04 Job Sequencing/04 Job Sequencing.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devparthgarg/Data-Structures-and-Algorithms/8064e301ad4ebf8dc4c6a1c3eb83e1c48db8c544/19. Greedy Algorithms/04 Job Sequencing/04 Job Sequencing.cpp -------------------------------------------------------------------------------- /19. Greedy Algorithms/05 Huffman Coding/05 Huffman Coding.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devparthgarg/Data-Structures-and-Algorithms/8064e301ad4ebf8dc4c6a1c3eb83e1c48db8c544/19. Greedy Algorithms/05 Huffman Coding/05 Huffman Coding.cpp -------------------------------------------------------------------------------- /20. Backtracking/01 Print Permutations of Strings with Given Condition/01a Print Permutations of Strings with Given Condition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | print all permutations of strings 6 | which do not contain the substring "AB" 7 | */ 8 | 9 | void permutations(string &s,int l,int r) 10 | { 11 | if(l==r) 12 | { 13 | if(s.find("AB")==string::npos) 14 | { 15 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | /* 6 | Dynamic Programming : 7 | 8 | 1.optimization over recursion 9 | 2.reuse the solutions of overlapping sub-problem 10 | 11 | Types: 12 | 13 | 1.Memoization (top down approach) 14 | 2.Tabulation (bottom up approach) 15 | */ 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /21. Dynamic Programming/02 Nth Fibonacci/02a Nth Fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Recursion 5 | int fib(int n)//time comp. O(2^n) 6 | { 7 | if (n<=1) 8 | { 9 | return n; 10 | } 11 | 12 | return fib(n - 1) + fib(n - 2); 13 | } 14 | 15 | int main() 16 | { 17 | int n = 5; 18 | 19 | cout << fib(n) << endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /21. Dynamic Programming/02 Nth Fibonacci/02b Nth Fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int dp[100000]; 5 | 6 | //Memoization 7 | int fib(int n)//time comp. O(n) ; space comp. O(n) 8 | { 9 | if (dp[n] == -1) 10 | { 11 | if (n <= 1) 12 | { 13 | return dp[n] = n; 14 | } 15 | 16 | return dp[n] = fib(n - 1) + fib(n - 2); 17 | } 18 | 19 | return dp[n]; 20 | } 21 | 22 | int main() 23 | { 24 | int n = 5; 25 | 26 | memset(dp, -1, sizeof(dp)); //fill memo array with -1 27 | 28 | cout << fib(n) << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /21. Dynamic Programming/02 Nth Fibonacci/02c Nth Fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Tabulation 5 | int fib(int n)//time comp. O(n) ; space comp. O(n) 6 | { 7 | int dp[n + 1]; 8 | memset(dp, -1, sizeof(dp)); 9 | 10 | dp[0] = 0; 11 | dp[1] = 1; 12 | 13 | for (int i = 2; i <= n; i++) 14 | { 15 | dp[i] = dp[i - 1] + dp[i - 2]; 16 | } 17 | 18 | return dp[n]; 19 | } 20 | 21 | int main() 22 | { 23 | int n = 5; 24 | 25 | cout << fib(n) << endl; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /21. Dynamic Programming/03 Longest Common Subsequence/03b Longest Common Subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int dp[10000][10000];//2D array because 2 parameters are changing m and n 5 | 6 | //Memoization 7 | int lcs(string s1, string s2, int m, int n) //time comp. O(m*n) 8 | { 9 | if (dp[m][n] == -1) 10 | { 11 | if (m == 0 or n == 0) 12 | { 13 | dp[m][n] = 0; 14 | } 15 | else 16 | { 17 | if (s1[m - 1] == s2[n - 1]) 18 | { 19 | dp[m][n] = 1 + lcs(s1, s2, m - 1, n - 1); 20 | } 21 | else 22 | { 23 | dp[m][n] = max(lcs(s1, s2, m - 1, n), lcs(s1, s2, m, n - 1)); 24 | } 25 | } 26 | } 27 | 28 | return dp[m][n]; 29 | } 30 | 31 | int main() 32 | { 33 | memset(dp, -1, sizeof(dp)); 34 | 35 | string s1 = "AXYZ"; 36 | string s2 = "BAZ"; 37 | 38 | int m = s1.length(); 39 | int n = s2.length(); 40 | 41 | cout << lcs(s1, s2, m, n) << endl; 42 | return 0; 43 | } -------------------------------------------------------------------------------- /21. Dynamic Programming/03 Longest Common Subsequence/03c Longest Common Subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Tabulation 5 | int lcs(string s1, string s2, int m, int n) //time comp. O(m*n) 6 | { 7 | int dp[m + 1][n + 1]; //2D array because 2 parameters are changing m and n 8 | memset(dp, 0, sizeof(dp)); 9 | 10 | for (int i = 1; i <= m; i++) 11 | { 12 | for (int j = 1; j <= n; j++) 13 | { 14 | if (s1[i - 1] == s2[j - 1]) 15 | { 16 | dp[i][j] = 1 + dp[i - 1][j - 1]; 17 | } 18 | else 19 | { 20 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 21 | } 22 | } 23 | } 24 | 25 | return dp[m][n]; 26 | } 27 | 28 | int main() 29 | { 30 | string s1 = "AXYZ"; 31 | string s2 = "BAZ"; 32 | 33 | int m = s1.length(); 34 | int n = s2.length(); 35 | 36 | cout << lcs(s1, s2, m, n) << endl; 37 | return 0; 38 | } -------------------------------------------------------------------------------- /21. Dynamic Programming/04 Coin Change/04a Coin Change.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Recursion 5 | int coinChange(int coins[], int n, int sum)//time comp. O(2^n) 6 | { 7 | if (sum == 0) 8 | { 9 | return 1; 10 | } 11 | 12 | if (n == 0) 13 | { 14 | return 0; 15 | } 16 | 17 | if (coins[n - 1] > sum) 18 | { 19 | return coinChange(coins, n - 1, sum); 20 | } 21 | 22 | return coinChange(coins, n, sum - coins[n - 1]) + coinChange(coins, n - 1, sum); 23 | } 24 | 25 | int main() 26 | { 27 | int coins[] = {2, 5, 3, 6}; 28 | int n = sizeof(coins) / sizeof(int); 29 | int sum = 10; 30 | 31 | cout << coinChange(coins, n, sum) << endl; 32 | return 0; 33 | } -------------------------------------------------------------------------------- /21. Dynamic Programming/04 Coin Change/04b Coin Change.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int dp[1000][1000]; 5 | 6 | //Memoization 7 | int coinChange(int coins[], int n, int sum)//time comp. O(n*m) 8 | { 9 | if (dp[n][sum] == -1) 10 | { 11 | if (sum == 0) 12 | { 13 | return dp[n][sum] = 1; 14 | } 15 | 16 | if (n == 0) 17 | { 18 | return dp[n][sum] = 0; 19 | } 20 | 21 | if (coins[n - 1] > sum) 22 | { 23 | return dp[n][sum] = coinChange(coins, n - 1, sum); 24 | } 25 | 26 | return dp[n][sum] = coinChange(coins, n, sum - coins[n - 1]) + coinChange(coins, n - 1, sum); 27 | } 28 | 29 | return dp[n][sum]; 30 | } 31 | 32 | int main() 33 | { 34 | int coins[] = {2, 5, 3, 6}; 35 | int n = sizeof(coins) / sizeof(int); 36 | int sum = 10; 37 | 38 | memset(dp, -1, sizeof(dp)); 39 | 40 | cout << coinChange(coins, n, sum) << endl; 41 | return 0; 42 | } -------------------------------------------------------------------------------- /21. Dynamic Programming/06 Maximum Sum with no 2 Consecutive/06a Maximum Sum with no 2 Consecutive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Recursion 5 | int maxSum(vector& nums, int n)//time comp. O(2^n) 6 | { 7 | if (n == 1) 8 | { 9 | return nums[0]; 10 | } 11 | else if (n == 2) 12 | { 13 | return max(nums[0], nums[1]); 14 | } 15 | else 16 | { 17 | return max(maxSum(nums, n - 1), maxSum(nums, n - 2) + nums[n - 1]); 18 | } 19 | } 20 | 21 | int main() 22 | { 23 | vector nums{10, 5, 15, 20, 2, 30}; 24 | int n = nums.size(); 25 | 26 | cout << maxSum(nums, n) << endl; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /21. Dynamic Programming/06 Maximum Sum with no 2 Consecutive/06b Maximum Sum with no 2 Consecutive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Memoization 5 | int maxSum(vector& nums, int n, int dp[])//time comp. O(n) 6 | { 7 | if (dp[n] == -1) 8 | { 9 | if (n == 1) 10 | { 11 | return nums[0]; 12 | } 13 | else if (n == 2) 14 | { 15 | return max(nums[0], nums[1]); 16 | } 17 | else 18 | { 19 | return dp[n] = max(maxSum(nums, n - 1, dp), maxSum(nums, n - 2, dp) + nums[n - 1]); 20 | } 21 | } 22 | 23 | return dp[n]; 24 | } 25 | 26 | int main() 27 | { 28 | vector nums{10, 5, 15, 20, 2, 30}; 29 | int n = nums.size(); 30 | 31 | int dp[n + 1]; 32 | memset(dp, -1, sizeof(dp)); 33 | 34 | dp[0] = 0; 35 | dp[1] = nums[0]; 36 | dp[2] = max(nums[0], nums[1]); 37 | 38 | cout << maxSum(nums, n, dp) << endl; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /21. Dynamic Programming/06 Maximum Sum with no 2 Consecutive/06c Maximum Sum with no 2 Consecutive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Tabulation 5 | int maxSum(vector& nums, int n, int dp[])//time comp. O(n) 6 | { 7 | int dp[n + 1]; 8 | memset(dp, 0, sizeof(dp)); 9 | 10 | dp[0] = 0; 11 | dp[1] = nums[0]; 12 | dp[2] = max(nums[0], nums[1]); 13 | 14 | for (int i = 3; i <= n; i++) 15 | { 16 | dp[i] = max(dp[i - 1], dp[i - 2] + nums[i - 1]); 17 | } 18 | 19 | return dp[n]; 20 | } 21 | 22 | int main() 23 | { 24 | vector nums{10, 5, 15, 20, 2, 30}; 25 | int n = nums.size(); 26 | 27 | int dp[n + 1]; 28 | memset(dp, -1, sizeof(dp)); 29 | 30 | dp[0] = 0; 31 | dp[1] = nums[0]; 32 | dp[2] = max(nums[0], nums[1]); 33 | 34 | cout << maxSum(nums, n, dp) << endl; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /22. Trie/01 Introduction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | 8 | --> Efficient for following 9 | operations on words of dictionary: 10 | 11 | 1. search 12 | 2. insert 13 | 3. delete 14 | 4. prefix search 15 | 5. lexiographical ordering of words 16 | 17 | --> Comparsion of Trie and Hashing 18 | 19 | 20 | Function | Trie | Hashing 21 | ----------------------------------------------------------------------------- 22 | search | O(word_len) worst case | O(word_len) average case 23 | 24 | insert | " | " 25 | 26 | delete | " | " 27 | 28 | prefix search | O(word_len+output_len) | Not Supported 29 | 30 | lexiographical | O(output_len) | " 31 | ordering | | 32 | 33 | */ 34 | return 0; 35 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data-Structures-and-Algorithms 2 |
logo
3 | A repository which contains DSA questions from basic to advanced for interview preparation. 4 | 5 | ## Setting up and running the project 6 | Fork the repo and clone it. 7 | ``` 8 | git clone https://github.com/whoparthgarg/Data-Structures-and-Algorithms.git 9 | ``` -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devparthgarg/Data-Structures-and-Algorithms/8064e301ad4ebf8dc4c6a1c3eb83e1c48db8c544/logo.png --------------------------------------------------------------------------------