# what is the logic behind this question can anyone explain me please

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]
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Are you going to tell us what particular part of the question you don't understand?
1
Commented:
You have to return an array which has n sets of n, arranged in n down to 1, left-to-right, order.
1
Author Commented:
how the 001 021 321 getting printed.  if its squareUp(3). it changes for squareUp(2) and squareUp(4).
0
Commented:
I don't understand your last comment.
0
Author Commented:
squareUp(3) means 001 021 321 how ? we want to use 2 loops or 1 loop.
0
Commented:
squareUp

I think the challenge title is somewhat misleading. The "square" idea here is that there are n*n array elements, not that the array can be split into a square matrix like :

[]1  []2
[]3

. . . because that square graphic (if that's what YOU mean), is not what they are asking for here.
1

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Forget programming. Describe in words what pattern you see, between n and what numbers are shown
0
Author Commented:
N =3 means we want to make array of 9 length so for that we are multiplying 3*3.  For 9 elements. we want to start  from 1 in reverse order.index[0-2] 001 for index[3-5] 021 and finally for index[6-9] 321.  My question is how to do it. Using one loop or two loop or making new array. How to iterate through 9 elements and value for each 3 sets reverse and decrement.
0
Commented:
You have answered your own question on explaining the logic. So now the next stage is to apply that analysis to a solution and its algorithm.
1
Commented:
I'll give you a clue. It's counter-intuitive that it's one dimensional so think in terms of rows. How do find the row in a 1d array? You can then use that value
0
Author Commented:
thanks for your help sir @CEHJ
0
Commented:
How are you progressing on this one gnanagowthaman sankar ?

Hope you haven't given up.
0
Author Commented:
Yes completed. But referring internet source code sorry sir. I tried my best but can't able to finish it sir sorry for that. Krakatoa.
0
Commented:
Which source code did you use? Can you provide a link or post the code here?
0
Author Commented:
``````public int[] squareUp(int n) {
int[] result = new int[n * n];
int x = n-1, pass = 1, index = 0;
if(n == 0) { return result; }
for(int i = n-1; i < result.length; i+=n) {
index = i;
for(int k = 1; k <= pass; k++) {
if(k == 0) { break; }
result[index] = k;
index--;
}
pass++;
}
return result;
}
``````
0
Commented:
I will take a closer look later, but variable k starts at 1, so why is it being tested to equal 0 do you know?
0
Author Commented:
yes sir
0
Commented:
I tried my best but can't able to finish it sir sorry for that.

So you could not do it yourself? What was the point in asking the question here, if you then gave up plus having taken the code from another source? Is this not really a waste of your time and a waste of ours ?  What about your other question to which I did give you an answer - what do you intend doing about that ?
0
Author Commented:
I finished others source code by mine. This one I can't get it. Am ashamed of my self. Sorry for doing others code and finishing my task. I won't repeat it sir.
0
Commented:
Here is my solution. Is it easier to understand?
``````import java.util.*;
public class A {
public static void main(String arg[]){
System.out.println(Arrays.toString(squareUp(2)));
System.out.println(Arrays.toString(squareUp(3)));
System.out.println(Arrays.toString(squareUp(4)));
}
public static int[] squareUp(int n) {
int[] result = new int[n * n]; // at this point each element has a value of zero
for(int i = n * n - 1 ; i > 0;) { // work backwards through the array
for(int j = n; j > 0; j--){ // loop through n elements in each of the n segments
for(int k = 1; k <= j; k++){
result[i] = k;	// assign values to j elements in each segment
i--; // increment for each assignment
}
i -= n - j; // skip through the remaining elements in each segment
}
}
return result;
}
}
``````
0
Commented:
The key to this challenge in my view is to determine the right index in the array for EACH n-th element.

One way to do this is to take the modulus of n*n using n, starting at n and continuing for all n*n.

In the case of squareUp(3) for example, this would give you key indices of 3, 6 and 9. For squareUp(4), the indexes would be : 4,8,12 and 16.

At that stage, if you keep track of how MANY times you find mod == 0 for n, then you know how many other index positions in that n-length "segment" of the array that need to be filled, and that can be done by incrementing the tracker variable, and applying it backwards along the array.

(When you find the modulus as explained above, you'll have to subtract 1 from it to conform to the normal indexing convention. Then you also subtract the tracker value so as to count back toward the beginning of the array).

0
Author Commented:
Thank you sir. Thanks a lot. Your notes and codes are helpful thank u sir.
0
Commented:
So based on what I have said in my previous post, this would be the sort of code that I would favour :

``````public static int[] squareUp(int n) {

if(n==0){return new int[0];}

int[] iA = new int[n*n];

int set = 0; // this signifies the "n-th segments or 'sets' if you like, in the array, which co-determines the number of index positions needed to be filled in it.

int starter = 1; // this will always be 1, each time a new 'set' is encountered.

for(int a=n;a<(n*n)+1;a++){

if(a%n==0){ // obtain your (first and subsequent) modulus index position. Start at a = n, because you are not interested in anything before that first mod.

set++;

for(int t = 0;t<set;t++){

iA[(set*n)-1-t]=starter; // -t, because you want to use the fact that t is incremented, to work toward the front of the array.
starter++;
}

}
starter = 1;  //set starter back to 1 again, ready for the next set / segment of the array, obtained via the mod method.
}

return iA;

}
``````

The advantage of the "modulus approach" is that you do not need to iterate the entire array just in order to find the entry point for the value insertion.
(You should know that modulus arithmetic is somewhat slower than other operators, and so another algorithm could outperform this easily).
0
Commented:
Actually, having said all that I have (which works btw), there is a far simpler algorithm for this :

``````public int[] squareUp(int n) {

int[] iA = new int[n*n];

for(int a=1;a<n+1;a++){

for(int t = 1;t<a+1;t++){

iA[(a*n)-t]=t;

}

}
return iA;
}
``````
0
Author Commented:
Super sir
0
Commented:
:)

So is that any clearer now ?

PS - I like the 'sir', but as I haven't been knighted yet, you can leave it out if you like. ;)
0
Author Commented:
It's a kind of respect to the teachers sir.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
arrays

From novice to tech pro — start learning today.