Minimum Distance Between Three Equal Elements I

🚀 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

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories