🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🪟 Windows TippsHeader and Footer not showing in Excel(14.09.2026 um 22:43 Uhr)
🕵️ SicherheitslückenBurn Out, Or Fade Away(14.09.2026 um 14:25 Uhr)
🪟 Windows TippsKB5129194 Windows 11 26H1 Out of Band Update - Deskmodder.de(14.09.2026 um 19:25 Uhr)
🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🪟 Windows TippsHeader and Footer not showing in Excel(14.09.2026 um 22:43 Uhr)
🕵️ SicherheitslückenBurn Out, Or Fade Away(14.09.2026 um 14:25 Uhr)
🪟 Windows TippsKB5129194 Windows 11 26H1 Out of Band Update - Deskmodder.de(14.09.2026 um 19:25 Uhr)

🔧 Programmierung 🕛 vor 5 Monaten 2 Min Lesezeit
0

First and Last Occurrences

↗ Quelle (dev.to)
🗣️ Stimme:

Problem Statement



Given a sorted array arr (which may contain duplicates), find the first and last occurrences of an element x.



If x is not present in the array, return [-1, -1].



Examples:



Input: arr = [1, 3, 5, 5, 5, 5, 67, 123, 125], x = 5

Output: [2, 5]

Input: arr = [1, 3, 5, 5, 5, 5, 7, 123, 125], x = 7

Output: [6, 6]

Input: arr = [1, 2, 3], x = 4

Output: [-1, -1]



Constraints:



1 ≤ arr.size() ≤ 10^6

1 ≤ arr[i], x ≤ 10^9



Approach: Binary Search



Since the array is sorted, binary search allows us to efficiently find the first and last occurrences of x.



Steps:



First Occurrence:

Perform binary search.

If arr[mid] == x, move the high pointer to mid - 1 to check if x occurs earlier.

Last Occurrence:

Perform binary search.

If arr[mid] == x, move the low pointer to mid + 1 to check if x occurs later.



This ensures O(log n) time complexity for each search, perfect for large arrays.



Python CODE



from typing import List



class Solution:

def find(self, arr: List[int], x: int) -> List[int]:

n = len(arr)




CODE
    # Find first occurrence
first, last = -1, -1
low, high = 0, n - 1

while low <= high:
mid = low + (high - low) // 2
if arr[mid] < x:
low = mid + 1
elif arr[mid] > x:
high = mid - 1
else:
first = mid
high = mid - 1 # Move left to find first occurrence

# If element not found
if first == -1:
return [-1, -1]

# Find last occurrence
low, high = first, n - 1
while low <= high:
mid = low + (high - low) // 2
if arr[mid] > x:
high = mid - 1
else:
last = mid
low = mid + 1 # Move right to find last occurrence

return [first, last]




How It Works:



Binary search allows skipping unnecessary elements.

First occurrence moves left when arr[mid] == x.

Last occurrence moves right when arr[mid] == x.

Handles no occurrence by returning [-1, -1].



Time Complexity: O(log n)

Space Complexity: O(1)



This approach is optimal for large arrays and demonstrates the power of binary search in sorted arrays.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
The Gemini desktop app is now available for Windows
1 Quelle
Header and Footer not showing in Excel
1 Quelle
Burn Out, Or Fade Away
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten First and Last Occurrences

Thematisch verwandte Begriffe: First, Last, Occurrences · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...