🚀 Day 46 / 100 | Permutations Intuition: If we have n numbers, there can be n! different permutations. The idea is to try placing every number in every possible position. Once a number is used in the current permutation, we skip it until we backtrack. Approach: O(n! * n) Use backtracking to explore all possible arrangements. Maintain a list to store the current permutation. Keep track of which elements are already used.(flag) Add an unused element, explore deeper recursively, then remove it to try another option. Repeat this process until all permutations are generated. Complexity: Time Complexity: O(n * n!) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode #Backtracking
Generating Permutations with Backtracking in Java
More Relevant Posts
-
🚀 Day 29 of #75daysofLeetCode 2095 – Delete the Middle Node of a Linked List Just solved an interesting linked list problem that perfectly demonstrates the power of the two-pointer technique (slow & fast pointers). 🔍 Problem Insight: Given a linked list, delete its middle node where the middle is defined as ⌊n/2⌋ (0-based indexing). 💡 Key Idea: Instead of calculating the length, we can efficiently find the middle using: 🐢 Slow pointer (1 step) ⚡ Fast pointer (2 steps) When the fast pointer reaches the end, the slow pointer will be at the middle node! 🛠 Approach: ✔ Handle edge case (single node → return null) ✔ Traverse using slow & fast pointers ✔ Keep track of previous node ✔ Remove the middle node in one pass ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) 🔥 Why this matters? This pattern is widely used in: Finding middle of linked list Detecting cycles Splitting lists Mastering this unlocks many problems! #LeetCode #DSA #LinkedList #Java #CodingInterview #ProblemSolving #TechLearning
To view or add a comment, sign in
-
-
✅ LeetCode Top Interview 150 – Day 76 Today I solved Construct Binary Tree from Preorder and Inorder Traversal Key Idea : Pick the first element from preorder → root. Find this root in inorder. Split inorder into: Left subtree Right subtree Recursively build left and right subtrees. To optimize, use a hash map for quick index lookup in inorder #DSA #Leetcode #Day76 #java #BinaryTree
To view or add a comment, sign in
-
-
🚀 𝗗𝗮𝘆 - 36/60 Problem: Indexes of Subarray Sum 🔍 Learned: Since the array contains only non-negative numbers, used the sliding window (two-pointer) approach. Expand the window by increasing the right pointer and shrink it when the sum exceeds the target. 😅 Struggles: Initially thought about prefix sum + hashmap, but that’s unnecessary here. Realizing that non-negative elements allow sliding window made the solution much simpler. 🧠 Key Learning: Sliding window works perfectly when elements are non-negative because the sum behaves predictably. This helps achieve O(n) time complexity efficiently. 📦 Concepts Used: #Arrays #SlidingWindow #TwoPointers #Subarrays #TimeComplexity #DSA Right approach depends on constraints—understanding that is the real skill. 🚀 #Java #CodingJourney #ProblemSolving #DSA
To view or add a comment, sign in
-
-
Day 34/75 — Sort a Linked List (Merge Sort) Today’s problem involved sorting a linked list efficiently. Since linked lists don’t support random access, merge sort is the best approach. Approach: • Use slow-fast pointers to find the middle • Split the list into two halves • Recursively sort both halves • Merge the sorted lists Key insight: prev.next = null; // split list Time Complexity: O(n log n) Space Complexity: O(log n) This problem strengthened understanding of linked list + divide & conquer. 34/75 🚀 #Day34 #DSA #LinkedList #MergeSort #Java #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 51 of #100DaysOfCode Solved 328. Odd Even Linked List on LeetCode 🔗 🧠 Key Insight: We need to rearrange the linked list such that: 🔹All odd-indexed nodes come first 🔹Followed by all even-indexed nodes 🔹While preserving the relative order ⚠️ Important: This is based on node position (index), not value. ⚙️ Approach: 1️⃣ Create two separate lists: 🔹odd → nodes at odd positions 🔹even → nodes at even positions 2️⃣ Traverse the list and distribute nodes accordingly 3️⃣ Connect: 🔹End of odd list → head of even list 4️⃣ Return the new head 🎯 This ensures a clean separation while maintaining order. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) (in-place re-linking) #100DaysOfCode #LeetCode #DSA #LinkedList #Algorithms #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
✅ LeetCode Top Interview 150 – Day 92 Today I solved Binary Search Tree Iterator Key Idea : Use Stack to simulate in-order traversal Push all left nodes initially For next(), pop from stack and process node If node has right child, push all its left nodes hasNext() checks if stack is not empty This approach ensures O(h) space and average O(1) time per operation #DSA #Java #Leetcode #Day92
To view or add a comment, sign in
-
-
Reorder List ✅ Runtime: 2ms 🚀 Took me 2 hours to crack this. But now? I'll never forget it. That's the difference between copying a solution and actually understanding one. My approach : 1.Find middle using slow & fast pointers 2. Reverse the second half 3. Merge both halves together To be frank — drawing it on paper first made me even more confused 😅 Watching those pointers move on paper made zero sense initially.😭 But slowly... it started coming together. Arrow by arrow. Node by node. Until the whole picture clicked. That moment of clarity after confusion? That's the real learning. Funny thing — this is marked Medium but once the logic clicks it feels Easy. That's what consistent practice does. Problems shrink. Drop a 🔥 if you also write diagrams before you code. #DSA #LeetCode #Java #LinkedList
To view or add a comment, sign in
-
-
🔥 Day 97/160 – GFG Challenge Today’s problem: Merge K Sorted Linked Lists 🧠 Key Idea: Instead of merging all lists one by one (which is slow), I used a Divide & Conquer approach (like Merge Sort). 👉 Break K lists into halves 👉 Merge smaller lists 👉 Combine step by step This reduces time complexity to: ⏱ O(N log K) — optimal solution! 💡 What I learned today: Breaking problems into smaller parts makes complex problems easier Reusing logic (merge two lists) is powerful Thinking in terms of patterns (Merge Sort) is a game changer 📌 Example: Input: 1→3→7 2→4→8 9 Output: 1→2→3→4→7→8→9 Slowly getting better every single day 🚀 #Day97 #GFG160 #LinkedList #DSA #CodingJourney #Java #PlacementPrep
To view or add a comment, sign in
-
-
Day 63 - Odd Even Linked List Today’s problem was about rearranging nodes based on their positions. Approach: • Maintain two pointers → odd and even • Traverse and rearrange links in-place • Keep track of even head to attach later • Merge odd list with even list at the end Key insight: 👉 Focus on node positions, not node values. Time Complexity: O(n) Space Complexity: O(1) #Day63 #LeetCode #Java #LinkedList #CodingPractice #TechJourney #DSA
To view or add a comment, sign in
-
-
Day 14 of LeetCode — Longest Common Prefix Today I solved the classic problem: finding the longest common prefix among a list of strings. Approach I used: Start with the first string as the prefix Compare it with each string in the array Shrink the prefix until it matches the start of every string If it becomes empty → no common prefix Time Complexity: O(n * m) (n = number of strings, m = length of prefix) This problem reinforced string manipulation and edge case handling (empty arrays, no matches). #Java #DSA #CodingJourney #LeetCode #ProblemSolving #TechGrowth
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