LeetCode 219: Contains duplicate II ⁇ Suppose you have a list of numbers, and you want to check if any number appears at least twice close together—within k positions apart. How does the code work? - We use a dictionary to track the last index where each number appeared. - As we go through the list: - If the current number has been seen before, - Check the gap between indices. - If the gap (i−j)is less than or equal to k, we found a nearby duplicate! - Otherwise, update the index to the current position. - If the number is new, just store this index. - If we finish the list with no matches, return False. Complexity: - Time Complexity: O(n) — Scan through all numbers in a single pass. - Space Complexity: O(n) — Need to remember each distinct number’s last position. Check out the problem here: https://lnkd.in/gUtKpsHf Keep going, keep revising, and keep building confidence! 💪🔥 #DSA #Coding #ProblemSolving #Learning
More Relevant Posts
-
LeetCode 1: Two sum 2️⃣➕7️⃣=9️⃣ Suppose you have a list of numbers and you need to find two numbers that add up to a target value. You want to know their positions in the list. How does the code work? - We use a dictionary to remember the numbers we’ve seen so far and their positions. - As we look at each number, we check if there’s another number (the complement) that would add up to the target. - If we’ve seen that complement before, we return the positions. - If not, we store the current number and its index for future checks. Complexity: - Time Complexity: O(n) — Only one pass through the array. - Space Complexity: O(n) — Extra space for the dictionary to store numbers and their indices. Check out the problem here: https://lnkd.in/gppmcPDB Keep going, keep revising, and keep building confidence! 💪🔥 #DSA #Coding #ProblemSolving #Learning
To view or add a comment, sign in
-
LeetCode 1512: Number of good pairs 1️⃣1️⃣ The problem: For a list of numbers, count how many pairs of indices (i,j) have the same value, with i<j. In other words, how many pairs are "identical"? How does the code work? - First, we use a Counter to count how many times each number appears. - For any number that appears more than once, the number of unique pairs is given by the combination formula: ( pairs=n×(n−1)/2 ), where n is how often the number appears. - Add up pairs for each number and return the total. Complexity: - Time Complexity: O(n) — One scan to count, then a quick calculation for each different value. - Space Complexity: O(n) — Storing counts of each number. Check out the problem here: https://lnkd.in/gYsrZBxu Keep going, keep revising, and keep building confidence! 💪🔥 #DSA #Coding #ProblemSolving #Learning
To view or add a comment, sign in
-
LeetCode 136: Single number 🔂 Approach 1: Hash Map - We use a dictionary to count occurrences of each number. - Go through the list and update counts. - Finally, look for the number with a count of 1—that’s our answer! Complexity: - Time Complexity: O(n) — Check all items. - Space Complexity: O(n) — Dictionary to track counts. Approach 2: XOR (Bit Manipulation) - Initialize a variable ans = 0. - XOR every number in the list with ans. - Pairs cancel out (because a ^ a = 0 and 0 ^ b = b), so only the single number remains at the end. Complexity: - Time Complexity: O(n) — Just one pass through all numbers. - Space Complexity: O(1) — No extras, just ans. The XOR trick is super efficient and works like magic due to properties of binary operations! Check out the problem here: https://lnkd.in/giUbz53v Keep going, keep revising, and keep building confidence! 💪🔥 #DSA #Coding #ProblemSolving #Learning
To view or add a comment, sign in
-
I’ve realized that DSA is not just about solving problems—it’s about how many you solve and how deeply you understand the underlying concepts. The more problems you practice, the easier it becomes to recognize patterns and apply the right approach. Today, I worked on LeetCode 1971 – Find if Path Exists in a Graph, marked as an Easy problem. But to solve it confidently, you actually need strong fundamentals in: 🔹 Graph theory 🔹 DFS & BFS (and when to use which) 🔹 Difference between Graphs vs Trees 🔹 Edge List vs Adjacency List 🔹 How to build an adjacency list in code 🔹 How DFS works internally on graphs After around two months of consistent practice, I’m finally able to identify the prerequisites required for each problem and approach them with a structured mindset. If you're exploring graphs, I highly recommend giving this problem a try. Here’s the problem: https://lnkd.in/g6hMyn88 And here’s my LeetCode profile if you'd like to connect or share feedback: https://lnkd.in/gp38YMN7 #LeetCode #DSA #Java #SoftwareEngineering #Graphs #Coding #LearningJourney
To view or add a comment, sign in
-
-
🚀 Solved LeetCode Problem: Single Number (C++) 🔢 Every element appears twice except one This problem is a great reminder that bit manipulation can often beat complex data structures with pure logic and simplicity. 💡 Approach: Used the XOR operator (^) — since x ^ x = 0 and x ^ 0 = x, all duplicate numbers cancel out, leaving the single unique number. 🧠 Key Takeaways: Time Complexity: O(n) Space Complexity: O(1) Concept: Bit Manipulation + XOR Property 💬 Elegant, fast, and minimal — that’s the beauty of C++. 🔖 #LeetCode #Cplusplus #DSA #Coding #ProblemSolving #BitManipulation #CodingInterview #SoftwareEngineering #CodeNewbie #TechLearning #C++Developer #Algorithm #DataStructures #CodingChallenge #XOR #CodeDaily #LearnToCode
To view or add a comment, sign in
-
-
DSA Practice – Day 52 🚀 Problem: Happy Number (LeetCode 202) 📌 Problem Statement: A number is called happy if repeatedly replacing it with the sum of the squares of its digits eventually leads to 1. Return true if it’s a happy number, otherwise false. 🧩 Brute Force Approach: Keep track of all numbers seen in a set to detect loops. If you reach 1, Happy Number If a number repeats, Not Happy Time Complexity: O(log n) Space Complexity: O(log n) ⚡ Optimal Approach (Floyd’s Cycle Detection): Use two pointers — slow and fast. Move slow by one step and fast by two steps (using sum of squares). If they meet, there’s a cycle (not happy). If you reach 1, it’s a happy number. Time Complexity: O(log n) Space Complexity: O(1) ✨ What I Learned: How to detect cycles in number transformations. Applying Floyd’s cycle detection beyond linked lists. Improved logical problem-solving for number-based questions. #LeetCode #Java #ProblemSolving #DSA #CodingJourney #PlacementPrep
To view or add a comment, sign in
-
-
Day 6 – LeetCode Challenge Today, I solved Problem #128: “Longest Consecutive Sequence” using C++. 🔍 Problem Overview: The task is to find the length of the longest consecutive elements sequence in an unsorted integer array. The key challenge is to ensure the solution works in O(n) time complexity. 💡 Approach: To achieve linear time, I used an unordered_set to quickly check if a number exists. For each number, I only begin counting a sequence when it is the start of a new streak (i.e., (num - 1) does not exist in the set). This ensures each number is processed only once. 🧠 Algorithm Design: Insert all elements into an unordered_set for O(1) average lookups. For every number, check if it's the start of a sequence. If yes, count forward (num + 1, num + 2, ... ) while elements exist in the set. Track and update the maximum streak length. ⏱ Time Complexity: O(n) 📌 Space Complexity: O(n) #LeetCode #CPlusPlus #DSA #100DaysOfCode #ProblemSolving #Algorithms #CodingChallenge #TechCommunity #GeetaUniversity
To view or add a comment, sign in
-
-
LeetCode 217: Contains duplicate ⁇ Suppose you have a list of numbers, and you want to know if any number appears more than once. It’s like making sure everyone in class has a unique roll number! Approach 1: Set - Go through each number. - If it’s not in your set, add it. - If you ever see a number already in your set, you’ve got a duplicate! Approach 2: Hash Table (Counter) - Use a Counter to count how often each number appears in the list. - If any number appears more than once, return True (there’s a duplicate). Complexity: - Time Complexity: O(n) — One pass through all numbers. - Space Complexity: O(n) — Storing seen numbers or counts. Both approaches are fast and effective for catching duplicates quickly! Let’s check our numbers and make sure everyone’s unique! Check out the problem here: https://lnkd.in/g-JgRTEn Keep going, keep revising, and keep building confidence! 💪🔥 #DSA #Coding #ProblemSolving #Learning
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