✨ Day 103 of My DSA Challenge – Permutations 🔷 Problem : 46. Permutations 🔷 Goal : Generate all possible orderings (permutations) of a given array of distinct integers. Example → Input: [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] 🔷 Key Insight : This problem is a classic recursion and backtracking pattern — exploring all possible arrangements by making choices and undoing them. Here’s how I approached it : 1️⃣ Recursion (Depth-First Search): Build each permutation step by step. 2️⃣ Visited Array: Track which elements have been used so far. 3️⃣ Backtracking: After exploring a path, undo the choice (remove the last element and mark it unvisited). Every recursive call expands the decision tree until all positions are filled, ensuring every unique ordering is generated. 🔷 My Java Approach : Recursive helper() function generates all permutations. Base case → when current list size equals array length. Used a boolean[] vis to manage state efficiently 🔷 Complexity : Time → O(n × n!) (since there are n! permutations and copying each list takes O(n)) Space → O(n) (for recursion and visited tracking) This problem beautifully tests recursion fundamentals, state management, and the art of backtracking — essential tools for mastering combinatorial problems. Each problem reminds me that problem-solving is less about memorizing patterns and more about learning how to think. #Day103 #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #Recursion #Backtracking #CodingChallenge #Programming #SoftwareEngineering #Algorithms #DataStructures #TechJourney #CodeEveryday #EngineerMindset #DeveloperJourney #GrowthMindset #KeepLearning #ApnaCollege #AlphaBatch #ShraddhaKhapra
"Day 103 of DSA Challenge: Generating Permutations in Java"
More Relevant Posts
-
💡 Day 104 of My DSA Challenge – Combinations 🔷 Problem : 77. Combinations 🔷 Goal : Generate all possible combinations of k numbers chosen from the range [1, n]. Example → Input: n = 4, k = 2 Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]] 🔷 Key Insight : This is a backtracking problem — where we explore all possible ways of picking elements while maintaining order and avoiding repetition. The core difference from permutations is that the order of elements doesn’t matter — [1,2] and [2,1] are the same combination. 🔷 Approach : 1️⃣ Start from the first number (idx = 1). 2️⃣ At each step, decide whether to include the current number in the combination or skip it. 3️⃣ Recursively build combinations until the list size reaches k. 4️⃣ Backtrack to explore other possibilities. 🔷 My Java Approach : Used recursion to explore all inclusion/exclusion choices. Added combinations when list size equals k. Backtracked after each recursive call to maintain correct state. 🔷 Complexity : Time → O(C(n, k)) Space → O(k) (for recursion and temporary list) This problem strengthens understanding of decision trees and combinatorial logic, which form the backbone of many recursive and dynamic programming patterns. Every problem adds a new layer to logical thinking — today, it was about choosing without caring about order, but caring deeply about structure. #Day104 #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #Backtracking #Recursion #Combinations #CodingChallenge #Programming #SoftwareEngineering #Algorithms #DataStructures #TechJourney #EngineerMindset #DeveloperJourney #GrowthMindset #KeepLearning #ApnaCollege #AlphaBatch #ShraddhaKhapra
To view or add a comment, sign in
-
-
🔥 Day 105 of My DSA Challenge – Permutations II 🔷 Problem : 47. Permutations II 🔷 Goal : Generate all unique permutations of a list of integers that may contain duplicates. Example → Input : nums = [1,1,2] Output : [[1,1,2], [1,2,1], [2,1,1]] 🔷 Key Insight : This is a backtracking + deduplication problem — similar to the basic permutation problem, but with an added twist : We must handle duplicates efficiently to avoid generating the same permutation multiple times. 🔷 Approach : 1️⃣ Sort the array to group duplicates together. 2️⃣ Use a boolean vis[] array to track visited elements. 3️⃣ Before choosing a number, skip it if it’s the same as the previous one and the previous one hasn’t been used — this avoids duplicate branches. 4️⃣ Backtrack after each recursive call to explore other paths. 🔷 My Java Approach : Sorted the array to make duplicate detection easier. Used recursion to build permutations step by step. Applied duplicate-skipping condition inside the loop. 🔷 Complexity : Time → O(N! × N) (in the worst case, when all numbers are unique) Space → O(N) (for recursion + visited array) This problem beautifully blends recursion, sorting, and logical pruning, teaching how to handle duplicate elements without extra data structures. Every step in backtracking builds precision — not just in code, but in thinking. ⚡ #Day105 #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #Backtracking #Recursion #Permutations #CodingChallenge #Programming #SoftwareEngineering #Algorithms #DataStructures #TechJourney #EngineerMindset #DeveloperJourney #Growth
To view or add a comment, sign in
-
-
💡 Day 99 of My DSA Challenge – Split Array Largest Sum 🔷 Problem: 410. Split Array Largest Sum 🔷 Goal: Split the array into k non-empty subarrays such that the largest subarray sum is as small as possible. 🔷 Key Insight: This is a classic Binary Search on the Answer problem — the goal isn’t to find a position, but the minimum possible value of the largest subarray sum. Here’s how: The lower bound of our search space is the maximum element in the array (a subarray must at least handle this value). The upper bound is the total sum of the array (one subarray takes all elements). For each mid (possible largest sum), we simulate how many subarrays are needed. If we need more than k, it means our mid is too small → move right. Else, we can try smaller sums → move left. 🔷 My Java Approach: 1️⃣ Define helper isPossible() to simulate how many subarrays form under a max limit. 2️⃣ Apply Binary Search on range [max(nums), sum(nums)]. 3️⃣ Narrow down to the smallest feasible largest sum. 🔷 Complexity: Time → O(n × log(sum(nums))) Space → O(1) This problem is a perfect blend of binary search intuition + greedy validation. It pushes you to think beyond array indices — to apply binary search to ranges of answers instead. Every problem like this sharpens both algorithmic depth and logical structure. 🚀 #100DaysOfCode #Day99 #LeetCode #DSA #Java #ProblemSolving #BinarySearch #CodingChallenge #Programming #LearnToCode #CodingLife #SoftwareEngineering #Algorithms #DataStructures #TechJourney #CodeEveryday #EngineerMindset #DeveloperJourney #GrowthMindset #CodeNewbie #KeepLearning #ApnaCollege #AlphaBatch #ShraddhaKhapra
To view or add a comment, sign in
-
-
💡 Day 57 of #100DaysOfCode 💡 Today, I solved LeetCode Problem #217 – Contains Duplicate 🧩 Given an integer array, the task is simple yet fundamental: 👉 Determine whether any value appears at least twice in the array. 💻 Language: Java ⚡ Runtime: 13 ms — Beats 87.70% 🚀 📉 Memory: 58.28 MB — Beats 63.02% 🧠 Key Learnings: Reinforced my understanding of HashSet and its O(1) lookup time. Learned how sets help efficiently identify duplicates in large datasets. Strengthened array traversal and data structure optimization skills. 🧩 Approach: 1️⃣ Initialize an empty HashSet. 2️⃣ Iterate through each element in the array. 3️⃣ If an element already exists in the set → return true. 4️⃣ Else, add it to the set. 5️⃣ If loop completes, return false. ✅ Complexity: Time: O(n) Space: O(n) ✨ Takeaway: Even basic problems like these teach the power of hash-based structures — simple yet powerful for building optimized systems and preventing redundant computations. #100DaysOfCode #LeetCode #Java #DataStructures #HashSet #ProblemSolving #CodingChallenge #CleanCode #Programming #SoftwareEngineering #TechLearning #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 60 of #100DaysOfCode 🚀 Today, I solved LeetCode Problem #137 – Single Number II 🧩 📘 Problem Statement: Given an integer array where every element appears three times except for one that appears exactly once, find that single element and return it. The challenge: solve it with O(n) time and O(1) space complexity. 💻 Language: Java ⚡ Runtime: 0 ms — Beats 100.00% 📉 Memory: 45.54 MB — Beats 56.30% 🧠 Concept Used: 🔹 Bit Manipulation — A powerful yet tricky technique that leverages binary operations to track occurrences of bits efficiently. 🔹 Instead of using extra space or hash maps, we track bits that appear once and twice using two integer variables: ✅ once → bits that appeared once ✅ twice → bits that appeared twice The trick lies in updating them using XOR (^) and NOT (~) to "cancel out" bits appearing three times. 🧩 Approach Summary: 1️⃣ Initialize two variables once = 0 and twice = 0. 2️⃣ For every number in the array: - Update once and twice based on how many times a bit has appeared. 3️⃣ After processing all numbers, once holds the value of the single element. ✅ Complexity: Time: O(n) Space: O(1) ✨ Takeaway: This problem teaches how bitwise logic can replace traditional data structures, achieving the same result with constant space — a crucial optimization in system-level programming and interviews. #100DaysOfCode #LeetCode #Java #BitManipulation #ProblemSolving #CleanCode #DataStructures #Algorithms #TechLearning #CodingChallenge #SoftwareEngineering #InterviewPreparation
To view or add a comment, sign in
-
-
🌟 Day 44 – LeetCode Practice Problem: Product of Array Except Self (LeetCode #238) 📌 Concept: Given an integer array, create a new array where each element equals the product of all other elements except itself, without using division. 🧠 My Approach: First pass → compute prefix products (multiply everything before current index) Second pass → compute suffix products (multiply everything after current index) Combine both to get the result for each index efficiently No extra multiplication array used — optimized and clean logic ⚡ This avoids division and runs in linear time — exactly what the problem demands ✅ 📈 Result: ✅ Accepted ⚡ Runtime: 2 ms (Beats ~87%) 📦 Memory: Efficient usage 💡 Key Learning: This problem reinforces the power of prefix & suffix computation — a common trick in array manipulation + interview favorite. Great way to improve problem-solving without relying on brute force or division. --- 🚀 Growing stronger every day in DSA! #LeetCode #DSA #Java #ProblemSolving #PrefixSuffix #CodingJourney #ArrayProblems #LearningMindset
To view or add a comment, sign in
-
-
#Day27 of my DSA Journey – Reverse Linked List 🔄 🧩 Problem: Given the head of a singly linked list, reverse the list and return the new head. ⚙️ Approach: Used both Iterative and Recursive methods — Iterative: Reverses pointers one by one in a single pass. Recursive: Reverses the rest of the list, then connects the first node at the end. 💡 Key Learning: Mastering pointer manipulation is crucial in linked list problems — it builds strong fundamentals for complex data structures. ✅ Time Complexity: O(n) ✅ Space Complexity: O(1) (Iterative) / O(n) (Recursive) #DSA #Java #LeetCode #CodingJourney #100DaysOfCode #ProblemSolving #LearnToCode #LinkedList #DataStructures
To view or add a comment, sign in
-
-
✅ Day 68 of LeetCode Medium/Hard Edition Today’s challenge was “Number of Ways to Form a Target String Given a Dictionary” — a brilliant Dynamic Programming and Combinatorics problem that tests precision in transitions and precomputation logic 🧩⚙️ 📦 Problem: You’re given an array of equal-length strings words and a target string target. You must form target from left to right by picking characters from the columns of words under these rules: 1️⃣ Once you use column k from any word, all columns ≤ k in every word become unusable. 2️⃣ You can use multiple characters from the same word, respecting column progression. Return the number of ways to form target modulo 1e9 + 7. 🔗 Problem Link: https://lnkd.in/gns9CwWa ✅ My Submission: https://lnkd.in/g7bsgZq9 💡 Thought Process: This problem is a clever mix of frequency compression and DP memoization. We precompute a frequency table freq[26][m], where each cell represents how many words contain a given letter at column m. Then, using recursion with memoization: 🎯 At each step (i, j) Either use freq[target[i]][j] if it exists → multiply by ways for the next position (i+1, j+1) Or skip the current column → move to (i, j+1) The recurrence relation: dp[i][j] = freq[target[i]][j] * dp[i+1][j+1] + dp[i][j+1] All computations are done modulo 1e9 + 7. ⚙️ Complexity: ⏱ Time: O(26 * n + t * m) — efficient due to frequency precomputation 💾 Space: O(t * m) — for memoized DP table 💭 Takeaway: This challenge reinforced how precomputation and state optimization can transform a seemingly exponential recursion into an elegant polynomial-time solution 🚀 #LeetCodeMediumHardEdition #100DaysChallenge #DynamicProgramming #Combinatorics #ProblemSolving #Java #CodingChallenge #DSA
To view or add a comment, sign in
-
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