🚀 LeetCode Day Problem Solving 🚀 Day-44 📌 Problem: You are given an integer array nums. A tuple (i, j, k) is called good if: ✔ i, j, k are distinct ✔ nums[i] == nums[j] == nums[k] 👉 Distance of a good tuple: |i - j| + |j - k| + |k - i| 🎯 Goal: Find the minimum possible distance among all good tuples. If none exist → return -1. 🧠 Example: Input: nums = [1,2,1,1,3] ✅ Output: 6 📖 Explanation: Good tuple → (0,2,3) Distance = 2 + 1 + 3 = 6 💡 Key Insight: ✔ For same values, we only care about indices ✔ Sort indices for each number ✔ Try triplets of closest indices 👉 Important formula simplification: For sorted i < j < k Distance = (j-i) + (k-j) + (k-i) = 2*(k - i) ✔ So minimize → difference between farthest indices ⚡ Approach: ✔ Group indices by value (HashMap) ✔ For each value: - If size ≥ 3 → check consecutive triplets - Compute minimum distance 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 🧠 What I Learned: ✔ Grouping using HashMap ✔ Optimizing triplet computation ✔ Using math to simplify distance formula ✅ Day 44 Completed 🚀 Leveling up in Arrays + Hashing + Optimization 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
Minimum Distance Among Three Elements in Array
More Relevant Posts
-
🚀 #LeetCode Day Problem Solving 🚀 Day-25 📌 Problem: You are given an integer array nums. A tuple (i, j, k) is called good if: ✔ i, j, k are distinct ✔ nums[i] == nums[j] == nums[k] 👉 Distance of a good tuple: |i - j| + |j - k| + |k - i| 🎯 Goal: Find the minimum possible distance among all good tuples. If none exist → return -1. 🧠 Example: Input: nums = [1,2,1,1,3] ✅ Output: 6 📖 Explanation: Good tuple → (0,2,3) Distance = 2 + 1 + 3 = 6 💡 Key Insight: ✔ For same values, we only care about indices ✔ Sort indices for each number ✔ Try triplets of closest indices 👉 Important formula simplification: For sorted i < j < k Distance = (j-i) + (k-j) + (k-i) = 2*(k - i) ✔ So minimize → difference between farthest indices ⚡ Approach: ✔ Group indices by value (HashMap) ✔ For each value: - If size ≥ 3 → check consecutive triplets - Compute minimum distance 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 🧠 What I Learned: ✔ Grouping using HashMap ✔ Optimizing triplet computation ✔ Using math to simplify distance formula ✅ Day 25 Completed 🚀 Leveling up in Arrays + Hashing + Optimization 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 LeetCode Day Problem Solving 🚀 Day-57 📌 Problem: For each index i in array nums, compute: 👉 arr[i] = sum of |i - j| for all j such that nums[j] == nums[i] and j ≠ i ✔ If no such j exists → arr[i] = 0 🧠 Example: Input: nums = [1,3,1,1,2] ✅ Output: [5,0,3,4,0] 📖 Explanation: For value 1 → indices = [0,2,3] For index 0: → |0-2| + |0-3| = 5 For index 2: → |2-0| + |2-3| = 3 For index 3: → |3-0| + |3-2| = 4 💡 Key Insight: ✔ Group indices by same values (using HashMap) ✔ For each group, compute distances efficiently 👉 Instead of brute force O(n²) ❌ Use Prefix Sum on indices 🔥 ⚡ Optimized Approach: 1️⃣ Store indices for each value 2️⃣ For each group: Maintain prefix sum of indices Use formula to compute distance in O(1) per index 👉 Left contribution + Right contribution 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 🧠 What I Learned: ✔ Grouping using HashMap ✔ Using prefix sums on indices ✔ Turning brute force into efficient solution ✅ Day 57 Completed 🚀 Leveling up in Hashing + Prefix Sum Optimization 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-37 📌 Problem: For each index i in array nums, compute: 👉 arr[i] = sum of |i - j| for all j such that nums[j] == nums[i] and j ≠ i ✔ If no such j exists → arr[i] = 0 🧠 Example: Input: nums = [1,3,1,1,2] ✅ Output: [5,0,3,4,0] 📖 Explanation: For value 1 → indices = [0,2,3] For index 0: → |0-2| + |0-3| = 5 For index 2: → |2-0| + |2-3| = 3 For index 3: → |3-0| + |3-2| = 4 💡 Key Insight: ✔ Group indices by same values (using HashMap) ✔ For each group, compute distances efficiently 👉 Instead of brute force O(n²) ❌ Use Prefix Sum on indices 🔥 ⚡ Optimized Approach: 1️⃣ Store indices for each value 2️⃣ For each group: Maintain prefix sum of indices Use formula to compute distance in O(1) per index 👉 Left contribution + Right contribution 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 🧠 What I Learned: ✔ Grouping using HashMap ✔ Using prefix sums on indices ✔ Turning brute force into efficient solution ✅ Day 37 Completed 🚀 Leveling up in Hashing + Prefix Sum Optimization 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 LeetCode Day Problem Solving 🚀 Day-50 📌 Problem: You are given a circular array nums and an array queries. For each query index i, find the minimum circular distance to another index j such that: ✔ nums[j] == nums[queries[i]] ✔ If no such index exists → return -1 🧠 Example: Input: nums = [1,3,1,4,1,3,2] queries = [0,3,5] ✅ Output: [2, -1, 3] 📖 Explanation: Query 0 → value = 1 → nearest same value at index 2 → distance = 2 Query 1 → value = 4 → no duplicate → -1 Query 2 → value = 3 → nearest at index 1 (circular path) → distance = 3 💡 Key Insight: ✔ Since array is circular, distance = 👉 min(|i - j|, n - |i - j|) ✔ Store indices of each value using a map (value → list of indices) ✔ For each query: 👉 Use binary search to find nearest index efficiently 📊 Complexity Analysis: ⏱ Time Complexity: O(n + q log n) 📦 Space Complexity: O(n) 🧠 What I Learned: ✔ Handling circular arrays smartly ✔ Using binary search on index lists ✔ Optimizing brute force to efficient lookup ✅ Day 50 Completed 🚀 Leveling up in Arrays + Binary Search + Optimization 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-30 📌 Problem: You are given a circular array nums and an array queries. For each query index i, find the minimum circular distance to another index j such that: ✔ nums[j] == nums[queries[i]] ✔ If no such index exists → return -1 🧠 Example: Input: nums = [1,3,1,4,1,3,2] queries = [0,3,5] ✅ Output: [2, -1, 3] 📖 Explanation: Query 0 → value = 1 → nearest same value at index 2 → distance = 2 Query 1 → value = 4 → no duplicate → -1 Query 2 → value = 3 → nearest at index 1 (circular path) → distance = 3 💡 Key Insight: ✔ Since array is circular, distance = 👉 min(|i - j|, n - |i - j|) ✔ Store indices of each value using a map (value → list of indices) ✔ For each query: 👉 Use binary search to find nearest index efficiently 📊 Complexity Analysis: ⏱ Time Complexity: O(n + q log n) 📦 Space Complexity: O(n) 🧠 What I Learned: ✔ Handling circular arrays smartly ✔ Using binary search on index lists ✔ Optimizing brute force to efficient lookup ✅ Day 30 Completed 🚀 Leveling up in Arrays + Binary Search + Optimization 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 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 🚀
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-26 📌 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 26 Completed 🚀 Leveling up in Arrays + Optimization + Math Tricks 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🧩 Problem: Minimum Distance Between Three Equal Elements I You are given an integer array nums. 🎯 Goal Find three indices i < j < k such that: nums[i] == nums[j] == nums[k] And minimize the value: |i - j| + |j - k| + |k - i| If no such triplet exists, return -1. 🧠 Key Intuition First, group all indices of the same value together. For each number: If it appears at least 3 times, we can form a valid triplet. Now the key observation: For minimizing the distance we will take the consecutive triplets. 🛠 Approach 1️⃣ Store indices of each number using a hash map 2️⃣ For each number: If frequency ≥ 3: Iterate through indices in order Consider every consecutive triplet 3️⃣ Compute: distance = |i - j| + |j - k| + |k - i| 4️⃣ Track the minimum distance 5️⃣ If no valid triplet found → return -1 📈 Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) 🔗 Problem https://lnkd.in/dFRkixe5 💻 Solution https://lnkd.in/diUjhYnr #LeetCode #Cpp #DSA #Algorithms #HashMap #Arrays #ProblemSolving #CodingChallenge #LeetCodeDailyChallenge #CompetitiveProgramming #CodingJourney 🚀
To view or add a comment, sign in
-
🚀 LeetCode Day Problem Solving 🚀 Day-52 📌 Problem: Given an integer n, define its mirror distance as: 👉 |n - reverse(n)| Where: ✔ reverse(n) = number formed by reversing digits ✔ Leading zeros are ignored (e.g., 10 → 01 → 1) 🧠 Examples: Input: n = 25 Output: 27 ➡ reverse(25) = 52 → |25 - 52| = 27 Input: n = 10 Output: 9 ➡ reverse(10) = 1 → |10 - 1| = 9 Input: n = 7 Output: 0 💡 Key Insight: ✔ Reverse the digits of the number ✔ Compute absolute difference 👉 Simple math + digit manipulation problem ⚙ Approach: 1️⃣ Extract digits using % 10 2️⃣ Build reversed number 3️⃣ Compute abs(n - reversed) 📊 Complexity Analysis: ⏱ Time Complexity: O(d) (d = number of digits) 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Number reversal logic ✔ Handling leading zeros properly ✔ Clean mathematical problem solving ✅ Day 52 Completed 🚀 Improving skills in Math + Implementation Problems 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
To view or add a comment, sign in
-
-
Just solved today’s LeetCode Daily: “Minimum Operations to Make a Uni-Value Grid” At first glance, it looks like a simple grid problem but the real trick is spotting the math and pattern behind it Key Insights: - You can only add or subtract x so all elements must have the same remainder mod x otherwise it's impossible - Convert 2D grid to 1D array for easier processing - The optimal target value is the median not the mean - Median minimizes total absolute operations Approach: 1. Flatten the grid 2. Check feasibility using modulo 3. Sort the array 4. Pick median 5. Count operations using distance divided by x Clean Rust implementation below impl Solution { pub fn min_operations(grid: Vec<Vec<i32>>, x: i32) -> i32 { let rows = grid.len(); let cols = grid[0].len(); let mut array: Vec<i32> = Vec::new(); let mut ans = 0; let rem = grid[0][0] % x; let n = rows * cols; let mid_index = n / 2; for row in 0..rows { for col in 0..cols { if grid[row][col] % x != rem { return -1; } array.push(grid[row][col]) } } array.sort(); let median = array[mid_index]; for val in array { ans += (val - median).abs() / x; } ans } } What I like about this problem: It’s not about brute force it’s about thinking in transformations and choosing the right mathematical anchor median This is why solving problems in Rust feels clean and structured Key Insight: When minimizing sum of absolute differences always think median Drop your intuition, thoughts, or improvements in comments #LeetCode #RustLang #DSA #ProblemSolving #CodingDaily #SoftwareEngineering #Algorithms
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