Day - 59 Subsets The problem - Given array of unique integers, return all possible subsets (power set). Solution must not contain duplicate subsets. Example : nums = [1,2,3] → [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] Brute Force - Generate all combinations iteratively, still exponential, but less elegant than recursion. Approach Used - •) Call createSubset(nums, 0, res, subset). •) Base case, if index == nums.length, add current subset to result. •) Recursive First case, include nums[index], add to subset, recurse with index+1, backtrack (remove). •) Recursive Second case, exclude nums[index]: recurse with index+1 (don't add). Complexity - Time - O(n × 2^n),each element has 2 choices. Space - O(n), recursion depth. Note - For each element, make two choices, include it or exclude it. Recursively build all combinations #DSA #Java #SoftwareEngineering #InterviewPrep #LearnToCode #CodeDaily #ProblemSolving
Generating Power Set of Unique Integers
More Relevant Posts
-
Day - 61 Subsets II The problem - Given array that may contain duplicates, return all possible unique subsets. Example : candidates = [10,1,2,7,6,1,5], target = 8 → [[1,1,6],[1,2,5],[1,7],[2,6]] Brute Force - Generate all combinations iteratively, still exponential, but less elegant than recursion. Approach Used - •) Sort the nums array.nums.lengthcktrack(0, nums, subset, res). •) Base case, if i == nums.length, reached end, add new ArrayList(subset) to res and return. •) Recursive First case, include nums[i], add to subset, recurse with i+1, backtrack (remove). •) Recursive Second case, exclude nums[i], recurse with i+1 (don't add). Complexity - Time - O(2^n), each element include/exclude. Space - O(n), recursion depth. Note - Sort array. After excluding an element, skip all duplicates before next recursion to avoid duplicate subsets. #DSA #Java #SoftwareEngineering #InterviewPrep #LearnToCode #CodeDaily #ProblemSolving
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟮𝟬/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 Solved 3Sum using Sorting + Two Pointer technique. ➤ Approach (O(n²), O(1) extra space apart from result): • First, sort the array • Fix one element i • Use two pointers (left, right) to find pairs such that --> nums[i] + nums[left] + nums[right] == 0 • Move pointers based on whether the sum is smaller or larger than zero • Store unique triplets ➤ Key Insight: Sorting simplifies the problem. Once sorted, the two-pointer technique efficiently avoids checking every combination. Brute force would be O(n³). Optimized approach reduces it to O(n²). #LeetCode #Java #DSA #TwoPointers #Sorting #ProblemSolving #20DaysChallenge #Consistency
To view or add a comment, sign in
-
-
🌳 Day 59/100: Preorder Traversal - Root, Left, Right Day 59. Learning tree traversals. Preorder: Visit root first, then explore. 🎯 📌 Problem: Return preorder traversal of binary tree. 🎯 Recursion: Base case: If node is null, return. Recursive case: Add value, go left, go right. Simple and clean. 📊 Complexity: O(n) time, O(h) space (recursion stack) 🌱 Tree Traversals: Preorder: Root → Left → Right Inorder: Left → Root → Right Postorder: Left → Right → Root Day 59. Traversal patterns building. 🌳 #100DaysOfCode #DSA #LeetCode #Day59 #Java #PreorderTraversal #BinaryTree #TreeTraversal
To view or add a comment, sign in
-
-
📘Day 2 – Time Complexity Learned how algorithm performance changes with input size: 🔹 O(log n) – Fast (divide by half) 🔹 O(n²) – Nested loops 🔹 O(n³) – Triple loops (very slow) 📌 log n < n² < n³ Efficiency matters as data grows 🚀 #DSA #TimeComplexity #Java #CodingJourney
To view or add a comment, sign in
-
Day 3 of Daily DSA 🚀 Solved LeetCode 1: Two Sum Approach: Used a HashMap to store numbers with their indices. For each element, checked if the complement (target - current) already exists. Complexity: • Time: O(n) • Space: O(n) Performance: Runtime: 2 ms (Beats 99.15%) Memory: 47.34 MB Focusing on writing clean and efficient solutions before over-optimizing. Consistency > Intensity 💯 #DSA #LeetCode #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🌳 Day 57/100: Insert into BST - Recursion in Trees Day 57. Yesterday searched, today inserted. BST property makes it elegant. 🎯 📌 Problem: Insert a value into a Binary Search Tree. 💡 Recursive Approach: If root is null → Create new node here If val < root → Insert in left subtree If val > root → Insert in right subtree Return root (unchanged) 🎯 The Beauty: Recursion handles the traversal. Each call returns the updated subtree. No manual pointer tracking needed. Example: Insert 4 into tree with root 5: 5 → 4 < 5 → go left 3 → 4 > 3 → go right null → create TreeNode(4) 📊 Complexity: O(h) where h = height 🌱 Learning: Trees + Recursion = Natural fit. Let the call stack do the walking. Day 57. Tree fundamentals building. 🌳 #100DaysOfCode #DSA #LeetCode #Day57 #Java #BST #Recursion #TreeInsertion
To view or add a comment, sign in
-
-
Day 10/30 – LeetCode streak Today’s problem: Sum of Root To Leaf Binary Numbers. Each root-to-leaf path in the tree forms a binary number (top is MSB, leaf is LSB), and you need the sum of all such numbers. The pattern is a clean DFS with a running value: -As you go down the tree, carry a 'current' integer that represents the binary number so far. -At each node, shift left and add the node’s bit: 'current = (current << 1) | node.val. -When you hit a leaf ('left == null' and 'right == null'), return 'current' because that path is now a complete binary number. -For internal nodes, return 'dfs(left, current) + dfs(right, current)' so the sums bubble up. Day 10 takeaway: This is a nice example of “carry state down the recursion instead of storing the whole path” — the bit shift builds the number on the fly, and the recursion naturally adds up all path values. #leetcode #dsa #java #binarytree #consistency
To view or add a comment, sign in
-
-
After exploring path-based recursion, I moved to generating all subsets of a string. This introduced a very clean pattern - For every element, you either include it or exclude it. That’s it. But that “either-or” creates a full decision tree. What became clear here: - Every element creates two branches. - Total subsets = 2ⁿ (tree expansion becomes visible). - Recursion can systematically explore combinations. - The structure is more important than the syntax. Core logic : if (index == str.length()) { System.out.println(current); return; } subset(index + 1, current + str.charAt(index)); // include subset(index + 1, current); // exclude This is where recursion started feeling predictable. Once the pattern is seen, similar problems start looking connected. #recursion #java #dsajourney #backtracking #problemSolving
To view or add a comment, sign in
-
🚀 #100DaysOfCode | Day 36 📌 LeetCode – Path Sum Today I solved the Path Sum problem using a recursive Depth-First Search (DFS) approach. Given a binary tree and a target sum, determine whether there exists a root-to-leaf path such that the sum of all node values equals the target. 💡 Approach: ✔ If the node is null, return false. ✔ If it's a leaf node, check whether the remaining sum equals the node value. ✔ Recursively subtract the current node value from targetSum. ✔ Check both left and right subtrees. ✔ If either returns true → path exists. ⏱ Complexity: Time Complexity: O(n) Space Complexity: O(h) — height of the tree Binary tree problems are all about mastering recursion and understanding base cases clearly. #Java #LeetCode #DSA #BinaryTree #Recursion #ProblemSolving
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟳/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲🎯 Solved Longest Valid Parentheses ➤ Problem: Given a string of ‘(’ and ‘)’, determine the length of the longest well-formed (valid) parentheses substring. ➤ Approach: Used a clean and structured stack-based technique to identify valid boundaries efficiently. • Initialize the stack with -1 as the base index. • Push every '(' index onto the stack. • On encountering ')', pop to attempt a match. • If the stack becomes empty, push the current index as the new starting point. • Otherwise, compute the valid substring length using currentIndex − stackTop. Continuously update the maximum length. This ensures each character is processed in a single pass, maintaining optimal efficiency. #LeetCode #Java #DSA #TwoPointers #ArrayManipulation #ProblemSolving #20DaysChallenge #Consistency
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