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
LeetCode 287: Find Duplicate Number in Array
More Relevant Posts
-
Day 88/100 – LeetCode Challenge ✅ Problem: #112 Path Sum Difficulty: Easy Language: Java Approach: Recursive DFS with Decreasing Target Time Complexity: O(n) Space Complexity: O(h) where h = tree height Key Insight: Subtract current node value from target as we traverse. When reaching leaf, check if remaining target equals zero. Solution Brief: Base case: if root is null → no path. Leaf node check: if both children null, verify targetSum - root.val == 0. Recursive case: subtract current value and check left or right subtree. #LeetCode #Day88 #100DaysOfCode #Tree #DFS #Java #Algorithm #CodingChallenge #ProblemSolving #PathSum #EasyProblem #Recursion #BinaryTree #DSA
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 2 Today I worked on a DSA problem based on arrays: Check if an array is sorted and rotated 🔍 Approach: Instead of finding the exact rotation point, I focused on identifying a pattern: In a sorted and rotated array, the order should break at most once. So, I checked how many times an element is greater than the next element while traversing the array in a circular manner. ✔️ If the count of such breaks is 0 or 1 → valid ❌ If it’s more than 1 → not a sorted rotated array 🧠 Key Takeaway: This problem taught me how pattern observation can simplify logic and avoid unnecessary complexity. Sometimes the best solution is not the most obvious one! 📈 Staying consistent and improving step by step 💪 #100DaysOfCode #DSA #DataStructures #Algorithms #Java #CodingJourney #ProblemSolving #LeetCode #Consistency
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 100/100 – LeetCode Challenge ✅ Problem: #41 First Missing Positive Difficulty: Hard Language: Java Approach: In-Place Index Marking Time Complexity: O(n) Space Complexity: O(1) Key Insight: Place each positive integer in its correct position (index i should contain i+1). Mark presence using negative values without extra space. Solution Brief: First pass: replace numbers outside range [1, n] with n+1 (ignore them) Second pass: treat each number as index, mark that index negative Third pass: first positive index = missing number If all marked, answer is n+1 #LeetCode #Day100 #100DaysOfCode #Array #Java #Algorithm #CodingChallenge #ProblemSolving #FirstMissingPositive #HardProblem #InPlace #Optimization #DSA
To view or add a comment, sign in
-
-
LeetCode Challenge – Day 51 Today I solved the Diameter of Binary Tree problem. Problem Insight: The task is to find the length of the longest path between any two nodes in a binary tree. The path does not necessarily pass through the root. Approach: Used Depth First Search (DFS) to calculate the height of each subtree For every node: Calculated left subtree height Calculated right subtree height Updated the maximum diameter as the sum of left and right heights Returned the maximum diameter found during traversal Key Learning: Tree problems often combine multiple concepts. Here, calculating height and updating diameter together in a single traversal makes the solution efficient. Complexity: Time: O(n) Space: O(h) This problem helped me understand how recursion can be used to compute multiple values in a single traversal. #LeetCode #Java #DSA #CodingJourney
To view or add a comment, sign in
-
#Day76 of my second #100DaysOfCode Binary search continues to surprise me with how many variations it has. DSA • Solved Find Peak Element (LeetCode 162) – Brute: check every element → O(n) – Optimal: binary search based on slope comparison → O(log n) • Key idea: if the next element is greater, move right; else move left — a peak is guaranteed • Didn’t need to check all elements, just follow the increasing/decreasing trend Interesting how this doesn’t require a fully sorted array, yet binary search still works. #DSA #BinarySearch #LeetCode #Algorithms #Java #100DaysOfCode #WomenWhoCode #BuildInPublic #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 21 of My DSA Journey Solved LeetCode Problem #704 – Binary Search today! 🔍 This problem is a classic example of how powerful Binary Search can be when working with sorted arrays. Instead of checking each element one by one, we reduce the search space by half every time — making it highly efficient. 💡 Key Learnings: Importance of sorted data for Binary Search Optimizing time complexity from O(n) to O(log n) Handling edge cases carefully (like target not found) 🧠 Approach: Used two pointers (low and high) and calculated the middle index to compare with the target value. Based on comparison, reduced the search space accordingly. 💻 Time Complexity: O(log n) 📦 Space Complexity: O(1) Consistency is key 🔑 — one step closer to mastering DSA! #Day21 #LeetCode #DSA #BinarySearch #Java #CodingJourney #ProblemSolving #TechGrowth
To view or add a comment, sign in
-
-
Day 12 of #50DaysOfDSA Today I solved “Missing Number” on LeetCode. This problem is a great exercise in array manipulation and mathematical reasoning. Given an array containing n distinct numbers in the range [0, n], the task is to find the one number that is missing from the sequence. Instead of checking each number individually, we can use the mathematical sum formula for the first n numbers and compare it with the sum of elements in the array. The difference between these two values gives the missing number. Time Complexity: O(n) Space Complexity: O(1) Onwards and Upwards! #50DaysOfDSA #LeetCode #DSA #ProblemSolving #CodingJourney #Java #SoftwareEngineering #50DaysOfCode #WomenInTech #Algorithms
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
Great i also did by question using slow and fast pointers