Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
YouTube Security VideosNutanix advances legacy and AI app management with AMD(01.10.2026 um 16:00 Uhr)
•
YouTube Security VideosPC-WELT: 32 TB SSD-Speicher in der HMX 6!(01.10.2026 um 16:15 Uhr)
••••
Videos & KonferenzenPC-WELT: 32 TB SSD-Speicher in der HMX 6!(01.10.2026 um 16:15 Uhr)
••
Sicherheitslücken (CVE)USN-8857-1: KCoreAddons vulnerability(01.10.2026 um 12:48 Uhr)
•••
YouTube Security VideosNutanix advances legacy and AI app management with AMD(01.10.2026 um 16:00 Uhr)
•
YouTube Security VideosPC-WELT: 32 TB SSD-Speicher in der HMX 6!(01.10.2026 um 16:15 Uhr)
••••
Videos & KonferenzenPC-WELT: 32 TB SSD-Speicher in der HMX 6!(01.10.2026 um 16:15 Uhr)
••
Sicherheitslücken (CVE)USN-8857-1: KCoreAddons vulnerability(01.10.2026 um 12:48 Uhr)
•••
Intelligence View
⚡ tsecurity.de Intelligence

Find First and Last Occurrences in a Sorted Array

Problem Given a sorted array arr that may contain duplicates, find the first and last occurrence of a target element x. If x is not present, return [-1, -1].…

Beitrag
0
Seite
0
↗ Quelle (dev.to)
Social ReaktionenReagiere als Erste:r — dein Feedback zählt!

Problem



Given a sorted array arr that may contain duplicates, find the first and last occurrence of a target element x.

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



Examples



Input

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



Output

[2, 5]



Explanation: First occurrence of 5 is at index 2, last at index 5.



Input



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



Output



[6, 6]



Explanation: First and last occurrence of 7 is at index 6.



Input

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



Output

[-1, -1]



Explanation: 4 is not present.



Approach



Use binary search to find the first occurrence of x.

Use binary search again to find the last occurrence.

If x is not found in either step, return [-1, -1].

This is efficient because the array is sorted, giving O(log n) time complexity.



Python Code

class Solution:

def find(self, arr, x):

n = len(arr)

first = -1

last = -1




    # Find first occurrence
low = 0
high = n - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == x:
first = mid
high = mid - 1
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1

# Find last occurrence
low = 0
high = n - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == x:
last = mid
low = mid + 1
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1

return [first, last]




Output

[2, 5]

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Find First and Last Occurrences in a Sorted Array

Thematisch verwandte Begriffe: Find, 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 ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
tsecurity.de Icon
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag