🥷 Day 343 of My 365 LeetCode Challenge is done! 💥 Kicked off strong, solved my problem, and the coding vibe is awesome! 🧠 🚀 Solved: Closest Equal Element Queries Today’s problem looked simple at first, but it had a clever twist involving circular arrays and efficient searching. 🔍 Problem Overview: You’re given an array and multiple queries. For each query index, the goal is to find the minimum distance to another index having the same value, considering the array as circular. If no such index exists, return -1. 💡 Key Observation: Brute force would be too slow ❌ Instead, the idea is to: Store positions of each value 📍 Use binary search to quickly find the nearest occurrence Handle the circular nature by checking both directions 🧠 Approach: Preprocess the array using a map (value → list of indices) For each query: Get the list of indices for that value Use binary search to locate the current index Check neighbors (left & right) to compute minimum distance Don’t forget to account for circular wrapping 🔄 ⚡ Why this works: Preprocessing reduces repeated work, and binary search ensures efficient lookups, making the solution scalable even for large inputs. 🔥 Takeaway: Sometimes optimization comes from organizing data smartly rather than complex logic. #LeetCode #DSA #BinarySearch #DataStructures #Coding #ProblemSolving #Cpp #TechJourney
Closest Equal Element Queries LeetCode Solution
More Relevant Posts
-
🚀 Day 23/60 — Learning Step by Step Today I worked on: 📌 Find All Numbers Disappeared in an Array (LeetCode #448) At first, the problem looked straightforward, but I was unsure how to solve it without using extra space. My initial instinct was to use a hash map, but that would increase space complexity. 💡 The real learning came when I discovered an in-place marking approach: • Using index mapping (value → index) • Marking visited elements by converting values to negative • Identifying missing numbers by checking positive indices • Achieving O(n) time and O(1) extra space This problem helped me understand how we can cleverly use the given array itself as a data structure instead of relying on extra memory. ✨ Key Takeaways: • Always look for in-place solutions before using extra space • Index-based patterns are very powerful in arrays • Constraints often guide you toward the optimal approach • Small observations can lead to big optimizations Still learning, still improving — one step at a time 🚀 Grateful for the guidance and inspiration from Shiv ram Sharma sir. #DSA #LeetCode #Arrays #ProblemSolving #LearningInPublic #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 89 of LeetCode Problem Solving Journey — 100 Days LeetCode Challenge Today, I solved LeetCode #162 — Find Peak Element using C++, under the guidance of Trainer NEKAL SINGH SALARIA Singh at REGex Software Services. 🔍 Problem Summary: Given an integer array nums, find a peak element and return its index. A peak element is greater than its neighbors. 🧠 Approach Used (Binary Search): I used the binary search technique to find a peak efficiently: Initialize low = 0 and high = n - 1 Calculate mid = (low + high) / 2 If nums[mid] < nums[mid + 1] → move to the right half Else → move to the left half (including mid) Continue until low == high At the end, low (or high) will point to a peak element. 📚 Key Learnings of the Day ✔ Binary search can be applied beyond sorted arrays ✔ Comparing adjacent elements helps identify direction ✔ Divide-and-conquer improves efficiency ✔ There can be multiple peaks — return any one ⏱ Complexity • Time Complexity: O(log n) • Space Complexity: O(1) 💡 Optimization Insight: A linear scan would take O(n) time, but binary search reduces it to O(log n), making it optimal for large inputs. Almost at the finish line — see you on Day 90 🚀 #Day89 #100DaysLeetCodeChallenge #LeetCode #RegexSoftwareServices #NekalSingh #ProblemSolving #DSA #CPlusPlus #CodingChallenge #ProgrammingJourney #BinarySearch #KeepGrowing
To view or add a comment, sign in
-
-
🚀 Building My DSA Foundation on LeetCode I’ve started focusing seriously on Data Structures & Algorithms and recently completed 30 problems on LeetCode. Instead of just solving problems, I focused on understanding the why behind each approach. 🔹 What I worked on: Arrays & Strings Two Pointer Technique Basic Sorting & Searching 🔹 Key challenges: Moving from brute force to optimized solutions Handling edge cases effectively Improving time & space complexity 🔹 What improved: Problem-solving approach Ability to recognize patterns Writing cleaner and more efficient code 📌 I’ve documented my solutions with explanations here: 👉 https://lnkd.in/dzvjyViC This is just the starting point. Next goal: 50+ problems with a focus on Medium-level questions and deeper pattern recognition. #DSA #LeetCode #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 92 of LeetCode Problem Solving Journey — 100 Days LeetCode Challenge Today, I solved LeetCode #28 — Find the Index of the First Occurrence in a String using C++, under the guidance of Trainer NEKAL SINGH SALARIA Singh at REGex Software Services. 🔍 Problem Summary: Given two strings haystack and needle, return the index of the first occurrence of needle in haystack. If needle is not part of haystack, return -1. 🧠 Approach Used (Brute Force String Matching): I used a simple comparison approach: Traverse the haystack string For each index, compare substring with needle If all characters match → return the starting index If no match is found → return -1 This approach checks all possible starting positions. 📚 Key Learnings of the Day ✔ String matching can be done using direct comparison ✔ Substring checking requires careful index handling ✔ Early stopping improves efficiency ✔ Understanding string traversal is important ⏱ Complexity • Time Complexity: O(n × m) (n = length of haystack, m = length of needle) • Space Complexity: O(1) 💡 Optimization Insight: More efficient algorithms like KMP (Knuth-Morris-Pratt) can reduce time complexity to O(n + m). Step by step, getting closer — see you on Day 93 🚀 #Day92 #100DaysLeetCodeChallenge #LeetCode #RegexSoftwareServices #NekalSingh #ProblemSolving #DSA #CPlusPlus #CodingChallenge #ProgrammingJourney #Strings #KeepGrowing
To view or add a comment, sign in
-
-
🚀 Day 84 of LeetCode Problem Solving Journey — 100 Days LeetCode Challenge Today, I solved LeetCode #73 — Set Matrix Zeroes using C++, under the guidance of Trainer NEKAL SINGH SALARIA Singh at REGex Software Services. 🔍 Problem Summary: Given an m x n matrix, if an element is 0, set its entire row and column to 0. The operation must be performed in-place. 🧠 Approach Used (Using Markers): I used the first row and first column as markers: Traverse the matrix and mark rows & columns that need to be zeroed Use matrix[0][j] and matrix[i][0] as flags Keep separate flags to track if the first row or first column should be zero Update the matrix based on these markers Finally update the first row and column if needed This avoids using extra space while keeping the solution efficient. 📚 Key Learnings of the Day ✔ Using matrix itself as storage avoids extra space ✔ Markers help track changes efficiently ✔ Careful handling of first row/column is important ✔ In-place modification requires planning ⏱ Complexity • Time Complexity: O(m × n) • Space Complexity: O(1) 💡 Optimization Insight: This is the optimal solution since it avoids extra arrays and uses constant space while modifying the matrix in-place. Consistency is becoming a habit — see you on Day 85 🚀 #Day84 #100DaysLeetCodeChallenge #LeetCode #RegexSoftwareServices #NekalSingh #ProblemSolving #DSA #CPlusPlus #CodingChallenge #ProgrammingJourney #Matrix #KeepGrowing
To view or add a comment, sign in
-
-
🚀 Day 95 of LeetCode Problem Solving Journey — 100 Days LeetCode Challenge Today, I solved LeetCode #1991 — Find the Middle Index in Array using C++, under the guidance of Trainer NEKAL SINGH SALARIA Singh at REGex Software Services. 🔍 Problem Summary: Given an integer array nums, return the middle index where the sum of elements to the left is equal to the sum of elements to the right. If no such index exists, return -1. 🧠 Approach Used (Prefix Sum): I used a prefix sum technique, similar to pivot index: Calculate the total sum of the array Initialize leftSum = 0 Traverse the array: Compute rightSum = totalSum - leftSum - nums[i] If leftSum == rightSum → return index i Update leftSum += nums[i] This efficiently finds the required index in one pass. 📚 Key Learnings of the Day ✔ Prefix sum avoids repeated calculations ✔ Left and right sum comparison simplifies logic ✔ Similar problems can reuse the same pattern ✔ Pattern recognition improves problem-solving speed ⏱ Complexity • Time Complexity: O(n) • Space Complexity: O(1) 💡 Optimization Insight: This is already the optimal solution since it solves the problem in a single pass without extra space. Consistency almost completed — see you on Day 96 🚀 #Day95 #100DaysLeetCodeChallenge #LeetCode #RegexSoftwareServices #NekalSingh #ProblemSolving #DSA #CPlusPlus #CodingChallenge #ProgrammingJourney #Arrays #KeepGrowing
To view or add a comment, sign in
-
-
Day 23/128 of my LeetCode journey 🚀 Solved Contains Duplicate II — a great problem that helped reinforce the sliding window technique and efficient use of data structures. 💡 Key takeaway: Instead of checking all pairs, I used a sliding window with a Set to track elements within a range of size k. This avoids unnecessary comparisons and keeps the solution efficient. 🔍 What I practiced: Sliding window technique Efficient use of Set Optimizing time complexity Every problem might not be complex, but each one strengthens the fundamentals — and that’s what builds strong problem-solving skills over time. 💪 #LeetCode #DSA #CodingJourney #128DaysOfCode #SoftwareEngineering #ProblemSolving
To view or add a comment, sign in
-
-
🚀 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
-
-
🚀 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 related topics
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