LeetCode Problem #1920 – Build Array from Permutation Today I solved an interesting array problem that helped me understand indexing and array traversal better. 📌 Problem: Given a 0-indexed array `nums`, build a new array `ans` such that: `ans[i] = nums[nums[i]]` 📌 Approach: * Create a new array `ans` with the same length as `nums` * Traverse the array using a `for` loop * For each index `i`, access the value `nums[i]` * Use that value as an index again to get `nums[nums[i]]` * Store the result in `ans[i]` 📌 Key Concepts Practiced: ✔ Array traversal ✔ Nested indexing ✔ Understanding how values can act as indexes 📌 Time Complexity: O(n) – since we traverse the array only once. 📌 Key Takeaway: Sometimes the value inside an array can also represent an index, and understanding this idea helps solve many array-based problems efficiently. 💡 Learning Note This problem improved my understanding of **index-based operations and array manipulation**, which are fundamental concepts for solving many coding interview questions. #LeetCode #CodingPractice #Java #DSA #Arrays #ProblemSolving
LeetCode Problem #1920 - Build Array from Permutation
More Relevant Posts
-
🚀 #60DaysOfLeetCode – Day 32 Today’s problem was about removing all adjacent duplicates in a string. 🔹 Approach Used: Stack To efficiently handle consecutive duplicates, I used a stack-based approach: Traverse the string character by character. If the stack is not empty and the top element matches the current character, pop it (this removes the duplicate pair). Otherwise, push the current character onto the stack. Finally, build the result string from the stack and reverse it to maintain the correct order. 🔹 Key Learning: Using a stack helps simulate the process of removing adjacent duplicates in a single pass, achieving O(n) time complexity and avoiding unnecessary reprocessing. This problem highlights how stacks are powerful for handling sequential patterns and cancellations, especially in string manipulation problems. On to Day 33! 💻🔥 #LeetCode #DSA #Java #CodingChallenge #ProblemSolving #LearningInPublic #60DaysOfCode
To view or add a comment, sign in
-
-
Day 91 of #365DaysOfLeetCode Challenge Today’s problem: **Arithmetic Slices (LeetCode 413)** An interesting problem that focuses on identifying patterns in subarrays. The goal is to count all contiguous subarrays of length ≥ 3 where the difference between consecutive elements remains constant. 💡 **Key Insight:** Instead of checking every subarray (which would be inefficient), we track the current streak of arithmetic sequences. * If the current 3 elements form an arithmetic sequence → extend the streak * Keep adding the count of valid slices ending at current index * Reset when the pattern breaks 📌 **Approach:** * Use two variables: * `curr` → counts current valid extensions * `total` → accumulates final answer * Traverse from index 2 onward * Compare consecutive differences ⚡ **Time Complexity:** O(n) ⚡ **Space Complexity:** O(1) **What I learned today:** Sometimes, problems that look like they need nested loops can be optimized using pattern tracking and dynamic accumulation. Consistency is key — 91 days down, 274 to go! #LeetCode #DSA #CodingChallenge #Java #ProblemSolving #100DaysOfCode #TechJourney
To view or add a comment, sign in
-
-
Day 18 – Same Tree 🚀 Continuing my DSA journey with Striver’s A2Z Sheet / LeetCode practice. Today’s problem focused on checking whether two binary trees are identical, which is a classic tree recursion problem. Approach We compare both trees node by node using recursion. At each step: 1️⃣ If both nodes are null → trees match 2️⃣ If one is null → not same 3️⃣ If values differ → not same 4️⃣ Recursively check left and right subtrees Time Complexity : O(N). Space Complexity : O(N) . Github Code :- https://lnkd.in/guF5Vq9m #LeetCode #DSA #StriverA2Z #CodingJourney #Java #BinaryTree #Recursion #ProblemSolving #CodingPractice #SoftwareEngineering #TechInterviewPrep #Day18 #DeveloperJourney
To view or add a comment, sign in
-
-
🚀 Day 11 – #50DaysLeetCodeChallenge Today I solved the 3Sum problem. 📌 Problem Statement Given an integer array nums, return all the unique triplets [nums[i], nums[j], nums[k]] such that: i ≠ j ≠ k nums[i] + nums[j] + nums[k] = 0 ⚠️ The solution must not contain duplicate triplets. Example: Input: [-1,0,1,2,-1,-4] Output: [[-1,-1,2], [-1,0,1]] 💡 Approach I Used – Sorting + Two Pointers 1️⃣ Sort the array 2️⃣ Fix one element i 3️⃣ Use two pointers: left = i + 1 right = end of array 4️⃣ Check the sum: If sum == 0 → add triplet If sum < 0 → move left++ If sum > 0 → move right-- 5️⃣ Skip duplicates to avoid repeated triplets ⚙️ Key Idea Reduce the problem from 3Sum → 2Sum (using two pointers) Sorting helps in efficient traversal and duplicate handling 🧠 What I learned today ✔️ How sorting simplifies complex problems ✔️ Converting problems into smaller subproblems (3Sum → 2Sum) ✔️ Handling duplicates carefully From simple arrays → advanced pointer techniques 🚀 Consistency is making a difference! #LeetCode #Java #DSA #CodingChallenge #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 45/100 | #100DaysOfDSA 🚀🧩 Today’s problem: Remove All Occurrences of a Substring The task is simple: Given a string s and a substring part, keep removing the leftmost occurrence of part until it no longer exists in s. Approach: • Use StringBuilder for efficient modifications • Find the index of part using indexOf() • Delete that portion from the string • Repeat the process until part is no longer found Time Complexity: O(n × k) in worst case (where n is length of string and k is length of substring) Big takeaway: Sometimes the easiest solution is just repeating a simple operation until the condition disappears. Clean string manipulation problem today. 🔥 Day 45 done. #100DaysOfCode #LeetCode #DSA #Algorithms #Java #String #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
🚀 Day 69 — LeetCode Practice 🚀 Today’s focus was on string manipulation and careful indexing. ✅ Problem solved today: 🔹 Find Common Characters 💡 Key learnings from today: • Understood how to find common characters across multiple strings • Learned the importance of character frequency counting • Practiced handling nested loops efficiently • Realized how small mistakes in indexing can affect the entire logic • Improved attention to detail while working with strings Initially, I made a mistake by using the wrong variable inside charAt(). This caused incorrect indexing, which can either lead to wrong output or even StringIndexOutOfBoundsException. After fixing it, the logic worked perfectly by correctly comparing characters and tracking their minimum frequency across all words. This problem reinforced an important lesson: Sometimes errors are not in logic — but in small details like indexing and variable usage. Accuracy matters as much as logic. Step by step, getting better 💪 On to Day 70 🚀 #Day69 #DSA #LeetCode #ProblemSolving #Java #CodingJourney #Consistency #FutureEngineer
To view or add a comment, sign in
-
-
🚀 Day 89 - #100DaysOfCode Today I solved a problem on finding the smallest common element between two sorted arrays. 🧩 Problem Given two integer arrays, return the first common element between them. If no common element exists, return -1. 💡 Approach I used a HashSet to efficiently check for common elements. Steps: 1️⃣ Insert all elements of the first array into a HashSet. 2️⃣ Traverse the second array. 3️⃣ Check if the current element exists in the set. 4️⃣ If found, return that element immediately. 5️⃣ If no common element is found, return -1. ⏱ Time & Space Complexity Time Complexity: O(n + m) Space Complexity: O(n) 📌 Key Learning: Using a HashSet helps reduce lookup time to O(1), making the solution efficient #DSA #Java #CodingJourney #LeetCode #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Every problem teaches something new — today it was LeetCode 50: Pow(x, n) At first, it looked simple: just calculate power. But the real challenge was optimizing it and handling edge cases like negative powers and Integer.MIN_VALUE overflow. Learned how Binary Exponentiation can turn an O(n) solution into O(log n) — that’s the beauty of algorithms! Moments like these remind me that problem-solving is not just about writing code, but thinking deeper. #LeetCodeJourney #DSA #KeepLearning #Java #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 44/100 Today’s problem: Reverse Vowels of a String. 🔹 Problem Summary: Given a string, reverse only the vowels while keeping other characters in the same position. 🔹 Approach I Used: I applied the Two Pointer Technique: - One pointer from the start, one from the end - Move both until vowels are found - Swap them and continue 🔹 Key Learning: ✔ Two pointers can simplify string problems a lot ✔ Always break the problem into smaller steps ✔ Practice improves pattern recognition. 🔹 Example: Input: "IceCreAm" Output: "AceCreIm" 🔹 Time Complexity: O(n) 🔹 Space Complexity: O(n) Consistency is the key 🔑 — showing up every day! #Day44 #CodingJourney #DSA #Java #Learning #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 LeetCode – Subsets | Backtracking Approach Given an integer array nums of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. 💡 Approach Used – Backtracking I used a recursive backtracking strategy where: 1. Start with an empty subset 2. Add the current subset to the result 3. Choose an element and explore further combinations 4. Backtrack by removing the element to try other possibilities This ensures that we explore all possible combinations systematically. Since each element has two choices (include or exclude), the total subsets become 2ⁿ. #LeetCode #DSA #Backtracking #Java #CodingPractice #Algorithms #ProblemSolving #SoftwareEngineering #TechLearning
To view or add a comment, sign in
-
Explore related topics
- Approaches to Array Problem Solving for Coding Interviews
- LeetCode Array Problem Solving Techniques
- Leetcode Problem Solving Strategies
- Problem Solving Techniques for Developers
- Why Use Coding Platforms Like LeetCode for Job Prep
- Common Algorithms for Coding Interviews
- Solving Sorted Array Coding Challenges
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