🔥 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
"Day 105 of DSA Challenge: Permutations II with Java"
More Relevant Posts
-
✨ 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
To view or add a comment, sign in
-
-
💡 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
-
-
✨ Two Paths, One Goal: From Patterns to Problems (Day 48) ✨ Today, in collaboration with Chitrang Potdar, we begin a brand-new chapter in our DSA journey — Stacks. After exploring the flexibility of Linked Lists, we now move into a structure that defines order, control, and execution flow — the Stack. 💡 What is a Stack? A Stack is a linear data structure that follows the LIFO (Last In, First Out) principle. The element inserted last is the first one to be removed — just like a stack of plates or books. 🧩 Terminologies: Push → Insert an element onto the top of the stack. Pop → Remove the topmost element. Peek / Top → View the top element without removing it. isEmpty() → Check if the stack is empty. 🏗 Structure of a Stack: Top → [ 30 | 20 | 10 ] → Bottom (New elements are always added and removed from the top.) ⚙️ Importance of Stacks: Simplifies management of nested and sequential tasks. Provides controlled access — only one end is accessible. Backbone of recursion, parsing, and expression evaluation. 🌍 Real-Life Applications: 1️⃣ Undo/Redo operations in editors 2️⃣ Function call management (Recursion Stack) 3️⃣ Backtracking algorithms (Maze, Sudoku) 4️⃣ Expression evaluation and syntax parsing 5️⃣ Browser navigation (Forward/Backward) 🧠 Implementation in Java: We implemented Stacks using: Arrays → Fixed-size, index-based implementation. Linked Lists → Dynamic memory allocation, no fixed limit. 📌 Concepts Covered: Stack fundamentals and LIFO principle Core stack operations (push, pop, peek) Array-based and Linked List-based implementations Real-world significance and use cases 🚀 From tomorrow, we’ll begin working on Stack operations and applications — exploring how logic control and recursion work under the hood. 👉 Swipe for Java code examples. 👉 Don’t miss the Python edition by Chitrang Potdar. #Java #DSA #Stack #TwoPathsOneGoal #PatternToProblem #CodingJourney #LearningByDoing
To view or add a comment, sign in
-
-
🚀 Day 28 of #100DaysOfCode Today’s challenge was all about understanding frequency patterns in arrays — a simple yet powerful concept in hashing 🔍 LeetCode 1207 – Unique Number of Occurrences 🔢 📌 Problem Summary: Given an integer array, determine whether each value appears a unique number of times. In other words, 👉 No two numbers should share the same frequency. 💡 Example: Input: [1,2,2,1,1,3] Output: true Because frequencies are: 1 → 3 times 2 → 2 times 3 → 1 time All unique ✔️ 🧠 My Approach: I solved it using a combination of HashMap + HashSet: 1️⃣ Count the occurrences of each number using a HashMap. 2️⃣ Insert all frequency values into a HashSet. 3️⃣ If the size of the HashSet equals the size of the map → all frequencies are unique ✔️ 4️⃣ Otherwise → duplicates exist ❌ A clean and efficient hashing technique. ⚙️ Complexity: Time: O(n) ⏱️ Space: O(n) 💾 (storing frequencies) 💡 Key Learning: HashMaps aren’t just for storing counts—they can reveal patterns, validations, and uniqueness constraints when combined with sets. This problem reinforced how frequency-based logic solves many real-world scenarios like: ✔ Detecting duplicates ✔ Comparing patterns ✔ Character/string analysis ✔ Data validation 🔥 Result: Code ran successfully with 0 ms Runtime, accepted on the first attempt! Small challenge, clean logic, satisfying finish 💪 On to the next one! 🚀 #100DaysOfCode #LeetCode #HashTable #Java #ProblemSolving #DSA #CodingJourney
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
-
-
🌟 Day 28 of #100DaysOfCode 🌟 🔍 Reverse Words in a String — Clean & Elegant String Manipulation 🔹 What I Solved Today, I solved the classic “Reverse Words in a String” problem — a deceptively simple challenge that tests your understanding of string manipulation, trimming spaces, and efficient iteration. This problem is all about cleaning messy input and reordering data logically, which is a valuable skill in real-world text processing tasks. 📝 Problem Statement Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The returned string should have only a single space separating words — with no leading or trailing spaces. Example 1: Input: s = "the sky is blue" Output: blue is sky the Example 2: Input: s = " hello world " Output: world hello Example 3: Input: s = "a good example" Output: example good a 🧠 Concepts Used String Splitting & Trimming Whitespace Handling Iteration & StringBuilder Efficient Memory Management ⚙️ Approach 1️⃣ Trim the string to remove extra leading/trailing spaces. 2️⃣ Split the string by one or more spaces using regex (\\s+). 3️⃣ Iterate backward through the words array. 4️⃣ Append words to a StringBuilder, adding spaces only between words. 5️⃣ Return the final reversed and clean string. 🚀 What I Learned This problem reinforced how attention to detail matters in coding — especially when handling strings and whitespace. It also reminded me that even simple problems can teach clean code principles, edge case thinking, and time-space optimization. Grateful to K.R. Mangalam University for continuously motivating me on this journey of learning and consistency. #100DaysOfCode #Java #ProblemSolving #CodingJourney #DataStructures #Algorithms
To view or add a comment, sign in
-
-
✅ Day 73 of LeetCode Medium/Hard Edition Today’s challenge was “K Inverse Pairs Array” — an elegant Dynamic Programming problem that combines combinatorics, recurrence optimization, and modular arithmetic ⚡💡 📦 Problem: Given two integers n and k, return the number of different arrays consisting of numbers from 1 to n such that there are exactly k inverse pairs. An inverse pair is [i, j] where 0 ≤ i < j < n and nums[i] > nums[j]. 🔗 Problem Link: https://lnkd.in/eqt3zX_D ✅ My Submission: https://lnkd.in/eX2ZyCCu 💡 Thought Process: This problem generalizes the concept of counting inversions across all permutations of 1..n. The key idea is to express dp[n][k] in terms of previously computed states using prefix-sum logic to reduce time complexity. We use the recurrence: dp[n][k] = dp[n][k - 1] + dp[n - 1][k] - (k >= n ? dp[n - 1][k - n] : 0) This formula cleverly builds on smaller subproblems by considering where the nth element can be inserted in permutations of size n-1. 🎯 Base Cases: dp[n][0] = 1 (sorted array, no inverse pairs) dp[1][k] = 0 for all k > 0 ⚙️ Complexity: ⏱ Time: O(n × k) 💾 Space: O(n × k) 🧩 Key Learnings: Deepened understanding of prefix-sum optimization in DP. Reinforced modular arithmetic handling (MOD = 10⁹ + 7). Learned how combinatorial recurrence can reduce nested loops. Strengthened recursive + memoized DP formulation in Java. #LeetCodeMediumHardEdition #100DaysChallenge #DynamicProgramming #Recursion #Combinatorics #Java #DSA #ProblemSolving #KeepCoding #LearnEveryday
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
-
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