🚀 Day 60 of #100DaysOfCode 🚀 Today, I solved LeetCode Problem #137 – Single Number II 🧩 📘 Problem Statement: Given an integer array where every element appears three times except for one that appears exactly once, find that single element and return it. The challenge: solve it with O(n) time and O(1) space complexity. 💻 Language: Java ⚡ Runtime: 0 ms — Beats 100.00% 📉 Memory: 45.54 MB — Beats 56.30% 🧠 Concept Used: 🔹 Bit Manipulation — A powerful yet tricky technique that leverages binary operations to track occurrences of bits efficiently. 🔹 Instead of using extra space or hash maps, we track bits that appear once and twice using two integer variables: ✅ once → bits that appeared once ✅ twice → bits that appeared twice The trick lies in updating them using XOR (^) and NOT (~) to "cancel out" bits appearing three times. 🧩 Approach Summary: 1️⃣ Initialize two variables once = 0 and twice = 0. 2️⃣ For every number in the array: - Update once and twice based on how many times a bit has appeared. 3️⃣ After processing all numbers, once holds the value of the single element. ✅ Complexity: Time: O(n) Space: O(1) ✨ Takeaway: This problem teaches how bitwise logic can replace traditional data structures, achieving the same result with constant space — a crucial optimization in system-level programming and interviews. #100DaysOfCode #LeetCode #Java #BitManipulation #ProblemSolving #CleanCode #DataStructures #Algorithms #TechLearning #CodingChallenge #SoftwareEngineering #InterviewPreparation
Solved LeetCode Problem #137 with Java and Bit Manipulation
More Relevant Posts
-
💻 Day 4 of #100DaysOfCode Challenge Topic: Binary Tree Traversals 🌳 Problems Solved: 🔹 94. Binary Tree Inorder Traversal 🔹 144. Binary Tree Preorder Traversal 🔹 145. Binary Tree Postorder Traversal Concept Recap: Today, I explored the three fundamental depth-first traversal techniques used in binary trees: ✅ Inorder (Left → Root → Right) – Produces a sorted order for BSTs. ✅ Preorder (Root → Left → Right) – Useful for creating a copy of the tree or serialization. ✅ Postorder (Left → Right → Root) – Ideal for deleting trees or evaluating expressions. Each traversal follows a recursive approach to explore nodes systematically. Implementing them helped me strengthen my understanding of recursion and how stack frames manage function calls behind the scenes. Key Learnings: 🧠 Understood how traversal order impacts output sequence. ⚙️ Practiced recursive depth-first traversal logic in Java. 🌱 Improved code readability by modularizing recursive functions. #LeetCode #DataStructures #BinaryTree #Recursion #Java #CodingChallenge #100DaysChallenge #Day4
To view or add a comment, sign in
-
-
✅ Day 68 of LeetCode Medium/Hard Edition Today’s challenge was “Number of Ways to Form a Target String Given a Dictionary” — a brilliant Dynamic Programming and Combinatorics problem that tests precision in transitions and precomputation logic 🧩⚙️ 📦 Problem: You’re given an array of equal-length strings words and a target string target. You must form target from left to right by picking characters from the columns of words under these rules: 1️⃣ Once you use column k from any word, all columns ≤ k in every word become unusable. 2️⃣ You can use multiple characters from the same word, respecting column progression. Return the number of ways to form target modulo 1e9 + 7. 🔗 Problem Link: https://lnkd.in/gns9CwWa ✅ My Submission: https://lnkd.in/g7bsgZq9 💡 Thought Process: This problem is a clever mix of frequency compression and DP memoization. We precompute a frequency table freq[26][m], where each cell represents how many words contain a given letter at column m. Then, using recursion with memoization: 🎯 At each step (i, j) Either use freq[target[i]][j] if it exists → multiply by ways for the next position (i+1, j+1) Or skip the current column → move to (i, j+1) The recurrence relation: dp[i][j] = freq[target[i]][j] * dp[i+1][j+1] + dp[i][j+1] All computations are done modulo 1e9 + 7. ⚙️ Complexity: ⏱ Time: O(26 * n + t * m) — efficient due to frequency precomputation 💾 Space: O(t * m) — for memoized DP table 💭 Takeaway: This challenge reinforced how precomputation and state optimization can transform a seemingly exponential recursion into an elegant polynomial-time solution 🚀 #LeetCodeMediumHardEdition #100DaysChallenge #DynamicProgramming #Combinatorics #ProblemSolving #Java #CodingChallenge #DSA
To view or add a comment, sign in
-
-
🚀 Day 52 of #100DaysOfCode 🚀 Today I solved LeetCode Problem #389 – Find the Difference 🧩 This problem is a great example of using ASCII value manipulation to solve what seems like a string comparison challenge. 💡 Key Learnings: Strengthened understanding of character encoding (ASCII values) and how they can simplify logic. Learned how summing character values can help detect differences efficiently without extra data structures. Reinforced the value of O(n) solutions for string-based problems. 💻 Language: Java ⚡ Runtime: 1 ms — Beats 99.32% 🚀 📉 Memory: 41.9 MB — Beats 65.84% 🧠 Approach: 1️⃣ Convert both strings to character arrays. 2️⃣ Compute the sum of ASCII values of both. 3️⃣ The difference between sums gives the ASCII value of the extra character in t. Simple, elegant, and efficient! ⚙️ Sometimes, a clever use of ASCII arithmetic can replace complex logic — efficiency lies in simplicity. #100DaysOfCode #LeetCode #Java #ProblemSolving #CodingChallenge #Algorithms #DataStructures #SoftwareDevelopment #LearningEveryday #CleanCode
To view or add a comment, sign in
-
-
🚀 Day 146 / 180 of #180DaysOfCode ✅ Today’s Highlight: Revisited LeetCode 3234 — “Count the Number of Substrings With Dominant Ones.” 🧩 Problem Summary: Given a binary string s, the task is to count how many of its substrings have dominant ones. A substring is considered dominant if: number of 1s ≥ (number of 0s)² This creates an interesting balance check between zeros and ones, pushing you to think about substring ranges, prefix behavior, and efficient counting techniques. 💡 Core Takeaways: Great exercise in string analysis and prefix-based reasoning Highlights how mathematical conditions influence algorithm design Reinforces handling frequency relationships inside a sliding or expanding window Efficient counting becomes essential due to potential O(n²) substrings 💻 Tech Stack: Java ⏱ Runtime: 115 ms (Beats 84.11%) 💾 Memory: 47 MB 🧠 Learnings: Strengthened understanding of substring behavior under unique constraints Refined thinking around optimizing brute-force patterns A good reminder of how theoretical conditions (like squaring zeros) change practical implementation choices 📈 Progress: Today’s problem significantly improved my intuition around constraint-based substring evaluation—valuable for more advanced algorithmic challenges ahead. #LeetCode #Java #Algorithms #SubstringProblems #DSA #ProblemSolving #CodingJourney #SoftwareEngineering #180DaysOfCode #LogicBuilding #LearningEveryday
To view or add a comment, sign in
-
-
✅Day 42 : Leetcode 154 - Find Minimum in Rotated Sorted Array-2 #60DayOfLeetcodeChallenge 🧩 Problem Statement You are given an array nums that is sorted in ascending order and then rotated between 1 and n times. The array may contain duplicates. Your task is to find and return the minimum element in the rotated sorted array. You must minimize the number of overall operations as much as possible. 💡 My Approach I used a modified binary search technique to handle both rotation and duplicates. Initialize two pointers — low = 0 and high = n - 1. Calculate the middle index mid = low + (high - low) / 2. Update the answer as ans = min(ans, nums[mid]). If nums[low] == nums[mid] && nums[mid] == nums[high], move both low++ and high-- to skip duplicates. If the left half is sorted (nums[low] <= nums[mid]), update the answer and move to the right half (low = mid + 1). Otherwise, move to the left half (high = mid - 1). Continue until low > high. This efficiently finds the minimum even when duplicates exist. ⏱️ Time Complexity Worst Case: O(n) — when many duplicates exist. Average Case: O(log n) — behaves like binary search when duplicates are few. #BinarySearch #LeetCode #RotatedArray #Java #DSA #ProblemSolving #CodingPractice #LeetCodeHard
To view or add a comment, sign in
-
-
✅ Day 73 of LeetCode Medium/Hard Edition Today’s challenge was “K Inverse Pairs Array” — an elegant Dynamic Programming problem that combines combinatorics, recurrence optimization, and modular arithmetic ⚡💡 📦 Problem: Given two integers n and k, return the number of different arrays consisting of numbers from 1 to n such that there are exactly k inverse pairs. An inverse pair is [i, j] where 0 ≤ i < j < n and nums[i] > nums[j]. 🔗 Problem Link: https://lnkd.in/eqt3zX_D ✅ My Submission: https://lnkd.in/eX2ZyCCu 💡 Thought Process: This problem generalizes the concept of counting inversions across all permutations of 1..n. The key idea is to express dp[n][k] in terms of previously computed states using prefix-sum logic to reduce time complexity. We use the recurrence: dp[n][k] = dp[n][k - 1] + dp[n - 1][k] - (k >= n ? dp[n - 1][k - n] : 0) This formula cleverly builds on smaller subproblems by considering where the nth element can be inserted in permutations of size n-1. 🎯 Base Cases: dp[n][0] = 1 (sorted array, no inverse pairs) dp[1][k] = 0 for all k > 0 ⚙️ Complexity: ⏱ Time: O(n × k) 💾 Space: O(n × k) 🧩 Key Learnings: Deepened understanding of prefix-sum optimization in DP. Reinforced modular arithmetic handling (MOD = 10⁹ + 7). Learned how combinatorial recurrence can reduce nested loops. Strengthened recursive + memoized DP formulation in Java. #LeetCodeMediumHardEdition #100DaysChallenge #DynamicProgramming #Recursion #Combinatorics #Java #DSA #ProblemSolving #KeepCoding #LearnEveryday
To view or add a comment, sign in
-
-
Day 50 / 100— DSA From Scratch (Medium) Problem Solved: Construct Binary Tree from Preorder and Inorder Traversal Task: Rebuild a binary tree using the given preorder and inorder traversal arrays. Approach Used: Used a recursive divide-and-conquer approach. The first element in the preorder array represents the root. Found the root’s position (mid) in the inorder array — everything to its left forms the left subtree, and everything to its right forms the right subtree. Used Arrays.copyOfRange() to split both arrays for recursive calls. Initially, I was stuck thinking about the algorithm — how to connect preorder and inorder logically — but once I saw how the recursion works, it became very clear. Also, I learned a new method — the copyOfRange() function in Java, which helped simplify array slicing during recursion. Complexity: Time Complexity: O(n²) — due to searching for the root index each time. Space Complexity: O(n²) — because of repeated array slicing. Reflection: This problem taught me more than just recursion — it showed how traversal patterns can reconstruct an entire tree when understood properly. Every problem like this builds not just coding skills but also clarity in algorithmic thinking. #Day50 #DSAFromScratch #BinaryTree #Preorder #Inorder #Recursion #Java #ProblemSolving #LeetCode #LearningEveryday #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 58 of #100DaysOfCode 🚀 Today, I solved LeetCode Problem #852 – Peak Index in a Mountain Array 🏔️ 📘 Problem Statement: Given an integer array that first increases and then decreases (a “mountain array”), find the index of the peak element — the point where the sequence changes from increasing to decreasing. 💻 Language: Java ⚡ Runtime: 0 ms — Beats 100.00% ⚡ 📉 Memory: 56.29 MB — Beats 60.76% 🧠 Concept Used: 🔹 Binary Search Optimization – Instead of linearly scanning the array, I applied binary search to achieve O(log n) time complexity. 🔹 This approach identifies the peak by checking midpoints and adjusting the search range efficiently. 🧩 Approach: 1️⃣ Initialize low = 0 and high = arr.length - 1. 2️⃣ Use binary search: ➡️ If arr[mid] < arr[mid + 1], the peak lies to the right → move low = mid + 1. ➡️ Else, the peak is at mid or to the left → move high = mid. 3️⃣ Return low when low == high — the index of the peak. ✅ Complexity: Time: O(log n) Space: O(1) ✨ Takeaway: Binary Search isn’t just for finding numbers — it’s a pattern for optimizing any “search in sorted behavior” scenario. Learning to spot monotonic trends in problems can turn O(n) solutions into O(log n) ones! #100DaysOfCode #LeetCode #Java #ProblemSolving #Algorithms #BinarySearch #DataStructures #CodingChallenge #CleanCode #SoftwareEngineering #TechLearning
To view or add a comment, sign in
-
-
Day 42/100 – #100DaysOfCode 🚀 | #Java #LeetCode #Math #Combinatorics ✅ Problem Solved: Permutation Sequence 🔢 🧩 Problem Summary: Given n and k, return the k-th permutation of numbers 1 to n in lexicographical order — without generating all permutations. 💡 Approach Used: To avoid brute-force permutation generation, I used a mathematical approach: Precomputed factorial values to understand how many permutations start with each digit. Determined the correct digit for each position by dividing k by factorial groups. Updated k and removed used digits as we built the sequence. This turns the problem into a factorial number system / combinatorics lookup, making it efficient. ⚙️ Time Complexity: O(n²) — due to list removals 📦 Space Complexity: O(n) ✨ Takeaway: Sometimes, the key to efficiency is switching from brute force to mathematical reasoning. This problem is a great example of turning permutations into indexed computation 🎯 #Java #LeetCode #Math #Combinatorics #ProblemSolving #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
🌿 Day 74 of #100DaysOfCode 🌿 💡 Problem: Binary Tree Preorder Traversal – LeetCode 🚀 Approach: Used a recursive traversal to explore nodes in the order Root → Left → Right. Preorder traversal is all about visiting the leader first — just like taking the initiative before exploring possibilities! 💫 📊 Complexity Analysis: Time Complexity: O(n)** — every node is visited once Space Complexity: O(n)** — due to recursion stack ✅ Runtime: 0 ms (⚡ Beats 100%) ✅ Memory: 43.06 MB 🔑 Key Insight: Recursion helps untangle even the deepest branches — one root call at a time 🌱 #LeetCode #100DaysOfCode #BinaryTree #Recursion #CodingJourney #DSA #ProblemSolving #Java #Algorithms #ProgrammerLife #WomenInTech #CodeEveryday
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