Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and exploring the core foundations for app libraries.

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]

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

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.

how the 001 021 321 getting printed. if its squareUp(3). it changes for squareUp(2) and squareUp(4).

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial```
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;
}
```

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 ?

```
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;
}
}
```

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

Follow ?

```
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).

```
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;
}
```

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. ;)

arrays

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.