PuzzlersWorld.com

  • Hacker Puzzle
  • Interview Puzzles
  • Number Puzzles
  • Maths Puzzles

Rotate an array by k elements

(5 votes, average: 3.60 out of 5)

November 18, 2012 by puzzler Leave a Comment

Given an array of n elements, write an algorithm to rotate it right by k element without using any other array. In other words rotate an array in place.
you can have temp variable to swap two elements.
Ex:-
Initial Array: A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Rotating array A right by 3 elements should result in
A = {8, 9, 10, 1, 2, 3, 4, 5, 6, 7}
Hint: There is a trick, think before doing much of the programming 🙂
See Solution: Rotate an array by k elementsHide Solution

So the trick is to reverse the whole array first then reverse the array from 0 to k-1 and k to n-1.
For example
Array A after reversing whole array: {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
Array A after reversing 0 to 2 elements: {8, 9, 10, 7, 6, 5, 4, 3, 2, 1}
Array A after reversing 3 to 9 elements: {8, 9, 10, 1, 2, 3, 4, 5, 6, 7}
Code:

void rotateRight(int[] a, int k){
	int length = a.length();//in C/C++ it will be given as input
	reverse(a, 0, length -1);//reverse whole array
	reverse(a, 0, k-1);//assuming 0 < k < length
	reverse(1, k, n-1);
}
void reverse(int[] a, int start, int end){
	while(start < end){
		swap(a, start, end);
		start++;
		end--;
	}
}
void swap(int[] a, int pos1, int pos2){
	int temp = a[pos1];
	a[pos1]= a[pos2];
	a[pos2] = temp;
}
//Note: in interviews you might be asked to handle negative scenarios
// like array out of bound etc.
  • Share on Whatsapp
  • Share on Facebook
  • Share on Twitter
Facebook Comments
Next Puzzle
Objective Questions set C/C++ – Part 6

Checkout more Interview Questions Tags: Array, Difficult, Interview, Solved Puzzles

Leave a Comment Cancel reply

Submit your Puzzle

You may also like

  • Tic-Tac-Toe-Tomek Solution: Google codejam 2013 Qual Round
  • 3 SUM problem
  • Subjective Question set C/C++ – Part 1
  • Reverse a link list without using another
  • Fair and Square Solution: Google codejam 2013 Qual Round
  • Find the Min solution: Facebook hacker Cup 2013 Qual Round
  • Deceitful War Solution – Google Code Jam 2014
  • Objective Questions set C/C++ – Part 6
  • Objective Questions set C/C++ – Part 3
  • Bullseye Solution: Google codejam 2013 Round 1A

Categories

  • Aive hi Puzzles
  • Akbar and Birbal
  • Alphabetical Puzzles
  • Bollywood Puzzles
  • Google Code jam
  • Hindi Riddles
  • Interview Puzzles
  • Interview Questions
  • Logical Puzzles
  • Malayalam Puzzles
  • Maths Puzzles
  • Miscellaneous
  • Number Puzzles
  • Picture Puzzles
  • Riddles
  • Tamil Puzzles
  • Technical

Social

  • View puzzlersworld’s profile on Twitter
privacy policy

Copyright © 2025 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in

  • Hacker Puzzle
  • Logo Puzzles
  • Optical Illusions
  • WhatsApp Puzzles
  • Picture Puzzles
  • Riddles
    â–¼
    • Hindi Riddles
  • Bollywood Puzzles
  • Alphabetical Puzzles
  • Aive hi Puzzles
  • Interview Puzzles
  • Logical Puzzles
  • Interview Questions
    â–²
    • Data Structures
    • Binary Tree
    • Algorithms
    • Recursion Questions
    • Amazon Interview Questions
    • Snapdeal Interview Questions
    • Google Code jam
  • Technical
  • Akbar and Birbal
  • Number Puzzles
  • Maths Puzzles
  • Miscellaneous