Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Intelligence View
⚡ tsecurity.de Intelligence

# Sorted Squares of a Sorted Array (Java) – Efficient Two-Pointer Approach

When working with sorted arrays, a seemingly simple transformation—like squaring each element—can break the order. This problem is a great example of how understanding patterns in data can help us design efficient algorithms. 🚀…

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

When working with sorted arrays, a seemingly simple transformation—like squaring each element—can break the order. This problem is a great example of how understanding patterns in data can help us design efficient algorithms.









🚀 Problem Statement



Given an integer array nums sorted in non-decreasing order, return a new array containing the squares of each number, also sorted in non-decreasing order.









👉 Example






Input:  nums = [-4, -1, 0, 3, 10]
Output: [0, 1, 9, 16, 100]












🤔 Why Is This Tricky?



At first glance, you might think:




“Just square every element and the array stays sorted.”




But that’s not true because:




  • Negative numbers become positive when squared

  • A large negative number (e.g., -10) becomes larger than smaller positives when squared






Example:






[-7, -3, 2, 3, 11]
Squares [49, 9, 4, 9, 121] Not sorted












💡 Key Insight



The largest square value will always come from:




  • Either the leftmost element (most negative), or

  • The rightmost element (largest positive)



👉 This leads us to the two-pointer technique.









⚡ Optimal Approach (Two Pointers)






Steps:




  1. Initialize two pointers:




  • left = 0


  • right = n - 1




    1. Create a result array of size n

    2. Fill the result array from the end to the beginning

    3. Compare:




  • nums[left]^2 and nums[right]^2




    1. Place the larger square at the current position and move the corresponding pointer











🧑‍💻 Java Implementation






class Solution {
public int[] sortedSquares(int[] nums) {
int n = nums.length;
int[] result = new int[n];

int left = 0, right = n - 1;
int pos = n - 1;

while (left <= right) {
int leftSq = nums[left] * nums[left];
int rightSq = nums[right] * nums[right];

if (leftSq > rightSq) {
result[pos] = leftSq;
left++;
} else {
result[pos] = rightSq;
right--;
}
pos--;
}

return result;
}
}












🔍 Dry Run



For:




nums = [-4, -1, 0, 3, 10]





















































Step Left Right Chosen Square Result
1 -4 10 100 [_, _, _, _, 100]
2 -4 3 16 [_, _, _, 16, 100]
3 -1 3 9 [_, _, 9, 16, 100]
4 -1 0 1 [_, 1, 9, 16, 100]
5 0 0 0 [0, 1, 9, 16, 100]








⏱️ Complexity Analysis




















Aspect Complexity
Time O(n)
Space O(n)


✔ We traverse the array only once









⚠️ Naive Approach (Less Efficient)






Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
nums[i] = nums[i] * nums[i];
}
Arrays.sort(nums);






❌ Time Complexity: O(n log n) due to sorting









🎯 Why This Approach Works




  • The array is already sorted

  • The largest absolute values lie at the edges

  • By comparing squares from both ends, we ensure correct placement









🏁 Conclusion



This problem highlights how:




  • A simple transformation can break sorted order

  • A clever two-pointer technique can restore efficiency



Mastering such patterns is essential for coding interviews and real-world problem solving.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten # Sorted Squares of a Sorted Array (Java) – Efficient Two-Pointer Approach

Thematisch verwandte Begriffe: Sorted, Squares, Array, Java · 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 Kritische Sicherheitsmeldung
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