├── DATA STRUCTURES ├── Binary Tree │ ├── Print All Full Nodes │ │ ├── code_2.cpp │ │ └── readMe.md │ ├── Sum of All Nodes │ │ └── readMe.md │ ├── Check if Two Trees are Mirror │ │ └── readMe.md │ ├── Diagonal Traversal of Binary Tree │ │ └── readMe.md │ ├── Reverse Level Order Traversal │ │ └── readMe.md │ ├── Find Distance Between Two Nodes │ │ └── readMe.md │ ├── Sum of All Left Leaves │ │ └── readMe.md │ ├── Sum of All Leaf Nodes │ │ └── readMe.md │ ├── Sum of All Non-Leaf Node │ │ └── readMe.md │ ├── Calculate Size of Tree │ │ └── readMe.md │ ├── Check If Two Trees are Identical or Not │ │ └── readMe.md │ ├── Sum Of All Right Leaves │ │ └── readMe.md │ ├── Check if Tree is Symmetry or Not │ │ └── readMe.md │ ├── Flip Binary Tree │ │ └── readMe.md │ ├── Sum of Heights of All Individual Nodes │ │ └── readMe.md │ ├── Kth Ancestor of A Node │ │ └── readMe.md │ ├── Convert Binary Tree to Doubly Linked List │ │ └── readMe.md │ ├── Print Ancestors of A Given Node │ │ └── readMe.md │ ├── Calculate Depth of A Full Binary Tree From Preorder │ │ ├── readMe.md │ │ └── code_1.cpp │ ├── Find All Root to Leaf Paths │ │ └── readMe.md │ ├── Print Level Traversal in Sorted Order │ │ └── readMe.md │ ├── Vertical Order Of Binary Tree │ │ └── readMe.md │ ├── Find The Deepest Node │ │ └── readMe.md │ ├── Find The Maximum Sum In A Path From Leaf To Root │ │ └── readMe.md │ ├── Number of Binary Trees for Given Preorder Sequence Length │ │ ├── readMe.md │ │ └── code_1.cpp │ ├── Sum of Leaf Nodes At Minimum Level │ │ └── readMe.md │ ├── Density of Binary Tree │ │ └── readMe.md │ ├── Maximum Value in A Binary Tree │ │ └── readMe.md │ ├── Minimum Value in A Binary Tree │ │ └── readMe.md │ ├── Check If Binary Tree Has Duplicate Values │ │ └── readMe.md │ ├── Check if Given Binary Tree is Sum Tree Or Not │ │ └── readMe.md │ ├── Nth Node in Inorder Traversal │ │ └── readMe.md │ ├── Print Postorder Traversal From Given Inorder And Preorder Traversals │ │ └── readMe.md │ ├── Sum of All the Parent Node Having Child N │ │ └── readMe.md │ ├── Diameter of Binary Tree │ │ └── readMe.md │ ├── Check If All Leaves are At Same Level │ │ └── readMe.md │ ├── Construct A Tree From Inorder and Level Order │ │ └── readMe.md │ ├── Convert A Tree To Sum Tree │ │ └── readMe.md │ ├── Maximum Spiral Sum │ │ └── readMe.md │ ├── Find Largest Subtree Sum │ │ └── readMe.md │ ├── Vertical Sum In Binary Tree │ │ └── readMe.md │ ├── Diagonal Sum of A Binary Tree │ │ └── readMe.md │ ├── Check Whether A Given Binary Tree is Perfect or Not │ │ └── readMe.md │ ├── Level Order Traversal in Spiral Form │ │ └── readMe.md │ ├── Merge Two Binary Trees │ │ └── readMe.md │ ├── Check Whether A Binary Tree is Complete Or Not │ │ └── readMe.md │ ├── Construct Tree From Given Inorder and Preorder │ │ └── readMe.md │ └── Top View of Binary Tree │ │ └── readMe.md ├── Linked List │ └── Detect Loop in Singly Linked List │ │ └── readMe.md ├── Array │ ├── Find The Largest Pair Sum in An Unsorted Array │ │ └── readMe.md │ ├── Find The Largest Three Elements in An Array │ │ ├── readMe.md │ │ └── code_1.cpp │ ├── Segregate 0s and 1s │ │ └── readMe.md │ ├── Find the Missing Number │ │ ├── readMe.md │ │ ├── code_1.cpp │ │ └── code_2.cpp │ ├── Segregate Even and Odd Numbers │ │ └── readMe.md │ ├── Product of Ranges in An Array │ │ └── readMe.md │ ├── Maximum of All SubArrays of Size k │ │ └── readMe.md │ ├── Kth Smallest Element │ │ ├── readMe.md │ │ ├── code_1.cpp │ │ └── code_6.cpp │ ├── Sort An Array of 0s - 1s and 2s │ │ └── readMe.md │ ├── Convert Array into Zig-Zag Fashion │ │ └── readMe.md │ ├── Replace Every Array Element │ │ └── readMe.md │ ├── Rearrange An Array in A Specific Order │ │ └── readMe.md │ ├── Shortest Un-ordered SubArray │ │ └── readMe.md │ ├── Largest SubArray with Equal Number of 1s and 0s │ │ └── readMe.md │ ├── Find The Smallest Missing Number │ │ └── readMe.md │ ├── Maximum Product Subarray │ │ └── readMe.md │ ├── Merge Overlapping Intervals │ │ └── readMe.md │ ├── Mean of Range in An Array │ │ └── readMe.md │ ├── Total Numbers With No Repeated Digits in A Range │ │ └── readMe.md │ ├── Array Rearrangement by Shifting Zero to end │ │ └── readMe.md │ ├── Count Number of Primes in Given Range │ │ └── readMe.md │ ├── Arrange Given Numbers to Form the Biggest Number │ │ └── readMe.md │ ├── Move All Zeros to end of Array │ │ ├── readMe.md │ │ └── code_1.cpp │ ├── Elements that Occurred Only Once │ │ ├── readMe.md │ │ └── code_2.cpp │ ├── Count Strictly Increasing Subarrays │ │ └── readMe.md │ ├── Check if An Array is Sorted and Rotated │ │ └── readMe.md │ ├── Count Smaller Elements On Right Side │ │ └── readMe.md │ ├── Min-Max Range Queries in Array │ │ └── readMe.md │ ├── Positive Elements at Even and Negative at Odd Position │ │ └── readMe.md │ ├── Search An Element in Sorted and Rotated Array │ │ ├── readMe.md │ │ └── code_1.cpp │ ├── Array Range Queries for Searching An Element │ │ └── readMe.md │ ├── Range LCM Queries │ │ └── readMe.md │ ├── Maximize Array Sum After K Negations │ │ └── readMe.md │ ├── Mean and Median for Unsorted Array │ │ └── readMe.md │ └── Next Greater Element │ │ └── readMe.md ├── Matrix │ ├── Addition of Two Matrices │ │ └── readMe.md │ ├── Multiplication of Two Matrices │ │ └── readMe.md │ ├── Transpose of A Matrix │ │ └── readMe.md │ ├── Print Triangular Matrix │ │ └── readMe.md │ └── Print A Given Matrix in Spiral Form │ │ └── readMe.md └── Graph │ ├── Number of Nodes at Given Level │ └── readMe.md │ ├── Depth First Search │ └── readMe.md │ └── Breadth First Search │ └── readMe.md ├── Maximum size square sub-matrix with all 1s └── readMe.md ├── Dijkstra's Algorithm └── readMe.md ├── Newman-Shanks-Williams prime ├── readMe.md ├── code_1.cpp └── code_2.cpp ├── Fractional Knapsack Problem └── readMe.md ├── Cubic Root of A Number └── readMe.md ├── Compute nCr mod p ├── readMe.md ├── code_1.cpp ├── code_2.cpp └── code_3.cpp ├── Dice Throw Problem └── readMe.md ├── Longest Common Prefix └── readMe.md ├── Maximize the sum of index by muliplying the element at the index ├── readMe.md └── code_1.cpp ├── Sudoku └── readMe.md ├── Median of Two Sorted Arrays of Same Size └── readMe.md ├── Fibonacci numbers ├── readMe.md ├── code_1.cpp ├── code_4.cpp ├── code_2.cpp └── code_3.cpp ├── Power Set in Lexicographic order ├── readMe.md └── code_1.cpp ├── Maximum Subset Sum With No Repeating Digits └── readMe.md ├── Largest Sum Contiguous Subarray ├── readMe.md ├── code_3.cpp ├── code_2.cpp └── code_1.cpp ├── Longest Repeated Subsequence └── readMe.md ├── Maximum Subarray Sum └── readMe.md ├── Longest Arithmetic Progression └── readMe.md ├── Longest Geometric Progression └── readMe.md ├── Count Distinct Subsequences ├── readMe.md └── code_1.cpp ├── Maximum Size Subset with Given Sum └── readMe.md ├── Smallest Sum Contiguous Subarray └── readMe.md ├── Longest Common Increasing Subsequence └── readMe.md ├── Binary Search └── readMe.md ├── Count Balanced Binary Trees of Height h ├── readMe.md ├── code_1.cpp ├── code_2.cpp └── code_3.cpp ├── Find the Element that appears once in a Sorted Array ├── readMe.md └── code_2.cpp ├── Subset Sum Problem ├── readMe.md └── code_1.cpp ├── Permutation Coefficient ├── readMe.md ├── code_3.cpp ├── code_4.cpp ├── code_2.cpp ├── code_5.cpp └── code_1.cpp ├── Longest Palindromic Subsequence ├── readMe.md └── code_1.cpp ├── Minimum Number of Jumps to Reach ( M , N ) From ( 1 , 1 ) └── readMe.md ├── Delannoy Number ├── readMe.md └── code_1.cpp ├── Number of Zeros └── readMe.md ├── N Queen Problem └── readMe.md ├── Perfect Sum Problem └── readMe.md ├── Split A Number into Maximum Composite Number ├── readMe.md └── code_1.cpp ├── Sum of All Substrings of A String Representing A Number ├── readMe.md ├── code_1.cpp └── code_2.cpp ├── Binomial Coefficient ├── readMe.md ├── code_1.cpp ├── code_3.cpp └── code_2.cpp ├── Entringer Number ├── readMe.md ├── code_1.cpp └── code_2.cpp ├── Count Inversions ├── readMe.md └── code_1.cpp ├── Count all subsequences having product less than K └── readMe.md ├── Find Closest Number in Array └── readMe.md ├── .gitignore ├── Minimum Number of Jumps to Reach End └── readMe.md ├── Minimum Insertions to Form a Palindrome ├── readMe.md └── code_2.cpp ├── Maximum Sum Increasing Subsequence └── readMe.md ├── Longest Increasing Subsequence ├── readMe.md └── code_2.cpp ├── Maximum path sum in a triangle ├── readMe.md └── code_1.cpp ├── Count Number of Ways to Reach A Given Score in A Game ├── readMe.md └── code_1.cpp ├── Newman-Conway Sequence ├── readMe.md ├── code_2.cpp └── code_1.cpp ├── Number of N Digit(s) Stepping Numbers └── readMe.md ├── Count of Different Ways to Express N As The Sum of 1, 3 And 4 ├── readMe.md ├── code_1.cpp └── code_2.cpp ├── Cutting a Rod ├── readMe.md ├── code_1.cpp └── code_2.cpp ├── Maximum length subsequence └── readMe.md ├── Maximum Length Chain of Pairs └── readMe.md ├── Minimum Insertions to Sort An Array └── readMe.md ├── Longest subsequence └── readMe.md ├── Size of The Subarray With Maximum Sum └── readMe.md ├── Longest Common Substring ├── readMe.md └── code_1.cpp ├── Moser-de Bruijn Sequence ├── readMe.md ├── code_2.cpp └── code_1.cpp ├── Bell Numbers ├── readMe.md └── code_1.cpp ├── The Celebrity Problem └── readMe.md ├── Ugly Numbers ├── readMe.md └── code_1.cpp ├── Rotation Count in Rotated Sorted Array ├── readMe.md └── code_1.cpp ├── Matrix Chain Multiplication └── readMe.md ├── Count Ways to Reach the Nth Stair Using Step 1, 2 or 3 ├── readMe.md ├── code_2.cpp ├── code_1.cpp └── code_3.cpp ├── Maximum Product Subset of an Array └── readMe.md ├── Minimum number of Coins ├── readMe.md └── code_1.cpp ├── Friends Pairing Problem ├── readMe.md ├── code_2.cpp ├── code_1.cpp └── SolutionByAastha.Java ├── Nth Catalan Number ├── readMe.md ├── code_1.cpp ├── code_2.cpp └── code_3.cpp ├── Find Maximum Possible Stolen Value From Houses └── readMe.md ├── Palindrome Partitioning └── readMe.md ├── Remove Invalid Parentheses └── readMe.md ├── Minimum Product Subset of an Array └── readMe.md ├── Floor in a Sorted Array └── readMe.md ├── Longest Common Subsequence ├── For Three Strings │ └── readMe.md ├── code_1.cpp └── code_2.cpp ├── Find a Fixed Point in a Given Array ├── readMe.md └── code_1.cpp ├── Minimum Sum of Absolute Difference of Pairs of Two Arrays ├── readMe.md └── code_1.cpp ├── 0-1 Knapsack Problem └── readMe.md ├── Minimum Partition └── readMe.md ├── Sum of Average of All Subsets └── readMe.md ├── Wildcard Pattern Matching ├── readMe.md └── For Three Symbols │ └── readMe.md ├── Maximum product of an increasing subsequence └── readMe.md ├── Majority Element └── readMe.md ├── Coin Change ├── readMe.md ├── code_1.cpp └── code_3.cpp ├── Maximum Product Cutting ├── readMe.md ├── code_3.cpp ├── code_1.cpp └── code_2.cpp ├── Rat in A Maze └── readMe.md ├── Super Ugly Number └── readMe.md ├── Maximum subsequence sum └── readMe.md ├── Maximum games played by winner ├── code_1.cpp └── readMe.md ├── Stolen Values Problem ├── readMe.md └── code_1.cpp ├── K-th Element of Two Sorted Arrays └── readMe.md ├── Golomb Sequence ├── code_2.cpp ├── readMe.md └── code_1.cpp ├── Find Bitonic Point in Given Bitonic Sequence └── readMe.md ├── Eulerian Number ├── readMe.md ├── code_1.cpp └── code_2.cpp ├── Count Ways to Build Street Under Given Constraints ├── readMe.md └── code_1.cpp ├── Boggle └── readMe.md ├── Tiling with Dominoes ├── readMe.md └── code_1.cpp ├── Maximum sum of pairs with specific difference └── readMe.md ├── Weighted Job Scheduling └── readMe.md ├── Combinational Sum └── readMe.md ├── Tiling Problem ├── code_1.cpp └── readMe.md ├── Graph Coloring Problem └── readMe.md ├── Count Number of Increasing SubSequence └── readMe.md ├── The Knight’s Tour Problem └── readMe.md ├── Minimum Sum of Multiplications of N Numbers └── readMe.md ├── Find A Peak Element ├── readMe.md └── code_1.cpp ├── Longest Bitonic Subsequence └── readMe.md ├── Painting Fence Algorithm ├── code_2.cpp └── code_1.cpp ├── Unbounded Knapsack └── readMe.md ├── Edit Distance └── readMe.md ├── Rencontres Number ├── readMe.md └── code_1.cpp ├── Tile Stacking Problem └── readMe.md ├── Hamiltonian Cycle └── readMe.md ├── Longest Substring Without Repeating Characters └── README.md └── Jacobsthal and Jacobsthal-Lucas numbers └── code_1.cpp /DATA STRUCTURES/Binary Tree/Print All Full Nodes/code_2.cpp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Sum of All Nodes/readMe.md: -------------------------------------------------------------------------------- 1 | ## Find Sum of All Nodes in Given Binary tree. 2 | -------------------------------------------------------------------------------- /Maximum size square sub-matrix with all 1s/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a binary matrix, find out the maximum size square sub-matrix with all 1s. 2 | -------------------------------------------------------------------------------- /Dijkstra's Algorithm/readMe.md: -------------------------------------------------------------------------------- 1 | ### Given a graph and a source vertex in the graph, find shortest paths from source to all vertices in the given graph. 2 | -------------------------------------------------------------------------------- /Newman-Shanks-Williams prime/readMe.md: -------------------------------------------------------------------------------- 1 | ## Recurrence relation for Newman–Shanks–Williams prime is: 2 | 3 | ### S(0) = 1 4 | ### S(1) = 1 5 | ### S(n) = 2*S(n-1) + S(n-2) 6 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Check if Two Trees are Mirror/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given two Binary Trees, write a function that returns true if two trees are mirror of each other, else false. 2 | -------------------------------------------------------------------------------- /Fractional Knapsack Problem/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given weights and values of n items, we need to put these items in a knapsack of capacity W to get the maximum total value in the knapsack. 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Linked List/Detect Loop in Singly Linked List/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a linked list, check if the the linked list has loop or not. Below diagram shows a linked list with a loop. 2 | -------------------------------------------------------------------------------- /Cubic Root of A Number/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a number n, find the cube root of n. 2 | Input : n = 3 3 | Output : Cubic Root -> 1.442250 4 | 5 | Input : n = 8 6 | Output : Cubic Root -> 2.000000 7 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Diagonal Traversal of Binary Tree/readMe.md: -------------------------------------------------------------------------------- 1 | ## Consider lines of slope -1 passing between nodes. Given a Binary Tree, print all diagonal elements in a binary tree belonging to same line. 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Find The Largest Pair Sum in An Unsorted Array/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an unsorted of distinct integers, find the largest pair sum in it. For example, the largest pair sum in {12, 34, 10, 6, 40} is 74. 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Reverse Level Order Traversal/readMe.md: -------------------------------------------------------------------------------- 1 | ## The idea is to print last level first, then second last level, and so on. Like Level order traversal, every level is printed from left to right. 2 | -------------------------------------------------------------------------------- /Compute nCr mod p/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given three numbers n, r and p, compute value of nCr mod p. 2 | 3 | ### Example: 4 | 5 | ### Input: n = 10, r = 2, p = 13 6 | ### Output: 6 7 | ### Explanation: 10C2 is 45 and 45 % 13 is 6. 8 | -------------------------------------------------------------------------------- /Dice Throw Problem/readMe.md: -------------------------------------------------------------------------------- 1 | ## There are n identical dices. Each dice has given number of faces. In how many ways, you can get the given sum. 2 | 3 | ``` 4 | n=2 5 | sum=4 6 | faces=3 7 | No. of ways=3 (1+3,2+2,3+1) 8 | ``` 9 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Matrix/Addition of Two Matrices/readMe.md: -------------------------------------------------------------------------------- 1 | ## Two Marices are given and you need to add them. 2 | 3 | ## Input 4 | 5 | - Two Matrices 6 | - their dimensions 7 | 8 | ## Output 9 | 10 | - Resultant Matrix 11 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Matrix/Multiplication of Two Matrices/readMe.md: -------------------------------------------------------------------------------- 1 | ## Two Matrices are given and you need to multiply them. 2 | 3 | ## Input 4 | 5 | - Two Matrices 6 | - their dimensions 7 | 8 | ## Output 9 | 10 | - Resultant Matrix 11 | -------------------------------------------------------------------------------- /Longest Common Prefix/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a set of strings, find the longest common prefix. 2 | 3 | > Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”} 4 | > Output : "gee" 5 | 6 | > Input : {"apple", "ape", "april"} 7 | > Output : "ap" 8 | -------------------------------------------------------------------------------- /Maximize the sum of index by muliplying the element at the index/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of N integers. You are allowed to rearrange the element of the array. The task is to find the maximum value of Σarr[i]*i, where i = 0, 1, 2,…., n – 1. 2 | -------------------------------------------------------------------------------- /Sudoku/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9. 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Find Distance Between Two Nodes/readMe.md: -------------------------------------------------------------------------------- 1 | ## Find the distance between two keys in a binary tree, no parent pointers are given. Distance between two nodes is the minimum number of edges to be traversed to reach one node from other. 2 | -------------------------------------------------------------------------------- /Median of Two Sorted Arrays of Same Size/readMe.md: -------------------------------------------------------------------------------- 1 | ## There are 2 sorted arrays A and B of size n each. Write an algorithm to find the median of the array obtained after merging the above 2 arrays(i.e. array of length 2n). The complexity should be O(log(n)). 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Sum of All Left Leaves/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a Binary Tree, find sum of all left leaves in it. 2 | 3 | ``` 4 | sum of all left leaves in below Binary Tree is 5 + 2 = 7. 5 | 6 | 1 7 | / \ 8 | 2 2 9 | / \ / 10 | 5 4 2 11 | ``` 12 | -------------------------------------------------------------------------------- /Fibonacci numbers/readMe.md: -------------------------------------------------------------------------------- 1 | ### The Fibonacci numbers are the numbers in the following integer sequence. 2 | #### 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….. 3 | ## Fn = Fn-1 + Fn-2 4 | ## F0 = 0 and F1 = 1 5 | ### PS: It can have multiple solution. Some of them are posted above. 6 | -------------------------------------------------------------------------------- /Power Set in Lexicographic order/readMe.md: -------------------------------------------------------------------------------- 1 | ### Power set in lexicographical order 2 | ``` 3 | Input : abc 4 | Output : a 5 | ab 6 | abc 7 | ac 8 | b 9 | bc 10 | c 11 | 12 | ``` 13 | -------------------------------------------------------------------------------- /Maximum Subset Sum With No Repeating Digits/readMe.md: -------------------------------------------------------------------------------- 1 | # Find Maximum Subset Sum which Can be obtained from an Array of Numbers where No Two Elements in subset have same digit. 2 | 3 | ``` 4 | array[] = { 121 , 333 , 3 , 4 , 23 , 12 } 5 | Maximum Sum = 458 6 | Subset[] = { 121 , 333 , 4 } 7 | ``` 8 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Find The Largest Three Elements in An Array/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array with all distinct elements, find the largest three elements. 2 | 3 | ``` 4 | Input : 10 , 4 , 3 , 50 , 23 , 90 5 | Output : 90 , 50 , 23 6 | 7 | Input : 1 , 6 , 2 , 8 , 3 8 | 9 | Output : 8 , 6 , 3 10 | ``` 11 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Segregate 0s and 1s/readMe.md: -------------------------------------------------------------------------------- 1 | ## You are given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array. Traverse array only once. 2 | ``` 3 | Input array = [0, 1, 0, 1, 0, 0, 1, 1, 1, 0] 4 | Output array = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] 5 | ``` 6 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Sum of All Leaf Nodes/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a binary tree, find the sum of all the leaf nodes. 2 | 3 | ``` 4 | Input : 5 | 1 6 | / \ 7 | 2 3 8 | / \ / \ 9 | 4 5 6 7 10 | \ 11 | 8 12 | Output : 13 | Sum = 4 + 5 + 8 + 7 = 24 14 | ``` 15 | -------------------------------------------------------------------------------- /Largest Sum Contiguous Subarray/readMe.md: -------------------------------------------------------------------------------- 1 | ## Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of numbers which has the largest sum. 2 | ### For Example : Array = {-2, -3, 4, -1, -2, 1, 5, -3}; 3 | ### Maximum contiguous sum is 7 4 | ### Starting index 2 5 | ### Ending index 6 6 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Sum of All Non-Leaf Node/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a binary tree, find the sum of all the Non-leaf nodes. 2 | 3 | ``` 4 | Input : 5 | 1 6 | / \ 7 | 2 3 8 | / \ / \ 9 | 4 5 6 7 10 | \ 11 | 8 12 | Output : 13 | Sum = 1 + 2 + 3 + 6 = 12 14 | ``` 15 | -------------------------------------------------------------------------------- /Longest Repeated Subsequence/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a string, print the longest repeating subsequence such that the two subsequence don’t have same string character at same position, i.e., any i’th character in the two subsequences shouldn’t have the same index in the original string. 2 | ### Input: str = "aabb" 3 | ### Output: "ab" 4 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Calculate Size of Tree/readMe.md: -------------------------------------------------------------------------------- 1 | ## Size of a tree is the number of elements present in the tree. Size of the below tree is 5. 2 | 3 | ``` 4 | Size() function recursively calculates the size of a tree. It works as follows: 5 | 6 | Size of a tree = Size of left subtree + 1 + Size of right subtree. 7 | ``` 8 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Check If Two Trees are Identical or Not/readMe.md: -------------------------------------------------------------------------------- 1 | ## Two trees are identical when they have same data and arrangement of data is also same. 2 | 3 | To identify if two trees are identical, we need to traverse both trees simultaneously, and while traversing we need to compare data and children of the trees. 4 | -------------------------------------------------------------------------------- /Maximum Subarray Sum/readMe.md: -------------------------------------------------------------------------------- 1 | ## You are given a one dimensional array that may contain both positive and negative integers, find the sum of contiguous subarray of numbers which has the largest sum. 2 | 3 | > For example, if the given array is {-2, -5, 6, -2, -3, 1, 5, -6}, then the maximum subarray sum is 7 (see highlighted elements). 4 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Find the Missing Number/readMe.md: -------------------------------------------------------------------------------- 1 | ## You are given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing in the list. Write an efficient code to find the missing integer. 2 | 3 | ``` 4 | I/P [1, 2, 4, ,6, 3, 7, 8] 5 | O/P 5 6 | ``` 7 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Matrix/Transpose of A Matrix/readMe.md: -------------------------------------------------------------------------------- 1 | ## A Matrix is given and you need to find the transpose of it. 2 | 3 | ## Input 4 | 5 | - A Matrix 6 | - its dimension 7 | 8 | ## Output 9 | 10 | - Transpose Matrix 11 | 12 | ``` 13 | Consider, the dimension of matrix is M x N. Don't just do for square matrices. 14 | ``` 15 | -------------------------------------------------------------------------------- /Longest Arithmetic Progression/readMe.md: -------------------------------------------------------------------------------- 1 | # Given a set of numbers, find the Length of the Longest Arithmetic Progression in it. 2 | ``` 3 | set[] = {1, 7, 10, 15, 27, 29} 4 | output = 3 5 | The longest arithmetic progression is {1, 15, 29} 6 | 7 | set[] = {5, 10, 15, 20, 25, 30} 8 | output = 6 9 | The whole set is in AP 10 | ``` 11 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Sum Of All Right Leaves/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a Binary Tree, find sum of all right leaves in it. 2 | ``` 3 | Input : 4 | 1 5 | / \ 6 | 2 3 7 | / \ \ 8 | 4 5 8 9 | \ / \ 10 | 2 6 7 11 | 12 | Output : 13 | sum = 2 + 5 + 7 = 14 14 | ``` 15 | -------------------------------------------------------------------------------- /Longest Geometric Progression/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a set of numbers, find the Length of the Longest Geometrix Progression (LLGP) in it. The common ratio of GP must be an integer. 2 | 3 | ``` 4 | set[] = {5, 7, 10, 15, 20, 29} 5 | output = 3 6 | The longest arithmetic progression is {5, 10, 20} 7 | 8 | set[] = {3, 9, 27, 81} 9 | output = 4 10 | ``` 11 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Segregate Even and Odd Numbers/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of integers, segregate even and odd numbers in the array. All the even numbers should be present first, and then the odd numbers. 2 | ``` 3 | 4 | Input : 1 9 5 3 2 6 7 11 5 | Output : 6 2 3 5 2 9 11 1 6 | 7 | Input : 1 3 2 4 7 6 9 10 8 | Output : 10 2 6 4 7 9 3 1 9 | ``` 10 | -------------------------------------------------------------------------------- /Count Distinct Subsequences/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a string, find the count of distinct subsequences of it. 2 | ``` 3 | Input : str = "gfg" 4 | Output : 7 5 | The seven distinct subsequences are "", "g", "f", 6 | "gf", "fg", "gg" and "gfg" 7 | 8 | Input : str = "ggg" 9 | Output : 4 10 | The four distinct subsequences are "", "g", "gg" 11 | and "ggg" 12 | ``` 13 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Check if Tree is Symmetry or Not/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a binary tree, check whether it is a mirror of itself. 2 | ``` 3 | this binary tree is symmetric: 4 | 5 | 1 6 | / \ 7 | 2 2 8 | / \ / \ 9 | 3 4 4 3 10 | But the following is not: 11 | 12 | 1 13 | / \ 14 | 2 2 15 | \ \ 16 | 3 3 17 | ``` 18 | -------------------------------------------------------------------------------- /Maximum Size Subset with Given Sum/readMe.md: -------------------------------------------------------------------------------- 1 | ## we need to find size of maximum size subset whose sum is equal to given sum. 2 | 3 | ``` 4 | Input : set[] = {2, 3, 5, 7, 10, 15}, 5 | sum = 10 6 | Output : 3 7 | The largest sized subset with sum 10 8 | is {2, 3, 5} 9 | 10 | Input : set[] = {1, 2, 3, 4, 5} 11 | sum = 4 12 | Output : 2 13 | ``` 14 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Flip Binary Tree/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a binary tree, the task is to flip the binary tree towards right direction that is clockwise. 2 | ## In the flip operation, left most node becomes the root of flipped tree and its parent become its right child and the right sibling become its left child and same should be done for all left most nodes recursively. 3 | -------------------------------------------------------------------------------- /Smallest Sum Contiguous Subarray/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array containing n integers. The problem is to find the sum of the elements of the contiguous subarray having the smallest(minimum) sum. 2 | 3 | ``` 4 | Input : arr[] = {3, -4, 2, -3, -1, 7, -5} 5 | Output : -6 6 | Subarray is {-4, 2, -3, -1} = -6 7 | 8 | Input : arr = {2, 6, 8, 1, 4} 9 | Output : 1 10 | ``` 11 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Product of Ranges in An Array/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array A[] of size N. Solve Q queries. Find the product in range [L, R] under modulo P ( P is Prime). 2 | ``` 3 | Input : A[] = {1, 2, 3, 4, 5, 6} 4 | L = 2, R = 5, P = 229 5 | Output : 120 6 | 7 | Input : A[] = {1, 2, 3, 4, 5, 6}, 8 | L = 2, R = 5, P = 113 9 | Output : 7 10 | ``` 11 | -------------------------------------------------------------------------------- /Longest Common Increasing Subsequence/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given two arrays, find length of the longest common increasing subsequence [LCIS] and print one of such sequences (multiple sequences may exist) 2 | 3 | ``` 4 | arr1[] = {3, 4, 9, 1} and 5 | arr2[] = {5, 3, 8, 9, 10, 2, 1} 6 | 7 | Our answer would be {3, 9} as this is the longest common subsequence which is increasing also. 8 | ``` 9 | -------------------------------------------------------------------------------- /Binary Search/readMe.md: -------------------------------------------------------------------------------- 1 | ## Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty. 2 | -------------------------------------------------------------------------------- /Count Balanced Binary Trees of Height h/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a height h, count and return the maximum number of balanced binary trees possible with height h. A balanced binary tree is one in which for every node, the difference between heights of left and right subtree is not more than 1. 2 | 3 | ``` 4 | Input : h = 3 5 | Output : 15 6 | 7 | Input : h = 4 8 | Output : 315 9 | ``` 10 | -------------------------------------------------------------------------------- /Find the Element that appears once in a Sorted Array/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a sorted array in which all elements appear twice (one after one) and one element appears only once. Find that element in O(log n) complexity. 2 | 3 | ``` 4 | Input: arr[] = {1, 1, 3, 3, 4, 5, 5, 7, 7, 8, 8} 5 | Output: 4 6 | 7 | Input: arr[] = {1, 1, 3, 3, 4, 4, 5, 5, 7, 7, 8} 8 | Output: 8 9 | ``` 10 | -------------------------------------------------------------------------------- /Subset Sum Problem/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a set of non-negative integers, and a value sum, determine if there is a subset of the given set with sum equal to given sum. 2 | 3 | ## Example: 4 | 5 | ### Input: set[] = {3, 34, 4, 12, 5, 2} , sum = 9 6 | 7 | ## Code 1 : using recursion 8 | ## Code 2 : using DP - Polynomial Space 9 | ## Code 3 : using DP - Space Optimization [ Linear Space ] 10 | -------------------------------------------------------------------------------- /Permutation Coefficient/readMe.md: -------------------------------------------------------------------------------- 1 | ## Permutation refers to the process of arranging all the members of a given set to form a sequence. The number of permutations on a set of n elements is given by n! , where “!” represents factorial. 2 | ## The Permutation Coefficient represented by P(n, k) is used to represent the number of ways to obtain an ordered subset having k elements from a set of n elements. 3 | -------------------------------------------------------------------------------- /Longest Palindromic Subsequence/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a sequence, find the length of the longest palindromic subsequence in it. 2 | 3 | ``` 4 | As another example, if the given sequence is “BBABCBCAB”, then the output should be 7 as 5 | “BABCBAB” is the longest palindromic subseuqnce in it. “BBBBB” and “BBCBB” are also 6 | palindromic subsequences of the given sequence, but not the longest ones. 7 | ``` 8 | -------------------------------------------------------------------------------- /Minimum Number of Jumps to Reach ( M , N ) From ( 1 , 1 )/readMe.md: -------------------------------------------------------------------------------- 1 | ## Find Minimum Jumps to reach ( M , N ) from ( 1, 1 ). If you are at ( X , Y ), then you can go to either ( X + Y , Y) or ( X , X + Y ). If it's not possible to reach ( M , N ), then return -1. 2 | 3 | ``` 4 | M = 3, N = 2 5 | output = 2 6 | 7 | 8 | M = 2, N = 2 9 | output = -1 10 | ``` 11 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Maximum of All SubArrays of Size k/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array and an integer k, find the maximum for each and every contiguous subarray of size k. 2 | 3 | ``` 4 | Input : 5 | arr[] = {1, 2, 3, 1, 4, 5, 2, 3, 6} 6 | k = 3 7 | Output : 8 | 3 3 4 5 5 5 6 9 | 10 | Input : 11 | arr[] = {8, 5, 10, 7, 9, 4, 15, 12, 90, 13} 12 | k = 4 13 | Output : 14 | 10 10 10 15 15 90 90 15 | ``` 16 | -------------------------------------------------------------------------------- /Delannoy Number/readMe.md: -------------------------------------------------------------------------------- 1 | ## In mathematics, a Delannoy number D describes the number of paths from the southwest corner (0, 0) of a rectangular grid to the northeast corner (m, n), using only single steps north, northeast, or east. 2 | 3 | ``` 4 | D(M,N) = 1 if M = 0 || N = 0 5 | D(M,N) = D(M-1 , N) + D( M , N -1 ) + D( M - 1 , N - 1 ) otherwise 6 | ``` 7 | -------------------------------------------------------------------------------- /Number of Zeros/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of 1s and 0s which has all 1s first followed by all 0s. Find the number of 0s. Count the number of zeroes in the given array. 2 | 3 | ``` 4 | Input: arr[] = {1, 1, 1, 1, 0, 0} 5 | Output: 2 6 | 7 | Input: arr[] = {1, 0, 0, 0, 0} 8 | Output: 4 9 | 10 | Input: arr[] = {0, 0, 0} 11 | Output: 3 12 | 13 | Input: arr[] = {1, 1, 1, 1} 14 | Output: 0 15 | ``` 16 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Kth Smallest Element/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array and a number k where k is smaller than size of array, we need to find the k’th smallest element in the given array. It is given that ll array elements are distinct. 2 | 3 | ``` 4 | Input: arr[] = {7, 10, 4, 3, 20, 15} 5 | k = 3 6 | Output: 7 7 | 8 | Input: arr[] = {7, 10, 4, 3, 20, 15} 9 | k = 4 10 | Output: 10 11 | ``` 12 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Sort An Array of 0s - 1s and 2s/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array A[] consisting 0s, 1s and 2s, write a function that sorts A[]. The functions should put all 0s first, then all 1s and all 2s in last. 2 | ``` 3 | 4 | Input : {0, 1, 2, 0, 1, 2} 5 | Output : {0, 0, 1, 1, 2, 2} 6 | 7 | Input : {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1} 8 | Output : {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2} 9 | ``` 10 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Sum of Heights of All Individual Nodes/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a binary tree, find sum of heights all individual Nodes in the tree. 2 | 3 | ``` 4 | 1 5 | 2 3 6 | 4 5 7 | 8 | 1). Height of Node 1 - 3 9 | 2). Height of Node 2 - 2 10 | 3). Height of Node 3 - 1 11 | 4). Height of Node 4 - 1 12 | 5). Height of Node 5 - 1 13 | 14 | Adding all of them = 8 15 | ``` 16 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Convert Array into Zig-Zag Fashion/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of DISTINCT elements, rearrange the elements of array in zig-zag fashion in O(n) time. The converted array should be in form a < b > c < d > e < f. 2 | 3 | ``` 4 | Input: arr[] = {4, 3, 7, 8, 6, 2, 1} 5 | Output: arr[] = {3, 7, 4, 8, 2, 6, 1} 6 | 7 | Input: arr[] = {1, 4, 3, 2} 8 | Output: arr[] = {1, 4, 2, 3} 9 | ``` 10 | -------------------------------------------------------------------------------- /N Queen Problem/readMe.md: -------------------------------------------------------------------------------- 1 | ## The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. 2 | 3 | ### The expected output is a binary matrix which has 1s for the blocks where queens are placed. For example, following is the output matrix for above 4 queen solution. 4 | { 0, 1, 0, 0} 5 | { 0, 0, 0, 1} 6 | { 1, 0, 0, 0} 7 | { 0, 0, 1, 0} 8 | -------------------------------------------------------------------------------- /Perfect Sum Problem/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of integers and a sum, the task is to print all subsets of given array with sum equal to given sum. 2 | 3 | ``` 4 | Input : arr[] = {2, 3, 5, 6, 8, 10} 5 | sum = 10 6 | Output : 5 2 3 7 | 2 8 8 | 10 9 | 10 | Input : arr[] = {1, 2, 3, 4, 5} 11 | sum = 10 12 | Output : 4 3 2 1 13 | 5 3 2 14 | 5 4 1 15 | ``` 16 | -------------------------------------------------------------------------------- /Split A Number into Maximum Composite Number/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given n, print the maximum number of composite numbers that sum up to n. First few composite numbers are 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, ……… 2 | 3 | ``` 4 | Input: 90 5 | Output: 22 6 | Explanation: If we add 21 4's, then we 7 | get 84 and then add 6 to it, we get 90. 8 | 9 | Input: 10 10 | Output: 2 11 | Explanation: 4 + 6 = 10 12 | ``` 13 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Replace Every Array Element/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of integers, update every element with multiplication of previous and next elements with following exceptions. 2 | - First element is replaced by multiplication of first and second. 3 | - Last element is replaced by multiplication of last and second last. 4 | 5 | ``` 6 | Input: arr[] = {2, 3, 4, 5, 6} 7 | Output: arr[] = {6, 8, 15, 24, 30} 8 | ``` 9 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Kth Ancestor of A Node/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a Binary Tree and a key, write a function that prints all the ancestors of the key in the given binary tree. 2 | 3 | ### if the given tree is following Binary Tree and key is 7, then 2nd Ancestor is 2. 4 | ``` 5 | 6 | 1 7 | / \ 8 | 2 3 9 | / \ 10 | 4 5 11 | / 12 | 7 13 | ``` 14 | -------------------------------------------------------------------------------- /Sum of All Substrings of A String Representing A Number/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a integer represented as a string, we need to get the sum of all possible substrings of this string. 2 | 3 | ``` 4 | Input : num = “1234” 5 | Output : 1670 6 | Sum = 1 + 2 + 3 + 4 + 12 + 23 + 7 | 34 + 123 + 234 + 1234 8 | = 1670 9 | 10 | Input : num = “421” 11 | Output : 491 12 | Sum = 4 + 2 + 1 + 42 + 21 + 421 = 491 13 | ``` 14 | -------------------------------------------------------------------------------- /Binomial Coefficient/readMe.md: -------------------------------------------------------------------------------- 1 | ## Following are common definition of Binomial Coefficients. 2 | ### A binomial coefficient C(n, k) can be defined as the coefficient of X^k in the expansion of (1 + X)^n. 3 | ### A binomial coefficient C(n, k) also gives the number of ways, disregarding order, that k objects can be chosen from among n objects; more formally, the number of k-element subsets (or k-combinations) of an n-element set. 4 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Convert Binary Tree to Doubly Linked List/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a Binary Tree (BT), convert it to a Doubly Linked List(DLL) In-Place. The left and right pointers in nodes are to be used as previous and next pointers respectively in converted DLL. The order of nodes in DLL must be same as Inorder of the given Binary Tree. The first node of Inorder traversal (left most node in BT) must be head node of the DLL. 2 | -------------------------------------------------------------------------------- /Entringer Number/readMe.md: -------------------------------------------------------------------------------- 1 | ## The Entringer Number E(n, k) are the number of permutations of {1, 2, …, n + 1}, starting with k + 1, which, after initally falling, alternatively fall then rise. The Entringer are given by: 2 | 3 | ``` 4 | E(n,k) = 0 if k == 0 5 | = 1 if k == 0 and n == 0 6 | = E(n,k-1) + E(n-1 , n-k) otherwise 7 | ``` 8 | -------------------------------------------------------------------------------- /Count Inversions/readMe.md: -------------------------------------------------------------------------------- 1 | ## Inversion Count for an array indicates – how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum. 2 | ## Formally speaking, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j 3 | 4 | ``` 5 | The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3). 6 | ``` 7 | -------------------------------------------------------------------------------- /Count all subsequences having product less than K/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a non negative array, find the number of subsequences having product smaller than K. 2 | ### Examples: 3 | ### Input : [1, 2, 3, 4] 4 | ### k = 10 5 | ### Output :11 6 | ### The subsequences are {1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2, 3}, {1, 2, 4} 7 | 8 | ### Input : [4, 8, 7, 2] 9 | ### k = 50 10 | ### Output : 9 11 | -------------------------------------------------------------------------------- /Find Closest Number in Array/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. 2 | ``` 3 | Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} 4 | Target number = 11 5 | Output : 9 6 | 9 is closest to 11 in given array 7 | 8 | Input :arr[] = {2, 5, 6, 7, 8, 8, 9}; 9 | Target number = 4 10 | Output : 5 11 | ``` 12 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Rearrange An Array in A Specific Order/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of integers, task is to print the array in the order – smallest number, Largest number, 2nd smallest number, 2nd largest number, 3rd smallest number, 3rd largest number and so on….. 2 | ``` 3 | Input : arr[] = [5, 8, 1, 4, 2, 9, 3, 7, 6] 4 | Output :arr[] = {1, 9, 2, 8, 3, 7, 4, 6, 5} 5 | 6 | Input : arr[] = [1, 2, 3, 4] 7 | Output :arr[] = {1, 4, 2, 3} 8 | ``` 9 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Print All Full Nodes/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a binary tree, print all nodes will are full nodes. Full Nodes are nodes which has both left and right children as non-empty. 2 | 3 | ``` 4 | Input : 10 5 | / \ 6 | 8 2 7 | / \ / 8 | 3 5 7 9 | Output : 10 8 10 | 11 | Input : 1 12 | / \ 13 | 2 3 14 | / \ 15 | 4 6 16 | Output : 1 3 17 | -------------------------------------------------------------------------------- /.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 | 34 | .DS_Store 35 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Print Ancestors of A Given Node/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a Binary Tree and a key, write a function that prints all the ancestors of the key in the given binary tree. 2 | 3 | ### if the given tree is following Binary Tree and key is 7, then your function should print 4, 2 and 1. 4 | ``` 5 | 6 | 1 7 | / \ 8 | 2 3 9 | / \ 10 | 4 5 11 | / 12 | 7 13 | ``` 14 | -------------------------------------------------------------------------------- /Minimum Number of Jumps to Reach End/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of integers where each element represents the max number of steps that can be made forward from that element. Write a function to return the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then cannot move through that element. 2 | 3 | ``` 4 | Input: arr[] = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9} 5 | Output: 3 (1-> 3 -> 8 ->9) 6 | ``` 7 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Calculate Depth of A Full Binary Tree From Preorder/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given preorder of a binary tree, calculate its depth(or height) [starting from depth 0]. The preorder is given as a string with two possible characters. 2 | 3 | - ‘L’ denotes the leaf 4 | - ‘N’ denotes internal node 5 | ## The given tree can be seen as a full binary tree where every node has 0 or two children. The two children of a node can ‘N’ or ‘L’ or mix of both. 6 | -------------------------------------------------------------------------------- /Minimum Insertions to Form a Palindrome/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a string, find the minimum number of characters to be inserted to convert it to palindrome. 2 | 3 | ``` 4 | ab: Number of insertions required is 1 i.e. bab 5 | aa: Number of insertions required is 0 i.e. aa 6 | abcd: Number of insertions required is 3 i.e. dcbabcd 7 | abcda: Number of insertions required is 2 i.e. adcbcda 8 | abcde: Number of insertions required is 4 i.e. edcbabcde 9 | ``` 10 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Shortest Un-ordered SubArray/readMe.md: -------------------------------------------------------------------------------- 1 | ## An array is given of n length, and problem is that we have to find the length of shortest unordered {neither increasing nor decreasing} sub array in given array. 2 | 3 | ``` 4 | Input : n = 5 5 | 7 9 10 8 11 6 | Output : 3 7 | Explanation : 9 10 8 unordered sub array. 8 | 9 | Input : n = 5 10 | 1 2 3 4 5 11 | Output : 0 12 | Explanation : Array is in increasing order. 13 | ``` 14 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Find All Root to Leaf Paths/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a binary tree, print all its root to leaf paths without using recursion. For example, consider the following Binary Tree. 2 | ``` 3 | 6 4 | / \ 5 | 3 5 6 | / \ \ 7 | 2 5 4 8 | / \ 9 | 7 4 10 | 11 | There are 4 leaves, hence 4 root to leaf paths - 12 | 6->3->2 13 | 6->3->5->7 14 | 6->3->5->4 15 | 6->5>4 16 | ``` 17 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Matrix/Print Triangular Matrix/readMe.md: -------------------------------------------------------------------------------- 1 | ### A Matrix is given and you need to print all triangular matrices 2 | 3 | ### Input 4 | 5 | - Matrix 6 | - Its dimension 7 | 8 | ### Output 9 | 10 | - Lower Triangular Matrix of Primary Diagonal 11 | - Upper Triangular Matrix of Primary Diagonal 12 | - Lower Triangular Matrix of Secondary Diagonal 13 | - Lower Triangular Matrix of Secondary Diagonal 14 | - Primary Diagonal 15 | - Secondary Diagonal 16 | -------------------------------------------------------------------------------- /Maximum Sum Increasing Subsequence/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of n positive integers. Write a program to find the sum of maximum sum subsequence of the given array such that the intgers in the subsequence are sorted in increasing order. For example, if input is {1, 101, 2, 3, 100, 4, 5}, then output should be 106 (1 + 2 + 3 + 100), if the input array is {3, 4, 5, 10}, then output should be 22 (3 + 4 + 5 + 10) and if the input array is {10, 5, 4, 3}, then output should be 10 2 | -------------------------------------------------------------------------------- /Longest Increasing Subsequence/readMe.md: -------------------------------------------------------------------------------- 1 | ## Let us discuss Longest Increasing Subsequence (LIS) problem as an example problem that can be solved using Dynamic Programming. 2 | ### The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order. For example, the length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 and LIS is {10, 22, 33, 50, 60, 80}. 3 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Print Level Traversal in Sorted Order/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a Binary tree, the task is to print its all level in sorted order 2 | 3 | ``` 4 | Input : 7 5 | / \ 6 | 6 5 7 | / \ / \ 8 | 4 3 2 1 9 | Output : 10 | 7 11 | 5 6 12 | 1 2 3 4 13 | 14 | Input : 7 15 | / \ 16 | 16 1 17 | / \ 18 | 4 13 19 | Output : 20 | 7 21 | 1 16 22 | 4 13 23 | ``` 24 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Vertical Order Of Binary Tree/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a binary tree, print it vertically. The following example illustrates vertical order traversal. 2 | ``` 3 | 4 | 1 5 | / \ 6 | 2 3 7 | / \ / \ 8 | 4 5 6 7 9 | \ \ 10 | 8 9 11 | 12 | The output of print this tree vertically will be: 13 | 4 14 | 2 15 | 1 5 6 16 | 3 8 17 | 7 18 | 9 19 | -------------------------------------------------------------------------------- /Maximum path sum in a triangle/readMe.md: -------------------------------------------------------------------------------- 1 | ## We have given numbers in form of triangle, by starting at the top of the triangle and moving to adjacent numbers on the row below, find the maximum total from top to bottom. 2 | **Examples :** 3 | ``` 4 | Input : 5 | 3 6 | 7 4 7 | 2 4 6 8 | 8 5 9 3 9 | Output : 23 10 | Explanation : 3 + 7 + 4 + 9 = 23 11 | 12 | Input : 13 | 8 14 | -4 4 15 | 2 2 6 16 | 1 1 1 1 17 | Output : 19 18 | Explanation : 8 + 4 + 6 + 1 = 19 19 | ``` 20 | -------------------------------------------------------------------------------- /Count Number of Ways to Reach A Given Score in A Game/readMe.md: -------------------------------------------------------------------------------- 1 | ## Consider a game where a player can score 3 or 5 or 10 points in a move. Given a total score n, find number of ways to reach the given score. 2 | 3 | ``` 4 | Input: n = 20 5 | Output: 4 6 | There are following 4 ways to reach 20 7 | (10, 10) 8 | (5, 5, 10) 9 | (5, 5, 5, 5) 10 | (3, 3, 3, 3, 3, 5) 11 | 12 | Input: n = 13 13 | Output: 2 14 | There are following 2 ways to reach 13 15 | (3, 5, 5) 16 | (3, 10) 17 | ``` 18 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Largest SubArray with Equal Number of 1s and 0s/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array containing only 0s and 1s, find the largest subarray which contain equal no of 0s and 1s. Expected time complexity is O(n). 2 | 3 | ``` 4 | Input: arr[] = {1, 0, 1, 1, 1, 0, 0} 5 | Output: 1 to 6 (Starting and Ending indexes of output subarray) 6 | 7 | Input: arr[] = {1, 1, 1, 1} 8 | Output: No such subarray 9 | 10 | Input: arr[] = {0, 0, 1, 1, 0} 11 | Output: 0 to 3 Or 1 to 4 12 | ``` 13 | -------------------------------------------------------------------------------- /Newman-Conway Sequence/readMe.md: -------------------------------------------------------------------------------- 1 | ## Newman-Conway Sequence is the one which generates the following integer sequence. 2 | 1 1 2 2 3 4 4 4 5 6 7 7… 3 | 4 | ## In mathematical terms, the sequence P(n) of Newman-Conway numbers is defined by recurrence relation 5 | 6 | P(n) = P(P(n - 1)) + P(n - P(n - 1)) 7 | with seed values P(1) = 1 and P(2) = 1 8 | 9 | 10 | ``` 11 | Input : n = 2 12 | Output : 1 1 13 | 14 | Input : n = 10 15 | Output : 1 1 2 2 3 4 4 4 5 6 16 | ``` 17 | -------------------------------------------------------------------------------- /Number of N Digit(s) Stepping Numbers/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given n, find count of n digit Stepping numbers. A number is called stepping number if all adjacent digits have an absolute difference of 1. 321 is a Stepping Number while 421 is not. 2 | ``` 3 | Input : 2 4 | Output : 17 5 | Explanation: The numbers are 10, 12, 21, 6 | 23, 32, 34, 43, 45, 54, 56, 65, 67, 76, 7 | 78, 87, 89, 98. 8 | 9 | Input : 1 10 | Output : 10 11 | Explanation: the numbers are 0, 1, 2, 3, 12 | 4, 5, 6, 7, 8, 9. 13 | ``` 14 | -------------------------------------------------------------------------------- /Count of Different Ways to Express N As The Sum of 1, 3 And 4/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given N, count the number of ways to express N as sum of 1, 3 and 4. 2 | 3 | ``` 4 | Input : N = 4 5 | Output : 4 6 | Explanation: 1+1+1+1 7 | 1+3 8 | 3+1 9 | 4 10 | 11 | Input : N = 5 12 | Output : 6 13 | Explanation: 1 + 1 + 1 + 1 + 1 14 | 1 + 4 15 | 4 + 1 16 | 1 + 1 + 3 17 | 1 + 3 + 1 18 | 3 + 1 + 1 19 | ``` 20 | -------------------------------------------------------------------------------- /Cutting a Rod/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a rod of length n inches and an array of prices that contains prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces. For example, if length of the rod is 8 and the values of different pieces are given as following, then the maximum obtainable value is 22 (by cutting in two pieces of lengths 2 and 6) 2 | 3 | ## length | 1 2 3 4 5 6 7 8 4 | ## price | 1 5 8 9 10 17 17 20 5 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Find The Smallest Missing Number/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a sorted array of n distinct integers where each integer is in the range from 0 to m-1 and m > n. Find the smallest number that is missing from the array. 2 | 3 | ``` 4 | Input: {0, 1, 2, 6, 9}, n = 5, m = 10 5 | Output: 3 6 | 7 | Input: {4, 5, 10, 11}, n = 4, m = 12 8 | Output: 0 9 | 10 | Input: {0, 1, 2, 3}, n = 4, m = 5 11 | Output: 4 12 | 13 | Input: {0, 1, 2, 3, 4, 5, 6, 7, 10}, n = 9, m = 11 14 | Output: 8 15 | ``` 16 | -------------------------------------------------------------------------------- /Maximum length subsequence/readMe.md: -------------------------------------------------------------------------------- 1 | # Maximum length subsequence with difference between adjacent elements as either 0 or 1 2 | ## Given an array of n integers. The problem is to find maximum length of the subsequence with difference between adjacent elements as either 0 or 1. 3 | 4 | **Examples: 5 | Input : arr[] = {2, 5, 6, 3, 7, 6, 5, 8} 6 | Output : 5 7 | The subsequence is {5, 6, 7, 6, 5}. 8 | Input : arr[] = {-2, -1, 5, -1, 4, 0, 3} 9 | Output : 4 10 | The subsequence is {-2, -1, -1, 0}** 11 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Find The Deepest Node/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a binary tree, find the deep­est node in it. 2 | 3 | ``` 4 | Input : Root of below tree 5 | 1 6 | / \ 7 | 2 3 8 | / \ / \ 9 | 4 5 6 7 10 | \ 11 | 8 12 | Output : 8 13 | 14 | Input : Root of below tree 15 | 1 16 | / \ 17 | 2 3 18 | / 19 | 6 20 | Output : 6 21 | ``` 22 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Find The Maximum Sum In A Path From Leaf To Root/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a Binary Tree, find the maximum sum path from a leaf to root. For example, in the following tree, there are three leaf to root paths 8->-2->10, -4->-2->10 and 7->10. The sums of these three paths are 16, 4 and 17 respectively. The maximum of them is 17 and the path for maximum is 7->10. 2 | 10 3 | / \ 4 | -2 7 5 | / \ 6 | 8 -4 7 | -------------------------------------------------------------------------------- /Maximum Length Chain of Pairs/readMe.md: -------------------------------------------------------------------------------- 1 | **You are given n pairs of numbers. In every pair, the first number is always smaller than the second number. A pair (c, d) can follow another pair (a, b) if b < c. Chain of pairs can be formed in this fashion. Find the longest chain which can be formed from a given set of pairs. 2 | For example, if the given pairs are {{5, 24}, {39, 60}, {15, 28}, {27, 40}, {50, 90} }, then the longest chain that can be formed is of length 3, and the chain is {{5, 24}, {27, 40}, {50, 90}}** 3 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Number of Binary Trees for Given Preorder Sequence Length/readMe.md: -------------------------------------------------------------------------------- 1 | ## Count the number of Binary Tree possible for a given Preorder Sequence length n. 2 | ``` 3 | Input : n = 1 4 | Output : 1 5 | 6 | Input : n = 2 7 | Output : 2 8 | 9 | Input : n = 3 10 | Output : 5 11 | ``` 12 | 13 | 14 | ``` 15 | This can also be done using Catalan number Cn = (2n)!/(n+1)!*n! 16 | 17 | For n = 0, 1, 2, 3, … values of Catalan numbers are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, …. 18 | ``` 19 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Sum of Leaf Nodes At Minimum Level/readMe.md: -------------------------------------------------------------------------------- 1 | # Given a binary tree containing n nodes. The problem is to get the sum of all the leaf nodes which are at minimum level in the binary tree. 2 | 3 | ``` 4 | Input : 5 | 1 6 | / \ 7 | 2 3 8 | / \ / \ 9 | 4 5 6 7 10 | / \ 11 | 8 9 12 | 13 | Output : 11 14 | Leaf nodes 4 and 7 are at minimum level. 15 | Their sum = (4 + 7) = 11. 16 | ``` 17 | -------------------------------------------------------------------------------- /Minimum Insertions to Sort An Array/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of integer numbers, we need to sort this array in a minimum number of steps where in one step we can insert any array element from its position to any other position. 2 | ``` 3 | Input : arr[] = [2, 3, 5, 1, 4, 7, 6] 4 | Output : 3 5 | We can sort above array in 3 insertion 6 | steps as shown below, 7 | 1 before array value 2 8 | 4 before array value 5 9 | 6 before array value 7 10 | 11 | Input : arr[] = {4, 6, 5, 1} 12 | Output : 2 13 | ``` 14 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Density of Binary Tree/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a Binary Tree, find density of it by doing one traversal of it. 2 | ``` 3 | Density of Binary Tree = Size / Height 4 | 5 | Input: Root of following tree 6 | 10 7 | / \ 8 | 20 30 9 | 10 | Output: 1.5 11 | Height of given tree = 2 12 | Size of given tree = 3 13 | 14 | Input: Root of following tree 15 | 10 16 | / 17 | 20 18 | / 19 | 30 20 | Output: 1 21 | Height of given tree = 3 22 | Size of given tree = 3 23 | ``` 24 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Maximum Value in A Binary Tree/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a binary tree, find the Maximum node Value in it. 2 | 3 | ``` 4 | Input : Root of below tree 5 | 1 6 | / \ 7 | 2 3 8 | / \ / \ 9 | 4 5 6 7 10 | \ 11 | 8 12 | Output : 8 13 | 14 | Input : Root of below tree 15 | 12 16 | / \ 17 | 2 3 18 | / 19 | 1 20 | Output : 12 21 | ``` 22 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Minimum Value in A Binary Tree/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a binary tree, find the Minimum node Value in it. 2 | 3 | ``` 4 | Input : Root of below tree 5 | 1 6 | / \ 7 | 2 3 8 | / \ / \ 9 | 4 5 6 7 10 | \ 11 | 8 12 | Output : 1 13 | 14 | Input : Root of below tree 15 | 12 16 | / \ 17 | 2 3 18 | / 19 | 1 20 | Output : 1 21 | ``` 22 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Graph/Number of Nodes at Given Level/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a tree represented as undirected graph. Count the number of nodes at given level l. It may be assumed that vertex 0 is root of the tree. 2 | ``` 3 | Input : 7 4 | 0 1 5 | 0 2 6 | 1 3 7 | 1 4 8 | 1 5 9 | 2 6 10 | 2 11 | Output : 4 12 | 13 | Input : 6 14 | 0 1 15 | 0 2 16 | 1 3 17 | 2 4 18 | 2 5 19 | 2 20 | Output : 3 21 | ``` 22 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Matrix/Print A Given Matrix in Spiral Form/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a 2D array, print it in spiral form. See the following examples. 2 | ``` 3 | Input: 4 | 1 2 3 4 5 | 5 6 7 8 6 | 9 10 11 12 7 | 13 14 15 16 8 | Output: 9 | 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 10 | 11 | 12 | Input: 13 | 1 2 3 4 5 6 14 | 7 8 9 10 11 12 15 | 13 14 15 16 17 18 16 | Output: 17 | 1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11 18 | ``` 19 | -------------------------------------------------------------------------------- /Longest subsequence/readMe.md: -------------------------------------------------------------------------------- 1 | # Longest subsequence such that difference between adjacents is one 2 | ## Given an array of n size, the task is to find the longest subsequence such that difference between adjacents is one. 3 | ``` 4 | Input : arr[] = {10, 9, 4, 5, 4, 8, 6} 5 | Output : 3 6 | As longest subsequences with difference 1 are, "10, 9, 8", 7 | "4, 5, 4" and "4, 5, 6" 8 | 9 | 10 | Input : arr[] = {1, 2, 3, 2, 3, 7, 2, 1} 11 | Output : 7 12 | As longest consecutive sequence is "1, 2, 3, 2, 3, 2, 1" 13 | ``` 14 | -------------------------------------------------------------------------------- /Size of The Subarray With Maximum Sum/readMe.md: -------------------------------------------------------------------------------- 1 | ## An array is given, find length of the subarray having maximum sum. 2 | 3 | > Examples : 4 | Input : a[] = {1, -2, 1, 1, -2, 1} 5 | Output : Length of the subarray is 2 6 | Explanation: Subarray with consecutive elements and maximum sum will be {1, 1}. So length is 2 7 | 8 | > Input : ar[] = { -2, -3, 4, -1, -2, 1, 5, -3 } 9 | Output : Length of the subarray is 5 10 | Explanation: Subarray with consecutive elements and maximum sum will be {4, -1, -2, 1, 5}. 11 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Check If Binary Tree Has Duplicate Values/readMe.md: -------------------------------------------------------------------------------- 1 | ## Check if a Binary Tree (not BST) has duplicate values 2 | 3 | ``` 4 | Input : Root of below tree 5 | 1 6 | / \ 7 | 2 3 8 | \ 9 | 2 10 | Output : Yes 11 | Explanation : The duplicate value is 2. 12 | 13 | Input : Root of below tree 14 | 1 15 | / \ 16 | 20 3 17 | \ 18 | 4 19 | Output : No 20 | Explanation : There are no duplicates. 21 | ``` 22 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Check if Given Binary Tree is Sum Tree Or Not/readMe.md: -------------------------------------------------------------------------------- 1 | ## Write a function that returns true if the given Binary Tree is SumTree else false. A SumTree is a Binary Tree where the value of a node is equal to sum of the nodes present in its left subtree and right subtree. An empty tree is SumTree and sum of an empty tree can be considered as 0. A leaf node is also considered as SumTree. 2 | 3 | ``` 4 | 26 5 | / \ 6 | 10 3 7 | / \ \ 8 | 4 6 3 9 | ``` 10 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Maximum Product Subarray/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array that contains both positive and negative integers, find the product of the maximum product subarray. Expected Time complexity is O(n) and only O(1) extra space can be used. 2 | 3 | ``` 4 | Input: arr[] = {6, -3, -10, 0, 2} 5 | Output: 180 // The subarray is {6, -3, -10} 6 | 7 | Input: arr[] = {-1, -3, -10, 0, 60} 8 | Output: 60 // The subarray is {60} 9 | 10 | Input: arr[] = {-2, -3, 0, -2, -40} 11 | Output: 80 // The subarray is {-2, -40} 12 | ``` 13 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Merge Overlapping Intervals/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a set of time intervals in any order, merge all overlapping intervals into one and output the result which should have only mutually exclusive intervals. Let the intervals be represented as pairs of integers for simplicity. 2 | ## For example, let the given set of intervals be {{1,3}, {2,4}, {5,7}, {6,8} }. The intervals {1,3} and {2,4} overlap with each other, so they should be merged and become {1, 4}. Similarly {5, 7} and {6, 8} should be merged and become {5, 8} 3 | 4 | -------------------------------------------------------------------------------- /Longest Common Substring/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given two strings ‘X’ and ‘Y’, find the length of the longest common substring. 2 | 3 | ``` 4 | Input : X = "GeeksforGeeks", y = "GeeksQuiz" 5 | Output : 5 6 | The longest common substring is "Geeks" and is of 7 | length 5. 8 | 9 | Input : X = "abcdxyz", y = "xyzabcd" 10 | Output : 4 11 | The longest common substring is "abcd" and is of 12 | length 4. 13 | 14 | Input : X = "zxabcdezy", y = "yzabcdezx" 15 | Output : 6 16 | The longest common substring is "abcdez" and is of 17 | length 6. 18 | ``` 19 | -------------------------------------------------------------------------------- /Moser-de Bruijn Sequence/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an integer ‘n’, print the first ‘n’ terms of the Moser-de Bruijn Sequence. 2 | 3 | ## The Moser-de Bruijn sequence is the sequence obtained by adding up the distinct powers of the number 4 (For example 1, 4, 16, 64, etc). 4 | 5 | ``` 6 | Input : 5 7 | Output : 0 1 4 5 16 8 | 9 | Input : 10 10 | Output : 0 1 4 5 16 17 20 21 64 65 11 | ``` 12 | 13 | 14 | ## Reccurence Relation 15 | ``` 16 | 1) S(2 * n) = 4 * S(n) 17 | 2) S(2 * n + 1) = 4 * S(n) + 1 18 | with S(0) = 0 and S(1) = 1 19 | ``` 20 | -------------------------------------------------------------------------------- /Bell Numbers/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a set of n elements, find number of ways of partitioning it. 2 | ### Examples: 3 | ### Input: n = 2 4 | ### Output: Number of ways = 2 5 | ### Explanation: Let the set be {1, 2} 6 | { {1}, {2} } 7 | { {1, 2} } 8 | 9 | ### Input: n = 3 10 | ### Output: Number of ways = 5 11 | ### Explanation: Let the set be {1, 2, 3} 12 | { {1}, {2}, {3} } 13 | { {1}, {2, 3} } 14 | { {2}, {1, 3} } 15 | { {3}, {1, 2} } 16 | { {1, 2, 3} }. 17 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Mean of Range in An Array/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of n integers. You are given q queries. Write a program to print floor value of mean in range l to r for each query in a new line. 2 | ``` 3 | Input : arr[] = {1, 2, 3, 4, 5} 4 | q = 3 5 | 0 2 6 | 1 3 7 | 0 4 8 | Output : 2 9 | 3 10 | 3 11 | Here for 0 to 2 (1 + 2 + 3) / 3 = 2 12 | 13 | Input : arr[] = {6, 7, 8, 10} 14 | q = 2 15 | 0 3 16 | 1 2 17 | Output : 7 18 | 7 19 | ``` 20 | -------------------------------------------------------------------------------- /The Celebrity Problem/readMe.md: -------------------------------------------------------------------------------- 1 | ## In a party of N people, only one person is known to everyone. Such a person may be present in the party, if yes, (s)he doesn’t know anyone in the party. We can only ask questions like “does A know B? “. Find the stranger (celebrity) in minimum number of questions. 2 | 3 | ## We can describe the problem input as an array of numbers/characters representing persons in the party. We also have a hypothetical function HaveAcquaintance(A, B) which returns true if A knows B, false otherwise. How can we solve the problem. 4 | 5 | -------------------------------------------------------------------------------- /Ugly Numbers/readMe.md: -------------------------------------------------------------------------------- 1 | ## Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 25 … shows some ugly numbers. 2 | ## By convention, 1 is included. 3 | 4 | ### 1st Approach - Brute force - Loop running till ugly number 5 | ### 2nd Approach - Dynamic Programming - We can find that every subsequence is the ugly-sequence itself (1, 2, 3, 4, 5, etc) 6 | ### multiply 2, 3, 5. Thento get every ugly number from the three subsequence. Every step we choose the smallest one, and move one step after. 7 | -------------------------------------------------------------------------------- /Rotation Count in Rotated Sorted Array/readMe.md: -------------------------------------------------------------------------------- 1 | ## Consider an array of distinct numbers sorted in increasing order. The array has been rotated (clockwise) k number of times. Given such an array, find the value of k. 2 | 3 | ``` 4 | Input : arr[] = {15, 18, 2, 3, 6, 12} 5 | Output: 2 6 | Explanation : Initial array must be {2, 3, 7 | 6, 12, 15, 18}. We get the given array after 8 | rotating the initial array twice. 9 | 10 | Input : arr[] = {7, 9, 11, 12, 5} 11 | Output: 4 12 | 13 | Input: arr[] = {7, 9, 11, 12, 15}; 14 | Output: 0 15 | ``` 16 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Total Numbers With No Repeated Digits in A Range/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a range L, R find total such numbers in the given range such that they have no repeated digits. 2 | 3 | ``` 4 | 12 has no repeated digit. 5 | 22 has repeated digit. 6 | 102, 194 and 213 have no repeated digit. 7 | 212, 171 and 4004 have repeated digits. 8 | 9 | 10 | Input : 10 12 11 | Output : 2 12 | Explanation : In the given range 13 | 10 and 12 have no repeated digit 14 | where as 11 has repeated digit. 15 | 16 | Input : 1 100 17 | Output : 90 18 | ``` 19 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Nth Node in Inorder Traversal/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given the binary tree and you have to find out the n-th node of inorder traversal. 2 | 3 | ``` 4 | Input : n = 4 5 | 10 6 | / \ 7 | 20 30 8 | / \ 9 | 40 50 10 | Output : 10 11 | Inorder Traversal is : 40 20 50 10 30 12 | 13 | Input : n = 3 14 | 7 15 | / \ 16 | 2 3 17 | / \ 18 | 8 5 19 | Output : 8 20 | Inorder: 2 7 8 3 5 21 | 3th node is 8 22 | ``` 23 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Print Postorder Traversal From Given Inorder And Preorder Traversals/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given Inorder and Preorder traversals of a binary tree, print Postorder traversal. 2 | 3 | ``` 4 | Input: 5 | Inorder traversal in[] = {4, 2, 5, 1, 3, 6} 6 | Preorder traversal pre[] = {1, 2, 4, 5, 3, 6} 7 | 8 | Output: 9 | Postorder traversal is {4, 5, 2, 6, 3, 1} 10 | Trversals in the above example represents following tree 11 | 12 | 1 13 | / \ 14 | 2 3 15 | / \ \ 16 | 4 5 6 17 | ``` 18 | -------------------------------------------------------------------------------- /Matrix Chain Multiplication/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a sequence of matrices, find the most efficient way to multiply these matrices together. The problem is not actually to perform the multiplications, but merely to decide in which order to perform the multiplications. 2 | 3 | ### We have many options to multiply a chain of matrices because matrix multiplication is associative. In other words, no matter how we parenthesize the product, the result will be the same. For example, if we had four matrices A, B, C, and D, we would have: (ABC)D = (AB)(CD) = A(BCD) = .... 4 | -------------------------------------------------------------------------------- /Count Ways to Reach the Nth Stair Using Step 1, 2 or 3/readMe.md: -------------------------------------------------------------------------------- 1 | ## A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs. 2 | 3 | There are two methods to solve this problem 4 | - Recursive Method 5 | - Dynamic Programming 6 | 7 | ``` 8 | Input : 4 9 | Output : 7 10 | 1 -> 1 -> 1 -> 1 11 | 1 -> 1 -> 2 12 | 1 -> 2 -> 1 13 | 1 -> 3 14 | 2 -> 1 -> 1 15 | 2 -> 2 16 | 3 -> 1 17 | 18 | 19 | Input : 3 20 | Output : 4 21 | ``` 22 | -------------------------------------------------------------------------------- /Maximum Product Subset of an Array/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array a, we have to find maximum product possible with the subset of elements present in the array. The maximum product can be single element also. 2 | 3 | Input : { -1, -1, -2, 4, 3 } 4 | Output : 24 5 | Explanation : Maximum product will be ( -2 * -1 * 4 * 3 ) = 24 6 | 7 | Input : { -1, 0 } 8 | Output : 0 9 | Explanation : 0(single element) is maximum product possible 10 | 11 | Input : { 0, 0, 0 } 12 | Output : 0 13 | -------------------------------------------------------------------------------- /Minimum number of Coins/readMe.md: -------------------------------------------------------------------------------- 1 | Given a value **value** , if we want to make change for **value** Rs, and we have infinite supply of each of the denominations in Indian currency, i.e., we have infinite supply of { 1, 2, 5, 10, 20, 50, 100, 200 ,500, 1000 , 2000} valued coins/notes, what is the minimum number of coins and/or notes needed to make the change? 2 | 3 | Input: V = 70 4 | Output: 2 5 | We need a 50 Rs note and a 20 Rs note. 6 | 7 | Input: V = 121 8 | Output: 3 9 | We need a 100 Rs note, a 20 Rs note and a 1 Rs coin. 10 | -------------------------------------------------------------------------------- /Friends Pairing Problem/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given n friends, each one can remain single or can be paired up with some other friend. Each friend can be paired only once. Find out the total number of ways in which friends can remain single or can be paired up. 2 | 3 | ``` 4 | Input : n = 3 5 | Output : 4 6 | 7 | Explanation 8 | {1}, {2}, {3} : all single 9 | {1}, {2,3} : 2 and 3 paired but 1 is single. 10 | {1,2}, {3} : 1 and 2 are paired but 3 is single. 11 | {1,3}, {2} : 1 and 3 are paired but 2 is single. 12 | Note that {1,2} and {2,1} are considered same. 13 | ``` 14 | -------------------------------------------------------------------------------- /Nth Catalan Number/readMe.md: -------------------------------------------------------------------------------- 1 | ## Catalan numbers are a sequence of natural numbers that occurs in many interesting counting problems like following. 2 | ## 1) Count the number of expressions containing n pairs of parentheses which are correctly matched. For n = 3, possible expressions are ((())), ()(()), ()()(), (())(), (()()). 3 | 4 | ## 2) Count the number of possible Binary Search Trees with n keys (See this) 5 | 6 | ## 3) Count the number of full binary trees (A rooted binary tree is full if every vertex has either two children or no children) with n+1 leaves. 7 | 8 | -------------------------------------------------------------------------------- /Find Maximum Possible Stolen Value From Houses/readMe.md: -------------------------------------------------------------------------------- 1 | ## There are n houses build in a line, each of which contains some value in it. A thief is going to steal the maximal value of these houses, but he can’t steal in two adjacent houses because owner of the stolen houses will tell his two neighbour left and right side. What is the maximum stolen value. 2 | ``` 3 | Input : hval[] = {6, 7, 1, 3, 8, 2, 4} 4 | Output : 19 5 | Thief will steal 6, 1, 8 and 4 from house. 6 | 7 | Input : hval[] = {5, 3, 4, 11, 2} 8 | Output : 16 9 | Thief will steal 5 and 11 10 | ``` 11 | -------------------------------------------------------------------------------- /Palindrome Partitioning/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a string, a partitioning of the string is a palindrome partitioning if every substring of the partition is a palindrome. For example, “aba|b|bbabb|a|b|aba” is a palindrome partitioning of “ababbbabbababa”. Determine the fewest cuts needed for palindrome partitioning of a given string. For example, minimum 3 cuts are needed for “ababbbabbababa”. The three cuts are “a|babbbab|b|ababa”. If a string is palindrome, then minimum 0 cuts are needed. If a string of length n containing all different characters, then minimum n-1 cuts are needed. 2 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Array Rearrangement by Shifting Zero to end/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of integers of size n. Assume ‘0’ as invalid number and all other as valid number. Convert the array in such a way that if next valid number is same as current number, double its value and replace the next number with 0. After the modification, rearrange the array such that all 0’s are shifted to the end. 2 | 3 | ``` 4 | Input : arr[] = {2, 2, 0, 4, 0, 8} 5 | Output : 4 4 8 0 0 0 6 | 7 | Input : arr[] = {0, 2, 2, 2, 0, 6, 6, 0, 0, 8} 8 | Output : 4 2 12 8 0 0 0 0 0 0 9 | ``` 10 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Count Number of Primes in Given Range/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a range [L, R], we need to find the count of total numbers of prime numbers in the the range [L, R] where 0 <= L <= R < 10000. Consider that there are a large number of queries for different ranges. 2 | 3 | ``` 4 | Input : Query 1 : L = 1, R = 10 5 | Query 2 : L = 5, R = 10 6 | Output : 4 7 | 2 8 | Explanation 9 | Primes in the range L = 1 to R = 10 are 10 | {2, 3, 5, 7}. Therefore for query, answer 11 | is 4 {2, 3, 5, 7}. 12 | For the second query, answer is 2 {5, 7}. 13 | ``` 14 | -------------------------------------------------------------------------------- /Remove Invalid Parentheses/readMe.md: -------------------------------------------------------------------------------- 1 | ## An expression will be given which can contain open and close parentheses and optionally some characters, No other operator will be there in string. We need to remove minimum number of parentheses to make the input string valid. If more than one valid output are possible removing same number of parentheses then print all such output. 2 | 3 | ``` 4 | Input : str = “()())()” - 5 | Output : ()()() (())() 6 | There are two possible solutions 7 | "()()()" and "(())()" 8 | 9 | Input : str = (v)())() 10 | Output : (v)()() (v())() 11 | ``` 12 | -------------------------------------------------------------------------------- /Minimum Product Subset of an Array/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array a, we have to find minimum product possible with the subset of elements present in the array. The minimum product can be single element also. 2 | 3 | Input : { -1, -1, -2, 4, 3 } 4 | Output : -24 5 | Explanation : Minimum product will be ( -2 * -1 * -1 * 4 * 3 ) = -24 6 | 7 | Input : { -1, 0 } 8 | Output : -1 9 | Explanation : -1(single element) is minimum product possible 10 | 11 | Input : { 0, 0, 0 } 12 | Output : 0 13 | -------------------------------------------------------------------------------- /Floor in a Sorted Array/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a sorted array and a value x, the floor of x is the largest element in array smaller than or equal to x. Write efficient functions to find floor of x. 2 | 3 | ``` 4 | Input : arr[] = {1, 2, 8, 10, 10, 12, 19}, x = 5 5 | Output : 2 6 | 2 is the largest element in arr[] smaller than 5. 7 | 8 | Input : arr[] = {1, 2, 8, 10, 10, 12, 19}, x = 20 9 | Output : 19 10 | 19 is the largest element in arr[] smaller than 20. 11 | 12 | Input : arr[] = {1, 2, 8, 10, 10, 12, 19}, x = 0 13 | Output : -1 14 | Since floor doesn't exist, output is -1 15 | ``` 16 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Arrange Given Numbers to Form the Biggest Number/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of non-negative numbers(of Integer Range), they are needed to be arranged in some order such that it gives the max number. For example given array is A[1, 34, 3, 98, 9, 76, 45, 4, 12, 121]. if we arrange these numbers in the following order, A[9, 98, 76, 45, 4, 34, 3, 12, 121, 1], then by joining them we get “99876454343121211” as largest number. 2 | 3 | ``` 4 | Input : [1, 34, 3, 98, 9, 76, 45, 4, 12, 121] 5 | Output : 99876454343121211 6 | 7 | Input : [12, 121] 8 | Output : 12121 9 | ``` 10 | -------------------------------------------------------------------------------- /Fibonacci numbers/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | int fib(int num) { 12 | if (num < 2) { 13 | return num; 14 | } 15 | return fib(num-1) + fib(num-2); 16 | } 17 | 18 | int main () { 19 | cout << "\nEnter Number\t:\t"; 20 | int number; 21 | cin >> number; 22 | cout <<"\nThe result is\t:\t" << fib(number); 23 | cout << endl; 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Longest Common Subsequence/For Three Strings/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given 3 strings of all having length < 100,the task is to find the longest common sub-sequence in all three given sequences. 2 | ### Examples: 3 | ### Input : str1 = "geeks" 4 | ### str2 = "geeksfor" 5 | ### str3 = "geeksforgeeks" 6 | ### Output : 5 7 | ### Longest common subsequence is "geeks" 8 | ### i.e., length = 5 9 | 10 | ### Input : str1 = "abcd1e2" 11 | ### str2 = "bc12ea" 12 | ### str3 = "bd1ea" 13 | ### Output : 3 14 | ### Longest common subsequence is "b1e" 15 | ### i.e. length = 3. 16 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Move All Zeros to end of Array/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of random numbers, Push all the zero’s of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time complexity is O(n) and extra space is O(1). 2 | 3 | ``` 4 | Input : arr[] = {1, 2, 0, 4, 3, 0, 5, 0}; 5 | Output : arr[] = {1, 2, 4, 3, 5, 0, 0}; 6 | 7 | Input : arr[] = {1, 2, 0, 0, 0, 3, 6}; 8 | Output : arr[] = {1, 2, 3, 6, 0, 0, 0}; 9 | ``` 10 | -------------------------------------------------------------------------------- /Find a Fixed Point in a Given Array/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of n distinct integers sorted in ascending order, write a function that returns a Fixed Point in the array, if there is any Fixed Point present in array, else returns -1. Fixed Point in an array is an index i such that arr[i] is equal to i. Note that integers in array can be negative. 2 | ``` 3 | Input: arr[] = {-10, -5, 0, 3, 7} 4 | Output: 3 // arr[3] == 3 5 | 6 | Input: arr[] = {0, 2, 5, 8, 17} 7 | Output: 0 // arr[0] == 0 8 | 9 | 10 | Input: arr[] = {-10, -5, 3, 4, 7, 9} 11 | Output: -1 // No Fixed Point 12 | ``` 13 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Elements that Occurred Only Once/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array arr that has numbers appearing twice or once. The task is to identify numbers that occurred only once in the array. 2 | 3 | Note: Duplicates appear side by side every time. Might be few numbers can occur one time and just assume this is a right rotating array (just say an array can rotate k times towards right). Order of the elements in the output doesn’t matter. 4 | 5 | ``` 6 | Input: arr[] = { 7, 7, 8, 8, 9, 1, 1, 4, 2, 2 } 7 | Output: 9 4 8 | 9 | Input: arr[] = {-9, -8, 4, 4, 5, 5, -1} 10 | Output: -9 -8 -1 11 | ``` 12 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Sum of All the Parent Node Having Child N/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a binary tree containing n nodes. The problem is to find the sum of all the parent node’s which have a child node with value x. 2 | ``` 3 | Input : Binary tree with x = 2: 4 | 4 5 | / \ 6 | 2 5 7 | / \ / \ 8 | 7 2 2 3 9 | Output : 11 10 | 11 | 4 12 | / \ 13 | 2 5 14 | / \ / \ 15 | 7 2 2 3 16 | 17 | The highlighted nodes (4, 2, 5) above 18 | are the nodes having 2 as a child node. 19 | ``` 20 | -------------------------------------------------------------------------------- /Minimum Sum of Absolute Difference of Pairs of Two Arrays/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given two arrays a[] and b[] of equal length n. The task is to pair each element of array a to an element in array b, such that sum S of absolute differences of all the pairs is minimum. 2 | 3 | ## Suppose, two elements a[i] and a[j] (i != j) of a are paired with elements b[p] and b[q] of b respectively, then p should not be equal to q. 4 | 5 | ``` 6 | Input : a[] = {3, 2, 1} 7 | b[] = {2, 1, 3} 8 | Output : 0 9 | 10 | Input : n = 4 11 | a[] = {4, 1, 8, 7} 12 | b[] = {2, 3, 6, 5} 13 | Output : 6 14 | ``` 15 | -------------------------------------------------------------------------------- /0-1 Knapsack Problem/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. In other words, given two integer arrays val[0..n-1] and wt[0..n-1] which represent values and weights associated with n items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item, or don’t pick it 2 | ## Method 1 - using recursion 3 | ## Method 2 - using Dynamic Programming 4 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Diameter of Binary Tree/readMe.md: -------------------------------------------------------------------------------- 1 | ## The diameter of a tree is the number of nodes on the longest path between two leaves in the tree. The diagram below shows two trees each with diameter nine, the leaves that form the ends of a longest path are colored (note that there may be more than one path in tree of same diameter). 2 | 3 | ``` 4 | 5 | Input : 1 6 | / \ 7 | 2 3 8 | / \ 9 | 4 5 10 | 11 | Output : 4 12 | 13 | Input : 1 14 | / \ 15 | 2 3 16 | / \ . \ 17 | 4 5 . 6 18 | 19 | Output : 5 20 | ``` 21 | -------------------------------------------------------------------------------- /Minimum Partition/readMe.md: -------------------------------------------------------------------------------- 1 | # Partition a set into two subsets such that the difference of subset sums is minimum 2 | 3 | ## Given a set of integers, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums is minimum. 4 | 5 | ## If there is a set S with n elements, then if we assume Subset1 has m elements, Subset2 must have n-m elements and the value of abs(sum(Subset1) – sum(Subset2)) should be minimum. 6 | ``` 7 | Input: arr[] = {1, 6, 11, 5} 8 | Output: 1 9 | Explanation: 10 | Subset1 = {1, 5, 6}, sum of Subset1 = 12 11 | Subset2 = {11}, sum of Subset2 = 11 12 | ``` 13 | -------------------------------------------------------------------------------- /Newman-Shanks-Williams prime/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | int nswp(int n) { 12 | if (n == 0 || n == 1) { 13 | return 1; 14 | } 15 | return 2 * nswp(n - 1) + nswp(n - 2); 16 | } 17 | 18 | int main () { 19 | cout << "\nEnter Number\t:\t"; 20 | int number; 21 | cin >> number; 22 | cout <<"\nThe result is\t:\t" << nswp(number); 23 | cout << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Sum of Average of All Subsets/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array arr of N integer elements, the task is to find sum of average of all subsets of this array. 2 | ``` 3 | Input : arr[] = [2, 3, 5] 4 | Output : 23.33 5 | Explanation : Subsets with their average are, 6 | [2] average = 2/1 = 2 7 | [3] average = 3/1 = 3 8 | [5] average = 5/1 = 5 9 | [2, 3] average = (2+3)/2 = 2.5 10 | [2, 5] average = (2+5)/2 = 3.5 11 | [3, 5] average = (3+5)/2 = 4 12 | [2, 3, 5] average = (2+3+5)/3 = 3.33 13 | 14 | Sum of average of all subset is, 15 | 2 + 3 + 5 + 2.5 + 3.5 + 4 + 3.33 = 23.33 16 | ``` 17 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Count Strictly Increasing Subarrays/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of integers, count number of subarrays (of size more than one) that are strictly increasing. 2 | ``` 3 | Expected Time Complexity : O(n) 4 | Expected Extra Space: O(1) 5 | ``` 6 | 7 | ``` 8 | Input: arr[] = {1, 4, 3} 9 | Output: 1 10 | There is only one subarray {1, 4} 11 | 12 | Input: arr[] = {1, 2, 3, 4} 13 | Output: 6 14 | There are 6 subarrays {1, 2}, {1, 2, 3}, {1, 2, 3, 4} 15 | {2, 3}, {2, 3, 4} and {3, 4} 16 | 17 | Input: arr[] = {1, 2, 2, 4} 18 | Output: 2 19 | There are 2 subarrays {1, 2} and {2, 4} 20 | ``` 21 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Check If All Leaves are At Same Level/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a Binary Tree, check if all leaves are at same level or not. 2 | ``` 3 | 4 | 12 5 | / \ 6 | 5 7 7 | / \ 8 | 3 1 9 | Leaves are at same level 10 | 11 | 12 12 | / \ 13 | 5 7 14 | / 15 | 3 16 | Leaves are Not at same level 17 | 18 | 19 | 12 20 | / 21 | 5 22 | / \ 23 | 3 9 24 | / / 25 | 1 2 26 | Leaves are at same level 27 | ``` 28 | -------------------------------------------------------------------------------- /Wildcard Pattern Matching/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a text and a wildcard pattern, implement wildcard pattern matching algorithm that finds if wildcard pattern is matched with text. The matching should cover the entire text (not partial text). 2 | 3 | The wildcard pattern can include the characters ‘?’ and ‘*’ 4 | - ‘?’ – matches any single character 5 | - ‘*’ – Matches any sequence of characters (including the empty sequence) 6 | 7 | ``` 8 | Text = "baaabab", 9 | Pattern = “*****ba*****ab", output : true 10 | Pattern = "baaa?ab", output : true 11 | Pattern = "ba*a?", output : true 12 | Pattern = "a*ab", output : false 13 | ``` 14 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Check if An Array is Sorted and Rotated/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of N distinct integers. The task is to write a program to check if this array is sorted and rotated. A sorted array is not considered as sorted and rotated, i.e., there should at least one rotation. 2 | ``` 3 | Input : arr[] = { 3, 4, 5, 1, 2 } 4 | Output : YES 5 | The above array is sorted and rotated. 6 | Sorted array: {1, 2, 3, 4, 5}. 7 | Rotating this sorted array clockwise 8 | by 3 positions, we get: { 3, 4, 5, 1, 2} 9 | 10 | Input: arr[] = {7, 9, 11, 12, 5} 11 | Output: YES 12 | 13 | Input: arr[] = {1, 2, 3} 14 | Output: NO 15 | ``` 16 | -------------------------------------------------------------------------------- /Maximum product of an increasing subsequence/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of numbers, find the maximum product formed by multiplying numbers of an increasing subsequence of that array. 2 | 3 | ### Input : arr[] = { 3, 100, 4, 5, 150, 6 } 4 | ### Output : 45000 5 | ### Maximum product is 45000 formed by the 6 | ### increasing subsequence 3, 100, 150. Note 7 | ### that the longest increasing subsequence 8 | ### is different {3, 4, 5, 6} 9 | 10 | ### Input : arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 } 11 | ### Output : 21780000 12 | ### Maximum product is 21780000 formed by the 13 | ### increasing subsequence 10, 22, 33, 50, 60. 14 | -------------------------------------------------------------------------------- /Majority Element/readMe.md: -------------------------------------------------------------------------------- 1 | ## Basically, we need to write a function say isMajority() that takes an array (arr[] ), array’s size (n) and a number to be searched (x) as parameters and returns true if x is a majority element (present more than n/2 times). 2 | 3 | ``` 4 | Input: arr[] = {1, 2, 3, 3, 3, 3, 10}, x = 3 5 | Output: True (x appears more than n/2 times in the given array) 6 | 7 | Input: arr[] = {1, 1, 2, 4, 4, 4, 6, 6}, x = 4 8 | Output: False (x doesn't appear more than n/2 times in the given array) 9 | 10 | Input: arr[] = {1, 1, 1, 2, 2}, x = 1 11 | Output: True (x appears more than n/2 times in the given array) 12 | ``` 13 | -------------------------------------------------------------------------------- /Coin Change/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a value N, if we want to make change for N cents, and we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins, how many ways can we make the change? The order of coins doesn’t matter. 2 | ### For example, for N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}. So output should be 4. For N = 10 and S = {2, 5, 3, 6}, there are five solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. So the output should be 5. 3 | 4 | # Method 1 - Using recursion 5 | # Method 2 - Using DP [ Auxiliary Space - O(m*n) ] 6 | # Method 3 - Using Optimization in DP [ Auxiliary Space - O(n) ] 7 | -------------------------------------------------------------------------------- /Maximum Product Cutting/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a rope of length n meters, cut the rope in different parts of integer lengths in a way that maximizes product of lengths of all parts. You must make at least one cut. Assume that the length of rope is more than 2 meters. 2 | ``` 3 | Input: n = 2 4 | Output: 1 (Maximum obtainable product is 1*1) 5 | 6 | Input: n = 3 7 | Output: 2 (Maximum obtainable product is 1*2) 8 | 9 | Input: n = 4 10 | Output: 4 (Maximum obtainable product is 2*2) 11 | 12 | Input: n = 5 13 | Output: 6 (Maximum obtainable product is 2*3) 14 | 15 | Input: n = 10 16 | Output: 36 (Maximum obtainable product is 3*3*4) 17 | ``` 18 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Construct A Tree From Inorder and Level Order/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is an example to illustrate the problem. 2 | 3 | ``` 4 | Input: Two arrays that represent Inorder 5 | and level order traversals of a 6 | Binary Tree 7 | in[] = {4, 8, 10, 12, 14, 20, 22}; 8 | level[] = {20, 8, 22, 4, 12, 10, 14}; 9 | 10 | Output: Construct the tree represented 11 | by the two arrays. 12 | For the above two arrays, the 13 | constructed tree is shown in 14 | the diagram on right side 15 | ``` 16 | -------------------------------------------------------------------------------- /Friends Pairing Problem/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Algorithm 3 | // 4 | // Created by Mohd Shoaib Rayeen on 23/11/18. 5 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 6 | // 7 | 8 | 9 | #include 10 | using namespace std; 11 | 12 | int countFriendsPairings(int n) { 13 | if ( n <= 2 ) { 14 | return n; 15 | } 16 | return countFriendsPairings(n-1) + (n-1)*countFriendsPairings(n-2); 17 | } 18 | 19 | 20 | int main() { 21 | int n; 22 | cout << "\nEnter Number of People\t\t:\t"; 23 | cin >> n; 24 | cout << "Number of Different Pairs\t:\t" << countFriendsPairings(n) << endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Rat in A Maze/readMe.md: -------------------------------------------------------------------------------- 1 | ## A Maze is given as N*N binary matrix of blocks where source block is the upper left most block i.e., maze[0][0] and destination block is lower rightmost block i.e., maze[N-1][N-1]. A rat starts from source and has to reach the destination. The rat can move only in two directions: forward and down. 2 | ## In the maze matrix, 0 means the block is a dead end and 1 means the block can be used in the path from source to destination. Note that this is a simple version of the typical Maze problem. For example, a more complex version can be that the rat can move in 4 directions and a more complex version can be with a limited number of moves. 3 | -------------------------------------------------------------------------------- /Super Ugly Number/readMe.md: -------------------------------------------------------------------------------- 1 | ## Super ugly numbers are positive numbers whose all prime factors are in the given prime list. Given a number n, the task is to find n’th Super Ugly number. 2 | 3 | ## It may be assumed that given set of primes is sorted. Also, first Super Ugly number is 1 by convention. 4 | 5 | ``` 6 | Input : prime[] = [2, 5] 7 | n = 5 8 | Output : 8 9 | Super Ugly numbers with given prime factors 10 | are 1, 2, 4, 5, 8, ... 11 | Fifth Super Ugly number is 8 12 | 13 | Input : prime[] = [2, 3, 5] 14 | n = 50 15 | Output : 243 16 | 17 | Input : prime[] = [3, 5, 7, 11, 13] 18 | n = 9 19 | Output: 21 20 | ``` 21 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Graph/Depth First Search/readMe.md: -------------------------------------------------------------------------------- 1 | ## Depth First Traversal (or Search) for a graph is similar to Depth First Traversal of a tree. The only catch here is, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid processing a node more than once, we use a boolean visited array. 2 | ## For example, in the following graph, we start traversal from vertex 2. When we come to vertex 0, we look for all adjacent vertices of it. 2 is also an adjacent vertex of 0. If we don’t mark visited vertices, then 2 will be processed again and it will become a non-terminating process. A Depth First Traversal of the following graph is 2, 0, 1, 3. 3 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Convert A Tree To Sum Tree/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a Binary Tree where each node has positive and negative values. Convert this to a tree where each node contains the sum of the left and right sub trees in the original tree. The values of leaf nodes are changed to 0. 2 | ``` 3 | the following tree 4 | 5 | 10 6 | / \ 7 | -2 6 8 | / \ / \ 9 | 8 -4 7 5 10 | should be changed to 11 | 12 | 20(4-2+12+6) 13 | / \ 14 | 4(8-4) 12(7+5) 15 | / \ / \ 16 | 0 0 0 0 17 | ``` 18 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Maximum Spiral Sum/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a binary tree containing n nodes. The problem is to find the maximum sum obtained when the tree is spirally traversed. In spiral traversal one by one all levels are being traversed with the root level traversed from right to left, then next level from left to right, then further next level from right to left and so on. 2 | 3 | ``` 4 | -2 5 | 6 | -3 4 7 | 8 | 5 1 -2 -1 9 | 10 | -3 2 11 | 12 | Maximum spiral sum = 4 + (-1) + (-2) + 1 + 5 = 7 13 | ``` 14 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Count Smaller Elements On Right Side/readMe.md: -------------------------------------------------------------------------------- 1 | ## Write a function to count number of smaller elements on right of each element in an array. Given an unsorted array arr[] of distinct integers, construct another array countSmaller[] such that countSmaller[i] contains count of smaller elements on right side of each element arr[i] in array. 2 | ``` 3 | Input: arr[] = {12, 1, 2, 3, 0, 11, 4} 4 | Output: countSmaller[] = {6, 1, 1, 1, 0, 1, 0} 5 | 6 | (Corner Cases) 7 | Input: arr[] = {5, 4, 3, 2, 1} 8 | Output: countSmaller[] = {4, 3, 2, 1, 0} 9 | 10 | Input: arr[] = {1, 2, 3, 4, 5} 11 | Output: countSmaller[] = {0, 0, 0, 0, 0} 12 | ``` 13 | -------------------------------------------------------------------------------- /Maximum subsequence sum/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a sequence of positive numbers, find the maximum sum that can be formed which has no three consecutive elements present. 2 | 3 | ### Examples : 4 | 5 | ### Input: arr[] = {1, 2, 3} 6 | ### Output: 5 7 | ### We can't take three of them, so answer is 8 | ### 2 + 3 = 5 9 | 10 | ### Input: arr[] = {3000, 2000, 1000, 3, 10} 11 | ### Output: 5013 12 | ### 3000 + 2000 + 3 + 10 = 5013 13 | 14 | ### Input: arr[] = {100, 1000, 100, 1000, 1} 15 | ### Output: 2101 16 | ### 100 + 1000 + 1000 + 1 = 2101 17 | 18 | ### Input: arr[] = {1, 1, 1, 1, 1} 19 | ### Output: 4 20 | 21 | ### Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8} 22 | ### Output: 27 23 | -------------------------------------------------------------------------------- /Maximum Product Cutting/code_3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_3.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | int getMaxProd(int n) { 14 | if (n == 2 || n == 3) { 15 | return (n-1); 16 | } 17 | int res = 1; 18 | while (n > 4) { 19 | n -= 3; 20 | res *= 3; 21 | } 22 | return (n * res); 23 | } 24 | 25 | int main() { 26 | int n; 27 | cout << "\nEnter n\t:\t"; 28 | cin >> n; 29 | cout << "\nMaximum Product\t:\t" << getMaxProd(n) << "\n"; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Binomial Coefficient/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int binomialCoeff(int n, int k) { 13 | if (k==0 || k==n) 14 | return 1; 15 | return binomialCoeff(n-1, k-1) + binomialCoeff(n-1, k); 16 | } 17 | 18 | 19 | int main () { 20 | cout << "\nEnter n\t:\t"; 21 | int n; 22 | cin >> n; 23 | cout << "\nEnter k\t:\t"; 24 | int k; 25 | cin >> k; 26 | cout <<"\nThe result is\t:\t" << binomialCoeff(n, k); 27 | cout << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Fibonacci numbers/code_4.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | int fib(int n){ 13 | double x = sqrt(5)/5; 14 | double termOne = x * pow((1+sqrt(5))/2, n); 15 | double termTwo = -x * pow((1-sqrt(5))/2, n); 16 | return (int)(termOne + termTwo); 17 | } 18 | 19 | int main () { 20 | cout << "\nEnter Number\t:\t"; 21 | int number; 22 | cin >> number; 23 | cout <<"\nThe result is\t:\t" << fib(number); 24 | cout << endl; 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Maximum games played by winner/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // q1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 17/10/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | #include 9 | using namespace std; 10 | 11 | int maxGameByWinner(int N) { 12 | int dp[N]; 13 | dp[0] = 1; 14 | dp[1] = 2; 15 | int i = 2; 16 | do { 17 | dp[i] = dp[i - 1] + dp[i - 2]; 18 | } while (dp[i++] <= N); 19 | 20 | return (i - 2); 21 | } 22 | 23 | int main() { 24 | int N; 25 | cout << "\nEnter N\t:\t"; 26 | cin >> N; 27 | cout << maxGameByWinner(N) << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Min-Max Range Queries in Array/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array arr[0 . . . n-1]. We need to efficiently find the minimum and maximum value from index qs (query start) to qe (query end) where 0 <= qs <= qe <= n-1. We are given multiple queries. 2 | 3 | ``` 4 | Input : arr[] = {1, 8, 5, 9, 6, 14, 2, 4, 3, 7} 5 | queries = 5 6 | qs = 0 qe = 4 7 | qs = 3 qe = 7 8 | qs = 1 qe = 6 9 | qs = 2 qe = 5 10 | qs = 0 qe = 8 11 | Output: Minimum = 1 and Maximum = 9 12 | Minimum = 2 and Maximum = 14 13 | Minimum = 2 and Maximum = 14 14 | Minimum = 5 and Maximum = 14 15 | Minimum = 1 and Maximum = 14 16 | ``` 17 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Positive Elements at Even and Negative at Odd Position/readMe.md: -------------------------------------------------------------------------------- 1 | ## You have been given an array and you have to make a program to convert that array such that positive elements occur at even numbered places in the array and negative elements occur at odd numbered places in the array. We have to do it in place. 2 | 3 | ## There can be unequal number of positive and negative values and the extra values have to left as it is. 4 | 5 | ``` 6 | Input : arr[] = {1, -3, 5, 6, -3, 6, 7, -4, 9, 10} 7 | Output : arr[] = {1, -3, 5, -3, 6, 6, 7, -4, 9, 10} 8 | 9 | Input : arr[] = {-1, 3, -5, 6, 3, 6, -7, -4, -9, 10} 10 | Output : arr[] = {3, -1, 6, -5, 3, -7, 6, -4, 10, -9} 11 | ``` 12 | -------------------------------------------------------------------------------- /Newman-Shanks-Williams prime/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | 13 | int nswp(int n) { 14 | int dp[n + 1]; 15 | dp[0] = dp[1] = 1; 16 | for (int i = 2; i <= n; i++) { 17 | dp[i] = 2 * dp[i - 1] + dp[i - 2]; 18 | } 19 | return dp[n]; 20 | } 21 | 22 | int main () { 23 | cout << "\nEnter Number\t:\t"; 24 | int number; 25 | cin >> number; 26 | cout <<"\nThe result is\t:\t" << nswp(number); 27 | cout << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Stolen Values Problem/readMe.md: -------------------------------------------------------------------------------- 1 | ## There are n houses built in a line, each of which contain some value in it. A thief is going to steal in these houses. But he cannot steal in two adjacent houses. What is maximum value he can steal? 2 | 3 | ## Approach - > Maximum stolen value from first i houses of the line can be either the maximum stolen value from first i-1 houses of the line or maximum stolen value from i-2 houses of the line plus value in ith house. So, we will choose maximum of these. We will calculate the values in bottom up manner. 4 | 5 | ``` 6 | number of houses=7 7 | values[]=9, 3, 5, 8, 2, 4, 7 8 | maximum stolen value=24 by stealing from first, fourth and seventh house 9 | ``` 10 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Search An Element in Sorted and Rotated Array/readMe.md: -------------------------------------------------------------------------------- 1 | ## An element in a sorted array can be found in O(log n) time via binary search. But suppose we rotate an ascending order sorted array at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. Devise a way to find an element in the rotated array in O(log n) time. 2 | 3 | ``` 4 | Input : arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 3}; 5 | key = 3 6 | Output : Found at index 8 7 | 8 | Input : arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 3}; 9 | key = 30 10 | Output : Not found 11 | 12 | Input : arr[] = {30, 40, 50, 10, 20} 13 | key = 10 14 | Output : Found at index 3 15 | ``` 16 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Find Largest Subtree Sum/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a binary tree, task is to find subtree with maximum sum in tree. 2 | 3 | ``` 4 | Input : 1 5 | / \ 6 | 2 3 7 | / \ / \ 8 | 4 5 6 7 9 | Output : 28 10 | As all the tree elements are positive, 11 | the largest subtree sum is equal to 12 | sum of all tree elements. 13 | 14 | Input : 1 15 | / \ 16 | -2 3 17 | / \ / \ 18 | 4 5 -6 2 19 | Output : 7 20 | Subtree with largest sum is : -2 21 | / \ 22 | 4 5 23 | Also, entire tree sum is also 7. 24 | ``` 25 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Vertical Sum In Binary Tree/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a Binary Tree, find vertical sum of the nodes that are in same vertical line. Print all sums through different vertical lines. 2 | 3 | ``` 4 | 1 5 | / \ 6 | 2 3 7 | / \ / \ 8 | 4 5 6 7 9 | The tree has 5 vertical lines 10 | 11 | Vertical-Line-1 has only one node 4 => vertical sum is 4 12 | Vertical-Line-2: has only one node 2=> vertical sum is 2 13 | Vertical-Line-3: has three nodes: 1,5,6 => vertical sum is 1+5+6 = 12 14 | Vertical-Line-4: has only one node 3 => vertical sum is 3 15 | Vertical-Line-5: has only one node 7 => vertical sum is 7 16 | 17 | So expected output is 4, 2, 12, 3 and 7 18 | 19 | ``` 20 | -------------------------------------------------------------------------------- /K-th Element of Two Sorted Arrays/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given two sorted arrays of size m and n respectively, you are tasked with finding the element that would be at the k’th position of the final sorted array. 2 | 3 | ``` 4 | Input : Array 1 - 2 3 6 7 9 5 | Array 2 - 1 4 8 10 6 | k = 5 7 | Output : 6 8 | Explanation: The final sorted array would be - 9 | 1, 2, 3, 4, 6, 7, 8, 9, 10 10 | The 5th element of this array is 6. 11 | Input : Array 1 - 100 112 256 349 770 12 | Array 2 - 72 86 113 119 265 445 892 13 | k = 7 14 | Output : 256 15 | Explanation: Final sorted array is - 16 | 72, 86, 100, 112, 113, 119, 256, 265, 349, 445, 770, 892 17 | 7th element of this array is 256. 18 | ``` 19 | -------------------------------------------------------------------------------- /Nth Catalan Number/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | unsigned long int catalan(unsigned int n) { 13 | if (n <= 1) return 1; 14 | unsigned long int res = 0; 15 | for (int i=0; i> number; 24 | cout <<"\nThe result is\t:\t" << catalan(number); 25 | cout << endl; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Array Range Queries for Searching An Element/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of N elements and Q queries of the form L R X. For each query, you have to output if the element X exists in the array between the indices L and R(included). 2 | 3 | - Prerequisite : Mo’s Algorithms 4 | 5 | ``` 6 | Input : N = 5 7 | arr = [1, 1, 5, 4, 5] 8 | Q = 3 9 | 1 3 2 10 | 2 5 1 11 | 3 5 5 12 | Output : No 13 | Yes 14 | Yes 15 | Explanation : 16 | For the first query, 2 does not exist between the indices 1 and 3. 17 | For the second query, 1 exists between the indices 2 and 5. 18 | For the third query, 5 exists between the indices 3 and 5. 19 | ``` 20 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Range LCM Queries/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of integers, evaluate queries of the form LCM(l, r). There might be many queries, hence evaluate the queries efficiently. 2 | ``` 3 | LCM (l, r) denotes the LCM of array elements 4 | that lie between the index l and r 5 | (inclusive of both indices) 6 | 7 | Mathematically, 8 | LCM(l, r) = LCM(arr[l], arr[l+1] , ......... , arr[r-1], arr[r]) 9 | ``` 10 | ``` 11 | Inputs : Array = {5, 7, 5, 2, 10, 12 ,11, 17, 14, 1, 44} 12 | Queries: LCM(2, 5), LCM(5, 10), LCM(0, 10) 13 | Outputs: 60 15708 78540 14 | Explanation : In the first query LCM(5, 2, 10, 12) = 60, 15 | similarly in other queries. 16 | ``` 17 | -------------------------------------------------------------------------------- /Golomb Sequence/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_2.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | void printGolomb(int n) { 14 | cout << "\nGolomb Sequence\t:\t"; 15 | int dp[n + 1]; 16 | dp[1] = 1; 17 | cout << dp[1] << " "; 18 | for (int i = 2; i <= n; i++) { 19 | dp[i] = 1 + dp[i - dp[dp[i - 1]]]; 20 | cout << dp[i] << " "; 21 | } 22 | cout << endl; 23 | } 24 | 25 | int main() { 26 | int n; 27 | cout << "\nEnter N\t\t\t:\t"; 28 | cin >> n; 29 | printGolomb(n); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Golomb Sequence/readMe.md: -------------------------------------------------------------------------------- 1 | ## In mathematics, the Golomb sequence is a non-decreasing integer sequence where n-th term is equal to number of times n appears in the sequence. 2 | ``` 3 | The first few values are 4 | 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, …… 5 | 6 | Explanation of few terms: 7 | Third term is 2, note that three appears 2 times. 8 | Second term is 2, note that two appears 2 times. 9 | Fourth term is 3, note that four appears 3 times. 10 | ``` 11 | 12 | ``` 13 | Input : n = 4 14 | Output : 1 2 2 3 15 | 16 | Input : n = 6 17 | Output : 1 2 2 3 3 4 18 | ``` 19 | 20 | 21 | ``` 22 | The recurrence relation to find the nth term of Golomb sequence: 23 | a(1) = 1 24 | a(n + 1) = 1 + a(n + 1 – a(a(n))) 25 | ``` 26 | -------------------------------------------------------------------------------- /Find Bitonic Point in Given Bitonic Sequence/readMe.md: -------------------------------------------------------------------------------- 1 | ## You are given a bitonic sequence, the task is to find Bitonic Point in it. A Bitonic Sequence is a sequence of numbers which is first strictly increasing then after a point strictly decreasing. 2 | 3 | ## A Bitonic Point is a point in bitonic sequence before which elements are strictly increasing and after which elements are strictly decreasing. A Bitonic point doesn’t exist if array is only decreasing or only increasing. 4 | 5 | ``` 6 | Input : arr[] = {6, 7, 8, 11, 9, 5, 2, 1} 7 | Output: 11 8 | All elements before 11 are smaller and all 9 | elements after 11 are greater. 10 | 11 | Input : arr[] = {-3, -2, 4, 6, 10, 8, 7, 1} 12 | Output: 10 13 | 14 | ``` 15 | -------------------------------------------------------------------------------- /Count Ways to Reach the Nth Stair Using Step 1, 2 or 3/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_2.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int countAllWays(int n) { 13 | int DP[n+1]; 14 | DP[0] = DP[1] = 1; 15 | DP[2] = 2; 16 | for (int i = 3; i <= n; i++) { 17 | DP[i] = DP[i-1] + DP[i-2] + DP[i-3]; 18 | } 19 | return DP[n]; 20 | } 21 | 22 | int main() { 23 | int n; 24 | cout << "\nEnter Number\t:\t"; 25 | cin >> n; 26 | cout << "\nTotal Number of Ways\t:\t " << countAllWays(n) << "\n"; 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Eulerian Number/readMe.md: -------------------------------------------------------------------------------- 1 | ## In combinatorics, the Eulerian Number E(n, m), is the number of permutations of the numbers 1 to n in which exactly m elements are greater than previous element. 2 | 3 | 4 | 5 | ## Recurrence Relation 6 | ``` 7 | E(m,n) = 0 if(m >= n || n == 0) 8 | E(m,n) = 1 if (m == 0) 9 | E(m,n) = (n - m) * E(n - 1, m - 1) + m + 1) * E(n - 1, m) otherwise 10 | ``` 11 | 12 | 13 | ``` 14 | Input : n = 3, m = 1 15 | Output : 4 16 | Please see above diagram (There 17 | are 4 permutations where 1 no. is 18 | greater. 19 | 20 | Input : n = 4, m = 1 21 | Output : 11 22 | ``` 23 | -------------------------------------------------------------------------------- /Count Ways to Build Street Under Given Constraints/readMe.md: -------------------------------------------------------------------------------- 1 | ## There is a street of length n and as we know it has two sides. Therefore a total of 2 * n spots are available. In each of these spots either a house or an office can be built with following 2 restrictions: 2 | - No two offices on the same side of the street can be adjacent. 3 | - No two offices on different sides of the street can be exactly opposite to each other i.e. they can’t overlook each other. 4 | ## There are no restrictions on building houses and each spot must either have a house or office. 5 | ## Given length of the street n, find total number of ways to build the street. 6 | 7 | ``` 8 | Input : 2 9 | Output : 7 10 | Input : 3 11 | Output : 17 12 | ``` 13 | -------------------------------------------------------------------------------- /Eulerian Number/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 13/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int eulerian(int n, int m) { 13 | if (m >= n || n == 0) { 14 | return 0; 15 | } 16 | if (m == 0) { 17 | return 1; 18 | } 19 | return (n - m) * eulerian(n - 1, m - 1) + (m + 1) * eulerian(n - 1, m); 20 | } 21 | 22 | int main() { 23 | int n , m; 24 | cout << "\nEnter n\t:\t"; 25 | cin >> n; 26 | cout << "\nEnter m\t:\t"; 27 | cin >> m; 28 | cout << eulerian(n, m) << endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Boggle/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a dictionary, a method to do lookup in dictionary and a M x N board where every cell has one character. Find all possible words that can be formed by a sequence of adjacent characters. Note that we can move to any of 8 adjacent characters, but a word should not have multiple instances of same cell. 2 | 3 | ``` 4 | Input: dictionary[] = {"GEEKS", "FOR", "QUIZ", "GO"}; 5 | boggle[][] = {{'G','I','Z'}, 6 | {'U','E','K'}, 7 | {'Q','S','E'}}; 8 | isWord(str): returns true if str is present in dictionary 9 | else false. 10 | 11 | Output: Following words of the dictionary are present 12 | GEEKS 13 | QUIZ 14 | ``` 15 | -------------------------------------------------------------------------------- /Permutation Coefficient/code_3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int permutationCoeff(int n, int k) { 13 | int res = 1; 14 | while ( k > 0 ) { 15 | res *= n; 16 | --n; 17 | --k; 18 | } 19 | return res; 20 | } 21 | 22 | 23 | int main () { 24 | cout << "\nEnter n\t:\t"; 25 | int number; 26 | cin >> n; 27 | cout << "\nEnter k\t:\t"; 28 | int k; 29 | cin >> k; 30 | cout <<"\nThe result is\t:\t" << permutationCoeff(n,k); 31 | cout << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Tiling with Dominoes/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a 3 x n board, find the number of ways to fill it with 2 x 1 dominoes. 2 | ``` 3 | Input : 2 4 | Output : 3 5 | 6 | Input : 8 7 | Output : 153 8 | 9 | Input : 12 10 | Output : 2131 11 | ``` 12 | 13 | 14 | ``` 15 | An = No. of ways to completely fill a 3 x n board. (We need to find this) 16 | Bn = No. of ways to fill a 3 x n board with top corner in last column not filled. 17 | Cn = No. of ways to fill a 3 x n board with bottom corner in last column not filled. 18 | ``` 19 | 20 | 21 | 22 | ``` 23 | Final Recursive Relations are: 24 | 25 | A[n] = A[n-2] + 2*(B[n-1]) 26 | B[n] = A[n-1] + B[n-2] 27 | Base Cases: 28 | 29 | A[0]= 1 A[1] = 0 30 | B[0]= 0 B[1] = 1 31 | ``` 32 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Maximize Array Sum After K Negations/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of size n and a number k. We must modify array K number of times. Here modify array means in each operation we can replace any array element arr[i] by -arr[i]. We need to perform this operation in such a way that after K operations, sum of array must be maximum? 2 | ``` 3 | 4 | Input : arr[] = {-2, 0, 5, -1, 2} 5 | K = 4 6 | Output: 10 7 | // Replace (-2) by -(-2), array becomes {2, 0, 5, -1, 2} 8 | // Replace (-1) by -(-1), array becomes {2, 0, 5, 1, 2} 9 | // Replace (0) by -(0), array becomes {2, 0, 5, 1, 2} 10 | // Replace (0) by -(0), array becomes {2, 0, 5, 1, 2} 11 | 12 | Input : arr[] = {9, 8, 8, 5} 13 | K = 3 14 | Output: 20 15 | ``` 16 | -------------------------------------------------------------------------------- /Fibonacci numbers/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | //This will be done using bottom-up approach 9 | #include 10 | using namespace std; 11 | int fib(int num) { 12 | int temp[num+2]; 13 | int i; 14 | temp[0] = 0; 15 | temp[1] = 1; 16 | for (i = 2; i <= num; i++) { 17 | temp[i] = temp[i-1] + temp[i-2]; 18 | } 19 | 20 | return temp[num]; 21 | } 22 | 23 | int main () { 24 | cout << "\nEnter Number\t:\t"; 25 | int number; 26 | cin >> number; 27 | cout <<"\nThe result is\t:\t" << fib(number); 28 | cout << endl; 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Count of Different Ways to Express N As The Sum of 1, 3 And 4/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int countAllWays(int n) { 13 | int DP[n + 1]; 14 | DP[0] = DP[1] = DP[2] = 1; 15 | DP[3] = 2; 16 | for (int i = 4; i <= n; i++) { 17 | DP[i] = DP[i - 1] + DP[i - 3] + DP[i - 4]; 18 | } 19 | return DP[n]; 20 | } 21 | 22 | int main() { 23 | int n; 24 | cout << "\nEnter Number\t:\t"; 25 | cin >> n; 26 | cout << "\nTotal Number of Ways\t:\t " << countAllWays(n) << "\n"; 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Fibonacci numbers/code_3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int fib(int num) { 13 | int prev = 0 , res = 1 , next;; 14 | if( num <= 0) { 15 | return 0; 16 | } 17 | for (int i = 2; i <= num; i++) { 18 | next = prev + res; 19 | prev = res; 20 | res = next; 21 | } 22 | return res; 23 | } 24 | 25 | int main () { 26 | cout << "\nEnter Number\t:\t"; 27 | int number; 28 | cin >> number; 29 | cout <<"\nThe result is\t:\t" << fib(number); 30 | cout << endl; 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Maximum sum of pairs with specific difference/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of integers and a number k. We can pair two number of array if difference between them is strictly less than k. The task is to find maximum possible sum of disjoint pairs. Sum of P pairs is sum of all 2P numbers of pairs. 2 | 3 | > Examples: 4 | > Input : arr[] = {3, 5, 10, 15, 17, 12, 9}, K = 4 5 | Output : 62 6 | Then disjoint pairs with difference less than K are, 7 | (3, 5), (10, 12), (15, 17) 8 | So maximum sum which we can get is 3 + 5 + 12 + 10 + 15 + 17 = 62 9 | Note that an alternate way to form disjoint pairs is, 10 | (3, 5), (9, 12), (15, 17), but this pairing produces lesser sum. 11 | 12 | > Input : arr[] = {5, 15, 10, 300}, k = 12 13 | Output : 25 14 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Diagonal Sum of A Binary Tree/readMe.md: -------------------------------------------------------------------------------- 1 | ## Consider lines of slope -1 passing between nodes (dotted lines in below diagram). Diagonal sum in a binary tree is sum of all node’s data lying between these lines. Given a Binary Tree, print all diagonal sums. 2 | 3 | For the following input tree, output should be 9, 19, 42. 4 | ``` 5 | 1 6 | 7 | 2 3 8 | 9 | 9 6 4 5 10 | 11 | 10 11 12 7 12 | 13 | 14 | 9 is sum of 1, 3 and 5. 15 | 19 is sum of 2, 6, 4 and 7. 16 | 42 is sum of 9, 10, 11 and 12. 17 | ``` 18 | -------------------------------------------------------------------------------- /Tiling with Dominoes/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | 14 | int countWays(int n) { 15 | int A[n + 1], B[n + 1]; 16 | A[0] = 1; 17 | A[1] = 0; 18 | B[0] = 0; 19 | B[1] = 1; 20 | for (int i = 2; i <= n; i++) { 21 | A[i] = A[i - 2] + 2 * B[i - 1]; 22 | B[i] = A[i - 1] + B[i - 2]; 23 | } 24 | 25 | return A[n]; 26 | } 27 | 28 | int main() { 29 | int n; 30 | cout << "\nEnter N\t:\t"; 31 | cin >> n; 32 | cout << "\nNumber of Ways\t:\t" << countWays(n) << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Weighted Job Scheduling/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given N jobs where every job is represented by following three elements of it. 2 | 3 | - Start Time 4 | - Finish Time 5 | - Profit or Value Associated 6 | ### Find the maximum profit subset of jobs such that no two jobs in the subset overlap. 7 | 8 | ``` 9 | Input: Number of Jobs n = 4 10 | Job Details {Start Time, Finish Time, Profit} 11 | Job 1: {1, 2, 50} 12 | Job 2: {3, 5, 20} 13 | Job 3: {6, 19, 100} 14 | Job 4: {2, 100, 200} 15 | Output: The maximum profit is 250. 16 | We can get the maximum profit by scheduling jobs 1 and 4. 17 | Note that there is longer schedules possible Jobs 1, 2 and 3 18 | but the profit with this schedule is 20+50+100 which is less than 250. 19 | ``` 20 | -------------------------------------------------------------------------------- /Combinational Sum/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array of positive integers arr[] and a sum x, find all unique combinations in arr[] where the sum is equal to x. The same repeated number may be chosen from arr[] unlimited number of times. Elements in a combination (a1, a2, …, ak) must be printed in non-descending order. (ie, a1 <= a2 <= … <= ak). 2 | ## The combinations themselves must be sorted in ascending order, i.e., the combination with smallest first element should be printed first. If there is no combination possible the print "Empty" (without quotes). 3 | 4 | Examples: 5 | Input : arr[] = { 2, 4, 6, 8 } 6 | sum = 8 7 | Output : [2, 2, 2, 2] 8 | [2, 2, 4] 9 | [2, 6] 10 | [4, 4] 11 | [8] 12 | -------------------------------------------------------------------------------- /Compute nCr mod p/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int binomialCoeff(int n, int k) { 13 | if (k==0 || k==n) 14 | return 1; 15 | return binomialCoeff(n-1, k-1) + binomialCoeff(n-1, k); 16 | } 17 | 18 | 19 | int main () { 20 | cout << "\nEnter n\t:\t"; 21 | int n; 22 | cin >> n; 23 | cout << "\nEnter k\t:\t"; 24 | int k; 25 | cin >> k; 26 | cout << "\nEnter p\t:\t"; 27 | int p; 28 | cin >> p; 29 | cout <<"\nThe result is\t:\t" << binomialCoeff(n,k)%p; 30 | cout << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Golomb Sequence/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | int findGolomb(int n) { 14 | if (n == 1) { 15 | return 1; 16 | } 17 | return 1 + findGolomb(n - findGolomb(findGolomb(n - 1))); 18 | } 19 | 20 | void printGolomb(int n) { 21 | cout << "\nGolomb Sequence\t:\t"; 22 | for (int i = 1; i <= n; i++) { 23 | cout << findGolomb(i) << " "; 24 | } 25 | cout << endl; 26 | } 27 | 28 | int main() { 29 | int n; 30 | cout << "\nEnter N\t\t\t:\t"; 31 | cin >> n; 32 | printGolomb(n); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Tiling Problem/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int tiling_problem(int num) { 13 | int prev = 0 , res = 1 , next;; 14 | if( num <= 2) { 15 | return n; 16 | } 17 | for (int i = 3; i <= num; i++) { 18 | next = prev + res; 19 | prev = res; 20 | res = next; 21 | } 22 | return res; 23 | } 24 | 25 | int main () { 26 | cout << "\nEnter Number\t:\t"; 27 | int number; 28 | cin >> number; 29 | cout <<"\nThe result is\t:\t" << tiling_problem(number); 30 | cout << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Bell Numbers/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int bellNumber(int n) { 13 | int bell[n+1][n+1]; 14 | bell[0][0] = 1; 15 | for (int i=1; i<=n; i++) { 16 | bell[i][0] = bell[i-1][i-1]; 17 | for (int j=1; j<=i; j++) 18 | bell[i][j] = bell[i-1][j-1] + bell[i][j-1]; 19 | } 20 | return bell[n][0]; 21 | } 22 | int main () { 23 | cout << "\nEnter Number\t:\t"; 24 | unsigned int number; 25 | cin >> number; 26 | cout <<"\nThe result is\t:\t" << bellNumber(number); 27 | cout << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Count Ways to Reach the Nth Stair Using Step 1, 2 or 3/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int countAllWays(int n) { 13 | if (n == 1 || n == 0) { 14 | return 1; 15 | } 16 | else if (n == 2) { 17 | return 2; 18 | } 19 | else { 20 | return countAllWays(n - 3) + countAllWays(n - 2) + countAllWays(n - 1); 21 | } 22 | } 23 | 24 | int main() { 25 | int n; 26 | cout << "\nEnter Number\t:\t"; 27 | cin >> n; 28 | cout << "\nTotal Number of Ways\t:\t " << countAllWays(n) << "\n"; 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Permutation Coefficient/code_4.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int permutationCoeff(int n, int k) { 13 | int fact[n + 1]; 14 | fact[0] = 1; 15 | for (int i = 1; i <= n; i++) { 16 | fact[i] = i * fact[i - 1]; 17 | } 18 | return fact[n] / fact[n - k]; 19 | } 20 | 21 | int main () { 22 | cout << "\nEnter n\t:\t"; 23 | int number; 24 | cin >> n; 25 | cout << "\nEnter k\t:\t"; 26 | int k; 27 | cin >> k; 28 | cout <<"\nThe result is\t:\t" << permutationCoeff(n,k); 29 | cout << endl; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Graph/Breadth First Search/readMe.md: -------------------------------------------------------------------------------- 1 | ## Breadth First Traversal (or Search) for a graph is similar to Breadth First Traversal of a tree (See method 2 of this post). The only catch here is, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid processing a node more than once, we use a boolean visited array. For simplicity, it is assumed that all vertices are reachable from the starting vertex. 2 | ## For example, in the following graph, we start traversal from vertex 2. When we come to vertex 0, we look for all adjacent vertices of it. 2 is also an adjacent vertex of 0. If we don’t mark visited vertices, then 2 will be processed again and it will become a non-terminating process. A Breadth First Traversal of the following graph is 2, 0, 3, 1. 3 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Check Whether A Given Binary Tree is Perfect or Not/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a Binary Tree, write a function to check whether the given Binary Tree is a prefect Binary Tree or not. 2 | 3 | ## A Binary tree is Perfect Binary Tree in which all internal nodes have two children and all leaves are at same level. 4 | 5 | ``` 6 | 7 | The following tree is a perfect binary tree 8 | 9 | 10 10 | / \ 11 | 20 30 12 | / \ / \ 13 | 40 50 60 70 14 | 15 | 16 | 18 17 | / \ 18 | 15 30 19 | The following tree is not a perfect binary tree 20 | 21 | 1 22 | / \ 23 | 2 3 24 | \ / \ 25 | 4 5 6 26 | ``` 27 | -------------------------------------------------------------------------------- /Maximize the sum of index by muliplying the element at the index/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // q4.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 28/10/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | 8 | // array[i]*i 9 | 10 | #include 11 | using namespace std; 12 | 13 | int maxSum(int arr[], int n) { 14 | sort(arr, arr + n); 15 | int sum = 0; 16 | for (int i = 0; i < n; i++) { 17 | sum += (arr[i]*i); 18 | } 19 | return sum; 20 | } 21 | 22 | int main() { 23 | cout << "\nEnter Size\t:\t"; 24 | int n; 25 | cin >> n; 26 | int a[n]; 27 | for ( int i = 0; i < n; i++ ) { 28 | cin >> a[i]; 29 | } 30 | cout << "\nResult\t:\t" << maxSum(a, n) << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Permutation Coefficient/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int permutationCoeff(int n, int k) { 13 | if ( k == 0) { 14 | return 1; 15 | } 16 | else if ( k == 1 ) { 17 | return n; 18 | } 19 | else { 20 | return n*permutationCoeff(n-1,k-1); 21 | } 22 | } 23 | 24 | 25 | int main () { 26 | cout << "\nEnter n\t:\t"; 27 | int number; 28 | cin >> n; 29 | cout << "\nEnter k\t:\t"; 30 | int k; 31 | cin >> k; 32 | cout <<"\nThe result is\t:\t" << permutationCoeff(n,k); 33 | cout << endl; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Level Order Traversal in Spiral Form/readMe.md: -------------------------------------------------------------------------------- 1 | ## Write a function to print spiral order traversal of a tree. 2 | 3 | ## Recursive 4 | ``` 5 | printSpiral(tree) 6 | bool ltr = 0; 7 | for d = 1 to height(tree) 8 | printGivenLevel(tree, d, ltr); 9 | ltr ~= ltr /*flip ltr*/ 10 | Function to print all nodes at a given level 11 | 12 | printGivenLevel(tree, level, ltr) 13 | if tree is NULL then return; 14 | if level is 1, then 15 | print(tree->data); 16 | else if level greater than 1, then 17 | if(ltr) 18 | printGivenLevel(tree->left, level-1, ltr); 19 | printGivenLevel(tree->right, level-1, ltr); 20 | else 21 | printGivenLevel(tree->right, level-1, ltr); 22 | printGivenLevel(tree->left, level-1, ltr); 23 | ``` 24 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Merge Two Binary Trees/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given two binary trees. We need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the non-null node will be used as the node of new tree. 2 | 3 | ``` 4 | Input: 5 | Tree 1 Tree 2 6 | 2 3 7 | / \ / \ 8 | 1 4 6 1 9 | / \ \ 10 | 5 2 7 11 | 12 | Output: Merged tree: 13 | 5 14 | / \ 15 | 7 5 16 | / \ \ 17 | 5 2 7 18 | ``` 19 | -------------------------------------------------------------------------------- /Count Ways to Build Street Under Given Constraints/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | long countAllWays(int n) { 13 | long dp[2][n + 1]; 14 | dp[0][1] = 1; 15 | dp[1][1] = 2; 16 | for (int i = 2; i <= n; i++) { 17 | dp[0][i] = dp[0][i - 1] + dp[1][i - 1]; 18 | dp[1][i] = dp[0][i - 1] * 2 + dp[1][i - 1]; 19 | } 20 | return dp[0][n] + dp[1][n]; 21 | } 22 | 23 | int main() { 24 | int n; 25 | cout << "\nEnter Number\t:\t"; 26 | cin >> n; 27 | cout << "\nTotal Number of Ways\t:\t " << countAllWays(n) << "\n"; 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Count Ways to Reach the Nth Stair Using Step 1, 2 or 3/code_3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_3.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int countAllWays(int n) { 13 | int DP[4]; 14 | DP[0] = DP[1] = 1; 15 | DP[2] = 2; 16 | for (int i = 3; i <= n; i++) { 17 | DP[3] = DP[2] + DP[1] + DP[0]; 18 | DP[0] = DP[1]; 19 | DP[1] = DP[2]; 20 | DP[2] = DP[3]; 21 | } 22 | return DP[3]; 23 | } 24 | 25 | int main() { 26 | int n; 27 | cout << "\nEnter Number\t:\t"; 28 | cin >> n; 29 | cout << "\nTotal Number of Ways\t:\t " << countAllWays(n) << "\n"; 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Find a Fixed Point in a Given Array/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | int linearSearch(int arr[], int n) { 14 | for(int i = 0; i < n; i++) { 15 | if(arr[i] == i) { 16 | return i; 17 | } 18 | } 19 | return -1; 20 | } 21 | 22 | int main() { 23 | int n; 24 | cout << "\nEnter Size\t:\t"; 25 | cin >> n; 26 | int arr[n]; 27 | cout << "\nEnter Array Elements\n"; 28 | for( int i = 0; i < n; i++ ) { 29 | cin >> arr[i]; 30 | } 31 | cout << "\nFixed Point\t:\t" << linearSearch(arr, n) << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Find the Element that appears once in a Sorted Array/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_2.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | void search(int *arr, int n) { 14 | int xorValue = arr[0]; 15 | for ( int i = 1; i < n; i++ ) { 16 | xorValue ^= arr[i]; 17 | } 18 | cout << "\nElement is\t:\t" << xorValue << endl; 19 | } 20 | 21 | int main() { 22 | int n; 23 | cout << "\nEnter Size\t:\t"; 24 | cin >> n; 25 | int arr[n]; 26 | cout << "\nEnter Array Elements\n"; 27 | for ( int i = 0; i < n; i++ ) { 28 | cin >> arr[i]; 29 | } 30 | search(arr, n); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Friends Pairing Problem/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | int countFriendsPairings(int n) { 14 | int dp[n + 1]; 15 | for (int i = 0; i <= n; i++) { 16 | if (i <= 2) { 17 | dp[i] = i; 18 | } 19 | else { 20 | dp[i] = dp[i - 1] + (i - 1) * dp[i - 2]; 21 | } 22 | } 23 | 24 | return dp[n]; 25 | } 26 | 27 | int main() { 28 | int n; 29 | cout << "\nEnter Number of People\t\t:\t"; 30 | cin >> n; 31 | cout << "Number of Different Pairs\t:\t" << countFriendsPairings(n) << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Maximum Product Cutting/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | int max(int a, int b, int c) { 14 | return max(a, max(b, c)); 15 | } 16 | 17 | int getMaxProd(int n) { 18 | if (n == 0 || n == 1) { 19 | return 0; 20 | } 21 | int max_val = 0; 22 | for (int i = 1; i < n; i++) { 23 | max_val = max(max_val, i*(n-i), getMaxProd(n-i)*i); 24 | } 25 | return max_val; 26 | } 27 | 28 | int main() { 29 | int n; 30 | cout << "\nEnter n\t:\t"; 31 | cin >> n; 32 | cout << "\nMaximum Product\t:\t" << getMaxProd(n) << "\n"; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Entringer Number/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 13/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int Entringer_Number(int n, int k) { 13 | if (n == 0 && k == 0) { 14 | return 1; 15 | } 16 | if (k == 0) { 17 | return 0; 18 | } 19 | return Entringer_Number(n, k - 1) + Entringer_Number(n - 1, n - k); 20 | } 21 | 22 | int main() { 23 | int n; 24 | cout << "\nEnter n\t:\t"; 25 | int number; 26 | cin >> n; 27 | cout << "\nEnter k\t:\t"; 28 | int k; 29 | cin >> k; 30 | cout << "\nEntringer Number\t:\t" << Entringer_Number(n, k) << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Graph Coloring Problem/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an undirected graph and a number m, determine if the graph can be colored with at most m colors such that no two adjacent vertices of the graph are colored with same color. Here coloring of a graph means assignment of colors to all vertices. 2 | 3 | Input: 4 | 1) A 2D array graph[V][V] where V is the number of vertices in graph and graph[V][V] is adjacency matrix representation of the graph. A value graph[i][j] is 1 if there is a direct edge from i to j, otherwise graph[i][j] is 0. 5 | 2) An integer m which is maximum number of colors that can be used. 6 | 7 | Output: 8 | An array color[V] that should have numbers from 1 to m. color[i] should represent the color assigned to the ith vertex. The code should also return false if the graph cannot be colored with m colors. 9 | -------------------------------------------------------------------------------- /Newman-Conway Sequence/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_2.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | 14 | 15 | void sequence(int n) { 16 | int P[n+1]; 17 | P[0] = 0; 18 | P[1] = 1; 19 | P[2] = 1; 20 | for (int i = 3; i <= n; i++) { 21 | P[i] = P[P[i - 1]] + P[i - P[i - 1]]; 22 | } 23 | 24 | cout << "\nNewman-Conway Sequence\t:\t"; 25 | for ( int i = 1; i <= n; i++ ) { 26 | cout << P[i] << " "; 27 | } 28 | cout << endl; 29 | } 30 | 31 | int main() { 32 | int n; 33 | cout << "\nEnter N\t\t\t:\t"; 34 | cin >> n; 35 | sequence(n); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Longest Palindromic Subsequence/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Algorithm 3 | // 4 | // Created by Mohd Shoaib Rayeen on 23/11/18. 5 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 6 | // 7 | 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int lps(string X, long i, long j) { 14 | if (i == j) { 15 | return 1; 16 | } 17 | if (X[i] == X[j] && i + 1 == j) { 18 | return 2; 19 | } 20 | if (X[i] == X[j]) { 21 | return lps (X, i+1, j-1) + 2; 22 | } 23 | return max( lps(X, i, j-1), lps(X, i+1, j) ); 24 | } 25 | 26 | int main() { 27 | string X; 28 | cout << "\nEnter String\t\t:\t"; 29 | getline(cin , X); 30 | cout << "\nLength of LPS\t\t:\t" << lps(X, 0, X.length()-1) << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Newman-Conway Sequence/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | int sequence(int n) { 14 | if (n == 1 || n == 2) { 15 | return 1; 16 | } 17 | else { 18 | return sequence(sequence(n - 1)) + sequence(n - sequence(n - 1)); 19 | } 20 | } 21 | 22 | void print(int n) { 23 | cout << "\nNewman-Conway Sequence\t:\t"; 24 | for ( int i = 1; i <= n; i++ ) { 25 | cout << sequence(i) << " "; 26 | } 27 | cout << endl; 28 | } 29 | int main() { 30 | int n; 31 | cout << "\nEnter N\t\t\t:\t"; 32 | cin >> n; 33 | print(n); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Permutation Coefficient/code_5.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int PermutationCoeff(int n, int k) { 13 | int Fn = 1, Fk; 14 | for (int i = 1; i <= n; i++) { 15 | Fn *= i; 16 | if (i == n - k) 17 | Fk = Fn; 18 | } 19 | int coeff = Fn / Fk; 20 | return coeff; 21 | } 22 | 23 | 24 | int main () { 25 | cout << "\nEnter n\t:\t"; 26 | int number; 27 | cin >> n; 28 | cout << "\nEnter k\t:\t"; 29 | int k; 30 | cin >> k; 31 | cout << "\nThe result is\t:\t" << PermutationCoeff(n,k); 32 | cout << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Count Number of Increasing SubSequence/readMe.md: -------------------------------------------------------------------------------- 1 | ## We are given an array of digits (values lie in range from 0 to 9). The task is to count all the sub sequences possible in array such that in each subsequence every digit is greater than its previous digits in the subsequence. 2 | ``` 3 | Input : arr[] = {1, 2, 3, 4} 4 | Output: 15 5 | There are total increasing subsequences 6 | {1}, {2}, {3}, {4}, {1,2}, {1,3}, {1,4}, 7 | {2,3}, {2,4}, {3,4}, {1,2,3}, {1,2,4}, 8 | {1,3,4}, {2,3,4}, {1,2,3,4} 9 | 10 | Input : arr[] = {4, 3, 6, 5} 11 | Output: 8 12 | Sub-sequences are {4}, {3}, {6}, {5}, 13 | {4,6}, {4,5}, {3,6}, {3,5} 14 | 15 | Input : arr[] = {3, 2, 4, 5, 4} 16 | Output : 14 17 | Sub-sequences are {3}, {2}, {4}, {3,4}, 18 | {2,4}, {5}, {3,5}, {2,5}, {4,5}, {3,2,5} 19 | {3,4,5}, {4}, {3,4}, {2,4} 20 | ``` 21 | -------------------------------------------------------------------------------- /Count Balanced Binary Trees of Height h/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int countAllBalancedBinaryTree(int height) { 13 | if ( height == 0 || height == 1) { 14 | return 1; 15 | } 16 | return countAllBalancedBinaryTree(height-1) * (2 * countAllBalancedBinaryTree(height-2) + countAllBalancedBinaryTree(height-1)); 17 | 18 | } 19 | 20 | int main() { 21 | int height; 22 | cout << "\nEnter Height\t:\t"; 23 | cin >> height; 24 | cout << "\nTotal Balanced Binary Tree at Height - " << height << "\t:\t " << countAllBalancedBinaryTree(height) << "\n"; 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Count Balanced Binary Trees of Height h/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_2.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | long long int countAllBalancedBinaryTree(int height) { 13 | long long int DP[height+1]; 14 | DP[0] = DP[1] = 1; 15 | for (int i = 2; i <= height; i++ ) { 16 | DP[i] = DP[i-1] * (2*DP[i-2] + DP[i-1]); 17 | } 18 | 19 | return DP[height]; 20 | 21 | } 22 | 23 | int main() { 24 | int height; 25 | cout << "\nEnter Height\t:\t"; 26 | cin >> height; 27 | cout << "\nTotal Balanced Binary Tree at Height - " << height << "\t:\t " << countAllBalancedBinaryTree(height) << "\n"; 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Kth Smallest Element/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | int kthSmallest(int arr[], int n, int k) { 14 | sort(arr, arr+n); 15 | return arr[k-1]; 16 | } 17 | 18 | int main() { 19 | int n; 20 | cout << "\nEnter Size\t:\t"; 21 | cin >> n; 22 | int *a = new int[n]; 23 | cout << "\nEnter Array Elements\n"; 24 | for ( int i = 0; i < n; i++ ) { 25 | cin >> a[i]; 26 | } 27 | int k; 28 | cout << "Enter K\t:\t"; 29 | cin >> k; 30 | cout << "\nKth Smallest Element\t:\t" << kthSmallest( a , n , k ) << endl; 31 | delete[] a; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Check Whether A Binary Tree is Complete Or Not/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a Binary Tree, write a function to check whether the given Binary Tree is a Complete Binary Tree or not. 2 | 3 | ## A full binary tree is defined as a binary tree in which all nodes have either zero or two child nodes. Conversely, there is no node in a full binary tree, which has one child node. 4 | 5 | ``` 6 | 7 | The following tree is a complete binary tree 8 | 9 | 10 10 | / \ 11 | 20 30 12 | / \ 13 | 40 50 14 | 15 | 16 | 18 17 | / \ 18 | 15 30 19 | The following tree is not a complete binary tree 20 | 21 | 1 22 | / \ 23 | 2 3 24 | / \ 25 | 5 6 26 | ``` 27 | -------------------------------------------------------------------------------- /Compute nCr mod p/code_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int binomialCoeff(int n, int k , int p ) { 5 | int C[n+1][k+1]; 6 | int i, j; 7 | for (i = 0; i <= n; i++) { 8 | for (j = 0; j <= min(i, k); j++) { 9 | if (j == 0 || j == i) 10 | C[i][j] = 1; 11 | else 12 | C[i][j] = (C[i-1][j-1]%p + C[i-1][j]%p)%p; 13 | } 14 | } 15 | 16 | return C[n][k]; 17 | } 18 | 19 | 20 | int main () { 21 | cout << "\nEnter n\t:\t"; 22 | int n; 23 | cin >> n; 24 | cout << "\nEnter k\t:\t"; 25 | int k; 26 | cin >> k; 27 | cout << "\nEnter p\t:\t"; 28 | int p; 29 | cin >> p; 30 | cout <<"\nThe result is\t:\t" << binomialCoeff(n,k,p); 31 | cout << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Nth Catalan Number/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | unsigned long int catalanDP(unsigned int n) { 13 | int catalan[n+1]; 14 | catalan[0] = catalan[1] = 1; 15 | for (int i = 2; i <= n; i++) { 16 | catalan[i] = 0; 17 | for (int j = 0; j < i; j++) { 18 | catalan[i] += catalan[j] * catalan[i-j-1]; 19 | } 20 | } 21 | return catalan[n]; 22 | } 23 | 24 | int main () { 25 | cout << "\nEnter Number\t:\t"; 26 | unsigned int number; 27 | cin >> number; 28 | cout <<"\nThe result is\t:\t" << catalanDP(number); 29 | cout << endl; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /The Knight’s Tour Problem/readMe.md: -------------------------------------------------------------------------------- 1 | ## Backtracking is an algorithmic paradigm that tries different solutions until finds a solution that “works”. Problems which are typically solved using backtracking technique have following property in common. These problems can only be solved by trying every possible configuration and each configuration is tried only once. A Naive solution for these problems is to try all configurations and output a configuration that follows given problem constraints. Backtracking works in incremental way and is an optimization over the Naive solution where all possible configurations are generated and tried. 2 | 3 | ## For example, consider the following Knight’s Tour problem. 4 | ## The knight is placed on the first block of an empty board and, moving according to the rules of chess, must visit each square exactly once. 5 | -------------------------------------------------------------------------------- /Binomial Coefficient/code_3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int binomialCoeff(int n, int k) { 13 | int C[k+1]; 14 | memset(C, 0, sizeof(C)); 15 | 16 | C[0] = 1; 17 | 18 | for (int i = 1; i <= n; i++) { 19 | for (int j = min(i, k); j > 0; j--) 20 | C[j] = C[j] + C[j-1]; 21 | } 22 | return C[k]; 23 | } 24 | 25 | int main () { 26 | cout << "\nEnter n\t:\t"; 27 | int number; 28 | cin >> number; 29 | cout << "\nEnter k\t:\t"; 30 | int k; 31 | cin >> k; 32 | cout <<"\nThe result is\t:\t" << binomialCoeff(number, k); 33 | cout << endl; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Number of Binary Trees for Given Preorder Sequence Length/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Data Structure 4 | // 5 | // Created by Mohd Shoaib Rayeen on 19/03/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | #include 9 | using namespace std; 10 | 11 | int countTrees(int n) { 12 | int BT[n + 1]; 13 | memset(BT, 0, sizeof(BT)); 14 | 15 | BT[0] = BT[1] = 1; 16 | for (int i = 2; i <= n; ++i) { 17 | for (int j = 0; j < i; j++) { 18 | BT[i] += BT[j] * BT[i - j - 1]; 19 | } 20 | } 21 | return BT[n]; 22 | } 23 | 24 | int main() { 25 | cout << "\nEnter Sequence Length\t:\t"; 26 | int n; 27 | cin >> n; 28 | cout << "\nTotal Possible Binary Tree\t:\t" << countTrees(n) << endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Longest Common Subsequence/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int lcs( string s1 , string s2, long m, long n ) { 14 | if (m == 0 || n == 0) 15 | return 0; 16 | if (s1[m-1] == s2[n-1]) 17 | return 1 + lcs(s1, s2, m-1, n-1); 18 | else 19 | return max(lcs(s1, s2, m, n-1), lcs(s1, s2, m-1, n)); 20 | } 21 | 22 | int main() { 23 | string s1 , s2; 24 | cout << "\n1st String\t:\t"; 25 | cin >> s1; 26 | cout << "\n2nd String\t:\t"; 27 | cin >> s2; 28 | cout << "\nLength of LCS\t:\t" << lcs( s1, s2 , s1.length(), s2.length() ) << endl; 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Wildcard Pattern Matching/For Three Symbols/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a text and a wildcard pattern, implement wildcard pattern matching algorithm that finds if wildcard pattern is matched with text. The matching should cover the entire text (not partial text). 2 | 3 | The wildcard pattern can include the characters ‘?’, ‘*’ and ‘+’. 4 | 5 | - ‘?’ – matches any single character 6 | - ‘*’ – Matches any sequence of characters 7 | (including the empty sequence) 8 | - '+' – Matches previous single character 9 | of pattern 10 | ``` 11 | Input :Text = "baaabaaa", 12 | Pattern = “****+ba*****a+", output : true 13 | Pattern = "baaa?ab", output : false 14 | Pattern = "ba*a?", output : true 15 | Pattern = "+a*ab", output : false 16 | 17 | Input : Text = "aab" 18 | Pattern = "*+" output : false 19 | Pattern = "*+b" output : true 20 | ``` 21 | -------------------------------------------------------------------------------- /Count of Different Ways to Express N As The Sum of 1, 3 And 4/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_2.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int countAllWays(int n) { 13 | int DP[5]; 14 | DP[0] = DP[1] = DP[2] = 1; 15 | DP[3] = 2; 16 | for (int i = 4; i <= n; i++) { 17 | DP[4] = DP[3] + DP[1] + DP[0]; 18 | DP[0] = DP[1]; 19 | DP[1] = DP[2]; 20 | DP[2] = DP[3]; 21 | DP[3] = DP[4]; 22 | } 23 | return DP[3]; 24 | } 25 | 26 | int main() { 27 | int n; 28 | cout << "\nEnter Number\t:\t"; 29 | cin >> n; 30 | cout << "\nTotal Number of Ways\t:\t " << countAllWays(n) << "\n"; 31 | 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Minimum Sum of Multiplications of N Numbers/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given n integers. The task is to minimize the sum of multiplication of all the numbers by taking two adjacent numbers at a time and putting back their sum % 100 till a number is left. 2 | 3 | ``` 4 | Input : 40 60 20 5 | Output : 2400 6 | Explanation: There are two possible cases: 7 | 1st possibility: Take 40 and 60, so multiplication=2400 8 | and put back (60+40) % 100 = 0, making it 0, 20. 9 | Multiplying 0 and 20 we get 0 so 10 | multiplication = 2400+0 = 2400. Put back (0+20)%100 = 20. 11 | 2nd possibility: take 60 and 20, so 60*20 = 1200, 12 | put back (60+20)%100 = 80, making it [40, 80] 13 | multiply 40*80 to get 3200, so multiplication 14 | sum = 1200+3200 = 4400. Put back (40+80)%100 = 20 15 | 16 | Input : 5 6 17 | Output : 30 18 | Explanation: Only possibility is 5*6=30 19 | ``` 20 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Construct Tree From Given Inorder and Preorder/readMe.md: -------------------------------------------------------------------------------- 1 | ## Let us consider the below traversals: 2 | ``` 3 | Inorder sequence: D B E A F C 4 | Preorder sequence: A B D E C F 5 | 6 | In a Preorder sequence, leftmost element is the root of the tree. So we know ‘A’ 7 | is root for given sequences. By searching ‘A’ in Inorder sequence, we can find 8 | out all elements on left side of ‘A’ are in left subtree and elements on right 9 | are in right subtree. So we know below structure now. 10 | 11 | 12 | 13 | 14 | 15 | A 16 | / \ 17 | / \ 18 | D B E F C 19 | We recursively follow above steps and get the following tree. 20 | 21 | A 22 | / \ 23 | / \ 24 | B C 25 | / \ / 26 | / \ / 27 | D E F 28 | ``` 29 | -------------------------------------------------------------------------------- /Maximum Product Cutting/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_2.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | int max(int a, int b, int c) { 14 | return max(a, max(b, c)); 15 | } 16 | 17 | int getMaxProd(int n) { 18 | int val[n+1]; 19 | val[0] = val[1] = 0; 20 | for (int i = 1; i <= n; i++) { 21 | int max_val = 0; 22 | for (int j = 1; j <= i/2; j++) 23 | max_val = max(max_val, (i-j)*j, j*val[i-j]); 24 | val[i] = max_val; 25 | } 26 | return val[n]; 27 | } 28 | 29 | int main() { 30 | int n; 31 | cout << "\nEnter n\t:\t"; 32 | cin >> n; 33 | cout << "\nMaximum Product\t:\t" << getMaxProd(n) << "\n"; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Count Balanced Binary Trees of Height h/code_3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_3.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | long long int countAllBalancedBinaryTree(int height) { 13 | long long int DP[3]; 14 | DP[0] = DP[1] = 1; 15 | for (int i = 2; i <= height; i++ ) { 16 | DP[2] = DP[1] * (2*DP[0] + DP[1]); 17 | DP[0] = DP[1]; 18 | DP[1] = DP[2]; 19 | } 20 | 21 | return DP[1]; 22 | 23 | } 24 | 25 | int main() { 26 | int height; 27 | cout << "\nEnter Height\t:\t"; 28 | cin >> height; 29 | cout << "\nTotal Balanced Binary Tree at Height " << height << "\t:\t " << countAllBalancedBinaryTree(height) << "\n"; 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Find A Peak Element/readMe.md: -------------------------------------------------------------------------------- 1 | ### Given an array of integers. Find a peak element in it. An array element is peak if it is NOT smaller than its neighbors. For corner elements, we need to consider only one neighbor. For example, for input array {5, 10, 20, 15}, 20 is the only peak element. For input array {10, 20, 15, 2, 23, 90, 67}, there are two peak elements: 20 and 90. Note that we need to return any one peak element. 2 | 3 | Following corner cases give better idea about the problem. 4 | 1) If input array is sorted in strictly increasing order, the last element is always a peak element. For example, 50 is peak element in {10, 20, 30, 40, 50}. 5 | 2) If input array is sorted in strictly decreasing order, the first element is always a peak element. 100 is the peak element in {100, 80, 60, 50, 20}. 6 | 3) If all elements of input array are same, every element is a peak element. 7 | -------------------------------------------------------------------------------- /Tiling Problem/readMe.md: -------------------------------------------------------------------------------- 1 | # Given a “2 x n” board and tiles of size “2 x 1”, count the number of ways to tile the given board using the 2 x 1 tiles. A tile can either be placed horizontally i.e., as a 1 x 2 tile or vertically i.e., as 2 x 1 tile. 2 | ``` 3 | Input n = 3 4 | Output: 3 5 | Explanation: 6 | We need 3 tiles to tile the board of size 2 x 3. 7 | We can tile the board using following ways 8 | 1) Place all 3 tiles vertically. 9 | 2) Place first tile vertically and remaining 2 tiles horizontally. 10 | 3) Place first 2 tiles horizontally and remaining tiles vertically 11 | 12 | Input n = 4 13 | Output: 5 14 | Explanation: 15 | For a 2 x 4 board, there are 5 ways 16 | 1) All 4 vertical 17 | 2) All 4 horizontal 18 | 3) First 2 vertical, remaining 2 horizontal 19 | 4) First 2 horizontal, remaining 2 vertical 20 | 5) Corner 2 vertical, middle 2 horizontal 21 | ``` 22 | -------------------------------------------------------------------------------- /Compute nCr mod p/code_3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int binomialCoeff(int n, int k , int p ) { 13 | int C[k+1]; 14 | C[0] = 1; 15 | 16 | for (int i = 1; i <= n; i++) { 17 | for (int j = min(i, k); j > 0; j--) 18 | C[j] = (C[j] + C[j-1])%p; 19 | } 20 | return C[k]; 21 | } 22 | 23 | 24 | int main () { 25 | cout << "\nEnter n\t:\t"; 26 | int n; 27 | cin >> n; 28 | cout << "\nEnter k\t:\t"; 29 | int k; 30 | cin >> k; 31 | cout << "\nEnter p\t:\t"; 32 | int p; 33 | cin >> p; 34 | cout <<"\nThe result is\t:\t" << binomialCoeff(n,k,p); 35 | cout << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Longest Bitonic Subsequence/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array arr[0 … n-1] containing n positive integers, a subsequence of arr[] is called Bitonic if it is first increasing, then decreasing. Write a function that takes an array as argument and returns the length of the longest bitonic subsequence. 2 | ## A sequence, sorted in increasing order is considered Bitonic with the decreasing part as empty. Similarly, decreasing order sequence is considered Bitonic with the increasing part as empty. 3 | 4 | ``` 5 | Input arr[] = {1, 11, 2, 10, 4, 5, 2, 1}; 6 | Output: 6 (A Longest Bitonic Subsequence of length 6 is 1, 2, 10, 4, 2, 1) 7 | 8 | Input arr[] = {12, 11, 40, 5, 3, 1} 9 | Output: 5 (A Longest Bitonic Subsequence of length 5 is 12, 11, 5, 3, 1) 10 | 11 | Input arr[] = {80, 60, 30, 40, 20, 10} 12 | Output: 5 (A Longest Bitonic Subsequence of length 5 is 80, 60, 30, 20, 10) 13 | ``` 14 | -------------------------------------------------------------------------------- /Maximum games played by winner/readMe.md: -------------------------------------------------------------------------------- 1 | ## There are N players which are playing a tournament. We need to find the maximum number of games the winner can play. In this tournament, two players are allowed to play against each other only if the difference between games played by them is not more than one. 2 | 3 | **Examples:** 4 | ``` 5 | Input : N = 3 6 | Output : 2 7 | Maximum games winner can play = 2 8 | Assume that player are P1, P2 and P3 9 | First, two players will play let (P1, P2) 10 | Now winner will play against P3, 11 | making total games played by winner = 2 12 | 13 | Input : N = 4 14 | Output : 2 15 | Maximum games winner can play = 2 16 | Assume that player are P1, P2, P3 and P4 17 | First two pairs will play lets (P1, P2) and 18 | (P3, P4). Now winner of these two games will 19 | play against each other, making total games 20 | played by winner = 2 21 | ``` 22 | -------------------------------------------------------------------------------- /Painting Fence Algorithm/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_2.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | 14 | 15 | long countWays(int n, int k) { 16 | long total = k; 17 | int mod = 1000000007; 18 | long same = 0, diff = k; 19 | for (int i = 2; i <= n; i++) { 20 | same = diff; 21 | diff = total * (k-1); 22 | diff = diff % mod; 23 | total = (same + diff) % mod; 24 | } 25 | 26 | return total; 27 | } 28 | 29 | int main() { 30 | int n; 31 | cout << "\nEnter N\t:\t"; 32 | cin >> n; 33 | int k; 34 | cout << "\nEnter K\t:\t"; 35 | cin >> k; 36 | cout << "\nNumber of Ways\t:\t" << countWays(n,k) << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Unbounded Knapsack/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given a knapsack weight W and a set of n items with certain value vali and weight wti, we need to calculate minimum amount that could make up this quantity exactly. This is different from classical Knapsack problem, here we are allowed to use unlimited number of instances of an item. 2 | 3 | ``` 4 | Input : W = 100 5 | val[] = {1, 30} 6 | wt[] = {1, 50} 7 | Output : 100 8 | There are many ways to fill knapsack. 9 | 1) 2 instances of 50 unit weight item. 10 | 2) 100 instances of 1 unit weight item. 11 | 3) 1 instance of 50 unit weight item and 50 12 | instances of 1 unit weight items. 13 | We get maximum value with option 2. 14 | 15 | Input : W = 8 16 | val[] = {10, 40, 50, 70} 17 | wt[] = {1, 3, 4, 5} 18 | Output : 110 19 | We get maximum value with one unit of 20 | weight 5 and one unit of weight 3. 21 | ``` 22 | -------------------------------------------------------------------------------- /Sum of All Substrings of A String Representing A Number/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | int sumOfSubstrings(string num) { 14 | long n = num.length(); 15 | int sumofdigit[n]; 16 | sumofdigit[0] = num[0] - '0'; 17 | int res = sumofdigit[0]; 18 | for (int i = 1; i < n; i++ ) { 19 | int numi = num[i] - '0'; 20 | sumofdigit[i] = (i+1) * numi + 10 * sumofdigit[i-1]; 21 | res += sumofdigit[i]; 22 | } 23 | return res; 24 | } 25 | 26 | int main() { 27 | string str; 28 | cout << "\nEnter Number\t:\t"; 29 | getline(cin , str); 30 | cout << "\nTotal Sum\t\t:\t" << sumOfSubstrings(str) << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Binary Tree/Top View of Binary Tree/readMe.md: -------------------------------------------------------------------------------- 1 | ## Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Given a binary tree, print the top view of it. The output nodes can be printed in any order. Expected time complexity is O(n) 2 | 3 | ## A node x is there in output if x is the topmost node at its horizontal distance. Horizontal distance of left child of a node x is equal to horizontal distance of x minus 1, and that of right child is horizontal distance of x plus 1. 4 | ``` 5 | 1 6 | / \ 7 | 2 3 8 | / \ / \ 9 | 4 5 6 7 10 | Top view of the above binary tree is 11 | 4 2 1 3 7 12 | 13 | 1 14 | / \ 15 | 2 3 16 | \ 17 | 4 18 | \ 19 | 5 20 | \ 21 | 6 22 | Top view of the above binary tree is 23 | 2 1 3 6 24 | ``` 25 | -------------------------------------------------------------------------------- /Edit Distance/readMe.md: -------------------------------------------------------------------------------- 1 | 2 | ## Given two strings str1 and str2 and below operations that can performed on str1. Find minimum number of edits (operations) required to convert ‘str1’ into ‘str2’. 3 | ### Insert 4 | ### Remove 5 | ### Replace 6 | 7 | 8 | ### All of the above operations are of equal cost. 9 | 10 | 11 | ### Input: str1 = "geek", str2 = "gesek" 12 | 13 | ### Output: 1 14 | 15 | ### We can convert str1 into str2 by inserting a 's'. 16 | 17 | 18 | ### Input: str1 = "cat", str2 = "cut" 19 | 20 | ### Output: 1 21 | 22 | ### We can convert str1 into str2 by replacing 'a' with 'u'. 23 | 24 | 25 | ### Input: str1 = "sunday", str2 = "saturday" 26 | 27 | ### Output: 3 28 | 29 | ### Last three and first characters are same. We basically 30 | 31 | ### need to convert "un" to "atur". This can be done using below three operations. Replace 'n' with 'r', insert t, insert a 32 | -------------------------------------------------------------------------------- /Rencontres Number/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given two numbers, n >= 0 and 0 <= k <= n, count the number of derangements with k fixed points. 2 | 3 | ``` 4 | 5 | Input : n = 3, k = 0 6 | Output : 2 7 | Since k = 0, no point needs to be on its 8 | original position. So derangements 9 | are {3, 1, 2} and {2, 3, 1} 10 | 11 | Input : n = 3, k = 1 12 | Output : 3 13 | Since k = 1, one point needs to be on its 14 | original position. So partial derangements 15 | are {1, 3, 2}, {3, 2, 1} and {2, 1, 3} 16 | 17 | Input : n = 7, k = 2 18 | Output : 924 19 | ``` 20 | 21 | ## The recurrence relation to find Rencontres Number D (n, m): 22 | ``` 23 | D(0, 0) = 1 if n==0 & m==0 24 | D(1, 0) = 0 if n==1 & m==0 25 | D(n+2, 0) = (n+1) * (D(n+1, 0) + D(n, 0)) if only m==0 26 | D(n, m) = C(n , m) * D( n - m , 0)) otherwise 27 | ``` 28 | -------------------------------------------------------------------------------- /Largest Sum Contiguous Subarray/code_3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int maxSubArraySum(int a[], int size) { 13 | int max_so_far = a[0]; 14 | int curr_max = a[0]; 15 | for (int i = 1; i < size; i++) { 16 | curr_max = max(a[i], curr_max+a[i]); 17 | max_so_far = max(max_so_far, curr_max); 18 | } 19 | return max_so_far; 20 | } 21 | 22 | int main() { 23 | int n; 24 | cout << "\nEnter Size of Set\t:\t"; 25 | cin >> n; 26 | int a[n]; 27 | cout << "\nEnter Set Elements\n"; 28 | for(int i = 0; i < n ; i++ ) { 29 | cin >> a[i]; 30 | } 31 | cout << "\nMaximum contiguous sum is " << maxSubArraySum(a, n); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Moser-de Bruijn Sequence/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_2.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | void moserDeBruijn(int n) { 14 | int S[n+1]; 15 | 16 | S[0] = 0; 17 | S[1] = 1; 18 | 19 | for (int i = 2; i <= n; i++) { 20 | if (i % 2 == 0) { 21 | S[i] = 4 * S[i / 2]; 22 | } 23 | else { 24 | S[i] = 4 * S[i / 2] + 1; 25 | } 26 | } 27 | cout << "\nMoser-de Bruijn Sequence\t:\t"; 28 | for (int i = 0; i < n; i++) { 29 | cout << S[i] << " "; 30 | } 31 | cout << "\n"; 32 | } 33 | 34 | 35 | int main() { 36 | int n; 37 | cout << "\nEnter N\t\t\t:\t"; 38 | cin >> n; 39 | moserDeBruijn(n); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Rotation Count in Rotated Sorted Array/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | int countRotations(int arr[], int n) { 14 | int min = arr[0]; 15 | int min_index = -1; 16 | for (int i=0; i arr[i]) { 18 | min = arr[i]; 19 | min_index = i; 20 | } 21 | } 22 | return min_index; 23 | } 24 | 25 | int main() { 26 | int n; 27 | cout << "\nEnter Size\t:\t"; 28 | cin >> n; 29 | int arr[n]; 30 | cout << "\nEnter Array Elements\n"; 31 | for ( int i = 0; i < n; i++ ) { 32 | cin >> arr[i]; 33 | } 34 | cout << "\nRotation Count\t:\t" << countRotations(arr, n) << endl; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Mean and Median for Unsorted Array/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given n size unsorted array, find its mean and median. 2 | 3 | Mean of an array = (sum of all elements) / 4 | (number of elements) 5 | 6 | Median of a sorted array of size n is defined 7 | as below : 8 | It is middle element when n is odd and average 9 | of middle two elements when n is even. 10 | 11 | Since the array is not sorted here, we sort 12 | the array first, then apply above formula. 13 | 14 | ``` 15 | Input : a[] = {1, 3, 4, 2, 6, 5, 8, 7} 16 | Output : Mean = 4.5 17 | Median = 4.5 18 | Sum of the elements is 1 + 3 + 4 + 2 + 6 + 19 | 5 + 8 + 7 = 36 20 | Mean = 36/8 = 4.5 21 | Since number of elements are even, median 22 | is average of 4th and 5th largest elements. 23 | which means (4 + 5)/2 = 4.5 24 | 25 | Input : a[] = {4, 4, 4, 4, 4} 26 | Output : Mean = 4 27 | Median = 4 28 | ``` 29 | -------------------------------------------------------------------------------- /Moser-de Bruijn Sequence/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | int gen(int n) { 14 | if (n == 0) { 15 | return 0; 16 | } 17 | else if (n == 1) { 18 | return 1; 19 | } 20 | else if (n % 2 == 0) { 21 | return 4 * gen(n / 2); 22 | } 23 | else { 24 | return 4 * gen(n / 2) + 1; 25 | } 26 | } 27 | 28 | void moserDeBruijn(int n) { 29 | cout << "\nMoser-de Bruijn Sequence\t:\t"; 30 | for (int i = 0; i < n; i++) { 31 | cout << gen(i) << " "; 32 | } 33 | cout << "\n"; 34 | } 35 | 36 | int main() { 37 | int n; 38 | cout << "\nEnter N\t\t\t:\t"; 39 | cin >> n; 40 | moserDeBruijn(n); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Maximum path sum in a triangle/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // q1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 17/10/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | #include 9 | using namespace std; 10 | #define N 3 11 | 12 | int maxPathSum(int tri[N][N], int m, int n) { 13 | for (int i=m-1; i>=0; i--) { 14 | for (int j=0; j<=i; j++) { 15 | if (tri[i+1][j] > tri[i+1][j+1]) { 16 | tri[i][j] += tri[i+1][j]; 17 | } 18 | else { 19 | tri[i][j] += tri[i+1][j+1]; 20 | } 21 | } 22 | } 23 | return tri[0][0]; 24 | } 25 | 26 | int main() { 27 | int tri[N][N] = { {1, 0, 0}, 28 | {4, 8, 0}, 29 | {1, 5, 3} }; 30 | cout << "\nResult\t:\t" << maxPathSum(tri, 2, 2); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Find the Missing Number/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | 15 | unsigned long getMissingNo (vector a) { 16 | unsigned long n = a.size(); 17 | unsigned long total = (n+1)*(n+2)/2; 18 | for (int i = 0; i< n; i++) { 19 | total -= a[i]; 20 | } 21 | return total; 22 | } 23 | 24 | int main() { 25 | int n; 26 | cout << "\nEnter Size\t:\t"; 27 | cin >> n; 28 | vector a(n); 29 | cout << "\nEnter Elements\n"; 30 | for ( int i = 0; i < n; i++ ) { 31 | cin >> a[i]; 32 | } 33 | cout << "\nMissing Number\t:\t" << getMissingNo(a); 34 | a.clear(); 35 | cout << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Friends Pairing Problem/SolutionByAastha.Java: -------------------------------------------------------------------------------- 1 | /* 2 | Given n friends, each one can remain single or can be paired up with some other friend. Each friend can be paired only once. Find out the total number of ways in which friends can remain single or can be paired up. 3 | Input : n = 3 4 | Output : 4 5 | */ 6 | 7 | import java.io.*; 8 | import java.util.*; 9 | public class Main { 10 | 11 | public static void main(String[] args) throws Exception { 12 | Scanner scn = new Scanner(System.in); 13 | int n = scn.nextInt(); // number of friends 14 | 15 | int[] dp = new int[n + 1]; 16 | dp[1] = 1; // as there is only one way 17 | dp[2] = 2; // as both friends can pair up or can be seperated 18 | 19 | for (int i = 3; i < dp.length; i++) { 20 | dp[i] = dp[i - 1] + (i - 1) * dp[i - 2]; 21 | } 22 | 23 | System.out.println(dp[n]); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Nth Catalan Number/code_3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | unsigned long int binomialCoeff(unsigned int n, unsigned int k) { 14 | unsigned long int res = 1; 15 | if (k > n - k) 16 | k = n - k; 17 | for (int i = 0; i < k; ++i) { 18 | res *= (n - i); 19 | res /= (i + 1); 20 | } 21 | return res; 22 | } 23 | unsigned long int catalan(unsigned int n) { 24 | unsigned long int c = binomialCoeff(2*n, n); 25 | return c/(n+1); 26 | } 27 | 28 | int main () { 29 | cout << "\nEnter Number\t:\t"; 30 | unsigned int number; 31 | cin >> number; 32 | cout <<"\nThe result is\t:\t" << catalan(number); 33 | cout << endl; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Tile Stacking Problem/readMe.md: -------------------------------------------------------------------------------- 1 | ## A stable tower of height n is a tower consisting of exactly n tiles of unit height stacked vertically in such a way, that no bigger tile is placed on a smaller tile. An example is shown below : 2 | 3 | ## We have infinite number of tiles of sizes 1, 2, …, m. The task is calculate the number of different stable tower of height n that can be built from these tiles, with a restriction that you can use at most k tiles of each size in the tower. 4 | 5 | ## Note: Two tower of height n are different if and only if there exists a height h (1 <= h <= n), such that the towers have tiles of different sizes at height h. 6 | 7 | ``` 8 | Input : n = 3, m = 3, k = 1 9 | Output : 1 10 | Possible sequences: { 1, 2, 3}. 11 | Hence answer is 1. 12 | 13 | Input : n = 3, m = 3, k = 2 14 | Output : 7 15 | {1, 1, 2}, {1, 1, 3}, {1, 2, 2}, 16 | {1, 2, 3}, {1, 3, 3}, {2, 2, 3}, 17 | {2, 3, 3}. 18 | ``` 19 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Find The Largest Three Elements in An Array/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | 15 | void largestThree(vector a) { 16 | sort(a.begin() , a.end() , greater()); 17 | cout << "\nLargest Three Elements\t:\t"; 18 | for ( int i = 0; i < 3; i++ ) { 19 | cout << a[i] << " "; 20 | } 21 | cout << endl; 22 | } 23 | 24 | int main() { 25 | int n; 26 | cout << "\nEnter Size\t:\t"; 27 | cin >> n; 28 | vector a(n); 29 | cout << "\nEnter Elements\n"; 30 | for ( int i = 0; i < n; i++ ) { 31 | cin >> a[i]; 32 | } 33 | largestThree(a); 34 | a.clear(); 35 | cout << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Delannoy Number/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // q10.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 13/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int dealnnoy(int n, int m) { 13 | int dp[m + 1][n + 1]; 14 | for (int i = 0; i <= m; i++) { 15 | dp[i][0] = 1; 16 | } 17 | for (int i = 0; i <= m; i++) { 18 | dp[0][i] = 1; 19 | } 20 | for (int i = 1; i <= m; i++) { 21 | for (int j = 1; j <= n; j++) { 22 | dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1] + dp[i][j - 1]; 23 | } 24 | } 25 | return dp[m][n]; 26 | } 27 | 28 | int main() { 29 | int n , m; 30 | cout << "\nEnter n\t:\t"; 31 | cin >> n; 32 | cout << "\nEnter m\t:\t"; 33 | cin >> m; 34 | cout << dealnnoy(n, m) << endl; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Painting Fence Algorithm/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | 14 | 15 | long countWays(int n, int k) { 16 | long dp[n + 1]; 17 | memset(dp, 0, sizeof(dp)); 18 | int mod = 1000000007; 19 | dp[1] = k; 20 | long same = 0, diff = k; 21 | for (int i = 2; i <= n; i++) { 22 | same = diff; 23 | diff = dp[i-1] * (k-1); 24 | diff = diff % mod; 25 | dp[i] = (same + diff) % mod; 26 | } 27 | return dp[n]; 28 | } 29 | 30 | int main() { 31 | int n; 32 | cout << "\nEnter N\t:\t"; 33 | cin >> n; 34 | int k; 35 | cout << "\nEnter K\t:\t"; 36 | cin >> k; 37 | cout << "\nNumber of Ways\t:\t" << countWays(n,k) << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Power Set in Lexicographic order/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // q5.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 05/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | void permuteRec(string str, int n , int index = -1, string curr = "") { 12 | if (index == n) { 13 | return; 14 | } 15 | cout << curr << "\n"; 16 | for (int i = index + 1; i < n; i++) { 17 | curr += str[i]; 18 | permuteRec(str, n, i, curr); 19 | curr = curr.erase(curr.size() - 1); 20 | } 21 | return; 22 | } 23 | 24 | void powerSet(string str) { 25 | sort(str.begin(), str.end()); 26 | permuteRec(str, str.size()); 27 | } 28 | 29 | int main() { 30 | string str; 31 | cout << "\nEnter String\t:\t"; 32 | cin >> str; 33 | powerSet(str); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Binomial Coefficient/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int binomialCoeff(int n, int k) { 13 | int C[n+1][k+1]; 14 | int i, j; 15 | for (i = 0; i <= n; i++) { 16 | for (j = 0; j <= min(i, k); j++) { 17 | if (j == 0 || j == i) 18 | C[i][j] = 1; 19 | else 20 | C[i][j] = C[i-1][j-1] + C[i-1][j]; 21 | } 22 | } 23 | 24 | return C[n][k]; 25 | } 26 | 27 | 28 | int main () { 29 | cout << "\nEnter n\t:\t"; 30 | int n; 31 | cin >> n; 32 | cout << "\nEnter k\t:\t"; 33 | int k; 34 | cin >> k; 35 | cout <<"\nThe result is\t:\t" << binomialCoeff(n,k); 36 | cout << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Sum of All Substrings of A String Representing A Number/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_2.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | int sumOfSubstrings(string num) { 14 | long n = num.length(); 15 | int sumofdigit[2]; 16 | sumofdigit[0] = num[0] - '0'; 17 | int res = sumofdigit[0]; 18 | for (int i = 1; i < n; i++ ) { 19 | int numi = num[i] - '0'; 20 | sumofdigit[1] = (i+1) * numi + 10 * sumofdigit[0]; 21 | res += sumofdigit[1]; 22 | sumofdigit[0] = sumofdigit[1]; 23 | } 24 | return res; 25 | } 26 | 27 | int main() { 28 | string str; 29 | cout << "\nEnter Number\t:\t"; 30 | getline(cin , str); 31 | cout << "\nTotal Sum\t\t:\t" << sumOfSubstrings(str) << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Count Distinct Subsequences/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | const int MAX_CHAR = 256; 14 | 15 | int countSub(string str) { 16 | vector last(MAX_CHAR, -1); 17 | long n = str.length(); 18 | int dp[n+1]; 19 | dp[0] = 1; 20 | for (int i=1; i<=n; i++) { 21 | dp[i] = 2*dp[i-1]; 22 | if (last[str[i-1]] != -1) { 23 | dp[i] = dp[i] - dp[last[str[i-1]]]; 24 | } 25 | last[str[i-1]] = (i-1); 26 | } 27 | return dp[n]; 28 | } 29 | 30 | int main() { 31 | string s; 32 | cout << "\nEnter Sequence\t:\t"; 33 | cin >> s; 34 | cout << "\nNumber of Subsequences\t:\t" << countSub(s) << endl; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Longest Common Substring/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int lcs( string s1 , string s2, long m, long n , int count) { 13 | 14 | if (m == 0 || n == 0) { 15 | return count; 16 | } 17 | if (s1[m-1] == s2[n-1]) { 18 | count = lcs(s1 , s2 , m - 1, n - 1, count + 1); 19 | } 20 | count = max(count, max(lcs(s1 , s2 , m , n - 1, 0), lcs( s1 , s2 , m - 1, n , 0))); 21 | return count; 22 | } 23 | 24 | int main() { 25 | string s1 , s2; 26 | cout << "\n1st String\t:\t"; 27 | cin >> s1; 28 | cout << "\n2nd String\t:\t"; 29 | cin >> s2; 30 | cout << "\nLength of substring\t:\t " << lcs( s1, s2 , s1.length(), s2.length() , 0 ) << "\n"; 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Hamiltonian Cycle/readMe.md: -------------------------------------------------------------------------------- 1 | ## Hamiltonian Path in an undirected graph is a path that visits each vertex exactly once. A Hamiltonian cycle (or Hamiltonian circuit) is a Hamiltonian Path such that there is an edge (in graph) from the last vertex to the first vertex of the Hamiltonian Path. Determine whether a given graph contains Hamiltonian Cycle or not. If it contains, then print the path. Following are the input and output of the required function. 2 | 3 | ``` 4 | Input : A 2D array graph[V][V] where V is the number of vertices in graph and graph[V][V] is adjacency 5 | matrix representation of the graph. A value graph[i][j] is 1 if there is a direct edge from i to j, 6 | otherwise graph[i][j] is 0. 7 | 8 | Output: An array path[V] that should contain the Hamiltonian Path. path[i] should represent the ith 9 | vertex in the Hamiltonian Path. The code should also return false if there is no Hamiltonian Cycle 10 | in the graph. 11 | ``` 12 | -------------------------------------------------------------------------------- /Minimum Insertions to Form a Palindrome/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_2.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | int getLengthOfMinInsertions(string str) { 15 | int n = int(str.length()); 16 | int dp[n][n]; 17 | memset(dp, 0, sizeof(dp)); 18 | for ( int i = 0; i < n; i++ ) { 19 | for ( int j = 0 , k = i; k < n; ++j , ++k ) { 20 | dp[j][k] = (str[j] == str[k])? dp[j+1][k-1] : ( min( dp[j][k-1], dp[j+1][k] ) + 1 ); 21 | } 22 | } 23 | return dp[0][n-1]; 24 | } 25 | 26 | int main() { 27 | string str; 28 | cout << "\nEnter String\t:\t"; 29 | cin >> str; 30 | cout << "\nMinimum Insertions to form Palindrome\t:\t" << getLengthOfMinInsertions(str) << "\n"; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Entringer Number/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 13/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int Entringer_Number(int n, int k) { 13 | int dp[n + 1][k + 1]; 14 | memset(dp, 0, sizeof(dp)); 15 | dp[0][0] = 1; 16 | for (int i = 1; i <= n; i++) { 17 | dp[i][0] = 0; 18 | } 19 | for (int i = 1; i <= n; i++) { 20 | for (int j = 1; j <= i; j++) { 21 | dp[i][j] = dp[i][j - 1] + dp[i - 1][i - j]; 22 | } 23 | } 24 | return dp[n][k]; 25 | } 26 | 27 | int main() { 28 | int n; 29 | cout << "\nEnter n\t:\t"; 30 | cin >> n; 31 | cout << "\nEnter k\t:\t"; 32 | int k; 33 | cin >> k; 34 | cout << "\nEntringer Number\t:\t" << Entringer_Number(n, k) << endl; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Longest Substring Without Repeating Characters/README.md: -------------------------------------------------------------------------------- 1 | ## Longest Substring Without Repeating Characters 2 | 3 | https://leetcode.com/problems/longest-substring-without-repeating-characters/ 4 | 5 | Given a string s, find the length of the longest substring without repeating characters. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: s = "abcabcbb" 12 | Output: 3 13 | Explanation: The answer is "abc", with the length of 3. 14 | Example 2: 15 | 16 | Input: s = "bbbbb" 17 | Output: 1 18 | Explanation: The answer is "b", with the length of 1. 19 | Example 3: 20 | 21 | Input: s = "pwwkew" 22 | Output: 3 23 | Explanation: The answer is "wke", with the length of 3. 24 | Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. 25 | Example 4: 26 | 27 | Input: s = "" 28 | Output: 0 29 | 30 | 31 | Constraints: 32 | 33 | 0 <= s.length <= 5 * 104 34 | s consists of English letters, digits, symbols and spaces. 35 | 36 | 37 | -------------------------------------------------------------------------------- /Count Number of Ways to Reach A Given Score in A Game/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int countAllWays(int n) { 13 | int table[n + 1]; 14 | for(int j = 0; j < n + 1; j++) { 15 | table[j] = 0; 16 | } 17 | table[0] = 1; 18 | for (int i = 3; i <= n; i++) { 19 | table[i] += table[i - 3]; 20 | } 21 | for (int i = 5; i <= n; i++) { 22 | table[i] += table[i - 5]; 23 | } 24 | for (int i = 10; i <= n; i++) { 25 | table[i] += table[i - 10]; 26 | } 27 | return table[n]; 28 | } 29 | 30 | int main() { 31 | int n; 32 | cout << "\nEnter Number\t:\t"; 33 | cin >> n; 34 | cout << "\nTotal Number of Ways\t:\t " << countAllWays(n) << "\n"; 35 | 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Cutting a Rod/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int cutRod(int *price, int n) { 13 | if (n <= 0) 14 | return 0; 15 | int max_val = -1; //price can't be negative so using negative value initially to compare. 16 | for (int i = 0; i < n; i++) { 17 | max_val = max(max_val, price[i] + cutRod(price, n-i-1)); 18 | } 19 | return max_val; 20 | } 21 | 22 | int main () { 23 | int number_rod; 24 | cout << "\nNumber of Rods\t:\t"; 25 | cin >> number_rod; 26 | int array[number_rod]; 27 | cout << "\nEnter Price\n"; 28 | for( int i = 0; i < number_rod; ++i ) { 29 | cin >> array[i]; 30 | } 31 | cout <<"\nThe result is\t:\t" << cutRod(array , number_rod); 32 | cout << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Next Greater Element/readMe.md: -------------------------------------------------------------------------------- 1 | ## Given an array, print the Next Greater Element (NGE) for every element. The Next greater Element for an element x is the first greater element on the right side of x in array. Elements for which no greater element exist, consider next greater element as -1. 2 | 3 | - For any array, rightmost element always has next greater element as -1. 4 | - For an array which is sorted in decreasing order, all elements have next greater element as -1. 5 | - For the input array [4, 5, 2, 25}, the next greater elements for each element are as follows. 6 | ``` 7 | Element NGE 8 | 4 --> 5 9 | 5 --> 25 10 | 2 --> 25 11 | 25 --> -1 12 | ``` 13 | - For the input array [13, 7, 6, 12}, the next greater elements for each element are as follows. 14 | ``` 15 | Element NGE 16 | 13 --> -1 17 | 7 --> 12 18 | 6 --> 12 19 | 12 --> -1 20 | ``` 21 | -------------------------------------------------------------------------------- /Cutting a Rod/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int cutRod(int price[], int n) { 13 | int val[n+1]; 14 | val[0] = 0; 15 | int i, j; 16 | for (i = 1; i<=n; i++) { 17 | int max_val = -1; 18 | for (j = 0; j < i; j++) 19 | max_val = max(max_val, price[j] + val[i-j-1]); 20 | val[i] = max_val; 21 | } 22 | 23 | return val[n]; 24 | } 25 | 26 | int main () { 27 | int number_rod; 28 | cout << "\nNumber of Rods\t:\t"; 29 | cin >> number_rod; 30 | int array[number_rod]; 31 | cout << "\nEnter Price\n"; 32 | for( int i = 0; i < number_rod; ++i ) { 33 | cin >> array[i]; 34 | } 35 | cout <<"\nThe result is\t:\t" << cutRod(array , number_rod); 36 | cout << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Minimum number of Coins/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 06/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int available[] = {1, 2, 5, 10, 20, 50, 100, 200 , 500, 1000 , 2000}; 13 | int n = sizeof(available)/sizeof(available[0]); 14 | 15 | void findMin(int value) { 16 | vector res; 17 | for (int i = n-1; i >= 0; i-- ) { 18 | while (value >= available[i]) { 19 | value -= available[i]; 20 | res.push_back(available[i]); 21 | } 22 | } 23 | for (int i = 0; i < res.size(); i++) { 24 | cout << res[i] << "\t"; 25 | } 26 | } 27 | 28 | int main() { 29 | int n; 30 | cout << "\nEnter Value\t:\t"; 31 | cin >> n; 32 | cout << "\nMinimal number of change for " << n << "\t:\t"; 33 | findMin(n); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Count Inversions/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int countInversion(vector arr) { 14 | long n = arr.size(); 15 | int inv_count = 0; 16 | for (int i = 0; i < n - 1; i++) { 17 | for (int j = i + 1; j < n; j++) { 18 | if (arr[i] > arr[j]) { 19 | inv_count++; 20 | } 21 | } 22 | } 23 | return inv_count; 24 | } 25 | 26 | int main(int argv, char** args) { 27 | int n; 28 | cout << "\nEnter Number of Element\t:\t"; 29 | cin >> n; 30 | cout << "\nEnter Elements\n"; 31 | vector arr(n); 32 | for ( int i = 0; i < n; i++ ) { 33 | cin >> arr[i]; 34 | } 35 | cout << "\nNumber of Inversions\t:\t" << countInversion(arr) << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Longest Increasing Subsequence/code_2.cpp: -------------------------------------------------------------------------------- 1 | / 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | #include 9 | using namespace std; 10 | 11 | int lis_fun( int *arr, int n ) { 12 | int lis[n]; 13 | 14 | lis[0] = 1; 15 | for (int i = 1; i < n; i++ ){ 16 | lis[i] = 1; 17 | for (int j = 0; j < i; j++ ) { 18 | if ( arr[i] > arr[j] && lis[i] < lis[j] + 1) { 19 | lis[i] = lis[j] + 1; 20 | } 21 | } 22 | } 23 | return *max_element(lis, lis+n); 24 | } 25 | 26 | 27 | int main() { 28 | int n; 29 | cout << "\nEnter Size\t:\t"; 30 | cin >> n; 31 | int arr[n]; 32 | cout << "\nEnter Sequence\n"; 33 | for(int i =0; i < n; i++ ) { 34 | cin >> arr[i]; 35 | } 36 | cout << "\Length of LIS\t:\t" << lis_fun(arr,n); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Split A Number into Maximum Composite Number/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | int count(int n) { 14 | if (n < 4) { 15 | return -1; 16 | } 17 | int rem = n % 4; 18 | if (rem == 0) { 19 | return n / 4; 20 | } 21 | else if (rem == 1) { 22 | if (n < 9) { 23 | return -1; 24 | } 25 | return (n - 9) / 4 + 1; 26 | } 27 | else if (rem == 2) { 28 | return (n - 6) / 4 + 1; 29 | } 30 | else { 31 | if (n < 15) { 32 | return -1; 33 | } 34 | return (n - 15) / 4 + 2; 35 | } 36 | } 37 | 38 | 39 | int main() { 40 | cout << "\nEnter N\t:\t"; 41 | int n; 42 | cin >> n; 43 | cout << "\nMaximum Composite Number\t:\t" << count(n) << endl; 44 | } 45 | -------------------------------------------------------------------------------- /Coin Change/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int count( int *S , int m , int n ) { 13 | if (n == 0) 14 | return 1; 15 | if (n < 0) 16 | return 0; 17 | if (m <=0 && n >= 1) 18 | return 0; 19 | return count( S, m - 1, n ) + count( S, m, n-S[m-1] ); 20 | } 21 | 22 | int main () { 23 | int number_coin; 24 | cout << "\nNumber of Coins\t:\t"; 25 | cin >> number_coin; 26 | int array[number_coin]; 27 | cout << "\nEnter Coins\n"; 28 | for( int i = 0; i < number_coin; ++i ) { 29 | cin >> array[i]; 30 | } 31 | cout << "\nValue for Coin Change\t:\t"; 32 | int number; 33 | cin >> number; 34 | cout <<"\nThe result is\t:\t" << count(array , number_coin , number ); 35 | cout << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Eulerian Number/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_2.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 13/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int eulerian(int n, int m) { 13 | int dp[n + 1][m + 1]; 14 | for (int i = 1; i <= n; i++) { 15 | for (int j = 0; j <= m; j++) { 16 | if (i > j) { 17 | if (j == 0) { 18 | dp[i][j] = 1; 19 | } 20 | else { 21 | dp[i][j] = ((i - j) * dp[i - 1][j - 1]) + ((j + 1) * dp[i - 1][j]); 22 | } 23 | } 24 | } 25 | } 26 | 27 | return dp[n][m]; 28 | } 29 | 30 | int main() { 31 | int n , m; 32 | cout << "\nEnter n\t:\t"; 33 | cin >> n; 34 | cout << "\nEnter m\t:\t"; 35 | cin >> m; 36 | cout << eulerian(n, m) << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Jacobsthal and Jacobsthal-Lucas numbers/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 13/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int Jacobsthal(int n) { 13 | if (n == 0) { 14 | return 0; 15 | } 16 | if (n == 1) { 17 | return 1; 18 | } 19 | return Jacobsthal(n - 1) + 2 * Jacobsthal(n - 2); 20 | } 21 | 22 | int Jacobsthal_Lucas(int n) { 23 | if (n == 0) { 24 | return 2; 25 | } 26 | if (n == 1) { 27 | return 1; 28 | } 29 | return Jacobsthal_Lucas(n - 1) + 2 * Jacobsthal_Lucas(n - 2); 30 | } 31 | 32 | int main() { 33 | int n; 34 | cout << "\nEnter n\t:\t"; 35 | cin >> n; 36 | cout << "\nJacobsthal number\t:\t" << Jacobsthal(n) << endl; 37 | cout << "\nJacobsthal-Lucas number\t:\t" << Jacobsthal_Lucas(n) << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Find the Missing Number/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_2.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | 15 | int getMissingNo (vector a) { 16 | unsigned long n = a.size(); 17 | int first = a[0]; 18 | for (int i = 1; i< n; i++) { 19 | first ^= a[i]; 20 | } 21 | int second = 1; 22 | for (int i = 2; i <= n+1; i++) { 23 | second ^= i; 24 | } 25 | return (first^second); 26 | } 27 | 28 | int main() { 29 | int n; 30 | cout << "\nEnter Size\t:\t"; 31 | cin >> n; 32 | vector a(n); 33 | cout << "\nEnter Elements\n"; 34 | for ( int i = 0; i < n; i++ ) { 35 | cin >> a[i]; 36 | } 37 | cout << "\nMissing Number\t:\t" << getMissingNo(a); 38 | a.clear(); 39 | cout << endl; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Permutation Coefficient/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | 12 | int permutationCoeff(int n, int k) { 13 | int P[n + 1][k + 1]; 14 | for (int i = 0; i <= n; i++) { 15 | for (int j = 0; j <= min(i, k); j++) { 16 | if (j == 0) { 17 | P[i][j] = 1; 18 | } 19 | else { 20 | P[i][j] = P[i - 1][j] + (j * P[i - 1][j - 1]); 21 | } 22 | P[i][j + 1] = 0; 23 | } 24 | } 25 | return P[n][k]; 26 | } 27 | 28 | 29 | int main () { 30 | cout << "\nEnter n\t:\t"; 31 | int number; 32 | cin >> n; 33 | cout << "\nEnter k\t:\t"; 34 | int k; 35 | cin >> k; 36 | cout <<"\nThe result is\t:\t" << permutationCoeff(n,k); 37 | cout << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Subset Sum Problem/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | #include 9 | using namespace std; 10 | bool isSubsetSum(int* set, int n, int sum) { 11 | if (sum == 0) 12 | return true; 13 | if (n == 0 && sum != 0) 14 | return false; 15 | if (set[n-1] > sum) 16 | return isSubsetSum(set, n-1, sum); 17 | return isSubsetSum(set, n-1, sum) || isSubsetSum(set, n-1, sum-set[n-1]); 18 | } 19 | 20 | int main() { 21 | int n; 22 | cout << "\nEnter Size of Set\t:\t"; 23 | cin >> n; 24 | int a[n]; 25 | cout << "\nEnter Set Elements\n"; 26 | for(int i = 0; i < n ; i++ ) { 27 | cin >> a[i]; 28 | } 29 | int sum; 30 | cout << "\nEnter sum value\t:\t"; 31 | cin >> sum; 32 | if (isSubsetSum(set, n, sum) == true) 33 | cout << "\nTrue\n"; 34 | else 35 | cout << "\nFalse\n"; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Stolen Values Problem/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | /* 10 | Space Complexity : O(n) 11 | Time Complexity : O(n) 12 | 13 | */ 14 | #include 15 | using namespace std; 16 | 17 | int maxStolen(int n, int values[]) { 18 | int dp[n+1]; 19 | dp[0] = 0; 20 | dp[1] = values[1]; 21 | 22 | for(int i = 2; i <= n; i++) { 23 | dp[i] = max( dp[i-1] , dp[i-2]+values[i] ); 24 | } 25 | return dp[n]; 26 | } 27 | 28 | 29 | int main() { 30 | int n; 31 | cout << "\nEnter the number of houses\t:\t"; 32 | cin >> n; 33 | int values[n+1]; 34 | cout << "\nEnter the values in the houses\n" << endl; 35 | for( int i = 1; i <= n; i++) { 36 | cin >> values[i]; 37 | } 38 | cout << "\nMaximum stolen value\t:\t" << maxStolen(n, values); 39 | cout< 10 | using namespace std; 11 | int findDepthRec(string tree, int n, int& index) { 12 | if (index >= n || tree[index] == 'L') { 13 | return 0; 14 | } 15 | index++; 16 | int left = findDepthRec(tree, n, index); 17 | index++; 18 | int right = findDepthRec(tree, n, index); 19 | 20 | return max(left, right) + 1; 21 | } 22 | 23 | int findDepth(string str) { 24 | int index = 0; 25 | int n = int(str.length()); 26 | return findDepthRec(str, n, index); 27 | } 28 | 29 | int main() { 30 | string str; 31 | cout << "\nEnter Preorder N for Node and L for Leave\t:\t"; 32 | cin >> str; 33 | cout << "\nDepth of Tree\t:\t" << findDepth(str) << endl; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Longest Common Subsequence/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int lcs( string X, string Y, long m, long n ) { 14 | int L[m+1][n+1]; 15 | int i, j; 16 | for (i=0; i<=m; i++) { 17 | for (j=0; j<=n; j++) { 18 | if (i == 0 || j == 0) 19 | L[i][j] = 0; 20 | 21 | else if (X[i-1] == Y[j-1]) 22 | L[i][j] = L[i-1][j-1] + 1; 23 | 24 | else 25 | L[i][j] = max(L[i-1][j], L[i][j-1]); 26 | } 27 | } 28 | return L[m][n]; 29 | } 30 | 31 | int main() { 32 | string s1 , s2; 33 | cout << "\n1st String\t:\t"; 34 | cin >> s1; 35 | cout << "\n2nd String\t:\t"; 36 | cin >> s2; 37 | cout << "\nLength of LCS\t:\t" << lcs( s1, s2 , s1.length(), s2.length() ) << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Ugly Numbers/code_1.cpp: -------------------------------------------------------------------------------- 1 | 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | // CPP program to find nth ugly number 10 | #include 11 | using namespace std; 12 | int maxDivide(int a, int b) { 13 | while (a%b == 0) { 14 | a = a/b; 15 | } 16 | return a; 17 | } 18 | 19 | int isUgly(int num) { 20 | num = maxDivide(num, 2); 21 | num = maxDivide(num, 3); 22 | num = maxDivide(num, 5); 23 | 24 | return (num == 1)? 1 : 0; 25 | } 26 | int getNthUglyNo(int n) { 27 | int i = 1; 28 | int count = 1; 29 | while (n > count) { 30 | i++; 31 | if (isUgly(i)) 32 | count++; 33 | } 34 | return i; 35 | } 36 | int main() { 37 | cout << "\nEnter Number\t:\t"; 38 | unsigned int number; 39 | cin >> number; 40 | cout <<"\nThe result is\t:\t" << getNthUglyNo(number); 41 | cout << endl; 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Kth Smallest Element/code_6.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_6.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | int kthSmallest(int arr[], int n, int k) { 15 | set s; 16 | for (int i = 0; i < n; i++) { 17 | s.insert(arr[i]); 18 | } 19 | auto it = s.begin(); 20 | for (int i = 0; i < k - 1; i++) { 21 | it++; 22 | } 23 | return *it; 24 | } 25 | 26 | int main() { 27 | int n; 28 | cout << "\nEnter Size\t:\t"; 29 | cin >> n; 30 | int *a = new int[n]; 31 | cout << "\nEnter Array Elements\n"; 32 | for ( int i = 0; i < n; i++ ) { 33 | cin >> a[i]; 34 | } 35 | int k; 36 | cout << "Enter K\t:\t"; 37 | cin >> k; 38 | cout << "\nKth Smallest Element\t:\t" << kthSmallest( a , n , k ) << endl; 39 | delete[] a; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Largest Sum Contiguous Subarray/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int maxSubArraySum(int a[], int size) { 13 | int max_so_far = 0, max_ending_here = 0; 14 | for (int i = 0; i < size; i++) 15 | { 16 | max_ending_here = max_ending_here + a[i]; 17 | if (max_ending_here < 0) 18 | max_ending_here = 0; 19 | else if (max_so_far < max_ending_here) 20 | max_so_far = max_ending_here; 21 | } 22 | return max_so_far; 23 | } 24 | 25 | int main() { 26 | int n; 27 | cout << "\nEnter Size of Set\t:\t"; 28 | cin >> n; 29 | int a[n]; 30 | cout << "\nEnter Set Elements\n"; 31 | for(int i = 0; i < n ; i++ ) { 32 | cin >> a[i]; 33 | } 34 | cout << "\nMaximum contiguous sum is " << maxSubArraySum(a, n); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Minimum Sum of Absolute Difference of Pairs of Two Arrays/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | int MinSum(int a[], int b[], int n) { 14 | sort(a, a+n); 15 | sort(b, b+n); 16 | int sum = 0 ; 17 | for (int i = 0; i < n; i++ ) { 18 | sum = sum + abs(a[i]-b[i]); 19 | } 20 | return sum; 21 | } 22 | 23 | int main() { 24 | int n; 25 | cout << "\nEnter Size\t:\t"; 26 | cin >> n; 27 | int *a = new int[n]; 28 | int *b = new int[n]; 29 | cout << "\nEnter First Array Elements\n"; 30 | for ( int i = 0; i < n; i++ ) { 31 | cin >> a[i]; 32 | } 33 | cout << "\nEnter Second Array Elements\n"; 34 | for ( int i = 0; i < n; i++ ) { 35 | cin >> b[i]; 36 | } 37 | cout << "Minimum Sum\t:\t" << MinSum(a, b, n) << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Coin Change/code_3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace std; 11 | int count( int *S, int m, int n ) { 12 | int table[n+1]; 13 | memset(table, 0, sizeof(table)); 14 | table[0] = 1; 15 | for(int i=0; i> number_coin; 25 | int array[number_coin]; 26 | cout << "\nEnter Coins\n"; 27 | for( int i = 0; i < number_coin; ++i ) { 28 | cin >> array[i]; 29 | } 30 | cout << "\nValue for Coin Change\t:\t"; 31 | int number; 32 | cin >> number; 33 | cout <<"\nThe result is\t:\t" << count(array , number_coin , number ); 34 | cout << endl; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Search An Element in Sorted and Rotated Array/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Data Structure 4 | // 5 | // Created by Mohd Shoaib Rayeen on 19/03/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | int search(int arr[], int l, int h, int key) { 10 | for(int i = l; i <= h; i++ ) { 11 | if(arr[i] == key ) { 12 | return i; 13 | } 14 | } 15 | return -1; 16 | } 17 | 18 | int main() { 19 | int n; 20 | cout << "\nEnter Number of Elements\t:\t"; 21 | cin >> n; 22 | cout << "\nEnter Array Element\t:\t"; 23 | int array[n]; 24 | for ( int i = 0; i < n; i++ ) { 25 | cin >> array[i]; 26 | } 27 | cout << "\nEnter Key Element\t:\t"; 28 | int key; 29 | cin >> key; 30 | int i = search(array, 0, n-1, key); 31 | if (i != -1) { 32 | cout << "\nKey Found at Index\t:\t" << i << endl; 33 | } 34 | else { 35 | cout << "\nKey not found\n"; 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Find A Peak Element/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | int findPeakUtil(int arr[], int low, int high, int n) { 14 | int mid = low + (high - low)/2; 15 | if ((mid == 0 || arr[mid-1] <= arr[mid]) && (mid == n-1 || arr[mid+1] <= arr[mid])) { 16 | return mid; 17 | } 18 | else if (mid > 0 && arr[mid-1] > arr[mid]) { 19 | return findPeakUtil(arr, low, (mid -1), n); 20 | } 21 | else return findPeakUtil(arr, (mid + 1), high, n); 22 | } 23 | 24 | int main() { 25 | int n; 26 | cout << "\nEnter Size\t:\t"; 27 | cin >> n; 28 | int arr[n]; 29 | cout << "\nEnter Array Elements\n"; 30 | for( int i = 0; i < n; i++ ) { 31 | cin >> arr[i]; 32 | } 33 | cout << "\nIndex of a peak point\t:\t" << findPeakUtil(arr,0 , n-1 , n) << endl; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Largest Sum Contiguous Subarray/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 31/07/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int maxSubArraySum(int a[], int size) { 13 | int max_so_far = INT_MIN, max_ending_here = 0; 14 | for (int i = 0; i < size; i++) { 15 | max_ending_here = max_ending_here + a[i]; 16 | if (max_so_far < max_ending_here) 17 | max_so_far = max_ending_here; 18 | 19 | if (max_ending_here < 0) 20 | max_ending_here = 0; 21 | } 22 | return max_so_far; 23 | } 24 | 25 | int main() { 26 | int n; 27 | cout << "\nEnter Size of Set\t:\t"; 28 | cin >> n; 29 | int a[n]; 30 | cout << "\nEnter Set Elements\n"; 31 | for(int i = 0; i < n ; i++ ) { 32 | cin >> a[i]; 33 | } 34 | cout << "\nMaximum contiguous sum is " << maxSubArraySum(a, n); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Rencontres Number/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 13/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | #include 9 | using namespace std; 10 | 11 | int binomialCoeff(int n, int k) { 12 | if (k==0 || k==n) 13 | return 1; 14 | return binomialCoeff(n-1, k-1) + binomialCoeff(n-1, k); 15 | } 16 | 17 | int RencontresNumber(int n, int m) { 18 | if (n == 0 && m == 0) { 19 | return 1; 20 | } 21 | if (n == 1 && m == 0) { 22 | return 0; 23 | } 24 | if (m == 0) { 25 | return (n - 1) * (RencontresNumber(n - 1, 0) + RencontresNumber(n - 2, 0)); 26 | } 27 | return binomialCoeff(n, m) * RencontresNumber(n - m, 0); 28 | } 29 | 30 | int main() { 31 | int n , m; 32 | cout << "\nEnter n\t:\t"; 33 | cin >> n; 34 | cout << "\nEnter m\t:\t"; 35 | cin >> m; 36 | cout << "\nResult\t:\t" << RencontresNumber(n, m) << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Elements that Occurred Only Once/code_2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_2.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | void occurredOnce(int arr[], int n) { 15 | unordered_map mp; 16 | for (int i = 0; i < n; i++) { 17 | mp[arr[i]]++; 18 | } 19 | for (auto it = mp.begin(); it != mp.end(); it++) { 20 | if (it->second == 1) { 21 | cout << it->first << " "; 22 | } 23 | } 24 | } 25 | 26 | int main() { 27 | int n; 28 | cout << "\nEnter Size\t:\t"; 29 | cin >> n; 30 | int *a = new int[n]; 31 | cout << "\nEnter Array Elements\n"; 32 | for ( int i = 0; i < n; i++ ) { 33 | cin >> a[i]; 34 | } 35 | cout << "\nElement that appeared once\t:\t"; 36 | occurredOnce( a , n); 37 | delete[] a; 38 | cout << endl; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /DATA STRUCTURES/Array/Move All Zeros to end of Array/code_1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // code_1.cpp 3 | // Algorithm 4 | // 5 | // Created by Mohd Shoaib Rayeen on 23/11/18. 6 | // Copyright © 2018 Shoaib Rayeen. All rights reserved. 7 | // 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | void moveZerosToEnd(int arr[], int n) { 14 | int count = 0; 15 | for (int i = 0; i < n; i++) { 16 | if (arr[i] != 0) { 17 | swap(arr[count++], arr[i]); 18 | } 19 | } 20 | } 21 | 22 | void printArray(int arr[], int n) { 23 | for (int i = 0; i < n; i++) { 24 | cout << arr[i] << " "; 25 | } 26 | cout << "\n"; 27 | } 28 | 29 | int main() { 30 | int n; 31 | cout << "\nEnter Size\t:\t"; 32 | cin >> n; 33 | int *a = new int[n]; 34 | cout << "\nEnter Array Elements\n"; 35 | for ( int i = 0; i < n; i++ ) { 36 | cin >> a[i]; 37 | } 38 | moveZerosToEnd(a, n); 39 | cout << "\nArray After Moving Zeros\t:\t"; 40 | printArray(a, n); 41 | return 0; 42 | } 43 | --------------------------------------------------------------------------------