Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β
Please use the discussions tab for any queries.
Use the index given below to search for problems and solutions. Problems are arranged in non-descending order of difficulty.
Data Structures
Algorithms
- Dynamic programming
- Binary Search
- Sliding Window
- Depth First Search
- Breadth First Search
- Strings
- Two pointers
- Bit manipulation
- Backtracking
Extra
- CodeChef DSA learning series
- Miscellaneous/Uncategorized Problems
- Love Babbar 450 questions DSA sheet
What is data structure(DS)?
- A way of organizing data, so it can be used effectively
Why do we use them?
- To create fast and powerful algorithms
- To make code easier for understanding
What is abstract data type(ADT)?
- An abstraction of a DS which provides only an interface. An interface is not specific to any programming language, instead, it's completely free of all of them.
- ADTs are implemented using data structures
Some examples are :
# | ADT | Implementation(DS) |
---|---|---|
1 | Lists | Dynamic Array Linked List |
2 | Queue | Array based queue Stack based queue Linked List based queue |
3 | Maps | Hash table Tree |
What is Big-O and why do we use it?
Big-O notation helps us identify the time and space complexity of an algorithm. There are many other notations as well, like, Big-Theta, or, Big-Omega, but we use Big-O because it deals with the worst case scenario of an algorithm, i.e., when the size of input tends to infinity.
Size of input is denoted by 'n'.
Ascending order of complexity
# | Notation | Name | Example (time) |
---|---|---|---|
1 | O (1) | Constant | Accessing an array |
2 | O (log n) | Logarithmic | Binary search |
3 | O (n) | Linear | Traversing an array |
4 | O (n log n) | Linearithmic | Merge sort |
5 | O (n^2) | Quadratic | 2 Nested loops |
6 | O (n^3) | Cubic | 3 Nested loops |
... | |||
7 | O (nm) | O of n m | Iterating over a matrix of (n X m) |
... | |||
8 | O (b^n) | Exponential | All subsets of a set -> O (2^n) |
9 | O (n!) | Factorial | All permutations of a string |
Property
- Each and every element is indexable(can be referenced with a number) from range 0 to n-1. 'n' is the size of array.
Used
- for sorting
- to access sequential data
- as buffer by I/O routines
- as lookup / inverse lookup tables
- to return multiple values from a function
- as cache in dynamic programming
Types of arrays
- Static : fixed length
- Dynamic : variable length; implemented using static array; when size capacity is reached, a new static array of double size is created and elements are copied.
PROBLEMS on arrays
Key : π’ Easy, π‘ Medium, π΄ Hard
What is it?
- A sequential list of data holding nodes that point to other nodes.
Uses
- Stack, queue, list, circular list implementation
- Adjancy list for graphs
- Separate chaining to deal with hashing collisions
Properties
- Node : contains data and pointer
- Pointer : reference to another node
- Head : first node in the list
- Tail : last node in the list
Types
- Singly linked list : reference to next node only
- Doubly linked list : reference to next and previous nodes
Type | Pros | Cons |
---|---|---|
Singly | Less memory usage, Simple implementation |
Difficult to access previous element |
Doubly | Backward traversal possible | Takes much more memory (Pointers can take lot of memory) |
Complexity Analysis
Action | Singly LL | Doubly LL |
---|---|---|
Search | O (n) | O (n) |
Insert at head | O (1) | O (1) |
Insert at tail | O (1) | O (1) |
Remove at head | O (1) | O (1) |
Remove at tail | O (n) | O (1) |
Remove in between | O (n) | O (n) |
PROBLEMS on linked lists
- Doubly linked list -
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
1472 | Design browser history | C++ | π‘ | Create list structure | |||
0430 | Flatten a multilevel doubly linked list | C++ | O(n) | O(n) | π‘ | Recursion |
Stacks as ADT
We only care about the features and operations of stacks. We don't care about the implementation details. Therefore, I am going to define stack only as mathematical model.
Definition
A list with restriction of insertion and deletion from one end only.
Property
Elements are inserted and removed from the same end, also called, the top of stack. This is not just a property, but a constraint of stack. Hence, stack is called, LAST-IN-FIRST-OUT, or, LIFO, collection of items.
Operations
- Push( ) : for insertion of an element
- Pop( ) : for deletion of an element
Apart from the above mentioned fundamental operations, there can be other operations as well, like :-
- Top( ) : return the top element of stack
- IsEmpty( ) : return TRUE if stack is empty, FALSE if not
Complexity Analysis
All opertaions mentioned above are performed in constant, or, O( 1 ) time.
Overflow condition : O( n ), a larger array is created and all elements are copied. This only happens in array implementation of stack.
Uses
- Function calls / Recursion in dynamic memory allocation
- implement Undo operation
- balance of brackets by a compiler
Implementation of stacks
Stacks can be implemeted in 2 ways :-
-
Stacks using arrays :
// declaration of array that will act as stack int A[n] // pointer to the top of stack top = -1 Push(x){ top = top + 1 A[top] = x } Pop(){ top = top - 1 } Top(){ return A[top] } IsEmpty(){ if (top == -1) return TRUE else return FALSE }
-
Stacks using linked lists :
// structure for linked list struct Node { int data struct Node* link } Push(x) { temp->data = x temp->link = top top = temp } Pop() { temp = top top = top->link free(temp) }
PROBLEMS on Stacks
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
0020 | Valid parantheses | C++ | π’ | ||||
1614 | Maximum nesting depth of the parantheses | C++ | π’ | ||||
1021 | Remove outermost parantheses | C++ | π’ | ||||
1475 | Final prices with a special discount in shop | C++ | π’ | ||||
0232 | Implement queue using stacks | C++ | π’ | ||||
0155 | Min stack | C++ | π’ | ||||
0071 | Simplify path | C++ | π‘ | ||||
1249 | Minimum remove to make valid parantheses | C++ | π‘ | ||||
0739 | Daily temperatures | C++ | π‘ | ||||
0227 | Basic calculator II | C++ | π‘ | ||||
0084 | Largest rectangle in histogram | C++ | π΄ |
PROBLEMS on queues
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
0225 | Implement stack using queues | C++ | π’ | ||||
1823 | Find the winner of the circular game | C++ | π‘ |
PROBLEMS on heaps
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
1354 | Construct target array with multiple sums | C++ | π΄ | ||||
1383 | Maximum performance of a team | C++ | π΄ |
PROBLEMS on trees
PROBLEMS on tries
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
0208 | Implement trie | C++ | π‘ | ||||
0421 | Maximum xor of two numbers in an array | C++ | π‘ | ||||
0211 | Design add and search word data structure | C++ | π‘ | ||||
0745 | Prefix and suffix search | C++ | π‘ | ||||
0336 | Palindrome pairs | C++ | π΄ |
PROBLEMS on graphs
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
0997 | Find the town judge | C++ | π’ | ||||
1557 | Minimum number of vertices to reach all nodes | C++ | π‘ | ||||
0841 | Keys and rooms | C++ | π‘ | ||||
0797 | All paths from source to target | C++ | π‘ | ||||
0310 | Minimum height trees | C++ | π‘ | ||||
2359 | Find closest node to given two nodes | C++ | π‘ |
PROBLEMS on dynamic programming
PROBLEMS on binary search
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
0704 | Binary Search | C++ | π’ | ||||
0278 | First bad version | C++ | π’ | ||||
0035 | Search insert position | C++ | π’ | ||||
0441 | Arranging coins | C++ | π’ | ||||
0875 | Koko eating banans | C++ | π‘ | ||||
0162 | Peak element | C++ | π‘ | ||||
0153 | Find minimum in rotqted sorted array | C++ | π‘ | ||||
0034 | Find first and last position of element in sorted array | C++ | π‘ | ||||
0033 | Search in sorted array | C++ | π‘ | ||||
0074 | Search a 2D matrix | C++ | π‘ | ||||
1237 | Find positive integer solution for a given equation | C++ | π‘ | ||||
0240 | Search a 2D matrix II | C++ | π‘ | ||||
1011 | Capacity to ship packages within D days | C++ | π‘ | ||||
1901 | Find a peak element II | C++ | π‘ | ||||
0668 | Kth smallest number in multiplication table | C++ | π΄ | ||||
0878 | Nth magical number | C++ | π΄ | ||||
0354 | Russian doll envelopes | C++ | π΄ |
PROBLEMS on sliding window
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
0438 | Find all anagrams in a string | C++ | π‘ | ||||
0713 | Subarray product less than k | C++ | π‘ | ||||
0209 | Minimum size subarray sum | C++ | π‘ | ||||
1658 | Minimum operations to reduce x to zero | C++ | π‘ |
PROBLEMS on DFS
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
0463 | Island perimeter | C++ | π’ | Bruteforce, DFS |
|||
0733 | Flood fill | C++ | π’ | ||||
0079 | Word search | C++ | π‘ | Optimised for space | |||
0695 | Max area of island | C++ | π‘ | ||||
0130 | Surrounded regions | C++ | π‘ | ||||
0337 | House robber III | C++ | π‘ | ||||
1306 | Jump game III | C++ | π‘ | ||||
1319 | Number of operations to make network connected | C++ | π‘ | ||||
1379 | Find a corresponding node of a binary tree in a clone of that tree | C++ | π‘ | ||||
1291 | Sequential digits | C++ | π‘ | ||||
0200 | Number of islands | C++ | π‘ | ||||
0547 | Number of provinces | C++ | π‘ | ||||
0980 | Unique paths III | C++ | π΄ |
PROBLEMS on BFS
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
0994 | Rotting oranges | C++ | π‘ | ||||
0116 | Polpulating next right pointers in each node | C++ | π‘ | ||||
0542 | 01 matrix | C++ | π‘ | ||||
0117 | Populating next right pointers in each node II | C++ | π‘ | ||||
1091 | Shortest path in binary matrix | C++ | π‘ |
PROBLEMS on strings
PROBLEMS on two pointers
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
0977 | Squares of a sorted array | C++ | O(n) | π’ | |||
0283 | Move zeroes | C++ | O(n) | π’ | |||
0344 | Reverse string | C++ | π’ | ||||
0557 | Reverse in a string III | C++ | O(n) | π’ | |||
0167 | Two sum II - input array is sorted | C++ | O(log n) | π’ | |||
0189 | Rotate array | C++ | π‘ | ||||
1679 | Max number of K-Sum pairs | C++ | O(n log n) | π‘ | |||
0986 | Interval list intersections | C++ | π‘ | ||||
0844 | Backspace string compare | C++ | π‘ |
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
0231 | Power of two | C++ | O(n), O(1) |
π’ | |||
0191 | Number of 1 bits | C++ | π’ | ||||
0190 | Reverse bits | C++ | O(1) | π’ | |||
0461 | Hamming distance | C++ | π’ | ||||
0268 | Missing number | C++ | π’ | ||||
0342 | Power of four | C++ | π’ | ||||
1342 | Number of steps to reduce a number to zero | C++ | π’ | ||||
1009 | Complement of base 10 integer | C++ | π’ | 2 solutions | |||
0260 | Single number III | C++ | O(1) | π‘ | |||
0318 | Maximum product of word lengths | C++ | π‘ | ||||
0029 | Divide two integers | C++ | O(1) | π‘ | |||
1461 | Check if a strin contains all binary codes of size k | C++ | π‘ | ||||
1178 | Number of valid words for each puzzle | C++ | π΄ |
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
0077 | Combinations | C++ | π‘ | ||||
0046 | Permutations | C++ | π‘ | ||||
0784 | Letter case permutation | C++ | π‘ | ||||
1286 | Iterator for combination | C++ | π‘ | ||||
0131 | Palindrome partitioning | C++ | π‘ | ||||
0090 | Subsets II | C++ | π‘ | ||||
0017 | Letter combinations of a phone number | C++ | π‘ | ||||
0022 | Generate parantheses | C++ | π‘ | ||||
0051 | N-Queens | C++ | π΄ | Typical, Important | |||
0037 | Sudoku solver | C++ | π΄ |
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
BUYPLSE | Buy please | C | π’ | ||||
ISBOTH | Is both or not | C | π’ | ||||
DIFACTRS | Factors finding | C | π’ | ||||
SECLAR | Find second largest | C | π’ | ||||
RNGEODD | Range odd | C | π’ | ||||
VALTRI | Raju and his trip | C | π’ | ||||
REVMEE | Reverse me | C, C++ |
π’ | ||||
FINDMELI | Find me | C | π’ | ||||
TRIVALCH | Valid triangle or not | C, C++ |
π’ | ||||
REVSTRPT | Reverse star pattern | C, C++ |
π’ | ||||
ADDNATRL | Add natural numbers | C, C++ |
π’ | ||||
SUMEVOD | Sum is everywhere | C | π’ | ||||
ANGTRICH | Triangle with angle | C | π’ | ||||
EXTRICHK | Triangle everywhere | C | π’ | ||||
SQUALPAT | Alternative square pattern | C | π’ | ||||
TEST | Life, the universe, and everything | C, C++ |
π’ | ||||
FLOW007 | Reverse the number | C, C++ |
π’ | ||||
LAPIN | Lapindromes | C, C++ |
π’ | ||||
ZCO14003 | Smart phone | C, C++ |
π’ | ||||
CARVANS | Carvans | C, C++ |
π’ | ||||
FCTRL | Factorial | C, C++ |
π’ | ||||
CONFLIP | Coin flip | C | π’ | ||||
LADDU | Laddu | C, C++ |
π’ |
LeetCode
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
1925 | Count square sum triplets | C++ | π’ | ||||
0007 | Reverse integer | C++ | π’ | ||||
0326 | Power of three | C++ | π’ | ||||
1323 | Maximum 69 number | C++ | π’ | greedy | |||
0009 | Palindrome Number | C++ | π’ | ||||
0223 | Rectangle area | C++ | O(1) | π’ | maths | ||
0496 | Next greater element I | C++ | π’ | bruteforce, stacks |
|||
0374 | Guess number higher or lower | C++ | π’ | ||||
0013 | Roman to integer | C++ | π’ | ||||
1523 | Count odd numbers in an integer interval | C++ | π’ | ||||
0201 | Bitwise AND of number range | C++ | π‘ | ||||
0012 | Integer to roman | C++ | π‘ | ||||
0858 | Mirror reflection | C++ | π‘ | ||||
0380 | Insert delete GetRandom O(1) | C++ | O(1) | π‘ | |||
1695 | Maximum erasure value | C++ | π‘ | ||||
0460 | LFU cache | C++ | π΄ |
Codeforces
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
1343A | Candies | C++ | π’ | bruteforce, math |
|||
1525B | Permutation Sort | C++ | π’ | greedy | |||
451A | Game with Sticks | C++ | π’ | ||||
1703A | Yes or yes | C++ | π’ | div4 | |||
1703B | ICPC balloons | C++ | π’ | div4 | |||
1703C | Cypher | C++ | π’ | div4 |
ID | Title | Solution | Time | Space | Difficulty | Tags | Note |
---|---|---|---|---|---|---|---|
0001 | Reverse a string | C++ | π’ | ||||
0002 | Minimum and maximum of an array | C++ | π’ |