Day 57 of #100DaysOfLeetCode Today’s problem focused on finding the minimum distance between a target element and a given index in an array. I solved it using a simple and efficient approach: Traversed the array once Checked for target occurrences Calculated distance using absolute difference Maintained the minimum value throughout Runtime: 0 ms (Beats 100%) Optimized space usage Key takeaway: Even straightforward iteration can lead to highly optimized solutions when combined with the right logic. #LeetCode #Java #DataStructures #Algorithms #100DaysOfCode #CodingJourney #ProblemSolving
Minimum Distance in Array with Target Element
More Relevant Posts
-
Day 80/90 Solved: Largest Rectangle in Histogram Core idea: → Use monotonic stack to find nearest smaller to left & right → Width = (right - left - 1), maximize area This is one of those problems where the pattern isn’t obvious at first. Once the left/right boundary logic clicks, the solution becomes systematic. #Consistency #DSA #LeetCode #Stack #MonotonicStack #ProblemSolving #Java #Algorithms
To view or add a comment, sign in
-
-
Day 72 of #100DaysOfCode Problem: Convert Sorted Array to Height-Balanced BST Today I learned how to efficiently convert a sorted array into a balanced Binary Search Tree using Divide & Conquer. Key Insight: Pick the middle element as the root to maintain balance. Recursively build: Left subtree from left half Right subtree from right half This ensures: Optimal height Faster search operations ⏱ Time Complexity: O(n) 📦 Space Complexity: O(log n) Consistency is the real game changer #DSA #Java #BinaryTree #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Day 9/ #100DaysOfCode Solved LeetCode 1848: Minimum Distance to the Target Element. Approach: The problem can be solved using a straightforward linear traversal. Iterate through the array and check for indices where the value equals the target. For each such index, compute the absolute difference between the current index and the given start index. Maintain a variable to track the minimum distance encountered during the traversal. Solution Insight: Initialize a variable with a large value (e.g., Integer.MAX_VALUE). Traverse the array once. Whenever the target element is found, update the minimum distance using Math.abs(i - start). Return the minimum value after the loop. Complexity: Time Complexity: O(n) Space Complexity: O(1) Result: 72/72 test cases passed with optimal runtime performance. This problem reinforces the importance of simple iteration and careful tracking of minimum values in array-based problems. #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 3/100 – LeetCode Challenge 🔍 Problem: Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be inserted to maintain the sorted order. 💡 Key Concepts Used: • Binary Search • Time Complexity Optimization – O(log n) • Efficient searching in sorted arrays 📚 What I Learned: • How binary search reduces search time significantly compared to linear search • Handling edge cases when the target element is not present • Determining the correct insertion index while maintaining sorted order hashtag #100DaysOfCode #LeetCode #Java #DataStructures #Algorithms #BinarySearch #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 90 of #100DaysOfCode – Unique Binary Search Trees II Today’s problem was all about generating all structurally unique BSTs for values from 1 to n. At first glance, it feels tricky because it's not just counting trees — we actually need to build every possible tree structure. 💡 Key Insight: Pick each number i as the root Recursively build: Left subtree from [1 ... i-1] Right subtree from [i+1 ... n] Combine every left & right subtree pair 🌳 This is a classic Recursion + Backtracking problem and is closely related to Catalan Numbers. 📌 Example: For n = 3, we get 5 unique BSTs ⚡ What I learned today: How recursion can generate combinations of structures Importance of base case (start > end → null) Combining subproblems effectively 💻 Time Complexity: Approximately O(4ⁿ / √n) Consistency is key — 90 days strong and still going 💪 Next target: 💯 #DSA #Java #LeetCode #Recursion #BinaryTree #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Day 70 - Next Greater Node Understanding how to find the next larger value for each node using stack optimization. Approach: • Convert linked list → array for easy indexing • Use a stack to track indices • Compare current value with stack top • Update results when a greater value is found Key Insight: Monotonic stack helps avoid unnecessary comparisons Time Complexity: O(n) Space Complexity: O(n) #Day70 #LeetCode #Java #CodingPractice #TechJourney #DSA #LinkedList
To view or add a comment, sign in
-
-
Day 72/100 Completed ✅ 🚀 Solved LeetCode – Matrix Diagonal Sum (Java) ⚡ Implemented an efficient approach to calculate the sum of both primary and secondary diagonals of a square matrix in a single traversal. Avoided double-counting the center element by handling the odd-sized matrix case separately. 🧠 Key Learnings: • Efficient diagonal traversal in a matrix • Handling overlapping elements (center in odd n) • Writing optimal O(n) solutions instead of nested loops • Clean and concise index-based logic 💯 This problem improved my understanding of matrix patterns and how to optimize traversal by reducing unnecessary iterations. 🔗 Profile: https://lnkd.in/gaJmKdrA #leetcode #datastructures #algorithms #java #matrix #arrays #problemSolving #100DaysOfCode 🚀
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟕𝟕 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding all elements that appear more than n/3 times in an array. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Majority Element II 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 – 𝐇𝐚𝐬𝐡𝐌𝐚𝐩 • Counted frequency of each element using a map • Calculated threshold = n / 3 • Collected elements whose frequency exceeded the threshold 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • At most 2 elements can appear more than n/3 times • HashMap is straightforward for frequency counting • Understanding constraints helps reduce possibilities • This problem has an optimized Boyer-Moore Voting (extended) solution 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(n) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Constraints often reveal hidden patterns — understanding them leads to better optimizations. 77 days consistent 🚀 On to Day 78. 🔗 Problem Link: https://lnkd.in/dDwdWYJs #DSA #Arrays #HashMap #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
🚀 Day 38 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Maximum Product of Two Elements in an Array. Problem Insight: Given an integer array, the goal is to find two elements such that: (nums[i] - 1) * (nums[j] - 1) is maximized Approach: • First, sort the array using Arrays.sort() • Use two nested loops to check all possible pairs • For each pair, calculate → (nums[i] - 1) * (nums[j] - 1) • Keep track of the maximum product Time Complexity: • O(n²) — due to nested loops Space Complexity: • O(1) — no extra space used Key Learnings: • Understanding operator precedence is very important in expressions • Sorting helps in simplifying many problems • Even simple problems can have optimized solutions beyond brute force Takeaway: Brute force helps in understanding the problem deeply, but optimization (like using the two largest elements directly) makes the solution efficient 🚀 #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #Arrays
To view or add a comment, sign in
-
-
Day 36/50 🚀 Solved “Valid Parentheses” today — a classic stack problem, but a great reminder that simple concepts can be powerful when applied correctly. 💡 Key takeaway: Using a stack makes it easy to track opening brackets and validate matching pairs efficiently. Every closing bracket should correspond to the most recent unmatched opening one — LIFO in action. ⚙️ What I focused on: Clean conditional checks Avoiding unnecessary complexity Writing readable, structured code 📈 Result: Accepted ✅ Optimized runtime & solid performance On to Day 37 🔥 #50DaysOfCode #DataStructures #Algorithms #Java #LeetCode #CodingJourney
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