With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.
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]
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]
You shift the numbers into the array from the right in groups. Example for 5:this is clear now. let me think on it now
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 shift the numbers into the array from the right in groups. Example for 5:how to represent this push in code?
not yet
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.
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,0,0,0, 1
0,0,0, 2,1
0,0, 3,2,1
0, 4,3,2,1
5,4,3,2,1
squareUp(1) → [0, 1]
0
1
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;
}
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
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.