Solved

squareUp  challenge

Posted on 2016-08-14
22
174 Views
Last Modified: 2016-08-23
Hi,

I am working on below challenge
http://codingbat.com/prob/p155405

Array-3 > squareUp
prev  |  next  |  chance
Given n>=0, create an array length n*n with the following pattern, shown here for n=3 : {0, 0, 1,    0, 2, 1,    3, 2, 1} (spaces added to show the 3 groups).

squareUp(3) → [0, 0, 1, 0, 2, 1, 3, 2, 1]
squareUp(2) → [0, 1, 2, 1]
squareUp(4) → [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1]
0
Comment
Question by:gudii9
  • 8
  • 6
  • 5
  • +1
22 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 41756424
Given n>=0, create an array length n*n with the following pattern, shown here for n=3 : {0, 0, 1,    0, 2, 1,    3, 2, 1} (spaces added to show the 3 groups).

squareUp(3) → [0, 0, 1, 0, 2, 1, 3, 2, 1]
squareUp(2) → [0, 1, 2, 1]
squareUp(4) → [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1]

i have not understood the challenge description.

How below are expected outputs?

squareUp(3) → [0, 0, 1, 0, 2, 1, 3, 2, 1]
squareUp(2) → [0, 1, 2, 1]
squareUp(4) → [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1]
0
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 250 total points
ID: 41756439
You shift the numbers into the array from the right in groups. Example for 5:

0,0,0,0,1
0,0,0,2,1
0,0,3,2,1
0,4,3,2,1
5,4,3,2,1
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41756501
As a warm-up, how would you make an array that contained the integers from  0  to  n² -1 ??
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 7

Author Comment

by:gudii9
ID: 41756527
You shift the numbers into the array from the right in groups. Example for 5:

0,0,0,0,1
0,0,0,2,1
0,0,3,2,1
0,4,3,2,1
5,4,3,2,1
this is clear now. let me think on it now
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41756564
OK ;)
0
 
LVL 7

Author Comment

by:gudii9
ID: 41760204
looks like i may need two loops for this also
0
 
LVL 7

Author Comment

by:gudii9
ID: 41760209
You shift the numbers into the array from the right in groups. Example for 5:
how to represent this push in code?
0
 
LVL 27

Assisted Solution

by:d-glitch
d-glitch earned 125 total points
ID: 41760380
You shift the numbers into the array from the right in groups. Example for 5:
0,0,0,0,1
0,0,0,2,1
0,0,3,2,1
0,4,3,2,1
5,4,3,2,1

You don't have to build the array by shifting.

What numbers are in the first row?
And the second?
Do you see the pattern?
And the pattern in the number of 0's?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41764758
not yet
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 125 total points
ID: 41764796
not yet

Wow, how about now with some extra spaces in certain spots...

0,0,0,0,     1
0,0,0,     2,1
0,0,     3,2,1
0,     4,3,2,1
     5,4,3,2,1
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41765353
"Wow" indeed.
Do you realize that printing the array in an n x n grid rather than an n² x 1 string is an enormous hint?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41765452
Do you realize that printing the array in an n x n grid rather than an n² x 1 is an enormous hint?
not able to realize. I just see some additional spaces.


0,0,0,0,1
0,0,0,2,1
0,0,3,2,1
0,4,3,2,1
5,4,3,2,1

compared to above below?

0,0,0,0,     1
0,0,0,     2,1
0,0,     3,2,1
0,     4,3,2,1
     5,4,3,2,1
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41765550
There are two ways to build an array with n² elements:

  for i= 0 to n² by 1

  for i= 0 to n by 1
     for j= 0 to n by 1

The second method might be useful if you can see the pattern in the grid.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41767259
 for i= 0 to n by 1
     for j= 0 to n by 1

The second method might be useful if you can see the pattern in the grid.

which grid? what is grid?
you mean below?
0,0,0,0,     1
0,0,0,     2,1
0,0,     3,2,1
0,     4,3,2,1
     5,4,3,2,1

to me above looks lik e2 D array not 1 Dimensional?
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41767319
Yes, you have to build a 1-D array.
Gerwin Jansen gave you an enormous hint by drawing it as an nxn matrix so you can see the pattern.
1
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41767330
The grid is the 2D array we've shown you to help understand the question. Your function should return a single string of numbers.

squareUp(2) → [0, 1, 2, 1]
0,1
2,1

squareUp(1) → [0, 1]
0
1
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41767502
I am sure this is not correct.  There can only be n*n elements.
squareUp(1) → [0, 1]

0
1

The answer is probably [1], but it may depend on your implementation.

And squareUp(0) → [ ]  because it must have 0*0 elements.
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41767626
@d-glitch - You are correct, 1*1=1 :D

squareUp(1) would have to result in [1] - independent of the implementation.

Each group is n wide starting with a 1 on the right side, this would result in [1]
0
 
LVL 7

Author Comment

by:gudii9
ID: 41767634
public int[] squareUp(int n) {
  
	    int[] finalRes = new int[n * n];
	    int indx = 0;
	 
	    for (int i = 1; i <= n; i++) {
	        for (int k = 1; k <= n - i; k++)
	        	finalRes[indx++] = 0;
	        for (int j = i; j > 0; j--) 
	        	finalRes[indx++] = j;
	    }
	    return finalRes;
	
}

Open in new window


above passes all tests.
Expected      Run            
squareUp(3) → [0, 0, 1, 0, 2, 1, 3, 2, 1]      [0, 0, 1, 0, 2, 1, 3, 2, 1]      OK      
squareUp(2) → [0, 1, 2, 1]      [0, 1, 2, 1]      OK      
squareUp(4) → [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1]      [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1]      OK      
squareUp(1) → [1]      [1]      OK      
squareUp(0) → []      []      OK      
squareUp(6) → [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 1, 0, 0, 0, 3, 2, 1, 0, 0, 4, 3, 2, 1, 0, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1]      [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 1, 0, 0, 0, 3, 2, 1, 0, 0, 4, 3, 2, 1, 0, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1]      OK      

All Correct

any improvements/refinements/alternate approaches?
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41767645
It is passing all tests, code looks OK to me.
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP/VB email question 4 46
Formula for calculating ROI on training 6 51
Java Eclipse Loop 3 21
WordPress: Debugging from my Windows 10 Desktop 6 40
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question