LeetCode 78 — Subsets Worked on generating all possible subsets (power set) of a given array of unique elements. Approach — Backtracking (Inclusion / Exclusion) - For every element, there are two choices: include it or exclude it - Used recursion to explore both branches at each index - Base case: when index reaches the array length → store the current subset - Maintained state using add → recurse → remove (backtracking step) - Total subsets formed = 2ⁿ Key learning: - This problem made the recursion tree very clear. - Every element doubles the number of possibilities. Once the inclusion/exclusion pattern is understood, many subset and combination problems follow the same structure. #leetcode #recursion #backtracking #dsa #algorithms #codingjourney #problemSolving
LeetCode 78: Generating Subsets with Backtracking
More Relevant Posts
-
LeetCode 90 — Subsets II Worked on generating all possible subsets of an array that may contain duplicates. This is an extension of the Subsets problem, but duplicates add a small twist. If handled carelessly, duplicate subsets get generated. Approach — Backtracking + Duplicate Skipping - Sorted the array first to group duplicates together - At each index, made the usual include / exclude decision - While skipping (not taking), moved the index forward past duplicate values - Ensured that identical elements don’t start identical branches Core idea :- Instead of filtering duplicates at the end, avoid creating them during recursion itself. Key learning: - Handling duplicates is usually about controlling the starting point of recursion branches. - Small adjustments in index movement can prevent large amounts of redundant work. This felt like a refined version of the basic subsets pattern. #leetcode #recursion #backtracking #dsa #algorithms #problemSolving #codingjourney
To view or add a comment, sign in
-
-
LeetCode 22 — Generate Parentheses Worked on generating all valid combinations of n pairs of parentheses. This problem looks simple at first, just generate combinations, but the real challenge is ensuring only well-formed ones are created. Approach — Backtracking with Constraints - Kept track of number of open and close brackets used - Added "(" only if open < n - Added ")" only if close < open - When length becomes 2 * n, stored the valid string - Avoided generating invalid sequences in the first place Key learning: The important part wasn’t generating combinations — it was controlling the recursion tree with proper constraints. This problem clearly shows how pruning invalid branches early keeps the solution clean and efficient. #leetcode #recursion #backtracking #dsa #algorithms #problemSolving #codingjourney
To view or add a comment, sign in
-
-
#100DaysLeetCode Day 32 ✅ Solved LeetCode 169: Majority Element The task is to find the element that appears more than ⌊n/2⌋ times in the array. 🚀 Approach Used the Boyer–Moore Voting Algorithm. Idea: Maintain a candidate (majority element) and a count. If count becomes 0, update the candidate. If current element equals candidate → increase count. Otherwise → decrease count. Because the majority element appears more than half of the time, it will remain the final candidate. Time Complexity: O(n) Space Complexity: O(1) #100DaysLeetCode #Day32 #LeetCode169 #leetcode #cpp #dsa #arrays #algorithm #problemSolving #coding #interviewprep
To view or add a comment, sign in
-
-
LeetCode 442: Find All Duplicates in an Array Solved LeetCode 442 — Find All Duplicates in an Array. The problem gives an array of size n, where each number is in the range [1, n]. Some numbers appear twice, while others appear once. The task is to return all numbers that appear exactly twice. Approach — Cyclic Placement Pattern Since every number belongs to the range 1 to n, each value should ideally be placed at index value − 1. The steps are: - Traverse the array and attempt to place each number at its correct index. - If the current number is not at its correct position and the correct position does not already contain the same value, swap them. - Otherwise move forward. After arranging the numbers: - Scan the array once more. - Any index i where nums[i] ≠ i + 1 indicates that nums[i] is a duplicate number. This works because duplicates prevent some numbers from reaching their proper positions. The solution runs in: - O(n) time - O(1) extra space (excluding the result list) Accepted on LeetCode. #leetcode #dsa #algorithms #javaprogramming #problemSolving #learninginpublic
To view or add a comment, sign in
-
-
LeetCode Biweekly Contest 177 Approach :- 1) Smallest Pair With Different Frequencies :- Simply brute force to store all the valid pairs, and then through looping into the stored pairs, get the desired output x, y 2) Merge Close Characters :- As size of string is <= 100, so brute force for each character for each i , the maximum j can be max(n-i, i+k) , and get the expected string, 3) Minimum Operations to Make Array Parity Alternating :- There are only two possible valid patterns for an alternating parity array: a) Even, Odd, Even, Odd... b) Odd, Even, Odd, Even... Step 1: Minimize Operations For both patterns: Check each index , If the number’s parity doesn’t match the expected parity, It needs exactly 1 operation Minimum mismatches among the two patterns = Minimum operations required Step 2: Minimize (max − min) For each index: If parity is correct → value stays fixed If parity must change → value can become either a[i] - 1 or a[i] + 1 Now the problem reduces to: From 1 or 2 possible values per index, choose one per index such that the overall range (max − min) is minimized. This can be solved using: Sorting all candidate values Sliding window / two-pointer technique Find the smallest interval that covers at least one option from every index #leetcode #contest #leetcode2026 #dsa #coding #problemsolving #DSA #datastructure #algorithm
To view or add a comment, sign in
-
LeetCode 70 — Climbing Stairs Worked on finding the number of distinct ways to reach the top when you can climb either 1 or 2 steps at a time. Initially, it looks like a simple counting problem. But structurally, it follows a clear recurrence pattern. Approach — Recurrence Relation - If n ≤ 1 → only one possible way - From step n, you can: - Take 1 step → reduce to (n - 1) - Take 2 steps → reduce to (n - 2) - Total ways = ways(n - 1) + ways(n - 2) This is essentially the Fibonacci pattern in a different form. While testing with small inputs, the recursive solution worked correctly. However, on submission it resulted in Time Limit Exceeded for larger inputs. That made one thing very clear :- The logic is correct, but overlapping subproblems make the pure recursive approach inefficient. Key learning :- Correct logic is not enough. Efficiency matters. Recognizing when recursion needs optimization (memoization / DP) is just as important as writing it. #leetcode #recursion #dynamicprogramming #dsa #algorithms #problemSolving #codingjourney
To view or add a comment, sign in
-
-
Day 13 | LeetCode Learning Journal 🚀 Today I solved Maximum Depth of Binary Tree on LeetCode. This problem helped me understand how to find the maximum depth (height) of a binary tree, which represents the longest path from the root node to a leaf node. 🔑 Key Points: • Uses Depth-First Search (DFS) with recursion • Recursively explore the left and right subtrees • The depth of a tree is calculated as 1 + max(left depth, right depth) • Base case: if the node is null, return 0 • Continue the process until all nodes are visited 🌱 What I Learned: • How recursion works in tree problems • Understanding the concept of height or depth of a binary tree • Practical use of DFS traversal • Improved problem-solving skills in binary tree structures • Strengthened my understanding of recursive algorithms #LeetCode #100DaysOfCode #DSA #BinaryTree #DFS #TreeDepth #Day13 🚀
To view or add a comment, sign in
-
-
🔥 Day 163 of My LeetCode Journey Problem 22: Generate Parentheses 💡 Problem Insight: Today’s problem was about generating all valid combinations of n pairs of parentheses. This isn’t about generating all strings it’s about building only valid ones from the start. 🧠 Concept Highlight: The solution uses backtracking with constraints: Add '(' if you still have openings left Add ')' only if it won’t break validity (closing ≤ opening) Explore all valid paths and backtrack cleanly This avoids generating invalid combinations and keeps the search space controlled. 💪 Key Takeaway: Don’t generate everything and filter later. Build only valid states — that’s the difference between brute force and smart recursion. ✨ Daily Reflection: This problem reinforces that recursion is about decision trees and constraints, not just function calls. Once you visualize the tree, the logic becomes clear. #Day163 #LeetCode #Backtracking #GenerateParentheses #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 22/50 – LeetCode Challenge 🧩 Problem: Symmetric Tree Today’s problem focused on checking whether a binary tree is a mirror of itself — a great exercise in recursion and tree traversal. 📌 Problem Summary: Given the root of a binary tree, determine whether it is symmetric around its center. A tree is symmetric if the left subtree is a mirror reflection of the right subtree. 🔍 Approach Used ✔ Used recursion to compare nodes in a mirrored way ✔ Checked: If both nodes are null → symmetric If one is null → not symmetric If values are equal → continue checking ✔ Compared: left subtree of one side with right subtree of the other right subtree with left subtree ⏱ Time Complexity: O(n) 📦 Space Complexity: O(h) 💡 Key Learning ✔ Understanding mirror structure in trees ✔ Applying recursion with multiple conditions ✔ Importance of checking both sides (left ↔ right and right ↔ left) ✔ Strengthening binary tree problem-solving This problem showed how small logical mistakes (like missing one recursive check) can affect correctness — a great learning experience. Consistency builds mastery 🚀 🔗 Problem Link: https://lnkd.in/gSHvYR2S #50DaysOfLeetCode #LeetCode #DSA #BinaryTree #Recursion #ProblemSolving #CodingJourney #FutureAIEngineer #Consistency
To view or add a comment, sign in
-
-
🚀 LeetCode 1758 — Minimum Changes To Make Alternating Binary String Today’s problem focuses on recognizing patterns in binary strings. 🔹 A binary string is alternating if no two adjacent characters are the same. Valid patterns can only be: 010101... 101010... 💡 Key Insight Instead of constructing both patterns, we can compare the string with just one pattern (0101...) and count mismatches. "01"[i & 1] helps generate the expected character at index i. ops → number of changes required to convert the string to 0101... n - ops → changes required to convert it to 1010... The answer is simply the minimum of these two values. ⚡ Time Complexity: O(n) 💾 Space Complexity: O(1) 🧠 Idea: Count mismatches with one alternating pattern and derive the other automatically. #LeetCode #DSA #Algorithms #BinaryString #CodingPractice #ProblemSolving #CompetitiveProgramming
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development