🚀 Day 41 of 100 Days LeetCode Challenge Problem: Minimum Distance Between Three Equal Elements I Day 41 is a clean hashing + index tracking + greedy observation problem 🔥 💡 Key Insight: We need three indices (i, j, k) such that: 👉 nums[i] == nums[j] == nums[k] And minimize: 👉 |i - j| + |j - k| + |k - i| 🔍 Simplification Trick: If we sort indices → i < j < k, then: 👉 Distance becomes: (j - i) + (k - j) + (k - i) = 2 * (k - i) 🔥 So we only need to: 👉 Minimize (k - i) for any 3 equal elements 🔍 Core Approach: 1️⃣ Group Indices by Value Use a hashmap: value → list of indices 2️⃣ Check Each Group If size ≥ 3: Slide window of size 3 Compute k - i 3️⃣ Track Minimum Distance Answer = 2 * (k - i) 👉 If no valid triplet → return -1 🔥 What I Learned Today: Simplifying formulas can reduce complexity drastically Hashing helps group related data efficiently Sliding window works even on index lists 📈 Challenge Progress: Day 41/100 ✅ Beyond halfway to 50! LeetCode, HashMap, Arrays, Greedy, Sliding Window, Optimization, DSA Practice, Coding Challenge, Problem Solving #100DaysOfCode #LeetCode #DSA #CodingChallenge #Hashing #Arrays #ProblemSolving #TechJourney #ProgrammerLife #SoftwareDeveloper #CodingLife #LearnToCode #Developers #Consistency #GrowthMindset #InterviewPrep
Minimum Distance Between Three Equal Elements I
More Relevant Posts
-
🚀 Today’s LeetCode Solve: 3Sum (Problem #15) Today I solved LeetCode 15 – 3Sum, a classic problem that strengthens two-pointer and sorting concepts. 🔍 Problem summary: Given an array, find all unique triplets such that: nums[i] + nums[j] + nums[k] = 0 💡 Approach used: --> First, sort the array --> Fix one element and use two pointers (left, right) for the remaining part --> Skip duplicates to ensure only unique triplets --> Move pointers based on the sum: --> If sum < 0 → move left pointer --> If sum > 0 → move right pointer -->If sum == 0 → store result and move both ⚡ Efficiency: --> Time Complexity: O(n²) --> Space Complexity: O(1) (excluding result list) 📌 Key takeaway: Combining sorting + two-pointer technique is a powerful pattern for many array problems. Challenging problem, but great learning experience 💪 #LeetCode #DSA #3Sum #TwoPointers #ProblemSolving #Algorithms #CodingPractice #LearningInPublic
To view or add a comment, sign in
-
-
📌 Strengthening DSA Concepts through Problem Solving Recently, I worked on LeetCode 114 - Flatten Binary Tree to Linked List At first glance, it looks like a simple tree problem… but the real challenge is transforming the structure in-place while maintaining preorder traversal. 🔍 Problem You are given: 👉 A binary tree Rules: ➡️ Convert it into a "linked list" ➡️ Use the same tree structure ➡️ Left child should always be null ➡️ Right child should point to the next node Naive Thinking → Store preorder traversal in a list → Rebuild the tree as a linked list But this is: ❌ Uses extra space ❌ Not in-place ❌ Not optimal Optimized Approach (In-Place Traversal) 👉 Modify the tree directly without extra space 👉 Use a pointer and restructure nodes 🔑 Key Idea 1️⃣ Start from the root 2️⃣ If the current node has a left child: 👉 Find the rightmost node of left subtree 3️⃣ Connect: Rightmost node → current node’s right subtree Move left subtree to right Set left = null 4️⃣ Move to next node (right side) ⚙️ Important Code Snippets 1. Traverse the tree TreeNode node = root; while (node != null) 2. Check for left subtree if (node.left != null) 3. Find rightmost node TreeNode prev = node.left; while (prev.right != null) { prev = prev.right; } 4. Rearrange pointers prev.right = node.right; node.right = node.left; node.left = null; ⏱️ Complexity 👉 Time Complexity: O(n) 👉 Space Complexity: O(1) (in-place) 🎯 Takeaway 👉 Rewire pointers, don’t create new structures (This approach is similar to Morris Traversal thinking) 1. In-place transformation 2. Tree traversal optimization 3. Pointer manipulation Let’s keep learning and improving every day 💪 #DSA #BinaryTree #LeetCode #CodingInterview #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
The aha moments don't come from writing code. They come from staring at charts at 11 PM asking "why did this fail?" This is what it looks like before the automation. Before the signals. Before the clean dashboard. Manual backtesting. Bar by bar. Annotating every setup by hand. → "Made a new high here" → "Fade to value area" → "Unable to make new high" → "Watch inverse + divergence" Handwritten notes on a chart like a student studying for an exam. Before I built GCI Charts, I spent months doing this: Reading research papers and whitepapers on market microstructure Studying how VWAP, TRAMA, RSI divergence, and Initial Balance actually behave, Mean reversion, Trending Market, Options Greek and how can i build my edge. Manually marking entries, exits, and stops on historical charts Asking the hard questions: → Does this signal actually have edge? → What's the win rate across 100 trades? → Where does it fail? → Can I define rules tight enough for a bot to execute? My system now runs 17 confluence factors across 5 timeframes and fires signals automatically. But it started here. One chart. One pen. One question: "Does this actually work?" Respect the process. #Trading #Backtesting #AlgoTrading #Python #PineScript #Research #TradingDevelopment #BuildInPublic #Process
To view or add a comment, sign in
-
-
Leveling Up with Functions Focus: Functions, Scope, and Efficiency Day 8(04-04-2026): Today was the day I stopped repeating myself! I dove into Functions, and it’s a total game-changer for writing clean code. Instead of writing the same logic over and over, I can now "wrap" it into a function and call it whenever I need it. It’s all about working smarter, not harder. Key concepts I tackled: Parameters vs. Arguments: Understanding the difference between what a function expects and what I actually give it. The Return Statement: Learning how to get a result back from a function so I can use it elsewhere. Variable Scope: This one was tricky! Understanding why a variable created inside a function "disappears" once the function is done (Global vs. Local). In-built Libraries: Starting to explore the massive world of pre-written tools Python offers. It feels like I’m finally moving from writing "scripts" to building "systems." 🏗️ #PythonFunctions #CleanCode #Day8 #TechSkills #CodingLogic
To view or add a comment, sign in
-
-
🚀 Day 15 of My DSA Journey | Top K Frequent Elements 🔥 Today, I solved one of the most important and frequently asked problems on LeetCode — Top K Frequent Elements. 💡 Problem Understanding Given an integer array, we need to return the k most frequent elements. Sounds simple, but the challenge is to do it efficiently ⚡ 🧠 Brute Force Approach Count frequency using loops Sort elements based on frequency Pick top k ⛔ Time Complexity: O(n² log n) (inefficient for large inputs) ⚡ Optimized Approach (HashMap + Sorting) Here’s what I did: ✅ Step 1: Use a HashMap to store frequency of each element ✅ Step 2: Convert map into array of (frequency, element) pairs ✅ Step 3: Sort array in descending order of frequency ✅ Step 4: Pick first k elements 📌 Example Walkthrough Input: nums = [1,1,1,2,2,3], k = 2 Frequency Map: 1 → 3 times 2 → 2 times 3 → 1 time Sorted: (3,1), (2,2), (1,3) Output: [1,2] ⏱ Complexity Analysis Time: O(n log n) (due to sorting) Space: O(n) 🔥 Key Learning HashMap is super powerful for frequency problems Converting data into sortable structure simplifies logic Always think: Can I reduce nested loops? 🙏 Thanks to my mentor and consistency mindset — improving every single day 💪Day by day, problem by problem — becoming better than yesterday 🚀 #Day15 #DSAJourney #LeetCode #Java #Coding #ProblemSolving #HashMap #Sorting #TopKFrequent #Consistency #LearningInPublic
To view or add a comment, sign in
-
-
📌 Strengthening DSA Concepts through Problem Solving Recently, I worked on LeetCode 525 – Contiguous Array At first glance, it looks like a simple array problem… but the real challenge? Converting it into a prefix sum problem and spotting patterns 🔍 Problem You’re given: 👉 nums → a binary array (only 0s and 1s) Your task: 👉 Find the maximum length of a contiguous subarray with equal number of 0s and 1s 🧠 Naive Thinking → Generate all subarrays → Count 0s and 1s in each But this leads to: ❌ Time complexity ~ O(n²) ❌ Not efficient for large inputs 💡 Optimized Approach (Prefix Sum + HashMap) 👉 Convert the problem cleverly: Treat 0 as -1 Treat 1 as +1 Now the problem becomes: 👉 Find the longest subarray with sum = 0 🔑 Key Idea 1️⃣ Maintain a running sum 2️⃣ Store the first occurrence of each sum in a HashMap 3️⃣ If the same sum appears again → subarray in between has sum = 0 4️⃣ Track the maximum length 👉 Why first occurrence? Because it gives the longest possible subarray ⚙️ Core Logic 👉 If sum is seen before at index j 👉 Current index is i Then: 👉 subarray length = i - j 🚀 Why this works 👉 Equal 0s and 1s cancel each other out 👉 Same prefix sum means net effect is zero 👉 Efficiently avoids recomputation ⏱️ Complexity 👉 Time Complexity: O(n) 👉 Space Complexity: O(n) #DSA #PrefixSum #HashMap #LeetCode #ProblemSolving #CodingInterview #100DaysOfCode
To view or add a comment, sign in
-
Day 71 on LeetCode Find Peak Element ⛰️✅ Today’s problem introduced a Binary Search approach, but I first solved it using a brute-force traversal to build intuition. 🔹 Approach Used in My Solution (Brute Force) The goal was to find an element that is greater than its neighbors. Key idea: • Traverse the array from index 1 to n-1 • Check if current element is greater than its neighbors • Handle edge cases like last element separately • Return the index once a peak is found This approach is simple and works reliably. 🔹 Optimized Approach (Binary Search Insight) • If nums[mid] < nums[mid+1] → peak lies on the right side • Else → peak lies on the left side (including mid) • Continue narrowing until left == right ⚡ Complexity: • Brute Force: O(n) • Binary Search: O(log n) 💡 Key Takeaways: • Building intuition with brute force helps understand the problem deeply • Learned how to transition from linear scan → binary search optimization • Reinforced that multiple approaches can lead to the same result, but efficiency matters 🔥 Step by step, moving from basic logic to optimized patterns! #LeetCode #DSA #Algorithms #DataStructures #BinarySearch #Arrays #ProblemSolving #Coding #Programming #Cpp #STL #SoftwareEngineering #ComputerScience #CodingPractice #DeveloperLife #TechJourney #CodingDaily #100DaysOfCode #BuildInPublic #AlgorithmPractice #CodingSkills #Developers #TechCommunity #SoftwareDeveloper #EngineeringJourney
To view or add a comment, sign in
-
-
🚀 Staying consistent and improving problem-solving skills with each step. LeetCode Progress 15/50 — Missing Number I worked on a problem focused on arrays and mathematical optimization 🧩 💡 The challenge was to find the missing number in a given range from 0 to n using an efficient approach. 🧠 Approach: Applied the sum formula of the first n natural numbers and compared it with the actual sum of the array to identify the missing value. This avoids the need for extra space and keeps the solution efficient. ⏱ Time Complexity: O(n) 💡 What I learned: This problem highlighted how mathematical concepts can simplify problems and lead to clean and optimized solutions. 📈 Strengthening fundamentals and exploring different ways to approach the same problem efficiently. #LeetCode #DSA #ProblemSolving #Cpp #CodingJourney 🚀
To view or add a comment, sign in
-
-
🚀 Day 31/50 – LeetCode Challenge 🧩 Problem: Decode Ways Today’s problem focused on finding the number of ways to decode a numeric string into letters — a great exercise in recursion and backtracking. 📌 Problem Summary: Given a string of digits, count the number of ways to decode it where: '1' → 'A', '2' → 'B', … , '26' → 'Z' Example: Input: "226" Output: 3 Possible decodings: "2 2 6" → B B F "22 6" → V F "2 26" → B Z 🔍 Approach Used (Backtracking) ✔ Explored all possible ways recursively ✔ At each step: Took one digit (if valid) Took two digits (if between 10–26) ✔ Skipped invalid cases like: leading 0 numbers > 26 ✔ Counted all valid decoding paths ⏱ Time Complexity: O(2ⁿ) (exploring all possibilities) 📦 Space Complexity: O(n) (recursion stack) 💡 Key Learning ✔ Understanding backtracking for exploring choices ✔ Handling constraints carefully (like 0 cases) ✔ Breaking problems into smaller subproblems ✔ Realizing when optimization (DP) is needed This problem shows how recursion can generate all possibilities, but also highlights the need for optimization using DP for better performance. Consistency builds strong problem-solving skills 🚀 🔗 Problem Link: https://lnkd.in/gdQpmEeJ #50DaysOfLeetCode #LeetCode #DSA #Backtracking #Recursion #DynamicProgramming #ProblemSolving #CodingJourney #FutureAIEngineer #Consistency
To view or add a comment, sign in
-
-
🚀 Day 85 of LeetCode Problem Solving Journey — 100 Days LeetCode Challenge Today, I solved LeetCode #349 — Intersection of Two Arrays using C++, under the guidance of Trainer NEKAL SINGH SALARIA Singh at REGex Software Services. 🔍 Problem Summary: Given two integer arrays nums1 and nums2, return their intersection. Each element in the result must be unique, and the order does not matter. 🧠 Approach Used (Unordered Set): I used an unordered_set for faster lookup: Store elements of nums1 in an unordered set Traverse nums2 and check if elements exist in the set If found → add to result set (to maintain uniqueness) Convert result set into a vector and return This approach improves performance due to constant-time lookup. 📚 Key Learnings of the Day ✔ unordered_set provides average O(1) lookup time ✔ Useful for problems involving uniqueness and fast search ✔ Reduces overall time complexity compared to ordered structures ✔ Choosing the right data structure is crucial ⏱ Complexity • Time Complexity: O(n + m) • Space Complexity: O(n) 💡 Optimization Insight: Using unordered_set is already optimal for this problem due to fast lookup and uniqueness handling. 85 days strong — see you on Day 86 🚀 #Day85 #100DaysLeetCodeChallenge #LeetCode #RegexSoftwareServices #NekalSingh #ProblemSolving #DSA #CPlusPlus #CodingChallenge #ProgrammingJourney #Hashing #KeepGrowing
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