Avatar of gudii9
gudii9
Flag for United States of America asked on

squareUp challenge

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]
JavaJava EEProgramming Languages-OtherProgrammingProgramming Theory

Avatar of undefined
Last Comment
Gerwin Jansen

8/22/2022 - Mon
gudii9

ASKER
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]
ASKER CERTIFIED SOLUTION
Gerwin Jansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
d-glitch

As a warm-up, how would you make an array that contained the integers from  0  to  n² -1 ??
gudii9

ASKER
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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Gerwin Jansen

OK ;)
gudii9

ASKER
looks like i may need two loops for this also
gudii9

ASKER
You shift the numbers into the array from the right in groups. Example for 5:
how to represent this push in code?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
d-glitch

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gudii9

ASKER
not yet
SOLUTION
mccarl

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
d-glitch

"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?
gudii9

ASKER
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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
d-glitch

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.
gudii9

ASKER
 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?
d-glitch

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Gerwin Jansen

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
d-glitch

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.
Gerwin Jansen

@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]
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
gudii9

ASKER
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?
Gerwin Jansen

It is passing all tests, code looks OK to me.