Absolute Permutation

UPDATE:  The technical issue that prevented me from posting a solution was promptly resolved by HackerRank. I received a message indicating all was well to proceed.

It is Sunday and a sunny day in the Twin Cities of Minneapolis and St. Paul. I tried solving the Absolute Permutation challenge from Hacker Rank but ran into an issue. Will comment on it and show my solution which at this time has not been accepted.

Today is Mothers Day. If you are a mom hope you enjoy the day with your family. My wife and I have two sons. One lives in Indiana, and the other in Minnesota. The one in Minnesota stopped by yesterday afternoon with flowers and his two kids. Earlier this morning our son and wife called to wish my wife a nice day.

Yesterday morning something happened with the Internet at home. I wake up early every day and go down to my office to study for two hours. Access to the internet was intermittent. I powered cycled the modem and router a few times. They are connected to a power strip so I can power cycle both when needed. In general it seems that I have to do it about once a month.

We called Spectrum (our Internet provider) and the technician told us that our signal was quite low. In addition the bandwidth of our modem was quite small. In the past two years there have been several improvements. We ended up with an appointment between 3 and 4 PM. At 03:00 PM sharp, three vans showed up at home. They looked at the modem and ran some tests. They opened a box in the utility room and disconnected all the lines that we have in the house. We have all rooms wired for internet and cable. Today we simply use WiFi for all our devices including watching TV. The crew left around 04:00 PM. Since then we have not have a single issue. The technicians did a great job!

OK, enough chit chat. I chose Java 8 to solve the challenge and used Eclipse. When I felt I had something working, I copied it to HackerRank and clicked on the <Run Code> button. All seemed well at that time.

Following is the edited screen capture of the IDE console showing my tests:

3
2 1
3 0
3 2

2 1
1 2 3
-1


1
4 2

3 4 1 2


1
2 1 

2 1


1
3 0

1 2 3


1
3 2

-1


1
10 5

6 7 8 9 10 1 2 3 4 5


1
10 1

2 1 4 3 6 5 8 7 10 9

1
7 5

-1


10
2 1
10 5
7 5
2 1
2 0
2 0
1 0
10 5
10 0
6 0

2 1
6 7 8 9 10 1 2 3 4 5
-1
2 1
1 2
1 2
1
6 7 8 9 10 1 2 3 4 5
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6

I then submitted the solution by clicking on the <Submit Code> button. The screen indicated that the tests had started. There are 13 tests for this challenge. After a couple minutes all 13 tests were still spinning. Obviously something was going on.

I restarted the test, erased my code, edited the code, and after each attempt I clicked on <Run Code> followed by <Submit Code>. The test cases worked but the actual set 13 tests will spin and not complete.

After 15 or so minutes, I went to the discussion page and looked for a solution in Java from fellow developers. I copied and ran the base tests. They succeeded. When I tried submitting the code, the 13 tests cases just kept on spinning. It seems it was not my code but something with the site. Sent a message and received the following replied:

HackerRank Community <support@hackerrankcommunity.zendesk.com>
8:28 AM (1 hour ago)
to me

##- Please type your reply above this line -##
Your request (161424) has been received and is being reviewed by our support team.

To add additional comments, reply to this email.

	
John Canessa

May 12, 06:28 MST

Posted some code.
Passed test cases.
When submitting 13 test cases spin, and spin, and spin.

Copied Java code from the discussions.
When submitting 13 test cases spin, and spin, and spin.

Any ideas?

John

My solution follows:

	/*
	 * Complete the absolutePermutation function below.
	 *                n!
	 * C(n, k) = -----------
	 *           k! (n - k)!
	 */
    static int[] absolutePermutation(int n, int k) {

//    	// ???? ????
//    	System.out.println("<<< n: " + n + " k: " + k);
    	
    	// **** ****
        if((k != 0) && (n % (2 * k) != 0)) {
        	
//        	// ???? ????
//        	System.out.println("<<< (n % (2 * k): " + n % (2 * k));
        	
        	// ***** ****
            return new int[] {-1};
        }
    	
        // **** ****
        int[] absPerm = new int[n];
        
        // **** traverse values ****
        for(int i = 0; i < n; i++) {
        	
            if((k != 0) && ((i / k) % 2 == 0))
                absPerm[i] = (i + 1) + k;
            else
                absPerm[i] = (i + 1) - k;
            
//            // ???? ????
//            dumpPerm(absPerm);
        }
        
        // **** ****
        return absPerm;
    }

I decided to traverse the digits in the initial permutation. There are three checks. The first two address the |pos[i] – i| = k as specified by the requirement. The last check determines if there is no solution.

I experimented with a hash map but after I ran into the issue testing, cleared it out and went with the base approach.

I will try my solution tomorrow morning. My wife is up and we will spend time grilling for lunch. Not sure what we have in store for the afternoon. I know we have plans to talk with some friends over Skype this evening. We are planning a trip to Italy early this fall.

My entire solution including the modified scaffolding for testing can be found in my GitHub repository.

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!

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.