Finding First and Last Occurrence of an Element Using Binary Search in Python

Problem Explanation You are given a sorted array arr[] (may contain duplicates) and a number x. Your task is to find the first and last occurrence of x. If x is not found, return [-1, -1]. Example:...

By · · 1 min read
Finding First and Last Occurrence of an Element Using Binary Search in Python

Source: DEV Community

Problem Explanation You are given a sorted array arr[] (may contain duplicates) and a number x. Your task is to find the first and last occurrence of x. If x is not found, return [-1, -1]. Example: Input: arr = [1, 3, 5, 5, 5, 5, 67, 123, 125], x = 5 Output: [2, 5] Input: arr = [1, 3, 5, 5, 5, 7, 123, 125], x = 7 Output: [6, 6] Method Used: Binary Search Idea Since the array is sorted: Use binary search to find the first occurrence Use binary search again to find the last occurrence Why This Method? Time complexity: O(log n) Much faster than linear search (O(n)) Efficient for large arrays Python Code with Explanation class Solution: def find(self, arr, x): Defines the function. Finding First Occurrence left = 0 right = len(arr) - 1 first = -1 Initialize pointers and variable to store first occurrence. while left <= right: mid = (left + right) // 2 Standard binary search setup. if arr[mid] == x: first = mid right = mid - 1 If found: Store index Move left to find earlier occurrence el

Related Posts

Similar Topics

#data science (2759)#machine learning (1440)#artificial intelligence (737)#ai (604)#hands on tutorials (447)#data visualization (306)#data engineering (238)#deep dives (294)#coding (201)#webdev (311)#large language models (262)#science and technology (277)#editors pick (267)#software engineering (181)#tutorial (199)#productivity (208)#deep learning (134)#data analysis (173)#pandas (158)#llm (159)

Trending on ShareHub

  1. Understanding Modern JavaScript Frameworks in 2026
    by Alex Chen · Feb 12, 2026 · 0 likes
  2. The System Design Primer
    by Sarah Kim · Feb 12, 2026 · 0 likes
  3. Just shipped my first open-source project!
    by Alex Chen · Feb 12, 2026 · 0 likes
  4. OpenAI Blog
    by Sarah Kim · Feb 12, 2026 · 0 likes
  5. Building Accessible Web Applications: A Practical Guide
    by Alex Chen · Feb 12, 2026 · 0 likes
  6. Rapper Lil Poppa dead at 25, days after releasing new music
    Rapper Lil Poppa dead at 25, days after releasing new music
    by Anonymous User · Feb 19, 2026 · 0 likes
  7. write-for-us
    by Volt Raven · Mar 7, 2026 · 0 likes
  8. Before the Coffee Gets Cold: Heartfelt Story of Time Travel and Second Chances
    Before the Coffee Gets Cold: Heartfelt Story of Time Travel and Second Chances
    by Anonymous User · Feb 12, 2026 · 0 likes
    #coffee gets cold #the #time travel
  9. Best DoorDash Promo Code Reddit Finds for Top Discounts
    Best DoorDash Promo Code Reddit Finds for Top Discounts
    by Anonymous User · Feb 12, 2026 · 0 likes
    #doordash #promo #reddit
  10. Premium SEO Services That Boost Rankings & Revenue | VirtualSEO.Expert
    by Anonymous User · Feb 12, 2026 · 0 likes
  11. NBC under fire for commentary about Team USA women's hockey team
    NBC under fire for commentary about Team USA women's hockey team
    by Anonymous User · Feb 18, 2026 · 0 likes
  12. Where to Watch The Nanny: Streaming and Online Viewing Options
    Where to Watch The Nanny: Streaming and Online Viewing Options
    by Anonymous User · Feb 12, 2026 · 0 likes
    #streaming #the nanny #where
  13. How Much Is Kindle Unlimited? Subscription Cost and Plan Details
    How Much Is Kindle Unlimited? Subscription Cost and Plan Details
    by Anonymous User · Feb 12, 2026 · 0 likes
    #kindle unlimited #subscription #unlimited
  14. Russian skater facing backlash for comment about Amber Glenn
    Russian skater facing backlash for comment about Amber Glenn
    by Anonymous User · Feb 18, 2026 · 0 likes
  15. Google News
    Google News
    by Anonymous User · Feb 18, 2026 · 0 likes

Latest on ShareHub

Browse Topics

#artificial intelligence (36887)#data science (24144)#generative ai (19046)#ai (17908)#crypto (15051)#machine learning (14735)#bitcoin (14343)#featured (13586)#news & insights (13064)#crypto news (11119)

Around the Network