Find Minimum Distance of Duplicate Indices in Array

🚀 LeetCode Day Problem Solving 🚀 Day-45 📌 Problem: Given an array nums[], find a good tuple (i, j, k) such that: ✔ nums[i] == nums[j] == nums[k] ✔ i < j < k 👉 Distance of tuple = |i - j| + |j - k| + |k - i| 🎯 Return the minimum possible distance among all such tuples ❌ If no valid tuple exists → return -1 🧠 Example: Input: nums = [1,2,1,1,3] ✅ Output: 6 📖 Explanation: Tuple → (0, 2, 3) Distance = 2 + 1 + 3 = 6 💡 Key Insight: ✔ For same values, indices matter ✔ Distance simplifies to: 👉 2 * (k - i) (since i < j < k) ✔ So we just need: 👉 Closest 3 indices of same number ⚡ Optimized Approach: ✔ Use HashMap / Array of lists to store indices ✔ For each value:  👉 Traverse indices and check every consecutive triplet 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 🧠 What I Learned: ✔ Pattern observation simplifies formula ✔ Grouping indices using hashmap ✔ Optimization by avoiding brute force O(n³) ✅ Day 45 Completed 🚀 Leveling up in Arrays + Optimization + Math Tricks 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀

  • graphical user interface, text, application, email

To view or add a comment, sign in

Explore content categories