✅ Solved: Majority Element — LeetCode | Boyer-Moore Voting Algorithm Just got accepted on all 53/53 test cases with 2ms runtime (beats 70.59% of Java submissions)! 🎯 Here's what I learned: 🧠 The Problem: Given an array, find the element that appears MORE than ⌊n/2⌋ times. It's guaranteed to always exist. 💡 The Approach — Boyer-Moore Voting Algorithm: Instead of using a HashMap (O(n) space), I used a clever O(1) space trick: Treat elements like "votes" — keep a candidate and a counter When count hits 0 → pick the current element as the new candidate When you see the same element → count++ When you see a different element → count-- The majority element always "survives" the cancellation 🔑 Key Insight: The majority element appears more than all other elements COMBINED. So even if every other element "cancels" one occurrence of the majority, it still has votes left at the end. 🗓️ Submitted: April 13, 2026 If you're preparing for DSA interviews, this is a must-know pattern. It shows up in stream processing, voting systems, and distributed consensus problems too. Drop a comment if you've solved this a different way — I'd love to compare approaches! 🚀 #LeetCode #DSA #Java #BoyerMoore #CodingInterview #ProblemSolving #100DaysOfCode #SoftwareEngineering
Boyer-Moore Voting Algorithm for Majority Element
More Relevant Posts
-
🚀 Day 31 of #100DaysOfCode Solved Find Target Indices After Sorting Array 🔍 Today’s problem looked like a sorting question, but the real trick was avoiding sorting altogether 😏 💡 Key Insight: Count elements less than target → starting index Count elements equal to target → number of indices ⚡ No sorting needed → O(n) time, O(1) space 🔍 Learned: Optimize before you implement brute force Counting can replace sorting in many cases Cleaner logic = faster code 💻 Implemented in Java #DSAwithEdSlash #LeetCode #Java #ProblemSolving #100DaysOfCode @edslash
To view or add a comment, sign in
-
-
💻 Interesting Java problem: Find the closest target in a circular array Given a words[] array, a target, and a startIndex, the task is to find the shortest circular distance to the target. Leetcode Problem :https://lnkd.in/g3RbdChE Core idea: For every matching target: find direct distance → Math.abs(i - startIndex) find circular distance → n - dist take the minimum. This is a neat example of how circular arrays require us to think beyond normal linear traversal. Small twist, smart logic. 🚀 #Java #Algorithms #DSA #CodingInterview #ProblemSolving #LeetCode
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟓𝟔 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding a peak element using binary search. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Find Peak Element 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • Used binary search instead of linear scan • Compared the middle element with its next element Logic: • If nums[mid] > nums[mid + 1] → peak lies on the left side (including mid) • Else → peak lies on the right side • Continued until left == right 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Binary search can be applied on patterns, not just sorted arrays • A peak always exists due to problem constraints • Comparing adjacent elements helps determine direction • Reducing the search space is the key idea 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(log n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Binary search is not about sorted arrays — it’s about eliminating half of the search space using logic. 56 days consistent 🚀 On to Day 57. #DSA #Arrays #BinarySearch #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
#Day98 of #100DaysOfCode Learned Binary Search, an efficient searching algorithm for sorted arrays. Implemented logic to find an element by repeatedly dividing the search space. Focused on understanding how optimized searching works compared to linear search. #Java #DSA #BinarySearch #100DaysOfCode
To view or add a comment, sign in
-
🚀 Day 3/30 — LeetCode Challenge Solved Median of Two Sorted Arrays on using Java. This wasn’t about merging arrays — the real challenge was applying binary search on partitions to achieve optimal time complexity. ✅ Key takeaway: Efficiency comes from thinking differently, not doing more work. Using partition-based binary search reduces complexity to O(log(min(n, m))). This problem pushed me to understand the logic deeply rather than just implementing it. #LeetCode #Java #Algorithms #BinarySearch #ProblemSolving #Consistency Archana J E Divya Suresh Harini B harilakshmi s Bavani k Bhavya B Devipriya R
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟵𝟰/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟭𝟱𝟯𝟵. 𝗞𝘁𝗵 𝗠𝗶𝘀𝘀𝗶𝗻𝗴 𝗣𝗼𝘀𝗶𝘁𝗶𝘃𝗲 𝗡𝘂𝗺𝗯𝗲𝗿 | 🟢 Easy | Java Marked as Easy — but the optimal solution is pure binary search brilliance. 🎯 🔍 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 Given a sorted array, find the kth missing positive integer. Linear scan works — but can we do O(log n)? 💡 𝗧𝗵𝗲 𝗞𝗲𝘆 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 At index i, the value arr[i] should be i+1 in a complete sequence. So missing numbers before arr[i] = arr[i] - 1 - i This lets us binary search on the count of missing numbers! ⚡ 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵 ✅ If arr[mid] - 1 - mid < k → not enough missing numbers yet, go right ✅ Else → too many missing, go left ✅ After the loop, left + k gives the exact answer 𝗪𝗵𝘆 𝗹𝗲𝗳𝘁 + 𝗸? After binary search, left is the index where the kth missing number falls beyond. left numbers exist in the array before that point, so the answer is left + k. No extra passes needed. ✨ 📊 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 ⏱ Time: O(log n) — vs O(n) linear scan 📦 Space: O(1) This is a perfect example of binary searching on a derived condition, not just a value. A real upgrade from the naive approach. 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gVYcjNS6 𝟲 𝗺𝗼𝗿𝗲 𝗱𝗮𝘆𝘀. 𝗧𝗵𝗲 𝗳𝗶𝗻𝗶𝘀𝗵 𝗹𝗶𝗻𝗲 𝗶𝘀 𝗿𝗶𝗴𝗵𝘁 𝘁𝗵𝗲𝗿𝗲! 🏁 #LeetCode #Day94of100 #100DaysOfCode #Java #DSA #BinarySearch #Arrays #CodingChallenge #Programming
To view or add a comment, sign in
-
🚀 Day 26 of #100DaysOfCode Solved Search Insert Position using Binary Search 🔍 Today’s problem was all about efficiently finding the correct position of a target in a sorted array. Instead of linear search, I used Binary Search (O(log n)) to either find the element or determine its correct insertion index. 💡 Key Takeaways: Binary Search isn’t just for finding elements — it helps determine positions too Returning start after the loop gives the correct insert position Clean and efficient approach beats brute force every time 💻 Consistency + Optimization = Progress #DSAwithEdSlash #LeetCode #Java #BinarySearch #CodingJourney
To view or add a comment, sign in
-
-
Problem Solved: Super Reduced String (Stack Approach) Today I solved an interesting string problem where we repeatedly remove adjacent matching characters until no more reductions are possible. 💡 Key Idea: Used a StringBuilder as a stack to efficiently remove adjacent duplicates in a single pass. 🔧 Tech Used: Java | String Manipulation | Stack Concept 📌 What I Learned: How stack-based thinking simplifies string problems Writing optimized O(n) solutions Clean handling of edge cases like empty string 📊 Example: Input: aaabccddd Output: abd This problem is a great example of how simple logic + the right data structure can lead to efficient solutions. #Java #DataStructures #CodingPractice #ProblemSolving #Algorithms
To view or add a comment, sign in
-
-
Take a close look at the snippet below. At first glance, you might expect a compiler error. What is a URL doing in the middle of a method? public class Main { public static void main(String[] args) { http://www.google.com System.out.println("Hello World"); } } The Twist: This code runs perfectly and prints "Hello World." How? It’s a classic Java "Easter Egg" logic. The compiler doesn't see a broken URL; it sees two distinct things: • http: is interpreted as a Label. In Java, you can label almost any statement (often used with break or continue in nested loops). • //www.google.com is interpreted as a Single-line comment. Because the label is valid and the rest of the line is ignored as a comment, the JVM just skips right past it to the println statement. It’s a great reminder that even in a language as structured as Java, there are always unusual syntax behaviors. #Java #CodingTips #SoftwareEngineering #JavaDeveloper #CleanCode #ProgrammingLogic #TechCommunity
To view or add a comment, sign in
-
🚀 Ever wondered what actually happens under the hood when you run a Java program? It’s not just magic; it’s the Java Virtual Machine (JVM) at work. Understanding JVM architecture is the first step toward moving from "writing code" to "optimizing performance." Here is a quick breakdown of the core components shown in the diagram: 1️⃣ Classloader System The entry point. It loads, links, and initializes the .class files. It ensures that all necessary dependencies are available before execution begins. 2️⃣ Runtime Data Areas (Memory Management) This is where the heavy lifting happens. The JVM divides memory into specific areas: Method/Class Area: Stores class-level data and static variables. Heap Area: The home for all objects. This is where Garbage Collection happens! Stack Area: Stores local variables and partial results for each thread. PC Registers: Keeps track of the address of the current instruction being executed. Native Method Stack: Handles instructions for native languages (like C/C++). 3️⃣ Execution Engine The brain of the operation. It reads the bytecode and executes it using: Interpreter: Reads bytecode line by line. JIT (Just-In-Time) Compiler: Compiles hot spots of code into native machine code for massive speed boosts. Garbage Collector (GC): Automatically manages memory by deleting unreferenced objects. 4️⃣ Native Interface & Libraries The bridge (JNI) that allows Java to interact with native OS libraries, making it incredibly versatile. 💡 Pro-Tip: If you are debugging OutOfMemoryError or StackOverflowError, knowing which memory area is failing is half the battle won. #Java #JVM #BackendDevelopment #SoftwareEngineering #ProgrammingTips #TechCommunity #JavaDeveloper #CodingLife
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