This repository contains data structure programs and solutions in C++ of a problem using different techniques like Dynamic Programming , Greedy Algorithms , Divide and Conquer , Backtracking etc.
Dynamic Programming
Dynamic Programming is a method for solving a complex problem by breaking it down into a
collection of simpler subproblems, solving each of those subproblems just once, and storing
their solutions using a memory-based data structure (array, map,etc). Each of the subproblem
solutions is indexed in some way, typically based on the values of its input parameters, so
as to facilitate its lookup. So the next time the same subproblem occurs, instead of recomputing
its solution, one simply looks up the previously computed solution, thereby saving computation
time. This technique of storing solutions to subproblems instead of recomputing them is called
memoization.
- 0-1 Knapsack Problem
- Bell Numbers
- Binomial Coefficient
- Coin Change
- Compute nCr mod p
- Count All Subsequences having Product Less than K
- Count Balanced Binary Trees of Height h
- Count Distinct Subsequences
- Count of Different Ways to Express N As The Sum of 1, 3 And 4
- Count Number of Increasing SubSequence
- Count Number of Subsets having Given XOR Value
- Count Number of Ways to jump to Reach End
- Count Number of Ways to Reach A Given Score in A Game
- Count Ways to Build Street Under Given Constraints
- Count Ways to Reach the Nth Stair Using Step 1, 2 or 3
- Cutting A Rod
- Delannoy Number
- Dice Throw Problem
- Edit Distance
- Egg Dropping Puzzle
- Entringer Number
- Eulerian Number
- Find Maximum Possible Stolen Value From Houses
- Friends Pairing Problem
- Golomb Sequence
- Highway Billboard Problem
- Jacobsthal and Jacobsthal-Lucas numbers
- Longest Arithmetic Progression
- Largest Sum Contiguous Subarray
- Longest Bitonic Subsequence
- Longest Common Increasing Subsequence
- Longest Common Subsequence
- Longest Common Substring
- Longest Geometric Progression
- Longest Increasing Subsequence
- Longest Palindromic Subsequence
- Longest Repeated Subsequence
- Longest Subsequence
- Matrix Chain Multiplication
- Maximum Length Chain of Pairs
- Maximum Sum Increasing Subsequence
- Maximum Games Played by Winner
- Maximum Length Subsequence
- Maximum Path Sum in A Triangle
- Maximum Product Cutting
- Maximum Product of An Increasing Subsequence
- Maximum Size Square Sub-Matrix with All 1s
- Maximum Size Subset with Given Sum
- Maximum Subsequence Sum
- Maximum Subset Sum With No Repeating Digits
- Maximum Sum of Pairs with Specific Difference
- Minimum Cost to Fill Given Weight in A Bag
- Minimum Insertions to Form a Palindrome
- Minimum Insertions to Sort An Array
- Minimum Number of Jumps to Reach End
- Minimum Partition
- Minimum Sum of Multiplications of N Numbers
- Moser-de Bruijn Sequence
- Newman-Conway Sequence
- Newman-Shanks-Williams Prime
- Nth Catalan Number
- Number of N Digit(s) Stepping Numbers
- Optimal Strategy for a Game
- Painting Fence Algorithm
- Palindrome Partitioning
- Perfect Sum Problem
- Permutation Coefficient
- Size of The Subarray With Maximum Sum
- Smallest Sum Contiguous Subarray
- Stolen Values Problem
- Subset Sum Problem
- Sum of All Substrings of A String Representing A Number
- Sum of Average of All Subsets
- Super Ugly Number
- Temple Offerings
- Tile Stacking Problem
- Tiling Problem
- Tiling with Dominoes
- Ugly Numbers
- Unbounded Knapsack
- Weighted Job Scheduling
- Wildcard Pattern Matching
Greedy Algorithms
A greedy algorithm, as the name suggests, always makes the choice that seems to be the
best at that moment. This means that it makes a locally-optimal choice in the hope that
this choice will lead to a globally-optimal solution.
- Dijkstra's Algorithm
- Find Minimum number of Coins
- Fractional Knapsack Problem
- Maximize Array Sum After K Negations
- Maximize the sum of arr[i] x i
- Maximum Product Subset of an Array
- Minimum Product Subset of an Array
- Minimum Sum of Absolute Difference of Pairs of Two Arrays
- Minimum Sum of Product of Two Arrays
- Split A Number into Maximum Composite Number
Divide and Conquer
A typical Divide and Conquer algorithm solves a problem using following three steps.
- Divide: Break the given problem into subproblems of same type.
- Conquer: Recursively solve these subproblems
- Combine: Appropriately combine the answers
- Binary Search
- Count Inversions in an Array
- Cubic Root of A Number
- Find A Fixed Point in A Given Array
- Find A Peak Element
- Find Bitonic Point in Given Bitonic Sequence
- Find Closest Number in Array
- Find the Element that appears once in a Sorted Array
- Floor in a Sorted Array
- K-th Element of Two Sorted Arrays
- Longest Common Prefix
- Majority Element
- Maximum Contiguous Subarray Sum
- Median of Two Sorted Arrays of Same Size
- Number of Zeros
- Rotation Count in Rotated Sorted Array
Backtracking
Backtracking is a general algorithm for finding all (or some) solutions to some
computational problems, notably constraint satisfaction problems, that incrementally
builds candidates to the solutions, and abandons a candidate ("backtracks") as soon
as it determines that the candidate cannot possibly be completed to a valid solution.
- Boggle
- Combinational Sum
- Graph Coloring Problem
- Hamiltonian Cycle
- Minimum Number of Jumps to Reach ( M , N ) From ( 1 , 1 )
- N Queen Problem
- Power Set in Lexicographical Order
- Rat in A Maze
- Remove Invalid Parentheses
- Subset Sum Problem
- Sudoku
- The Knight’s Tour Problem
Miscellaneous
Except above algorithm design techniques , here's some important
algorithms.
- Dutch National Flag Algorithm
- Floyd’s Cycle detection algorithm
- MO’s Algorithm
- Reservoir Sampling
- String Matching Algorithms
- The Celebrity Problem
Array/Vector
An array is a collection of items stored at contiguous memory locations. The idea is to
store multiple items of the same type together. This makes it easier to calculate the position
of each element by simply adding an offset to a base value, i.e., the memory location of the
first element of the array (generally denoted by the name of the array).
Vector is Dynamic Array.
Matrix is 2D Array.
- Arrange Given Numbers to Form the Biggest Number
- Array Range Queries for Searching An Element
- Array Rearrangement by Shifting Zero to end
- Check if An Array is Sorted and Rotated
- Chocolate Distribution Problem
- Convert Array into Zig-Zag Fashion
- Count Number of Primes in Given Range
- Count Smaller Elements On Right Side
- Count Strictly Increasing Subarrays
- Elements that Occurred Only Once in An Array
- Find The Largest Pair Sum in An Unsorted Array
- Find The Largest Three Elements in An Array
- Find the Missing Number
- Find The Smallest Missing Number
- Kth Smallest/Largest Element
- Largest SubArray with Equal Number of 1s and 0s
- Maximize Array Sum After K Negations
- Maximum of All SubArrays of Size k
- Maximum Product Subarray
- Maximum Sum Such That No Two Elements Are Adjacent
- Mean and Median for Unsorted Array
- Mean of Range in An Array
- Merge Overlapping Intervals
- Min-Max Range Queries in Array
- Move All Zeros to end of Array
- Next Greater Element
- Positive Elements at Even and Negative at Odd Position in An Array
- Product of Ranges in An Array
- Query Square Root Decomposition
- Range LCM Queries
- Rearrange An Array in Order - Smallest - S , Largest - L , 2nd S , 2nd L
- Replace Array Element by Multiplication of Previous and Next
- Search An Element in Sorted and Rotated Array
- Segregate 0s and 1s in an Array
- Segregate Even and Odd Numbers
- Shortest Un-ordered SubArray
- Sort An Array of 0s , 1s and 2s
- Total Numbers With No Repeated Digits in A Range
Matrix
Matrix is 2D Array. A matrix is defined as an ordered rectangular array of
numbers. They can be used to represent systems of linear equations
Linked List
A linked list is a linear data structure, in which the elements are not stored at
contiguous memory locations.
- Detect Loop in Singly Linked List
- Insertion & Deletion in Singly Linked List
- Reverse Singly Linked List in Pairs
Binary Tree
A tree whose elements have at most 2 children is called a binary tree. Since each element
in a binary tree can have only 2 children, we typically name them the left and right child.
- Calculate Depth of A Full Binary Tree From Preorder
- Calculate Size of Tree
- Check If All Leaves are At Same Level
- Check If Binary Tree Has Duplicate Values
- Check if Given Binary Tree is Sum Tree Or Not
- Check if Tree is Symmetry or Not
- Check If Two Trees are Identical or Not
- Check if Two Trees are Mirror
- Check Whether A Binary Tree is Complete Or Not
- Check Whether A Given Binary Tree is Perfect or Not
- Construct A Tree From Inorder and Level Order
- Construct Tree From Given Inorder and Preorder
- Convert A Tree To Sum Tree
- Convert Binary Tree to Doubly Linked List
- Density of Binary Tree
- Diagonal Sum of A Binary Tree
- Diagonal Traversal of Binary Tree
- Diameter of Binary Tree
- Find All Root to Leaf Paths
- Find Distance Between Two Nodes
- Find Largest Subtree Sum in A Binary Tree
- Find The Deepest Node
- Find The Maximum Sum In A Path From Leaf To Root
- Flip Binary Tree
- Insertion and Traversal
- Left , Right and Boundary View
- Level Order Traversal in Spiral Form
- Kth Ancestor of A Node
- Maximum Spiral Sum
- Maximum Value in A Binary Tree
- Merge Two Binary Trees By Doing Node Sum
- Minimum Value in A Binary Tree
- Number of Binary Trees for Given Preorder Sequence Length
- Nth Node in Inorder Traversal
- Print All Full Nodes in Binary Tree
- Print Ancestors of A Given Node
- Print Level Traversal in Sorted Order
- Print Postorder Traversal From Given Inorder And Preorder Traversals
- Reverse Level Order Traversal
- Sum of All the Parent Node Having Child N
- Sum of All Leaf Nodes of Binary Tree
- Sum of All Left Leaves
- Sum of All Nodes in Binary Tree
- Sum of All Non-Leaf Node
- Sum Of All Right Leaves
- Sum of Heights of All Individual Nodes in Binary Tree
- Sum of Leaf Nodes At Minimum Level
- Top View of Binary Tree
- Vertical Order Of Binary Tree
- Vertical Sum In Binary Tree
Binary Search Tree
Binary Search Tree is a node-based binary tree data structure which has the following properties:
- The left subtree of a node contains only nodes with keys lesser than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- The left and right subtree each must also be a binary search tree.
Heap
A Heap is a special Tree-based data structure in which the tree is a complete binary tree.
Generally, Heaps can be of two types:
- Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys
present at all of it’s children. The same property must be recursively true for all
sub-trees in that Binary Tree.
- Min-Heap: In a Min-Heap the key present at the root node must be minimum among the keys
present at all of it’s children. The same property must be recursively true for all
sub-trees in that Binary Tree.
Hash Map
Hashing is an important Data Structure which is designed to use a special function
called the Hash function which is used to map a given value with a particular key for
faster access of elements. The efficiency of mapping depends of the efficiency of the
hash function used.
Graph
A Graph is a non-linear data structure consisting of nodes and edges. The nodes are
sometimes also referred to as vertices and the edges are lines or arcs that connect any
two nodes in the graph.
Trie
In computer science, a trie, also called digital tree, radix tree or prefix tree is a
kind of search tree—an ordered tree data structure used to store a dynamic set or associative
array where the keys are usually strings. Unlike a binary search tree, no node in the tree
stores the key associated with that node; instead, its position in the tree defines the key
with which it is associated. All the descendants of a node have a common prefix of the string
associated with that node, and the root is associated with the empty string. Keys tend to be
associated with leaves, though some inner nodes may correspond to keys of interest. Hence,
keys are not necessarily associated with every node. For the space-optimized presentation of
prefix tree, see compact prefix tree.