Python Nozioni di baseFacile

Rotazione dell'array

Guida dettagliata e implementazione Python per il problema della "rotazione degli array".

Dichiarazione del problema

Facile

Scrivi una funzione rotate_array(arr, k) che ruota l'array arr a sinistra di k posizioni e restituisce l'array ruotato. Gli elementi che vanno oltre l'inizio si avvolgono fino alla fine. Ad esempio, ruotando [1,2,3,4,5] a sinistra di 2 si ottiene [3,4,5,1,2].

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

Esempi

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

Rotate left by 2: first 2 elements [1,2] move to the end.

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

Rotate left by 1: 10 moves to the end.

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

Rotating by the array length returns the original array.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche degli array come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'editor
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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.