🚀 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 🚀
Circular Array Minimum Distance LeetCode Solution
More Relevant Posts
-
🚀 #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-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-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 🚀
To view or add a comment, sign in
-
-
🚀 #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-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 Daily | Day 78 🔥 LeetCode POTD – 3488. Closest Equal Element Queries (Medium) ✨ 📌 Problem Insight Given a circular array: ✔ For each query index, find nearest same value ✔ Distance is circular ✔ Return minimum distance ✔ If no same value exists → return -1 🔍 Initial Thinking – Brute Force ⚙️ 💡 Idea: ✔ For each query, scan entire array ✔ Check all indices with same value ⚠️ Problem: ✔ O(n) per query → too slow ✔ Total becomes O(n²) in worst case 💡 Key Observation 🔥 ✔ Same values repeat → group their indices ✔ Nearest answer lies among adjacent indices in that group ✔ Circular distance: → min(|i - j|, n - |i - j|) 🚀 Optimized Approach ✔ Store indices for each value (hash map) ✔ For each query: → Use binary search to find position → Check nearest left & right indices ✔ Handle circular wrap using modulo 🔧 Core Idea ✔ Reduce search space using grouping ✔ Use binary search for nearest neighbors ✔ Apply circular distance formula ⏱ Complexity ✔ Time: O(n + q log n) ✔ Space: O(n) 🧠 Key Learning ✔ Nearest element problems → check neighbors, not all ✔ Preprocessing (grouping) can drastically optimize queries ✔ Circular arrays often need wrap-around handling 🚀 Takeaway A great mix of hashing + binary search + circular logic — classic interview pattern to reduce brute force into efficient queries ⚡ #LeetCode #DSA #Algorithms #CPlusPlus #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 28/100 — Problem: Minimum Spanning Tree (Prim’s Algorithm) Find the minimum cost to connect all nodes in a graph Concept: Greedy + Priority Queue - Start from any node - Always pick the smallest edge - Expand the tree step by step import heapq def prim(n, graph): visited = set() min_heap = [(0, 0)] # (weight, node) total_cost = 0 while min_heap: weight, node = heapq.heappop(min_heap) if node in visited: continue visited.add(node) total_cost += weight for neighbor, w in graph[node]: if neighbor not in visited: heapq.heappush(min_heap, (w, neighbor)) return total_cost What I learned: Greedy works when choosing the best local option builds the global solution Priority queue helps efficiently select the next best edge Time Complexity: O(E log V) #100DaysOfCode #DSA #Graphs #PrimsAlgorithm #CodingInterview #ProblemSolving #PlacementPrep #LearnInPublic
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