Skip to content

Commit a748c54

Browse files
authored
Merge pull request Pradeepsingh61#586 from DeveloperViraj/docs/update-readme-schedule
feat(revision): add Day 01 - Two Pointers / Hashing + README plan update
2 parents 715ce70 + 3f363e7 commit a748c54

4 files changed

Lines changed: 389 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 25 Revision Questions — C++ (DSA patterns)
2+
3+
Curated 25-day DSA revision set in C++ by `DeveloperViraj` (single-author).
4+
Each folder contains short explanation + a runnable `.cpp` solution.
5+
6+
**Structure**
7+
- `day-xx-.../` — three problems per day (problem.md + solution `.cpp`).
8+
- `Makefile` — quick compile helper.
9+
- `.github/workflows/ci-cpp.yml` — CI that compiles every `.cpp`.
10+
11+
**Guidelines**
12+
- Maintainer: `DeveloperViraj`. This is a curated, single-author set.
13+
- Please **do not** modify existing day folders. To contribute, add your own revision folder (see below).
14+
- Use `g++ -std=c++17` to compile.
15+
16+
**Goal**
17+
- One day = one pattern (3 problems). Short explanations + complexity notes.
18+
19+
**Planned completion**
20+
- Target: finish Day 1 → Day 25 by **October 25, 2025** (one day per sheet).
21+
22+
**Daily pattern plan (surface-level)**
23+
24+
- Day 01 — Arrays / Two Pointers / Hashing
25+
- Day 02 — Strings (sliding window, parsing)
26+
- Day 03 — Dynamic Programming I (basic DP)
27+
- Day 04 — Dynamic Programming II (advanced DP)
28+
- Day 05 — Trees (binary tree basics)
29+
- Day 06 — Binary Search Trees (BST operations)
30+
- Day 07 — Backtracking (combinatorics)
31+
- Day 08 — Linked Lists (singly/doubly)
32+
- Day 09 — Stacks & Queues (monotonic stacks)
33+
- Day 10 — Graphs I (BFS / DFS)
34+
- Day 11 — Heaps / Priority Queues
35+
- Day 12 — Greedy Algorithms
36+
- Day 13 — Sliding Window & Two Pointers (advanced)
37+
- Day 14 — Bit Manipulation
38+
- Day 15 — Math & Number Theory
39+
- Day 16 — Sorting & Binary Search patterns
40+
- Day 17 — Two Heaps / Running Median
41+
- Day 18 — Union-Find (Disjoint Set)
42+
- Day 19 — Segment Tree / Fenwick Tree (range queries)
43+
- Day 20 — Computational Geometry basics
44+
- Day 21 — Tries / Prefix Trees
45+
- Day 22 — Randomized Algorithms / Reservoir Sampling
46+
- Day 23 — Advanced DP patterns (DP on trees, bitmask DP)
47+
- Day 24 — Recursion & Divide and Conquer patterns
48+
- Day 25 — Review day + mixed mock interview problems
49+
50+
**Contributing (how-to)**
51+
- To contribute, create your own revision folder under the repo root (e.g. `yourname-revision-set/`) and submit a PR. Do not edit the main curated sheets directly; open an issue to propose changes.
52+
53+
Enjoy — practice, learn, repeat.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// 01_3sum.cpp
2+
// Problem: 3Sum (Arrays / Two Pointers + Sorting)
3+
// LeetCode: https://leetcode.com/problems/3sum/
4+
// Author: DeveloperViraj (curated DSA set for Hacktoberfest 2025)
5+
// Compile: g++ -std=c++17 01_3sum.cpp -o 01_3sum
6+
// Run: ./01_3sum
7+
//
8+
// 🧩 Summary:
9+
// Given an integer array 'numbers', return all unique triplets (a, b, c)
10+
// such that a + b + c == 0. Each triplet should be printed one per line.
11+
//
12+
// 🧠 Approach (Two Pointers + Sorting):
13+
// 1. Sort the array.
14+
// 2. For each index i (value a), use two pointers (left, right) to find
15+
// pairs (b, c) such that b + c == -a.
16+
// 3. Skip duplicate values for i, left, and right to ensure uniqueness.
17+
//
18+
// ✅ Correctness:
19+
// Sorting enables ordered traversal and duplicate skipping.
20+
// Two pointers ensure linear-time scanning per fixed element.
21+
//
22+
// ⏱️ Complexity:
23+
// Time: O(n²)
24+
// Space: O(1) (ignoring output storage)
25+
//
26+
// 📘 LeetCode Link: https://leetcode.com/problems/3sum/
27+
// Try it yourself on LeetCode with various input cases.
28+
29+
#include <iostream>
30+
#include <vector>
31+
#include <algorithm>
32+
using namespace std;
33+
34+
vector<vector<int>> findThreeSumTriplets(vector<int> numbers) {
35+
vector<vector<int>> result;
36+
int n = numbers.size();
37+
if (n < 3) return result;
38+
39+
sort(numbers.begin(), numbers.end());
40+
41+
for (int i = 0; i < n; ++i) {
42+
// Skip duplicate 'a' values
43+
if (i > 0 && numbers[i] == numbers[i - 1]) continue;
44+
45+
// No need to continue if current number > 0 (as array is sorted)
46+
if (numbers[i] > 0) break;
47+
48+
int firstNumber = numbers[i];
49+
int target = -firstNumber;
50+
int left = i + 1, right = n - 1;
51+
52+
while (left < right) {
53+
int sum = numbers[left] + numbers[right];
54+
55+
if (sum == target) {
56+
result.push_back({firstNumber, numbers[left], numbers[right]});
57+
58+
// Skip duplicates for left and right
59+
int leftValue = numbers[left], rightValue = numbers[right];
60+
while (left < right && numbers[left] == leftValue) ++left;
61+
while (left < right && numbers[right] == rightValue) --right;
62+
}
63+
else if (sum < target) {
64+
++left;
65+
} else {
66+
--right;
67+
}
68+
}
69+
}
70+
return result;
71+
}
72+
73+
int main() {
74+
cout << "🔹 3Sum Problem — Arrays / Two Pointers\n";
75+
cout << "Input format:\n";
76+
cout << "n\n";
77+
cout << "a0 a1 a2 ... a(n-1)\n\n";
78+
cout << "Enter n and array elements:\n";
79+
80+
int n;
81+
if (!(cin >> n) || n <= 0) {
82+
cerr << "❌ Invalid input. Exiting.\n";
83+
return 0;
84+
}
85+
86+
vector<int> numbers(n);
87+
for (int i = 0; i < n; ++i) cin >> numbers[i];
88+
89+
auto triplets = findThreeSumTriplets(numbers);
90+
91+
if (triplets.empty()) {
92+
cout << "No triplets found.\n";
93+
} else {
94+
cout << "\n✅ Unique Triplets (a + b + c = 0):\n";
95+
for (auto &t : triplets) {
96+
cout << t[0] << " " << t[1] << " " << t[2] << "\n";
97+
}
98+
}
99+
100+
return 0;
101+
}
102+
103+
/*
104+
🔍 Example Input:
105+
6
106+
-1 0 1 2 -1 -4
107+
108+
🧾 Expected Output:
109+
-1 -1 2
110+
-1 0 1
111+
112+
📊 Edge Cases:
113+
1️⃣ n < 3 → "No triplets found"
114+
2️⃣ [0,0,0,0] → one triplet "0 0 0"
115+
3️⃣ All positives → "No triplets found"
116+
4️⃣ Large input → Works efficiently for up to 10^4 elements
117+
118+
🔗 LeetCode: https://leetcode.com/problems/3sum/
119+
*/
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// 02_container_with_most_water.cpp
2+
// Problem: Container With Most Water (Two Pointers / Greedy)
3+
// LeetCode: https://leetcode.com/problems/container-with-most-water/
4+
// Author: DeveloperViraj (curated DSA set for Hacktoberfest 2025)
5+
// Compile: g++ -std=c++17 02_container_with_most_water.cpp -o 02_container_with_most_water
6+
// Run: ./02_container_with_most_water
7+
//
8+
// 🧩 Problem Summary:
9+
// You are given n non-negative integers `height[i]` where each represents a vertical line
10+
// drawn at position i. Two lines form a container with the x-axis that can hold water.
11+
// Find two lines that together can contain the **maximum area of water**.
12+
// The container cannot be tilted.
13+
//
14+
// Example:
15+
// heights = [1,8,6,2,5,4,8,3,7]
16+
// Maximum water area = 49 (between lines with heights 8 and 7)
17+
//
18+
// 🧠 Approach (Two Pointers):
19+
// 1️⃣ Start with two pointers — left at 0 and right at n-1.
20+
// 2️⃣ Compute the area = (right - left) * min(height[left], height[right]).
21+
// 3️⃣ Move the pointer pointing to the *smaller height* inward —
22+
// because only that move can possibly increase the area.
23+
// 4️⃣ Continue until left >= right.
24+
//
25+
// ✅ Why it works:
26+
// The height of water is limited by the shorter of the two lines.
27+
// Moving the taller line inward won’t help — it only reduces width
28+
// without increasing the limiting height.
29+
//
30+
// ⏱️ Complexity:
31+
// Time: O(n)
32+
// Space: O(1)
33+
//
34+
// 📘 LeetCode Link:
35+
// https://leetcode.com/problems/container-with-most-water/
36+
// Try it out on LeetCode to test the same logic.
37+
38+
#include <iostream>
39+
#include <vector>
40+
#include <algorithm>
41+
using namespace std;
42+
43+
// 🔹 Function to calculate the maximum water area
44+
int maxWaterContainer(vector<int>& heights) {
45+
int left = 0, right = heights.size() - 1;
46+
int maxArea = 0;
47+
48+
while (left < right) {
49+
int width = right - left;
50+
int height = min(heights[left], heights[right]);
51+
int area = width * height;
52+
maxArea = max(maxArea, area);
53+
54+
// Move pointer of smaller height inward
55+
if (heights[left] < heights[right])
56+
++left;
57+
else
58+
--right;
59+
}
60+
61+
return maxArea;
62+
}
63+
64+
int main() {
65+
cout << "🔹 Problem: Container With Most Water\n";
66+
cout << "Input format:\n";
67+
cout << "n\nh0 h1 h2 ... h(n-1)\n\n";
68+
cout << "Enter n and the heights:\n";
69+
70+
int n;
71+
if (!(cin >> n) || n <= 1) {
72+
cerr << "❌ Invalid input. Need at least 2 heights.\n";
73+
return 0;
74+
}
75+
76+
vector<int> heights(n);
77+
for (int i = 0; i < n; ++i)
78+
cin >> heights[i];
79+
80+
int maxArea = maxWaterContainer(heights);
81+
cout << "\n✅ Maximum water that can be contained: " << maxArea << "\n";
82+
83+
return 0;
84+
}
85+
86+
/*
87+
🔍 Example Input:
88+
9
89+
1 8 6 2 5 4 8 3 7
90+
91+
🧾 Expected Output:
92+
✅ Maximum water that can be contained: 49
93+
94+
📊 Explanation:
95+
Pair chosen: heights[1] = 8, heights[8] = 7
96+
Width = 8 - 1 = 7
97+
Height = min(8, 7) = 7
98+
Area = 7 × 7 = 49
99+
100+
📘 LeetCode Link:
101+
https://leetcode.com/problems/container-with-most-water/
102+
103+
🧠 Key Insight:
104+
- The shorter height limits the water area.
105+
- Always move the smaller height inward to explore potential larger areas.
106+
*/
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// 03_longest_consecutive_sequence.cpp
2+
// Problem: Longest Consecutive Sequence (Hashing / unordered_set)
3+
// LeetCode: https://leetcode.com/problems/longest-consecutive-sequence/
4+
// Author: DeveloperViraj (curated DSA set for Hacktoberfest 2025)
5+
// Compile: g++ -std=c++17 03_longest_consecutive_sequence.cpp -o 03_longest_consecutive_sequence
6+
// Run: ./03_longest_consecutive_sequence
7+
//
8+
// 🧩 Problem Summary:
9+
// Given an unsorted array of integers, return the length of the longest
10+
// consecutive elements sequence. The sequence elements can appear in any order.
11+
//
12+
// Example:
13+
// Input: [100, 4, 200, 1, 3, 2]
14+
// Output: 4 (sequence: 1, 2, 3, 4)
15+
//
16+
// 🧠 Approach (Hashing):
17+
// 1️⃣ Insert all numbers into an unordered_set for O(1) lookups.
18+
// 2️⃣ Iterate through each number. If (number - 1) doesn’t exist in the set,
19+
// treat this number as the start of a new sequence.
20+
// 3️⃣ Count consecutive numbers (number + 1, number + 2, …) until the sequence breaks.
21+
// 4️⃣ Track the maximum sequence length found.
22+
//
23+
// ✅ Why it works:
24+
// - Each number is checked at most twice: once for start check, once while counting.
25+
// - By starting only at sequence beginnings, we avoid redundant checks.
26+
//
27+
// ⏱️ Complexity:
28+
// Time: O(n) on average (unordered_set gives O(1) lookups)
29+
// Space: O(n) for storing elements in the set
30+
//
31+
// 📘 LeetCode Link:
32+
// https://leetcode.com/problems/longest-consecutive-sequence/
33+
// Try it yourself to test your implementation.
34+
35+
#include <iostream>
36+
#include <vector>
37+
#include <unordered_set>
38+
using namespace std;
39+
40+
// 🔹 Function to find longest consecutive sequence length
41+
int longestConsecutiveSequence(const vector<int>& numbers) {
42+
if (numbers.empty()) return 0;
43+
44+
unordered_set<int> values(numbers.begin(), numbers.end());
45+
int bestLength = 0;
46+
47+
for (int num : numbers) {
48+
// Only start if current number is the first in sequence
49+
if (values.find(num - 1) == values.end()) {
50+
int current = num;
51+
int currentLength = 1;
52+
53+
// Walk forward for consecutive numbers
54+
while (values.find(current + 1) != values.end()) {
55+
++current;
56+
++currentLength;
57+
}
58+
59+
bestLength = max(bestLength, currentLength);
60+
}
61+
}
62+
63+
return bestLength;
64+
}
65+
66+
int main() {
67+
cout << "🔹 Problem: Longest Consecutive Sequence\n";
68+
cout << "Input format:\n";
69+
cout << "n\n";
70+
cout << "a0 a1 a2 ... a(n-1)\n\n";
71+
cout << "Enter n and the numbers:\n";
72+
73+
int n;
74+
if (!(cin >> n) || n < 0) {
75+
cerr << "❌ Invalid input. Exiting.\n";
76+
return 0;
77+
}
78+
79+
vector<int> numbers(n);
80+
for (int i = 0; i < n; ++i) cin >> numbers[i];
81+
82+
int answer = longestConsecutiveSequence(numbers);
83+
cout << "\n✅ Longest consecutive sequence length: " << answer << "\n";
84+
85+
return 0;
86+
}
87+
88+
/*
89+
🔍 Example Input:
90+
6
91+
100 4 200 1 3 2
92+
93+
🧾 Expected Output:
94+
✅ Longest consecutive sequence length: 4
95+
96+
📊 Explanation:
97+
Consecutive numbers: 1, 2, 3, 4 (length = 4)
98+
99+
📘 LeetCode:
100+
https://leetcode.com/problems/longest-consecutive-sequence/
101+
102+
📌 Edge Cases:
103+
- Empty array → 0
104+
- All unique non-consecutive → 1
105+
- Handles negatives too (e.g., -2, -1, 0 → 3)
106+
- Duplicates are ignored automatically via unordered_set
107+
108+
🧠 Key Insight:
109+
This avoids sorting (O(n log n)) and achieves O(n) average time
110+
by leveraging hash-based lookups to expand sequences efficiently.
111+
*/

0 commit comments

Comments
 (0)