🚀 Day 2/30 – LeetCode Challenge Solved “Majority Element” today using the Boyer–Moore Voting Algorithm. 🔹 Problem: Find the element that appears more than ⌊n/2⌋ times in an array. 🔹 Requirements: ✔ O(n) Time Complexity ✔ O(1) Space Complexity 💡 Approach Used – Boyer–Moore Voting Algorithm Instead of using a HashMap (which takes extra space), I used an optimized approach: Maintain a candidate and a count If count becomes 0 → update candidate If number equals candidate → increment count Else → decrement count This works because the majority element will always survive the cancellation process. ⚡ Result: ✅ Accepted (53/53 test cases) ⚡ Runtime: 1 ms (Beats 99.82%) 💾 Memory: 55.75 MB 📚 Learning: Today’s problem taught me how powerful algorithmic thinking can replace brute force solutions. Space optimization makes a big difference in interviews. On to Day 3 💪 #Day2 #30DaysOfCode #LeetCode #Java #Algorithms #DataStructures #BoyerMoore #ProblemSolving #CodingJourney #SoftwareEngineeringStudent #Consistency #TechGrowth
Majority Element Solved with Boyer–Moore Voting Algorithm
More Relevant Posts
-
🚀 Day 45 Out of #365DaysOfCode - LeetCode Github link: https://lnkd.in/gGUy_MKZ Today I solved an interesting problem: Given a sorted array that has been rotated between 1 and n times, find the minimum element in O(log n) time. 💡 Key Insight: Even though the array is rotated, it still contains two sorted halves. Using Binary Search, we can efficiently locate the pivot point (the smallest element) instead of scanning the whole array. 📌 This problem is a great reminder that understanding patterns in sorted structures can drastically improve performance. Consistency in practicing data structures & algorithms really sharpens problem-solving skills. #DataStructures #Algorithms #BinarySearch #Java #CodingPractice #InterviewPrep #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 63/100 – #LeetCodeChallenge 📌 Problem: Count Binary Substrings Today’s problem was all about spotting patterns in binary strings — and it turned out to be a fun exercise in run-length encoding logic! 🧠 🔍 Approach: Instead of storing counts separately, I used a simple linear scan to keep track of consecutive identical characters (strk) and the length of the previous run (prev). Whenever a run ends, we update prev and reset strk. Then, if the current streak is ≤ the previous one, it means we’ve found a valid substring — and we increment the result. 📈 ⚡ Result: ✅ Runtime: 10 ms – beats 87.50% 🚀 💾 Memory: 46.44 MB – beats 56.68% Feeling good about the balance between time and space efficiency here! Every day is a step closer to mastering problem-solving patterns. 💪 🔗 Takeaway: Sometimes the most elegant solutions come from simple observation — no need for complex data structures when a single pass and a few variables do the trick! #100DaysOfCode #LeetCode #Java #CodingChallenge #ProblemSolving #TechJourney #DevCommunity #StringManipulation #Algorithms #CodeNewbie #SoftwareEngineering #WomenInTech #CodingLife #InterviewPrep #DataStructures #DailyCoding
To view or add a comment, sign in
-
-
After understanding basic recursive flow, I moved to calculating power using recursion. The first version was straightforward — multiply a by itself b times. But that approach was linear. Then I implemented the logarithmic version. That’s where recursion started feeling powerful. What changed here: - Instead of reducing the problem by 1 step, I reduced it by half. - Learned that recursion can follow divide-and-conquer logic. - Understood how even/odd cases change the recurrence. - Saw how time complexity drops from O(n) to O(log n). Core logic : if (b == 0) return 1; long half = power(a, b / 2); if (b % 2 == 0) return half * half; else return a * half * half; This was the first time recursion felt like optimization, not just structure. It wasn’t about calling a function again. It was about reducing the problem smarter. #recursion #java #dsajourney #algorithms #problemSolving
To view or add a comment, sign in
-
🚀 DSA Daily Challenge – Day 4 🧠 Problem: 1. Two Sum (LeetCode) 💡 Goal: Given an array of integers nums and an integer target, return the indices of the two numbers such that they add up to target. You may assume: ✔ Exactly one solution exists ✔ You cannot use the same element twice ✔ Return indices (not values) 🔥 Intuition Brute force would check every pair → O(n²) ❌ But we can do better using a HashMap: 👉 For each number, calculate its complement 👉 Check if the complement already exists in the map 👉 If yes → we found the answer 👉 If not → store the current number with its index Why does this work? Because we reduce the lookup time to O(1) using hashing. 🛠 Approach (HashMap – One Pass) • Create a HashMap to store number → index • Traverse the array once • For each element: Compute complement = target - nums[i] If complement exists in map → return indices Else → store current number in map ⚙️ Complexity ⏱ Time Complexity: O(n) Each element is processed once 📦 Space Complexity: O(n) HashMap stores up to n elements #DSA #DataStructures #Algorithms #LeetCode #TwoSum #CodingInterview #SoftwareEngineer #Java #Programming #CodingPractice #InterviewPrep #100DaysOfCode #Developers #TechCareers #HashMap #ProblemSolving #BigO #TimeComplexity #CompetitiveProgramming
To view or add a comment, sign in
-
-
Good Morning LinkedIn Connections ☀️ Continuing my Algorithm Series — Bubble Sort Bubble Sort is one of the simplest sorting algorithms. 🔹 Core Idea: Repeatedly compare adjacent elements and swap them if they are in the wrong order. After each pass, the largest unsorted element “bubbles” to the end. 📊 Time Complexity: • Best Case → O(n) (when optimized & already sorted) • Average Case → O(n²) • Worst Case → O(n²) Space Complexity → O(1) Stable Sorting Algorithm Although inefficient for large datasets, Bubble Sort is excellent for understanding sorting fundamentals and swap logic. Mastering simple algorithms builds a strong foundation for solving complex problems. #DSA #Algorithms #Sorting #Java #CodingInterview
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟯𝟴/𝟭𝟬𝟬 | 𝗖𝗼𝗽𝘆 𝗟𝗶𝘀𝘁 𝘄𝗶𝘁𝗵 𝗥𝗮𝗻𝗱𝗼𝗺 𝗣𝗼𝗶𝗻𝘁𝗲𝗿 Day 38 ✅ — When linked lists get complex. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟭𝟯𝟴: Copy List with Random Pointer (Medium) 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: Deep copy a linked list where each node has a next pointer AND a random pointer that can point to any node. The challenge? You can't just copy nodes one-by-one because the random pointers reference nodes you might not have created yet. Solution: 𝗛𝗮𝘀𝗵 𝗠𝗮𝗽. Two passes: Create all nodes and store old → new mapping Connect next and random pointers using the map 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: 👉 First pass: Create clone nodes, map original → clone 👉 Second pass: Set clone.next and clone.random using map 👉 Return cloned head from map Time: O(n), Space: O(n) for the hash map 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: Nine linked list problems. This one showed that hash maps aren't just for arrays—they're powerful for pointer-based structures too. The pattern: when you need to track relationships between objects, think hash map. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/gAXPCssx 𝗗𝗮𝘆 𝟯𝟴/𝟭𝟬𝟬 ✅ | 𝟲𝟮 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #HashMap #DeepCopy #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #MediumLevel #Programming
To view or add a comment, sign in
-
🚀 Day 6/30 – Applying Binary Search Beyond the Obvious Today’s problem involved finding the **k-th missing positive number** in a sorted array. At first glance, it looks like a counting problem. But instead of iterating through numbers one by one, I approached it using **Binary Search** to optimize performance. ### 💡 Key Insight For any index `i`, the number of missing elements before it can be calculated using: `missing = arr[i] - (i + 1)` This observation transforms the problem into a monotonic condition — which makes it perfect for binary search. By narrowing the search space based on how many numbers are missing at mid, I was able to determine the correct position efficiently. ### 📊 Performance ✅ Accepted (All test cases passed) ⚡ 0 ms runtime (100% performance) 💾 Strong memory efficiency ### 📚 What I Learned Binary Search is not just about searching values — it’s about recognizing patterns where the search space can be reduced logically. The real improvement comes from spotting the mathematical relationship inside the problem. Day 6 complete. Consistency is turning into confidence 💪 #Day6 #30DaysOfCode #LeetCode #Java #BinarySearch #Algorithms #DataStructures #ProblemSolving #CodingJourney #SoftwareEngineering #Consistency
To view or add a comment, sign in
-
-
Day 8 – DSA Journey | Arrays 🚀 Today’s problem pushed me to think in terms of recursion 🔁, constraints 🧩, and backtracking ⏪ rather than simple iteration. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • 🧠 Sudoku Solver 🔹 Sudoku Solver 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • 🔁 Used backtracking to try all valid possibilities • 📊 Tracked constraints using boolean matrices for rows, columns, and 3×3 boxes • ✅ Placed a number only if it was valid across all constraints 𝐇𝐨𝐰 𝐢𝐭 𝐖𝐨𝐫𝐤𝐬 • 🔍 Find the first empty cell • 🔢 Try numbers from 1–9 • ▶️ Recurse if placement is valid • ⏪ Backtrack if it leads to an invalid state 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • 💡 Backtracking is about choices, recursion, and undoing • ⚡ Pre-tracking constraints avoid repeated checks • 🧠 Clear constraints make recursion easier to reason about • 🛑 Clean base cases prevent infinite recursion 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • ⏱ Time: Exponential (heavily pruned by constraints) • 📦 Space: O(1) (fixed board + recursion stack) 🧠 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Backtracking isn’t brute force — it’s controlled exploration with rules 🎯 On to Day 9 🔁🚀 #DSA #Arrays #Backtracking #Recursion #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
🚀 Day 13/100 — Recursive Deconstruction Today’s Challenge: LeetCode 761 - Special Binary String 🧩 Yesterday was about bits; today was about Structural Grammar. Special Binary Strings aren't just 1s and 0s; they are a language. The Realization: A special string is like a valid set of parentheses. 1 is ( and 0 is ). To find the "lexicographically largest" version, you have to break the string into its most basic components, sort them, and put them back together. My 0ms Optimization Strategy: 1. Mental Mapping: Treated the string as a nested structure. If 1...0 is a special string, I stripped the outer layer, solved the inside, and then re-wrapped it. 2. Greedy Reassembly: Used Collections.sort() with a reverse comparator. In binary, "larger" just means the 1s appear as early as possible. 3. Memory Management: Minimized object creation by identifying "split points" first before committing to substring allocations. Reflection: Sometimes the fastest way to solve a problem isn't to iterate forward, but to look at the problem as a recursive tree. Every "Special" chunk is a node that can be reordered to maximize the total value. 13 days down. The logic is getting deeper. 87 days to go. 🛠️ #Java #DSA #LeetCode #100DaysOfCode #Recursion #StringAlgorithms #SoftwareEngineer #CodingJourney #Optimization
To view or add a comment, sign in
-
-
📌 17. Letter Combinations of a Phone Number (Medium) Today’s problem was all about Backtracking & Recursion. 🧠 Problem Summary Given digits from 2–9, return all possible letter combinations based on phone keypad mapping. Example: Input: "23" Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"] 💡 Key Insight This is a classic Cartesian Product problem. Each digit expands into multiple choices → Perfect use case for Backtracking. 🚀 Approach Create a digit → letter mapping Use recursion to build combinations When current string length == digits length → Add to result Backtrack and explore next possibilities ⏱ Complexity Time: O(4ⁿ) Space: O(n) recursion stack Max combinations = 256 (since n ≤ 4) 🎯 What I Practiced Today ✅ Recursion Tree Thinking ✅ Backtracking Pattern ✅ StringBuilder Optimization ✅ Clean Code Structure Consistency > Motivation 💪 Day 18 completed. #LeetCode #Java #DataStructures #Algorithms #Backtracking #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