Created
October 8, 2014 19:10
-
-
Save ajaynitt/a3c7bf6c7037a9d18e06 to your computer and use it in GitHub Desktop.
Array Rotation :Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements.(left)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Input arr[] = [1, 2, 3, 4, 5, 6, 7], | |
d = 2, n =7 | |
Ans:arr[] = [3, 4, 5, 6, 7, 1, 2] | |
solution: (Ar Br)r | |
steps: | |
first reverse d elements | |
then reverse remaining (n-d) elements | |
now reverse entire elements. | |
Example: | |
d=3,total elements=10 | |
1 2 3 4 5 6 7 8 9 10 | |
=> 3 2 1 4 5 6 7 8 9 10 | |
=> 3 2 1 10 9 8 7 6 5 4 | |
=> 4 5 6 7 8 9 10 1 2 3 | |
Done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://www.geeksforgeeks.org/array-rotation/