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
More Relevant Posts
-
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
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 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
-
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
-
Ever wondered how a tiny ++ can turn a perfectly good program into a mystery? I’ve been digging into some of the most notorious ways the increment operator can cause undefined behavior (UB). Below are seven classic (and definitely not‑to‑be‑copied) examples that illustrate why you should always respect sequence points and operator precedence. 1. i+++++i – Multiple increments without an intervening sequence point. The compiler can evaluate the left‑hand and right‑hand sides in any order, leading to unpredictable results. 2. i = i++ – The assignment and the post‑increment are unsequenced; the final value of i is undefined. 3. i = ++i + i – Mixing pre‑increment with other reads of the same variable creates a race condition in the abstract machine. 4. i = i = i + 1 – Chained assignments with side effects are a recipe for UB. 5. *i = i**++* – The ** is not a valid operator; even if it were, the combination of dereference and increment without a sequence point would be illegal. 6. std::cout << i+++++i; – The stream insertion operator evaluates its operands in an unspecified order, so the increments may happen before or after the output. 7. i = ++i + ++i; – Two pre‑increments on the same variable in a single expression are unsequenced, giving different results on different compilers. > These snippets are pure examples of bad practice. Do NOT use them in production code. Why does UB matter?Undefined behavior isn’t just a theoretical concern—it can cause crashes, silent data corruption, or security vulnerabilities that are incredibly hard to track down. Modern compilers often exploit UB for optimization, so what looks “harmless” today may break spectacularly tomorrow. I’d love to hear from youHave you ever stumbled upon a tricky UB case in C/C++? How did you debug it, and what lesson did you take away? Share your stories in the comments—let’s help each other write safer, more predictable code! #Cpp #UndefinedBehavior #CodeQuality #SoftwareEngineering #LearningFromMistakes
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
-
-
🚀 Day 131 of #160DaysOfGFG – Boolean Parenthesization 📌 Problem: Given a boolean expression with symbols T (true), F (false), and operators &, |, and ^, count the number of ways to parenthesize it so that the expression evaluates to True. 🧠 Solution Approach: ✅ Use Dynamic Programming + Recursion + Memoization ✅ Split the expression at every operator and recursively compute: Number of ways left & right sub-expressions can be True or False ✅ Combine based on operator rules: For &, both must be true For |, at least one must be true For ^, exactly one must be true 🕒 Time Complexity: O(N³) 💾 Space Complexity: O(N²) 💡 Learning: This problem builds on Matrix Chain Multiplication (Day 130) — but instead of minimizing cost, we count valid combinations. It’s an elegant mix of divide & conquer with DP — a real DP classic! 🔖 #Day131 #160DaysOfGFG #DynamicProgramming #BooleanParenthesization #MatrixChainMultiplication #ProblemSolving #GeeksForGeeks #CodingChallenge #DSA #InterviewPrep #DailyDSA
To view or add a comment, sign in
-
-
🚀LeetCode Daily Challenge 🧩 Problem: Smallest Number With All Set Bits Problem Intuition: Given an integer n, your task is to find the smallest number that has the same number of bits as n when represented in binary, but with all bits set to 1. In simple terms — if n requires k bits to represent, you must find the smallest integer whose binary form has exactly k ones (111...1). 💡Example Insight: Let’s say n = 6 → Binary: 110 → requires 3 bits. The smallest number with all 3 bits set to 1 is 111 → which is 7 in decimal. Hence, the answer is 7. 🧠 Approach & Strategy: ✔ First, determine the number of bits needed to represent n: res = floor(log2(n)) + 1. ✔ Then, iterate from n to an upper limit and check for the first number having exactly res set bits. ✔ The check is performed using the built-in function __builtin_popcount(x) which counts the number of 1’s in the binary representation. ✔ Return the first number satisfying this condition. ⚙️ Complexity Analysis: Time Complexity: O(k) → Iterates until the matching number is found (within small bounds). Space Complexity: O(1) → Uses only integer variables. 🔗Problem: https://lnkd.in/dwR7Gp8i 💻 Solution: https://lnkd.in/dJU6Wrra #LeetCode #DSA #Cpp #ProblemSolving #CodingChallenge #LeetCodeDailyChallenge
To view or add a comment, sign in
-
Today, I learned two things: I cannot solve a LIS problem, and how to solve a LIS problem. For some peculiar reason, I didn't use this algorithm (though I thought I did). It's pretty straightforward when you read about it, but it did puzzle me a bit when I tried to understand it. I encountered it while solving a hard problem on LeetCode; I solved it using DFS, optimized DFS, and optimized BFS with an indegree map, but none of them finished on time. Only a modified LIS approach was mentioned as a valid solution (others were correct, but "too" slow). That's why it's important to learn basic algorithms. It's not that you'll be asked to solve the problems that directly ask about these specific solutions, but it might be mixed into a bigger problem, so it's annoying to get stuck in a small detail while understanding the overall approach. The post: https://lnkd.in/dtJW7F9A The problem: https://lnkd.in/dcwHHN9B #LeetCode #DSATips #DSA #ThinkDSA #ProblemSolving
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