Arrays Left Rotation

Last evening I read the article “Programmable Solid-State Storage in Future Cloud Datacenters” by Jaeyound Do, Sudipta Sengupta and Steven Swanson which appeared in volume 62 number 6 edition of Communications of the ACM. This morning I read it once again. As you might already know I am passionate about storage and have been working on it for a while. The article deal with SSD drives and how they can help improve performance by providing compute resources close to the data.

When it all started we had magnetic drums which were the first extension of magnetic core memory. The next iteration was with magnetic disks also known as hard disk drives. Then SSD drives showed up. An SDD is basically some memory and associated logic to mimic the operation of spinning disks. At least that was the initial intent. Size and speed were the target goals.

The article provides an introduction as to how SSD units with present and future modifications will be able to process data in order to reduce data network traffic. This will be accomplished allowing users / developers write software for the SDD.  For example, a database residing in a SDD could issue a query that could be processed by the SDD and return the results to the computer instead of having the data be processed by CPUs on servers. If you like storage and the possibilities of providing CPU and GPU power to SDD units, then I strongly recommend reading the article.

Now let’s get to the subject at hand. This post deals with a HackerRank challenge named Arrays: Left Rotation. As I mentioned in my previous post, JAVAAID posted a video on YouTube named “Arrays Left Rotation HackerRank Solution”. I decided to generate my own solution and compare it with the one shown in the video.

The requirements for the challenge are simple. You need to rotate an array left as specified by the parameters described in the challenge. The requirements seem to be clear.

Following are some test cases I used to develop and test the Java code before submitting my solution:

5 10
1 2 3 4 5

1 2 3 4 5


5 1
1 2 3 4 5

2 3 4 5 1


5 4
1 2 3 4 5

5 1 2 3 4


5 5
1 2 3 4 5

1 2 3 4 5


5 6
1 2 3 4 5

2 3 4 5 1


5 7
1 2 3 4 5

3 4 5 1 2


20 10
41 73 89 7 10 1 59 58 84 77 77 97 58 1 86 58 26 10 86 51

77 97 58 1 86 58 26 10 86 51 41 73 89 7 10 1 59 58 84 77


15 13
33 47 70 37 8 53 13 93 71 72 51 100 60 87 97

87 97 33 47 70 37 8 53 13 93 71 72 51 100 60

The heart of the function follows:

    // Complete the rotLeft function below.
    static int[] rotLeft(int[] a, int d) {

    	// **** return original array (if needed) ****
    	if ((d % a.length) == 0)
    		return a;
    	    	
    	// **** instantiate rotate array to return ****
    	int[] b = new int[a.length];
      	
    	// **** rotate the array ****
    	for (int i = 0, j = d % a.length; i < a.length; i++) { b[i] = a[j]; if (++j >= a.length)
    			j = 0;
    	}
     	
    	// **** return rotated array ****
    	return b;
    }

We first check if the number of rotations will return the same array. If so we are done.

Next we instantiate an array to return with the values rotated.

The following loop is used to rotate the array by copying the values from the initial array to the array to be returned.

If you are interested my complete solution may be found in my GitHub repository.

For my next post I am debating between DynamoDB and a next set of Java questions. Will decide and hopefully have a post ready around 07:00 AM tomorrow.

If you have comments or questions regarding this or any other post in this blog, or if you would like me to help with any phase in the SDLC (Software Development Life Cycle) of a product or service, please do not hesitate and leave me a note below. Requests for help will remain private.

Keep on reading and experimenting. It is the best way to learn and refresh your knowledge!

John

Follow me on Twitter:  @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.