Day 12 of Daily DSA 🚀 Solved LeetCode 704: Binary Search ✅ Approach: Used the classic binary search technique on a sorted array. Initialized start and end pointers and repeatedly narrowed the search space by comparing the target with the middle element. Calculated mid using start + (end - start) / 2 to avoid overflow and ensure correctness. This approach efficiently reduces the search range and guarantees optimal performance. ⏱ Complexity: • Time: O(log n) — search space halves every iteration • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 48.54 MB (Beats 29.13%) A solid example of how understanding fundamentals leads to maximum efficiency. #DSA #LeetCode #Java #BinarySearch #ProblemSolving #DailyCoding #Consistency
Binary Search Solution on LeetCode 704
More Relevant Posts
-
Day 14 of Daily DSA 🚀 Solved LeetCode 153: Find Minimum in Rotated Sorted Array ✅ Approach: Used Binary Search to locate the minimum element in a rotated sorted array. At each step, compared nums[mid] with nums[end] to determine which half is unsorted and contains the minimum. By shrinking the search space intelligently, the minimum element is found efficiently. A great example of how Binary Search adapts to rotated arrays. ⏱ Complexity: • Time: O(log n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.66 MB (Beats 87.66%) Binary Search mastery keeps leveling up 💪 #DSA #LeetCode #Java #BinarySearch #ProblemSolving #DailyCoding #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 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 22 of Daily DSA 🚀 Solved LeetCode 66: Plus One ✅ Approach: Treat the array as a number and simulate addition from the last digit: • If the last digit is not 9, increment and return • Handle carry by setting 9 → 0 and moving left • If all digits are 9, create a new array with leading 1 This avoids converting the array into an integer and handles large numbers safely. ⏱ Complexity: • Time: O(n) • Space: O(1) (extra array only when overflow happens) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.58 MB (Beats 38.49%) A simple problem that tests edge-case thinking 💡 #DSA #LeetCode #Java #ProblemSolving #DailyCoding #Consistency
To view or add a comment, sign in
-
-
Day 14 of Daily DSA 🚀 Solved LeetCode 35: Search Insert Position ✅ Approach: Applied classic Binary Search on the sorted array. If the target exists, return its index. If not, the final value of start naturally represents the correct insert position while maintaining sorted order. This problem nicely reinforces how Binary Search can be adapted beyond just “find or not find”. ⏱ Complexity: • Time: O(log n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 44.57 MB (Beats 92.62%) Simple logic, powerful efficiency 💪 #DSA #LeetCode #Java #BinarySearch #ProblemSolving #DailyCoding #Consistency
To view or add a comment, sign in
-
-
Day 13 of Daily DSA 🚀 Solved LeetCode 34: Find First and Last Position of Element in Sorted Array Approach: Applied Binary Search twice to efficiently locate the range of the target element. • First pass finds the leftmost (first) occurrence • Second pass finds the rightmost (last) occurrence A boolean flag helps reuse the same logic for both searches, keeping the code clean and optimal. This ensures the required logarithmic performance on a sorted array. ⏱ Complexity: • Time: O(log n) — two binary searches • Space: O(1) — constant extra space 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 48.50 MB (Beats 11.84%) A perfect example of how modifying binary search slightly can solve range-based problems efficiently. #DSA #LeetCode #Java #BinarySearch #ProblemSolving #DailyCoding #Consistency
To view or add a comment, sign in
-
-
Day 15 of Daily DSA 🚀 Solved LeetCode 162: Find Peak Element ✅ Approach: Used Binary Search to efficiently find a peak element. At each step, compared the middle element with its neighbors to decide which side must contain a peak. This works because at least one peak always exists in the array. ⏱ Complexity: • Time: O(log n) — binary search on array • Space: O(1) — no extra space used 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 44.20 MB (Beats 75.11%) A great example of how binary search can be applied beyond just sorted arrays. #DSA #LeetCode #Java #BinarySearch #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 26 of Practicing DSA on LeetCode Solved: • (1351) Count Negative Numbers in a Sorted Matrix – Easy Focused on: -Leveraging row-wise sorted property -Breaking early once a negative number is found -Reducing unnecessary iterations -Thinking about how this could be optimized further using binary search This problem reinforced that understanding data structure properties reduces time complexity naturally. Use the structure. Trust the pattern. Consistency over everything. #DSA #Java #LeetCode #BinarySearch #Matrix #ProblemSolving #Consistency #LearningInPublic
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