Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Segregate Positive and Negative Numbers in an Array Without Changing Order

When working with arrays, a common problem is rearranging elements based on a condition while preserving their original order. In this post, we will solve the problem of moving all negative numbers in an array to the end without disturbing…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

When working with arrays, a common problem is rearranging elements based on a condition while preserving their original order. In this post, we will solve the problem of moving all negative numbers in an array to the end without disturbing the order of positive and negative numbers.



Problem Statement



Given an array of integers, rearrange it so that all non-negative elements appear first, followed by negative elements. The relative order of elements must remain unchanged.



Example 1:



Input:



[1, -1, 3, 2, -7, -5, 11, 6]



Output:



[1, 3, 2, 11, 6, -1, -7, -5]



Example 2:



Input:



[-5, 7, -3, -4, 9, 10, -1, 11]



Output:



[7, 9, 10, 11, -5, -3, -4, -1]



Constraints:



1 ≤ array size ≤ 10⁶

-10⁹ ≤ arr[i] ≤ 10⁹



Expected Time Complexity: O(n)

Expected Auxiliary Space: O(n)



Approach

To solve this efficiently:

Traverse the array once and store all non-negative elements in one temporary list.

Store all negative elements in another temporary list.

Merge both lists back into the original array.



This approach ensures:

The order of elements is preserved (stable rearrangement).

Time complexity is linear, O(n).

Auxiliary space is O(n), which is acceptable according to the problem constraints.



Python Implementation Code



class Solution:

def segregateElements(self, arr):

pos = []

neg = []




    # Separate positive and negative elements
for i in arr:
if i >= 0:
pos.append(i)
else:
neg.append(i)

# Merge back into original array
i = 0
for x in pos:
arr[i] = x
i += 1

for x in neg:
arr[i] = x
i += 1




How It Works

Take input array:



[1, -1, 3, 2, -7, -5, 11, 6]

Separate into two lists:

pos = [1, 3, 2, 11, 6]

neg = [-1, -7, -5]

Merge back into original array:

[1, 3, 2, 11, 6, -1, -7, -5]

Order of positive and negative elements is preserved.



Complexity Analysis

Time Complexity: O(n) → One pass to separate + one pass to merge

Space Complexity: O(n) → Two auxiliary lists



Conclusion

This problem highlights the importance of stable rearrangement of elements in an array. Using a simple auxiliary list approach, we can efficiently move all negative numbers to the end without disturbing the original order of elements.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Segregate Positive and Negative Numbers in an Array Without Changing Order

Thematisch verwandte Begriffe: Segregate, Positive, Negative, Numbers · 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 ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-79918 | MaxKB is an open-source AI assistant for enterprise. Prior to version 2.…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
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
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick