𝗗𝗮𝘆 𝟯𝟵 𝗼𝗳 #𝟭𝟴𝟬𝗗𝗮𝘆𝘀𝗢𝗳𝗖𝗼𝗱𝗲 Today, I built on the first/last occurrence solution to 𝗰𝗼𝘂𝗻𝘁 𝗼𝗰𝗰𝘂𝗿𝗿𝗲𝗻𝗰𝗲𝘀 𝗼𝗳 𝗮 𝘁𝗮𝗿𝗴𝗲𝘁 𝗶𝗻 𝗮 𝘀𝗼𝗿𝘁𝗲𝗱 𝗮𝗿𝗿𝗮𝘆 𝘄𝗶𝘁𝗵 𝗱𝘂𝗽𝗹𝗶𝗰𝗮𝘁𝗲𝘀. Using the same lower_bound and upper_bound helpers: Lower bound → first index where element ≥ target Upper bound → first index where element > target The count is simply: count = upper_bound - lower_bound This gives an O(log n) solution — much faster than scanning the entire array, especially with many duplicates. It’s a great example of how breaking a problem into reusable pieces leads to clean and efficient code. Perfect for analytics, frequency analysis, and search optimizations! 📊 #Python #Algorithms #BinarySearch #FrequencyCount #Coding #ProblemSolving
Count occurrences in sorted array with Python
More Relevant Posts
-
Day 41 of #180DaysOfCode Today, I leveled up the 𝗿𝗼𝘁𝗮𝘁𝗲𝗱 𝘀𝗼𝗿𝘁𝗲𝗱 𝗮𝗿𝗿𝗮𝘆 𝗽𝗿𝗼𝗯𝗹𝗲𝗺: 𝗦𝗲𝗮𝗿𝗰𝗵 𝘄𝗶𝘁𝗵 𝗗𝘂𝗽𝗹𝗶𝗰𝗮𝘁𝗲𝘀! 🔍 When the array contains duplicates, the previous approach needed a tweak. If nums[low] == nums[mid] == nums[high], we can't decide which side is sorted. The solution? Trim duplicates by moving low and high inward, then proceed with the standard rotated search logic. This ensures we still achieve O(log n) performance in most cases, though it can degrade to O(n) with many duplicates. A subtle but important edge case that makes binary search even more interesting! #Python #Algorithms #BinarySearch #RotatedArray #Duplicates #Coding
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟯𝟲 𝗼𝗳 #𝟭𝟴𝟬𝗗𝗮𝘆𝘀𝗢𝗳𝗖𝗼𝗱𝗲 Today, I solved 𝗦𝗲𝗮𝗿𝗰𝗵 𝗜𝗻𝘀𝗲𝗿𝘁 𝗣𝗼𝘀𝗶𝘁𝗶𝗼𝗻 — a practical variation of binary search. The goal was to find the index of a target in a sorted array, or the position where it should be inserted to maintain order. Using binary search, I efficiently located the correct spot in O(log n) time. The key was to track the first position where the element is greater than or equal to the target — which is exactly the insert position if the target isn’t found. This is a great example of how small tweaks to a classic algorithm can solve new problems elegantly. A useful technique for search, insertion, and maintaining sorted data dynamically! #Python #Algorithms #BinarySearch #LeetCode #Coding #ProblemSolving
To view or add a comment, sign in
-
-
Day 30 / 100 – Majority Element II (LeetCode #229) Today’s challenge was all about identifying numbers that appear more than ⌊ n/3 ⌋ times in an array. This problem was a great way to explore the Boyer–Moore Voting Algorithm, an elegant and efficient method to find potential majority elements without using extra space. 🔍 Key Learnings Understanding how counters and candidates evolve in an iterative approach Strengthening logic for pattern recognition and frequency analysis Realizing how efficient algorithms can reduce time and memory usage 💭 Thought of the Day Consistency isn’t just about showing up — it’s about learning smarter each day. Every new problem isn’t a repetition; it’s a refinement of logic and discipline. 🔗 Problem Link:https://lnkd.in/gfiGVueC #100DaysOfCode #Day30 #LeetCode #Python #ProblemSolving #CodingChallenge #DataStructures #Algorithms #LearningJourney #CodeEveryday #TechGrowth
To view or add a comment, sign in
-
-
Day 16 of #100DaysOfLeetCode Problem: 448. Find All Numbers Disappeared in an Array Category: Arrays / Hashing / Set Operations Today’s challenge was about finding which numbers are missing from an array containing elements in the range 1 to n. This problem is a great example of how using a set can simplify lookups and help identify missing values efficiently. 🧠 Key Learnings: Converted the input array into a set to achieve constant-time membership checks. Iterated from 1 to n to identify which numbers don’t appear in the original list. Strengthened my understanding of how set-based lookups drastically reduce time complexity for search operations. Reinforced concepts of hashing, membership testing, and search optimization. 🎯 Takeaway: Simple tools like sets can make a huge difference — turning a potentially slow search into a clean and efficient solution. #LeetCode #100DaysOfCode #ProblemSolving #CodingJourney #Arrays #Hashing #Python #AIEngineer #Consistency
To view or add a comment, sign in
-
-
🚀Day 80 of #100DaysOfDSA 🚀 Solved LeetCode 451 — Sort Characters By Frequency 🔹 Problem: Sort a string based on the frequency of its characters in descending order. 🔹 Approach: Used Hash Map + Sorting 1️⃣ Count frequency of each character using a dictionary 2️⃣ Sort characters by their frequency (descending) 3️⃣ Rebuild the string by repeating characters based on count ✨ Key Insight: Hash maps are great for frequency counting, and sorting by values helps in many pattern-based problems. #LeetCode #DSA #Python #ProblemSolving #HashMap #Sorting #CodingJourney #100DaysOfCode 🚀
To view or add a comment, sign in
-
-
🚀 LeetCode #1526: Minimum Number of Increments on Subarrays to Form a Target Array Today I tackled an interesting Greedy Algorithm problem that really tests your ability to spot patterns and simplify complex logic. 🧩 Problem Brief: We start with an array of zeros and can increment any subarray by 1 in one operation. The goal is to form the target array using the minimum number of operations. 💡 Key Insight: Instead of simulating every operation, focus on how much each element increases compared to the previous one; each increase represents new operations needed. ⚙️ Formula: operations = target[0] + Σ(max(0, target[i] - target[i-1])) 🧠 Complexity: Time: O(n) Space: O(1) 🎯 Takeaway: Hard problems often become simple once you recognize the pattern behind the process. #LeetCode #Python #DSA #Coding #ProblemSolving #GreedyAlgorithm #LearningEveryday
To view or add a comment, sign in
-
-
🔢 Understanding Selection Sort 🎯 Problem: Given an array of numbers, sort them in ascending order using the Selection Sort algorithm. 🧩 Concept: Selection Sort works by repeatedly finding the minimum element from the unsorted part of the array and placing it at the beginning. 🔹In each iteration, we assume the first element is the smallest. 🔹Then, we find the actual minimum from the remaining unsorted part. 🔹Finally, we swap it with the current index element. 🕒 Time Complexity: O(N^2) - For each element, we search the rest of the array for the smallest element. 💾 Space Complexity: O(1) - Sorting is done in place (no extra memory used). #Python #DataStructures #Algorithms #Sorting #CodingJourney #ProblemSolving #LearningInPublic
To view or add a comment, sign in
-
-
Day 66: Find Minimum in Rotated Sorted Array II 📉 (Simple Version) I'm tackling an advanced Binary Search problem on Day 66 of #100DaysOfCode: "Find Minimum in Rotated Sorted Array II." The challenge is to find the minimum element in a rotated sorted array that may contain duplicates. My solution uses a modified Binary Search approach. The key difficulty is when the middle element (nums[mid]) equals the rightmost element (nums[right]). In this case, we can't tell if the minimum is on the left or the right. To resolve this ambiguity, I simply discard the rightmost element by setting right -= 1. This adjustment keeps the search on track, achieving an optimal O(log n) complexity in the typical case. My solution achieved 100% runtime efficiency! #Python #DSA #Algorithms #BinarySearch #Arrays #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
Today's LeetCode problem was the foundational 509. Fibonacci Number. While there are faster ways to solve this, I focused on implementing the classic recursive solution. It's such a fundamental concept to understand, as it directly mirrors the mathematical definition: $F(n) = F(n-1) + F(n-2)$. It's a great reminder that coding is often about translating mathematical logic directly into code. The pursuit of efficiency (like using memoization next!) never stops, but understanding the basics is paramount. #LeetCode #Recursion #DSA #Algorithms #Python #ComputerScience
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