This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Rotate left (arrays)  RSS feed

 
Bod MacNeil
Ranch Hand
Posts: 62
2
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the challenge:




Given an array of ints length 3, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.

rotateLeft3([1, 2, 3]) → [2, 3, 1]
rotateLeft3([5, 11, 9]) → [11, 9, 5]
rotateLeft3([7, 0, 0]) → [0, 0, 7]


My code works, but I feel there could be an easier way to do this?  Or is this fine?  If it was a bigger array I would probably need to use a loop?

 
Stephan van Hulst
Saloon Keeper
Posts: 7808
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, a loop would be better, but if you're allowed to use collections, you can do this extremely easily by converting an array to a Queue, and then removing the first element and then adding it back to the end of the queue. At the end, you can convert the queue back to an array.

Two good Queue implementations you can use for this are ArrayDeque and LinkedList.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You can also very easily accomplish it with a temporary variable, and a call to the System.arraycopy() method.

Henry
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bod MacNeil wrote:
My code works, but I feel there could be an easier way to do this?  Or is this fine?  If it was a bigger array I would probably need to use a loop?


You could actually do this in place, with just one temporary int variable as the additional space requirement. The way you're doing it and the other ones suggested so far require 2N space for an array with N elements. Basically, you note down what the first element is, then shift all other elements down one spot in the array using a loop, then put the first element's value in the last spot.
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, there are pros and cons to an in-place solution. The pro is that you minimize your memory requirements, needing only the temporary variable to remember what the original first element value was.  The potential con is that you are mutating the array.  This may or may not be a good thing depending on how that array is used by the rest of the program.  As an exercise, it doesn't matter much unless the exercise goal is to find a memory-efficient solution, a time-efficient solution, or a solution that will work in a multi-threaded context. If there's concurrency involved, it's generally better to work with data that is immutable.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also please tell us where that code comes from, to void copyright problems and so we can easily find it. It looks rather like something out of coding bat.
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys.  Yes its from CodingBat and I will link it next time as I'll probably be posting more stuff from there.  I tried to use  LInkedList but I'll need to practice with them more as I can't convert my array to it and then back.  I'll try something ese that was mentioned in the mean time.  cheers!
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which exercise number was it? I looked on coding bat earlier and couldn't find that question.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Yes, a loop would be better, but if you're allowed to use collections, you can do this extremely easily by converting an array to a Queue, and then removing the first element and then adding it back to the end of the queue. At the end, you can convert the queue back to an array.

@Bod: Good advice. However, if you want to do it yourself and make it generic (which I suspect may be the object of the exercise) have a look at the % operator, and think about how it might help you.
(Hint: rotateLeft(n) is usually the same as rotateRight(length - n), providing 0 < n < length).

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!