🚀 Day 46 of 150 Days LeetCode Challenge #150DaysOfCode #LeetCode #CodingChallenge #DSA #HashMap #CPlusPlus #Programming #SoftwareEngineering #TechLearning #ProblemSolving #CodeDaily #LeetCode150DaysChallenge #Algorithm #DataStructures 🔹 Problem Statement:Contains Duplicate II Given an integer array nums and an integer k, check if there exist two distinct indices i and j in the array such that: nums[i] == nums[j] |i - j| <= k In simple words: “Does the array contain a duplicate within a distance of k?” 🔹 Example: Input: nums = [1,2,3,1], k = 3 Output: true Explanation: nums[0] and nums[3] are both 1, and the distance between them is 3 ≤ k. 🔹 Approach: Use a hash map to store each number with its most recent index. Iterate through the array: If the number already exists in the map, calculate the distance between the current index and the previous index. If this distance ≤ k, return true. Update the map with the current index. Return false if no such pair is found. Time Complexity: O(n) | Space Complexity: O(n) 🔹 Key Insights: ✅ Hash maps allow O(1) lookups for duplicates. ✅ Always update the index to track the most recent occurrence. ✅ Brute-force approaches take O(n²), but hash maps reduce it to O(n).
Day 46 of 150 Days LeetCode Challenge: Contains Duplicate II
More Relevant Posts
-
🚀 Day 22/30 – 30DaysOfDSAChallenge 🚀 🧠 Topic: Subsets (Power Set Generation) 📘 Concepts Covered: Recursion | Backtracking | Power Set Today I solved the Subsets problem, where the goal is to generate all possible combinations (subsets) of a given array. This problem is a classic example of recursive backtracking and helps in building a solid foundation for combinatorial logic. 🔹 Approach – Recursive Backtracking 💡 Logic: At each recursive step, decide whether to include or exclude the current element. When we reach the end of the array, one subset is complete and added to the result. This method explores 2ⁿ possible subsets efficiently. ⚙️ Time Complexity: O(2ⁿ) ⚙️ Space Complexity: O(2ⁿ * n) (for storing all subsets) ✅ Result: Accepted ✅ | Runtime: 0 ms 🟢 | Beats: 100% 🚀 ✨ Key Takeaway: Recursion becomes powerful when you break a problem into smaller, self-similar parts. Every inclusion or exclusion step leads you toward mastering backtracking and decision trees. #Day22 #30DaysOfDSAChallenge #LeetCode #Recursion #Backtracking #PowerSet #Subsets #ProblemSolving #CPlusPlus #CodingChallenge #DSA #Programming #CodeEveryday #DeveloperJourney #Algorithms
To view or add a comment, sign in
-
-
📅 Day 36 of #100DaysOfCode Today’s challenge was one of the most interesting and tricky recursion problems! Problem: Regular Expression Matching (LeetCode #10) 🔍 Approach: The goal is to determine if a given string text matches a pattern that includes: . → Matches any single character * → Matches zero or more occurrences of the preceding element 1️⃣ Used recursion to check character-by-character matches. 2️⃣ For each step: Checked if the current characters match (text[0] == pattern[0] or pattern[0] == '.'). If the next pattern character is *, explored two possibilities: Skip * and the previous character (zero occurrence). Use * to match one or more occurrences (consume one character from text). 3️⃣ Continued recursively until the entire text and pattern were checked. #100DaysOfCode #LeetCode #DSA #ProblemSolving #Cplusplus #CodingChallenge #Algorithms #Programming #DeveloperLife #CodeNewbie #DailyDSA #SoftwareEngineering #CodingJourney #LearningInPublic #TechCommunity #KeepLearning #Recursion #GrowthMindset #Motivation
To view or add a comment, sign in
-
-
🚀 Day 16 of my 120 Days LeetCode Challenge! 🧩 Problem 16: 3Sum Closest (Medium) The problem statement is: Given an integer array nums and an integer target, find three integers in nums such that their sum is closest to the target. Return the sum of these three integers. You may assume that each input would have exactly one solution. 💡 Example: Input: nums = [-1, 2, 1, -4], target = 1 Output: 2 Explanation: The sum closest to 1 is 2 (-1 + 2 + 1 = 2). ⚙️ Algorithm Used: For this problem, I implemented a two-pointer approach after sorting the array. Here’s the step-by-step logic: Sort the array to arrange elements in increasing order. Fix one element and then use two pointers — one starting from the next index (left) and another from the end (right). Calculate the sum of these three elements. If the current sum is closer to the target than the previously recorded closest sum, update the closest sum. Move pointers accordingly: If currentSum < target, move left++ (to increase sum). If currentSum > target, move right-- (to decrease sum). If an exact match is found, return it immediately since it’s the best possible. ⏱ Time Complexity: O(n²) 💾 Space Complexity: O(1) 🔥 This problem helped me strengthen my understanding of sorting and two-pointer techniques for array-based problems. I’m steadily progressing with consistency and learning something new every day! 💪 #Day16 #LeetCode #CodingChallenge #Cplusplus #DSA #ProblemSolving #TwoPointer #LearningJourney #100DaysOfCode #Programming
To view or add a comment, sign in
-
-
🟩 Day 56 of Leetcode 150 days challenge – Add Two Numbers (LeetCode #2) #LeetCode150 #Day56 #AddTwoNumbers #LinkedList #DataStructures #Algorithms #ProblemSolving #LeetCode #CodingChallenge #CPP #Programming #150DaysOfCode #LearnDSA #SDEPrep #CodingJourney 🔹 Problem: Given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit. Add the two numbers and return the sum as a linked list. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807 🧠 Approach: We perform the addition just like we do on paper: Initialize a carry as 0. Traverse both lists simultaneously. Add the digits from both lists + carry. Store the last digit in a new node. Update carry = sum / 10. Continue until both lists are done and carry is 0. 🕒 Time Complexity: O(max(M, N)) 💾 Space Complexity: O(max(M, N)) 🏁 Key Takeaway: Linked list addition is a classic problem that strengthens your understanding of pointer manipulation, carry handling, and edge cases like unequal list lengths. 💡
To view or add a comment, sign in
-
-
Solved Pow(x, n) (LeetCode #50) recently, and this one turned out to be more than just another coding exercise. 💡 32-bit Integer Insights: 🔹 An integer uses 32 bits: 1 bit is for the sign (positive/negative), and 31 bits store the actual value. 🔹 Negative values: Represented from -1 to -2,147,483,648 (a total of 2,147,483,648 values). 🔹 Positive values: From 1 to 2,147,483,647 (a total of 2,147,483,647 values), since zero also needs a spot. 🔹 That’s why the maximum positive (2,147,483,647) and maximum negative (-2,147,483,648) values in 32-bit integers don’t match. 🔀 My Fix: Convert n to a long before using recursion. This avoids overflow issues and ensures the code works for all edge cases. This challenge not only strengthened my understanding of binary exponentiation, but also revised those crucial details about 32-bit integer representation. #LeetCode #DSA #Programming #100DaysOfCode #BinaryExponentiation #TechLearning #CodingJourney #PGPByAnchal
To view or add a comment, sign in
-
-
🥷 Day 164 of My 365 LeetCode Challenge is done! 💥 Kicked off strong, solved my problem, and the coding vibe is awesome! 🧠 💡 LeetCode 1625 — Lexicographically Smallest String After Applying Operations Today’s challenge was an interesting blend of string manipulation, BFS traversal, and modular arithmetic — the kind of problem that truly sharpens both your algorithmic thinking and pattern recognition skills. 🧠✨ We’re given a numeric string s and two integers a and b. Two operations can be performed repeatedly in any order: 1️⃣ Add a to all digits at odd indices (with digits wrapping around modulo 10). 2️⃣ Rotate the string to the right by b positions. The goal? 🔍 Find the lexicographically smallest string possible after performing any number of these operations. Sounds simple, right? But here’s the catch — since operations can be performed infinitely, the problem hides a state-space exploration challenge. The same string can appear multiple times via different paths, so blindly iterating could easily lead to infinite loops or redundant computations. ⚙️ Approach & Thought Process: To systematically explore all possible transformations, I used Breadth-First Search (BFS) — perfect for enumerating all reachable states in minimal steps. 🔸 Step 1: Start from the original string and push it into a queue. 🔸 Step 2: For each string, perform both allowed operations: - Add a to digits at odd indices (handling digit wraparound using (digit + a) % 10). - Rotate the string by b positions. 🔸 Step 3: Use a set to track all previously visited strings to prevent cycles. 🔸 Step 4: Continuously compare and update the smallest lexicographic string seen so far. Once the queue is exhausted, the smallest recorded string is our answer ✅ 🔥 Key Learning: This problem elegantly demonstrates how graph traversal (BFS) can be applied beyond traditional graphs — even on state transformations of strings. Recognizing this pattern is a huge step toward mastering advanced algorithmic thinking. ✨ Every problem like this one is a reminder that great coding isn’t about memorizing — it’s about seeing structure where others see chaos. #LeetCode #CPlusPlus #CodingChallenge #ProblemSolving #Algorithms #BFS #SoftwareEngineering #Programming #LearningJourney #CodeNewbie #DataStructures
To view or add a comment, sign in
-
-
💻 Day 15 of My 120 Days LeetCode Challenge 🚀 Problem: 3Sum (LeetCode #15) 🧩 Problem Statement: Given an integer array nums, the task is to find all unique triplets [nums[i], nums[j], nums[k]] such that: nums[i] + nums[j] + nums[k] == 0 and i, j, k are all different indices. If no such triplets exist, return an empty list. The output should not contain duplicate triplets. 🧠 Example: Input: nums = [-1, 0, 1, 2, -1, -4] Output: [[-1, -1, 2], [-1, 0, 1]] ⚙️ Approach / Algorithm Used: I solved this problem using the Two-Pointer Approach after sorting the array. 🔹 Steps: Sort the array to handle duplicates easily. Fix one element (nums[i]) and use two pointers — left and right. Move the pointers based on the sum: If sum < 0 → move left++ If sum > 0 → move right-- If sum == 0 → store the triplet and skip duplicates. Continue until all unique triplets are found. This method efficiently finds all possible triplets without repetition. 🕒 Time Complexity: O(n²) (because we fix one element and use two pointers for the rest) 💾 Space Complexity: O(1) (ignoring the output storage) ✅ Key Learning: Practiced handling duplicates in sorted arrays. Improved my understanding of two-pointer technique. Learned how to efficiently find triplets that satisfy given conditions. 🎯 #Day15 #LeetCodeChallenge #DSA #C++ #TwoPointer #ProblemSolving #CodingJourney #100DaysOfCode #LearnByDoing #Programming
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge — 3726. Remove Zeros in Decimal Representation (Easy) Ever thought how a simple number like 1020030 can turn into 123? 🤔 That’s the power of string manipulation in programming — short, clean, and efficient! 💡 🧩 Problem Summary: You’re given a positive integer n. Your task: Remove all zeros from its decimal representation. Example: Input: n = 1020030 Output: 123 ✅ Hint: Convert the number to a string → remove all '0' → convert back to integer. That’s it — one-liner solution for clean data transformation. 🔥 💻 I’ve explained this problem with step-by-step approaches and code on my website 👉 algopush.com Check it out for brute force to optimized explanations! #LeetCode #LeetCodeChallenge #Coding #ProblemSolving #DSA #Programming #100DaysOfCode #CodeSeCareer #TechLearning #Developers #algopush #LeetCodeDaily #StringManipulation #LearnToCode #erdelhiboy #leetcodeeasy
To view or add a comment, sign in
-
-
🚀 Day 63 of #100DaysOfCode Today I tackled LeetCode 84 — Largest Rectangle in Histogram using C++ 💪 📌 Problem Summary: Given an array representing the heights of bars in a histogram, the goal is to find the largest rectangular area that can be formed within the histogram. For example: Input: [2,1,5,6,2,3] Output: 10 (formed between heights 5 and 6) 📌 Approach: This is a classic stack-based problem involving Next Smaller Element (NSE) and Previous Smaller Element (PSE) logic. Steps: 1️⃣ Use a monotonic stack to find the index of the next smaller element for each bar. 2️⃣ Similarly, find the previous smaller element for each bar. 3️⃣ Compute width = nsi[i] - psi[i] - 1 4️⃣ Calculate area = height[i] * width and keep track of the maximum. 📌 Complexity: ⏱ Time: O(n) — each element is pushed and popped once. 💾 Space: O(n) — for storing stack and helper arrays. 📌 Key Learning: This problem strengthens the understanding of monotonic stacks, boundary calculations, and range-based area determination — a foundation for many advanced DSA problems (like Maximal Rectangle). Consistency pays off — 60 days strong and still coding daily! 🔥 Raj Vikramaditya Raghav Garg Nancy Solanki Shweta Arora Harsh Raj Harshita Verma Love Babbar Prince Singh Shivam Mahajan Rohit Negi Neeraj Walia Nishant Chahar Kushal Vijay #LeetCode #100DaysOfCode #CodingChallenge #Cplusplus #DataStructures #LinkedList #ProblemSolving #SoftwareEngineering #Developers #Programming #TechJourney #KeepLearning #DailyCoding #CodeNewbie#LeetCode #100DaysOfCode #Programming #Cplusplus #LinkedList #ProblemSolving #SoftwareEngineering #TechJourney #CodeNewbie #DSA
To view or add a comment, sign in
-
-
💻 Day 34 of #100DaysOfLeetCode Today’s Challenge: 83. Remove Duplicates from Sorted List 🔹 Problem: Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. Key Insight: Because the list is already sorted, duplicates will always be adjacent—this allows us to remove them in a single pass using pointer manipulation. 🔍 Approach: Traverse the list using a pointer. Compare current node with the next node. If values are equal → skip the next node by linking to the next of next. Otherwise → move forward. 🕒 Time Complexity: O(n) 📦 Space Complexity: O(1) ✨ Key Takeaway: This problem reinforces the importance of pointer manipulation and understanding how memory references work in linked lists. A simple check can eliminate duplicates efficiently without using extra space. Link:[https://lnkd.in/gsjVxHXM] #100DaysOfLeetCode #Day34 #LeetCode #ProblemSolving #DSA #Algorithms #LinkedList #CodingChallenge #CodeNewbie #InterviewPreparation #SoftwareEngineering #Programming #CodingCommunity #TechCareers #CareerGrowth #ArjunInfoSolution
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