Python GrundlagenEinfach

Kreisdrehung um K Positionen

Detaillierte Anleitung und Python-Implementierung für das Problem „Kreisdrehung um K Positionen“.

Problemstellung

Einfach

Schreiben Sie eine Funktion circular_rotate(arr, k), die eine kreisförmige Rechtsdrehung des Arrays arr um k Positionen durchführt und das Ergebnis zurückgibt. Elemente, die über den Endumbruch hinaus zum Anfang verschoben wurden. Wenn Sie beispielsweise [1,2,3,4,5] um 2 nach rechts drehen, erhalten Sie [4,5,1,2,3].

Einschränkungen
  • 0 <= len(arr) <= 10^5
  • 0 <= k <= 10^6
  • -10^9 <= arr[i] <= 10^9

Beispiele

Example 1
Input
arr = [1, 2, 3, 4, 5], k = 2
Output
[4, 5, 1, 2, 3]
Explanation

Right rotate by 2: last 2 elements [4,5] move to the front.

Example 2
Input
arr = [10, 20, 30, 40], k = 1
Output
[40, 10, 20, 30]
Explanation

Right rotate by 1: 40 moves to the front.

Example 3
Input
arr = [1, 2, 3], k = 6
Output
[1, 2, 3]
Explanation

k=6 is a multiple of 3 (array length), so the array returns to its original position.

Need a Hint?
Erwägen Sie die Verwendung von Array-spezifischen Datenstrukturen wie Sets oder Heaps.
Edge Cases to Watch
  • Leere Eingabestrukturen
  • Einzelelementeingaben
  • Große numerische Grenzen

Bereit zur Lösung?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Im Editor öffnen
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Empfohlene Python-Ressourcen

Erweitern Sie Ihr Wissen mit zugehörigen interaktiven Tutorials, Spickzetteln und Codevergleichen.