🚀 Day 16 of solving DSA problems 🧠 Problem: Majority Element Today I learned a powerful algorithm called 👉 Boyer–Moore Voting Algorithm Key Insight: Instead of counting frequency using HashMap, we can cancel out different elements. Since majority element appears > n/2 times, it can never be fully cancelled. ⏱ Complexity: Time → O(n) Space → O(1) 💡 Lesson: Always check problem constraints — if majority is guaranteed, we can use optimized logic instead of extra memory. Consistency is the real key to mastering DSA. 🔥 #DSA #Java #CodingJourney #ProblemSolving #LearningInPublic
Boyer–Moore Voting Algorithm for Majority Element
More Relevant Posts
-
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 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 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
-
-
🚀 Day 60 of DSA consistency Today I practiced a Binary Search Tree (BST) problem: Search in a Binary Search Tree. In this problem, we need to find a node with a given value in a BST and return the subtree rooted at that node. 🔹 Key Idea: A BST has the property: Left subtree values < root Right subtree values > root Using this property, we can efficiently search by moving left or right instead of traversing the entire tree. 📊 Complexity Analysis Time Complexity: O(h) → where h is the height of the BST Space Complexity: O(h) due to recursion stack If the tree is balanced, the complexity becomes O(log n). #DSA #Java #BinarySearchTree #CodingJourney #Consistency #LeetCode #100DaysOfCode
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 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 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
To view or add a comment, sign in
-
-
🔁 DSA Practice: Reverse String (LeetCode Problem) Today I practiced the Reverse String problem, a fundamental question that helps build a strong understanding of arrays and two-pointer techniques. In this problem, we are given a character array and the goal is to reverse the string in-place, meaning we must modify the original array without using extra memory. 💡 Key Concept: Two-Pointer Technique One pointer starts from the beginning of the array. Another pointer starts from the end of the array. We swap the characters and move the pointers toward the center until they meet. 📌 Why this problem is important Improves understanding of array manipulation Introduces the two-pointer approach Helps practice in-place algorithms with O(1) space complexity ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) Consistent practice with such problems strengthens problem-solving skills and builds a solid foundation for more advanced DSA challenges. #DSA #LeetCode #Java #ProblemSolving #CodingPractice #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 11 of Daily DSA 🚀 Solved LeetCode 1800: Maximum Ascending Subarray Sum ✅ 🔍 Approach: Iterated through the array while maintaining the sum of the current strictly increasing subarray. Whenever the sequence breaks, a new subarray sum is started and stored. Finally, the maximum subarray sum is obtained from the stored values. This method clearly separates each ascending subarray and helps in understanding the problem flow. ⏱ Complexity: • Time: O(n) — single traversal + max lookup • Space: O(n) — list used to store subarray sums 📊 LeetCode Stats: • Runtime: 1 ms ⚡ • Memory: 43.30 MB A good learning step before optimizing further to constant space. #DSA #LeetCode #Java #Arrays #ProblemSolving #DailyCoding #Consistency
To view or add a comment, sign in
-
-
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
-
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