1

Initial array: 2 23 34 27 89 14 26 30 60

k = 3

starting from index i = 1 we have to shift k elements so that they occur after the element 26 (i.e. after givenIndex = 6) in the array.

Final array: 2 89 14 26 23 34 27 30 60

We are NOT allowed to use extra space.

My approach:

count = 0;   
while(count < k)  
{  
  count++;  
  temp = arr[i];  
  shift all elements from (i+1) to givenIndex to their immediate left position;  
  arr[givenIndex] = temp;  
}

First iteration:
temp = 23
shift all elements from [i+1](i.e. index=2) to givenIndex(i.e. index=6) to left one by one
array after shifting: 2 34 27 89 14 26 26 30 60
arr[givenIndex] = temp
array after applying this operation: 2 34 27 89 14 26 23 30 60

Similarly
array after second iteration: 2 27 89 14 26 23 34 30 60
array after third iteration: 2 89 14 26 23 34 27 30 60

worst case complexity O(n*k) where n is the no. of elemnts in the array.

Can we solve the problem in O(n)

9
  • 3
    Wait... what? How did the shifting take place? Can you please explain a bit more. It's not clear much. From my point of view, I see some numbers swapped. Commented Aug 7, 2012 at 6:24
  • 2
    Do we have to preserve the ordering or not? Commented Aug 7, 2012 at 6:26
  • @MatinKh I have edited thequestion. I hope it is clear now. Commented Aug 7, 2012 at 7:01
  • 1
    We are allowed to use a temporary varaible. But we aren't allowed to use space proportional to the number of elements being swapped. i.e. in the example mentioned in my question we can't use an extra space of O(k) where k is the number of elements we have to shift. Commented Aug 7, 2012 at 7:26
  • 1
    As there is 'k' rotation, it would be o(n * k). right? Or am I still missing something? Commented Aug 7, 2012 at 7:32

3 Answers 3

1

This rotation can be done in linear time using a reverse() helper function. Assuming that reverse(x, y) reverses array[x]..array[y] in place.

reverse(1, 3); // 27 34 23 .. .. ..
reverse(4, 6); // .. .. .. 26 14 89
reverse(1, 6); // 89 14 26 23 34 27

Writing a linear-time reverse is simple and might even be available in your favorite language library (for example, C++ includes std::rotate and std::reverse).

Sign up to request clarification or add additional context in comments.

1 Comment

I don't think it improves the time-complexity.
0

as far as i can tell from your code(and the example), I think you are trying to put the block [i,i+k-1] after the block [i+k,givenIndex]

If that is the case, @Blastfurnace 's suggestion does lead to one solution in O(n) and no extra space:

simply do:

reverse(i,givenIndex)
reverse(i,givenIndex-k)
reverse(givenIndex-k+1,givenIndex)

and you have what you want :)

Comments

0

The question is quite amazing
See your requested answer here. If you are looking for a way to rotate your arraylist using JAVA, there is a complete solution for you here.
public static void rotate(List list, int distance)

Simply rotate your collection (ArrayList or LinkedList ...)
Its parameters are a list (in your case arraylist) and a distance (in your case k). Do you see the similarities? Simply use this.

2 Comments

That Java method uses the 3 reverse algorithm for large lists. The description even mentions Jon Bentley's Programming Pearls where I first learned the algorithm.
Sure it does. So? To find out the solution he has to learn the algorithm too. But if he is using java, he can simply take advantage of the rotation which is using that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.