Skip to content

Instantly share code, notes, and snippets.

@ajaynitt
Created October 8, 2014 19:10
Show Gist options
  • Save ajaynitt/a3c7bf6c7037a9d18e06 to your computer and use it in GitHub Desktop.
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)
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
@ajaynitt
Copy link
Author

ajaynitt commented Oct 8, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment