LeetCode 268 – Missing Number Today I solved “Missing Number”, a simple but powerful DSA problem. Problem summary: Given an array containing n distinct numbers in the range [0, n], return the one number that is missing from the array. What I learned from this problem: • We can solve it using sorting (but that’s not optimal) • A better approach is using the Sum Formula Expected sum = n × (n + 1) / 2 Missing number = Expected sum − Actual sum • Time Complexity: O(n) • Space Complexity: O(1) Another interesting approach is using XOR, which avoids overflow and also runs in O(n) time. This problem reminded me that sometimes the best solution is not complicated — it’s about recognizing mathematical patterns. Small problems like this build strong problem-solving intuition. #DSA #LeetCode #ProblemSolving #Java #CodingJourney #LearningInPublic
Missing Number Problem Solution in Java
More Relevant Posts
-
Day 9 of DSA Practice | LeetCode 268 – Missing Number Solved Missing Number, focusing on using the Mathematical Formula approach to find the missing value efficiently. Concept Used: Sum of first n natural numbers Approach: Compare expected sum n(n+1)/2 with the actual array sum Time Complexity: O(n) Space Complexity: O(1) This problem helped reinforce: • Array traversal • Mathematical optimization • Writing efficient solutions Learning and improving one problem at a time #DSA #LeetCode #Java #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 16 of Daily DSA 🚀 Solved LeetCode 75: Sort Colors ✅ Approach: Used the Dutch National Flag algorithm with three pointers. start → tracks position for 0s mid → current element end → tracks position for 2s By swapping elements in-place, the array is sorted in one pass without using any built-in sort. ⏱ Complexity: • Time: O(n) — single traversal • Space: O(1) — in-place sorting 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.63 MB (Beats 42.09%) A classic problem that perfectly demonstrates pointer-based thinking. #DSA #LeetCode #Java #BinarySearch #TwoPointers #ProblemSolving #DailyCoding #Consistency
To view or add a comment, sign in
-
-
Day 29 of Daily DSA 🚀 Solved LeetCode 287: Find the Duplicate Number ✅ Problem: Given an array containing n + 1 integers where each number is in the range [1, n], find the duplicate number. Approach: Used the index marking technique. Key Idea: Treat the value as an index Convert the value to absolute (Math.abs) Mark the visited index by making the number negative If we encounter an index that is already negative, that value is the duplicate This allows us to detect duplicates efficiently without extra space. ⏱ Complexity: • Time: O(n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 4 ms (Beats 91.67%) ⚡ • Memory: 82.75 MB A clever trick that uses the array itself as a visited map. #DSA #LeetCode #Java #ProblemSolving #Algorithms #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 85/100 – LeetCode Challenge ✅ Problem: #226 Invert Binary Tree Difficulty: Easy Language: Java Approach: Recursive DFS (Divide and Conquer) Time Complexity: O(n) Space Complexity: O(h) where h = tree height Key Insight: Swap left and right child at every node, then recursively invert both subtrees. Classic recursion problem — made famous by Google interview story. Solution Brief: Base case: if root is null, return null. Swap left and right children using temporary variable. Recursively call invertTree on left and right subtrees. Return the root (now inverted). #LeetCode #Day85 #100DaysOfCode #Tree #Java #Algorithm #CodingChallenge #ProblemSolving #InvertBinaryTree #EasyProblem #DFS #Recursion #DSA
To view or add a comment, sign in
-
-
Day 17 of DSA practice 🚀 (Deep Dive into Binary Search) Today I solved Problem #4 – Median of Two Sorted Arrays — and it was a really insightful one. Instead of merging the two sorted arrays (which would take O(n + m) time), I learned a smarter approach to find the median without actually creating the combined array. Key takeaways: 1) Using binary search on partitions instead of values 2) Dividing the arrays in a way that left partition ≤ right partition 3) Achieving O(log(min(n, m))) time complexity Beyond the algorithm, I also learned something important about code design: The importance of separation of concerns. Breaking the logic into clear responsibilities made the solution more readable, easier to debug, and conceptually simpler — even for a complex problem. Today wasn’t just about solving a hard problem, it was about improving how I think and structure solutions 🔥 #DSA #LeetCode #BinarySearch #Algorithms #ProblemSolving #CleanCode #Java #LearningInPublic #100DaysOfCode
To view or add a comment, sign in
-
Day 33 of DSA 🚀 | Binary Search Today I worked on one of the most fundamental algorithms in problem solving — Binary Search. 🔹 Task: Given a sorted array, find the index of a target element in O(log n) time. 🔹 Key realization: Binary Search is not about guessing. It’s about eliminating half of the search space at every step. 💡 What I learned: Binary Search works only on sorted arrays Using left, right, and mid pointers helps narrow the search efficiently Calculating mid as left + (right - left) / 2 avoids overflow Clear boundary conditions (left <= right) are critical ⏱ Time Complexity: O(log n) 📦 Space Complexity: O(1) This problem reinforced how mastering basics builds the foundation for advanced algorithms. #DSA #BinarySearch #Java #ProblemSolving #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
Day 15 of Daily DSA 🚀 Solved LeetCode 540: Single Element in a Sorted Array ✅ Approach: Used Binary Search on indices instead of values. Since elements appear in pairs, the unique element breaks the pairing pattern. By forcing mid to be even and comparing nums[mid] with nums[mid + 1], we can safely discard half of the array each time. ⏱ Complexity: • Time: O(log n) — binary search • Space: O(1) — constant extra space 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 52.96 MB (Beats 57.60%) A neat example of how index properties make binary search even more powerful. #DSA #LeetCode #Java #BinarySearch #ProblemSolving #DailyCoding #Consistency
To view or add a comment, sign in
-
-
Day 53 of My DSA Journey Today I solved a problem on searching an element in a rotated sorted array with duplicates. The array is originally sorted but rotated at some pivot. The task is to determine whether a given target value exists in the array. 🔎 My Approach Instead of directly jumping to complex techniques, I used a simple linear search approach: Traverse through the array from start to end. Compare each element with the target value. If a match is found, return true. If the loop finishes without finding the target, return false. 💡 Key Takeaway Sometimes starting with a simple and correct solution is important before optimizing. While more efficient approaches like modified binary search can reduce time complexity, this straightforward method helps clearly understand the problem first. ⏱ Time Complexity: O(n) Every day I’m improving my problem-solving skills in Java and Data Structures & Algorithms step by step. #Day53 #DSA #Java #ProblemSolving #CodingJourney #LeetCode #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 62 of DSA consistency Today I solved Lowest Common Ancestor in a Binary Search Tree. 🔹 Key Idea: In a Binary Search Tree (BST): Left subtree → values smaller than root Right subtree → values greater than root Using this property: If both nodes are greater than root, move right. If both nodes are smaller than root, move left. Otherwise, the current node is the Lowest Common Ancestor. 💡 This allows us to solve the problem efficiently without storing paths. 🧠 Time Complexity: O(H) (H = height of the tree) 💾 Space Complexity: O(H) due to recursion stack. #DSA #Java #BinarySearchTree #LeetCode #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Day 82/100 – LeetCode Challenge ✅ Problem: #43 Multiply Strings Difficulty: Medium Language: Java Approach: Manual Multiplication with Result Array Time Complexity: O(n × m) Space Complexity: O(n + m) Key Insight: Multiply digits from right to left (least significant first). Store intermediate results in array where index i + j + 1 holds current digit. Handle carry by adding to previous index. Solution Brief: Edge case: if either number is "0", return "0". Created result array of size n1 + n2 (max possible digits). Nested loops multiply each digit of num1 with each digit of num2. Accumulated results with proper carry handling. Built final string skipping leading zeros. #LeetCode #Day82 #100DaysOfCode #Math #String #Java #Algorithm #CodingChallenge #ProblemSolving #MultiplyStrings #MediumProblem #Multiplication #Array #DSA
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