🚀 Day 2 of #100DaysOfCode — Majority Element (LeetCode 169) Today I solved the Majority Element problem using the Boyer-Moore Voting Algorithm — a brilliant technique that finds the majority element in O(n) time and O(1) space. 💡 Key Takeaways: • Learned how cancelling non-majority elements still preserves the majority • Understood why Boyer-Moore is more optimal than sorting or HashMap • Practiced writing clean and efficient Java code Problem: Find the element that appears more than ⌊n/2⌋ times in an array. Every day, one step closer to mastering Data Structures & Algorithms and becoming a better Software Engineer. 🔥 Consistency > Perfection. #100DaysOfCode #LeetCode #Java #DSA #CodingJourney #ProblemSolving #SoftwareEngineering #LearningInPublic #TechJourney
Majority Element Solution with Boyer-Moore Voting Algorithm
More Relevant Posts
-
🚀 Day 82 – DSA Journey | Same Tree using Recursion Continuing my daily DSA practice, today I worked on a problem that strengthened my understanding of tree comparison and recursion. 📌 Problem Practiced: Same Tree (LeetCode 100) 🔍 Problem Idea: Given two binary trees, determine whether they are identical in both structure and node values. 💡 Key Insight: To check if two trees are the same, we need to compare nodes at every level — both their values and their left & right subtrees. Recursion makes this process clean and intuitive. 📌 Approach Used: • If both nodes are null → trees match at this point • If one is null or values differ → trees are not the same • Recursively compare left subtrees • Recursively compare right subtrees • Both sides must match for the trees to be identical 📌 Concepts Strengthened: • Tree traversal • Recursion • Structural comparison • Base case handling ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) (recursion stack) 🔥 Today’s takeaway: Tree problems often become simple when broken down recursively — solve smaller subtrees to solve the whole tree. On to Day 83! 🚀 #Day82 #DSAJourney #LeetCode #BinaryTree #Recursion #Java #ProblemSolving #Coding #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
Day 2 of my LeetCode Journey Today’s focus: Prefix Sum + HashMap - Problem Type: Counting “Good Subarrays” - Approach: Converted the problem into a prefix sum transformation Used a HashMap to track frequencies Optimized brute force (O(n²)) → O(n) Key Learning: The real trick isn’t coding — it’s recognizing how to transform the problem. Instead of checking every subarray, I tracked a derived value: prefix - (index + 1) and counted how many times it appeared before. That shift turns an impossible brute force into a clean linear solution. -Takeaway: Most DSA problems are not about new algorithms — they’re about seeing the hidden pattern faster. Building consistency, one day at a time. #LeetCode #Day2 #DSA #Java #ProblemSolving #Consistency #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 3/100 – LeetCode Challenge 🔍 Problem: Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be inserted to maintain the sorted order. 💡 Key Concepts Used: • Binary Search • Time Complexity Optimization – O(log n) • Efficient searching in sorted arrays 📚 What I Learned: • How binary search reduces search time significantly compared to linear search • Handling edge cases when the target element is not present • Determining the correct insertion index while maintaining sorted order hashtag #100DaysOfCode #LeetCode #Java #DataStructures #Algorithms #BinarySearch #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
#Day77 of my second #100DaysOfCode Finally wrapped up binary search on 1D arrays, now moving to binary search on answers. DSA • Solved Sqrt(x) (LeetCode 69) — finding the integer square root of a number without using built-in functions – Brute: iterate until square exceeds target → O(n) – Optimal: binary search on answer space → O(log n) • Key idea: instead of searching an index, we search for the answer itself • Learned how to narrow down the range based on mid² comparison Nice shift in thinking — binary search feels much more powerful now. #DSA #BinarySearch #LeetCode #Algorithms #Java #100DaysOfCode #WomenWhoCode #BuildInPublic #LearningInPublic
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟕𝟕 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding all elements that appear more than n/3 times in an array. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Majority Element II 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 – 𝐇𝐚𝐬𝐡𝐌𝐚𝐩 • Counted frequency of each element using a map • Calculated threshold = n / 3 • Collected elements whose frequency exceeded the threshold 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • At most 2 elements can appear more than n/3 times • HashMap is straightforward for frequency counting • Understanding constraints helps reduce possibilities • This problem has an optimized Boyer-Moore Voting (extended) solution 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(n) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Constraints often reveal hidden patterns — understanding them leads to better optimizations. 77 days consistent 🚀 On to Day 78. 🔗 Problem Link: https://lnkd.in/dDwdWYJs #DSA #Arrays #HashMap #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
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
-
-
🚀 #100DaysOfCode | Day 47 🔍 Solved: Find Minimum in Rotated Sorted Array Today I explored another interesting variation of Binary Search. Instead of searching for a target, the goal was to find the minimum element in a rotated sorted array. 💡 Key Insight: By comparing the middle element with the last element, we can determine which half contains the minimum value. Approach: ✔ Used Binary Search to achieve O(log n) time complexity ✔ Compared mid with end to identify the unsorted portion ✔ Narrowed down the search space efficiently What I Learned: This problem helped me understand how binary search can be applied beyond simple searching—especially in rotated and partially sorted arrays. #Java #DSA #LeetCode #BinarySearch #CodingJourney #ProblemSolving #TechSkills
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 31 Today’s focus: Binary Search for boundaries and square roots. Problems solved: • Sqrt(x) (LeetCode 69) • Search Insert Position (LeetCode 35) Concepts used: • Binary Search • Search space reduction • Boundary conditions Key takeaway: In Sqrt(x), the goal is to find the integer square root. Using binary search, we search in the range [1, x] and check mid * mid against x to narrow down the answer. This avoids linear iteration and achieves O(log n) time. In Search Insert Position, we use binary search to find either: • The exact position of the target, or • The correct index where it should be inserted The key idea is that when the target is not found, the final position of the left pointer gives the correct insertion index. These problems highlight how binary search is not just for finding elements, but also for determining positions and boundaries efficiently. Continuing to strengthen fundamentals and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 39 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Concatenation of Array Problem Insight: Given an integer array nums, the goal is to create a new array by concatenating the array with itself. Approach: • Created a new array of size 2 * nums.length • Used a single loop to iterate through the array • Stored elements at two positions: - result[i] = nums[i] - result[i + nums.length] = nums[i] • This avoids using extra loops and keeps the solution efficient Time Complexity: • O(n) — only one traversal required Space Complexity: • O(n) — new array is created Key Learnings: • Efficient index handling can simplify problems • Avoid unnecessary loops for better performance • Strong fundamentals make simple problems powerful Takeaway: Smart thinking beats brute force — even simple problems can be solved in an optimal and elegant way . #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #Arrays
To view or add a comment, sign in
-
-
Day 90 of #100DaysOfCode – Unique Binary Search Trees II Today’s problem was all about generating all structurally unique BSTs for values from 1 to n. At first glance, it feels tricky because it's not just counting trees — we actually need to build every possible tree structure. 💡 Key Insight: Pick each number i as the root Recursively build: Left subtree from [1 ... i-1] Right subtree from [i+1 ... n] Combine every left & right subtree pair 🌳 This is a classic Recursion + Backtracking problem and is closely related to Catalan Numbers. 📌 Example: For n = 3, we get 5 unique BSTs ⚡ What I learned today: How recursion can generate combinations of structures Importance of base case (start > end → null) Combining subproblems effectively 💻 Time Complexity: Approximately O(4ⁿ / √n) Consistency is key — 90 days strong and still going 💪 Next target: 💯 #DSA #Java #LeetCode #Recursion #BinaryTree #CodingJourney #100DaysOfCode
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
Keep going