DhanushNehru / Hacktoberfest2024

Hacktoberfest 2024 OPEN FIRST Pull Request - SUPPORT OPEN SOURCE - Don't forget to spread love and if you like give us a ⭐️
84 stars 520 forks source link

Add rotate_array.cpp: Rotate array by k steps #436

Closed milesmoralis2411 closed 2 weeks ago

milesmoralis2411 commented 2 weeks ago

This C++ program rotates a given array to the right by k steps. It uses an efficient algorithm that involves reversing the entire array and then reversing the first k elements and the remaining elements separately. This approach ensures a time complexity of O(n) and an in-place rotation without the need for additional arrays. The program includes a sample input to demonstrate the functionality.

Example Usage: Input: arr = [1, 2, 3, 4, 5, 6, 7], k = 3 Output: Rotated array: [5, 6, 7, 1, 2, 3, 4]