Welcome to my DSA (Data Structures and Algorithms) journey! This repository is a personal log of the concepts Iβve learned, problems Iβve solved, and progress Iβve made while mastering DSA. It serves both as a revision notebook and a motivation tracker.
- Topics Covered:
- Binary Search
- Pivot in Array
- β
Approach: Binary search for
arr[mid] < arr[mid+1]to locate the increasing slope end. - π‘ Method Used:
peakIndexInMountainArray(vector<int>& arr) - π» C++ Snippet:
while (low < high) {
int mid = low + (high - low) / 2;
if (arr[mid] < arr[mid + 1]) low = mid + 1;
else high = mid;
}- β Approach: Two binary searches: one for the first and one for the last occurrence.
- π‘ Method Used:
searchRange(vector<int>& nums, int target) - π» C++ Snippet:
int binarySearch(bool findFirst) {
int res = -1;
while (low <= high) {
int mid = (low + high) / 2;
if (nums[mid] == target) {
res = mid;
findFirst ? high = mid - 1 : low = mid + 1;
}
else if (nums[mid] < target) low = mid + 1;
else high = mid - 1;
}
return res;
}- Topics Covered:
- Binary Search Applications
- β Approach: Modified binary search using pivot logic.
- π‘ Method Used:
search(vector<int>& nums, int target)
- β Approach: Binary search between 1 and x/2, compare mid*mid.
- π‘ Method Used:
mySqrt(int x)
- Topics Covered:
- Strings
- String Arrays
- β Approach: Two pointers from both ends, skip non-alphanumerics.
- β Approach: Trim + reverse each word + reverse entire string.
- Topics Covered:
- Strings
- Sliding Window
- β Approach: Sliding window to compare substring matches.
- β Approach: Compare frequency maps of sliding window and target.
- β Approach: Use two pointers to modify array in-place.
- Topics Covered:
- 2D Arrays
- β Approach: Traverse columns alternatively top to bottom and bottom to top.
- β Approach: Maintain 4 boundaries and iterate in spiral fashion.
- Topics Covered:
- 2D Arrays
- β Approach: Flatten the matrix logically and apply binary search.
- β Approach: Start from top-right, eliminate row or column based on comparison.
- Topics Covered:
- Recursion
- β Approach: Pop elements recursively and insert them back in sorted order.
- π» C++ Snippet:
void insert(vector<int>& arr, int temp) {
if (arr.empty() || arr.back() <= temp) {
arr.push_back(temp);
return;
}
int val = arr.back(); arr.pop_back();
insert(arr, temp);
arr.push_back(val);
}- Topics Covered:
- Recursion
- β Approach: Base case and recursive reduction of search space.
- Topics Covered:
- Recursion
1. Subsets
- β Approach: Backtracking to include/exclude elements.
2. Subsets II
- β Approach: Same as Subsets but skip duplicates using sorted array.
- Topics Covered:
- Recursion
- β Approach: Map each digit to possible characters and recurse to build combinations.
- π» C++ Snippet:
void backtrack(string& digits, int index, string& path, vector<string>& res, vector<string>& map) {
if (index == digits.size()) {
res.push_back(path);
return;
}
for (char c : map[digits[index] - '0']) {
path.push_back(c);
backtrack(digits, index + 1, path, res, map);
path.pop_back();
}
}- Topics Covered:
- Linked List
- β Approach: Iteratively reverse the pointers using three variables.
- π» C++ Snippet:
ListNode* reverseList(ListNode* head) {
ListNode* prev = nullptr;
while (head) {
ListNode* next = head->next;
head->next = prev;
prev = head;
head = next;
}
return prev;
}- Topics Covered:
- Linked List
- β Approach: Reverse the part of linked list which from left to right and then connect the reversed linked list to the res of the elements. i.e left to prevright-> and right to x where x -> next = prevleft
- π» C++ Snippet:
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int left, int right) {
if (!head || left == right) return head;
// Create a dummy node to simplify edge cases
ListNode* dummy = new ListNode(0);
dummy->next = head;
// Step 1: Move to node before 'left'
ListNode* prevLeft = dummy;
for (int i = 1; i < left; ++i) {
prevLeft = prevLeft->next;
}
// Step 2: Reverse from left to right
ListNode* curr = prevLeft->next; // Node at 'left'
ListNode* prev = nullptr;
ListNode* tail = curr; // This will become the tail after reversing
for (int i = 0; i < right - left + 1; ++i) {
ListNode* nextTemp = curr->next;
curr->next = prev;
prev = curr;
curr = nextTemp;
}
// Step 3: Connect reversed part with remaining list
prevLeft->next = prev; // 'prev' is new head of reversed sublist
tail->next = curr; // 'curr' is the node after 'right'
return dummy->next;
}
};
To build a strong foundation in core DSA concepts and solve a wide range of problems with increasing difficulty, preparing for technical interviews and competitive programming.
- Language: Python / C++
- Platform: LeetCode, Codeforces, GeeksforGeeks
- Version Control: Git + GitHub
Feel free to explore the daily logs, code snippets, and approaches. You can fork this repository and start your own DSA journey too!
If you're also on your DSA journey, letβs connect and grow together!