Problem:
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}
Solution:
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; }
Declare initially the first n and as only last element is 1 just declare it.
ReplyDeletenow add the elements from previous into the present sequence in inner loop .
while exiting the outer loop just change the element to be changed to required.
public int[] squareUp(int n) {
int len=n*n;
int[] result=new int[len];
if(n==0){
return result;
}
result[n-1]=1;
for(int i=1;i<=n-1;i++){
for(int j=0;j<=n-1;j++){
result[n*i+j]=result[n*(i-1)+j];
}
result[(n*i)+(n-i-1)]=i+1;
}
return result;
}
I found a shorter solution:
ReplyDeletepublic int[] squareUp(int n) {
int nums[] = new int[n*n];
int a = n;
for(int i = 0; i < n; i++) {
int pos = n*n - i - 1;
for(int j = 0; j < a; j++) {
nums[pos -n*j] = i+1;
}
a--;
}
return nums;
}
genius!
Deletesolutions like this makes my code look so dumb. It's just brilliant
DeleteI think I found a good solution
Deletepublic int[] squareUp(int n) {
int[] array = new int[n*n];
int control=n;
int counter=1;
int fill=1;
for(int i=array.length-1;i>=0;i--){
array[i]=counter;
if(counter>control){
array[i]=0;
}
if(counter==n){
counter=0;
control--;
}
counter++;
}
return array;
}
Here's my solution. Only one for loop!
ReplyDeletepublic int[] squareUp(int n) {
int[] arr = new int[n*n];
if(n==0) return arr;
for(int i = n-1,j=1;i<arr.length;){
arr[i] = j;
j--;
if(j==0&&i<arr.length-1){
j=((i+1)/n)+1;
i+=n-((i+1)/n);
}else{
i++;
}
}
return arr;
}
Great solutions guys, keep it up!
ReplyDeleteCheck it pls)
ReplyDeletepublic int[] squareUp(int n) {
int[] nums = new int[n*n];
for(int z = 1;z <= n;z++) {
for(int j = 1,i = n*z - 1;j <= z;j++) {
nums[i--] = j;
}
}
return nums;
}
It is working. Great!!
DeleteIt also possible to decrease index in loops:
ReplyDeletepublic int[] squareUp(int n) {
int[] tab = new int[n*n];
int ile = 0;
for (int i = n-1; i >= 0; i--)
{
int counter = 1;
for (int j = n-1; j >= ile; j--)
{
tab[i*n + j] = counter++;
}
ile++;
}
return tab;
}
public int[] squareUp(int n) {
ReplyDeleteint[]a=new int[n*n];
for(int i=1 ; i<=n ; ++i)
for (int j=1 ; j<=i ; ++j)
a[i*n-j]=j;
return a;
}
Bro, you're pure class !!
Deletea[i*n-j]=j;
DeleteHow to deduce it
static int[] squareUp(int n) {
ReplyDeleteint[] arr = new int[n*n];
int j =0;
for(int i=n*n-1; i>=0; i-=n){
for(int k=0; k<n-j;k++){
arr[i-k]=k+1;
}
j++;
}
return arr;
}
shortest solution so far
ReplyDeletepublic int[] squareUp(int n) {
int num[] = new int[n*n] ;
for( int i= 0; i< n*n ; i ++){
if((i/n)+1 >= n- (i%n)) num[i]=n-(i%n);
}
return num;
}
public int[] squareUp(int n) {
ReplyDeleteint[] ats=new int[n*n];
for (int i=1; i<=n ; i++){
for (int j=n; j>=0+i; j--){
ats[n*j-i]=i;
}
}
return ats;
}
I'm quite new to Java. Is this solution any good?
public int[] squareUp(int n) {
ReplyDeleteint[] arr = new int[n*n];
if(n == 0)
return arr;
for(int i = n - 1; i < arr.length; i += n) {
for(int j = i; j >= i - i / n; j--)
arr[j] = i - j + 1;
}
return arr;
}
public int[] squareUp(int n) {
ReplyDeleteint[] array = new int[n * n];
int limit = 0;
int counter = 0;
for (int i = 0; i < array.length; i++) {
if (i % n == 0) {
limit++;
counter = n;
}
if (counter > limit)
array[i] = 0;
else
array[i] = counter;
counter--;
}
return array;
}
Maybe is the clearest solution
ReplyDeletepublic int[] squareUp(int n) {
int[] nums = new int [n * n];
int index = 1 ;
for(int i=1;i<=n;i++){
for(int j=index ; j<=n; j++){
nums[j*n-i] = i;
}
index++;
}
return nums;
}
public int[] squareUp(int n) {
ReplyDeleteint [] a = new int [n*n];
int s = 0;
for(int i = 1; i <= n; i++) {
int num = 0;
for(int j = 0; j < n; j++) {
if(n - j <= i) {
num = n - j;
}
a[s++] = num;
}
}
return a;
}
public int[] squareUp(int n) {
ReplyDeleteint[] result = new int[n*n];
int limit = n;
int count = 1;
if(n==1) {
result[0]=1;
return result;
}
for(int i=result.length-1, j=1; i>=0; i--){
if(j<=limit){
result[i] = j;
j++;
} else {
result[i] = 0;
}
if(count == n){
count = 0;
limit = limit-1;
j=1;
}
count++;
}
return result;
}
If you increment i and decrement j, you can compare the two (if(j<=i)} and add 0 or j accordingly:
ReplyDeletepublic int[] squareUp(int n) {
int[] data = new int[n * n];
int idx = 0;
for (int i = 1; i <= n; i++) {
for (int j = n; j >= 1; j--) {
if (j <= i) {
data[idx] = j;
} else {
data[idx] = 0;
}
idx++;
}
}
return data;
}
public int[] squareUp(int n) {
ReplyDeleteint[] arr = new int[n*n];
for(int i=1;i<n+1;i++)
for(int k=1;k<i+1;k++)
arr[i*n-k]=k;
return arr;
}public int[] squareUp(int n) {
int[] arr = new int[n*n];
for(int i=1;i<n+1;i++)
for(int k=1;k<i+1;k++)
arr[i*n-k]=k;
return arr;
}
public int[] squareUp(int n) {
ReplyDeleteint[] a = new int[n*n];
int k=a.length-1;
for(int i=n; i>0;i--){
int count=0;
for(int j=1;j<=i;j++){
a[k] = j;
k--;
count++;
}
if(count<n){
int s = n-count;
k=k-s;
}
}
return a;
}
I found a short solution:
ReplyDeletepublic int[] squareUp(int n) {
int[] array = new int[n*n];
int size = array.length;
for (int i = 0; i < array.length; i++) {
for (int x = 1; x <= n-i; x++) {
array[size-x] = x;
}
size -= n;
}
return array;
}
public int[] squareUp(int n) {
ReplyDelete//Interesting pattern//
int size = n;
int [] square = new int[n * n];
for (int i = square.length - 1; i >= size - 1 && i != -1; i -= size)
{
int pos = i;
for (int j = 0; j < n; j++)
{
square[pos] = j + 1;
pos--;
}
n--;
if (n < size - 1)
{
square[pos] = 0;
}
}
return square;
}
public int[] squareUp(int n) {
ReplyDeleteint[] arr = new int[n*n];
for(int i=0; in-2) arr[n*i+j] = n-j;
return arr;
}
It is not posting the whole code. It is missing these two line above if statement
ReplyDeletefor(int i=0; i<n; i++ )
for(int j=0; j<n; j++ )
it is totally messed up, sorry.It is not letting me to post all Here is the last two lines
Deleteif(i+j>n-2) arr[n*i+j] = n-j;
return arr;
}
public int[] squareUp(int n) {
ReplyDeleteint[] arr = new int[n*n];
for(int i = 0; i<n; i++) {
int m = 0;
int x = 0;
for(int p = i*n; p<(i+1)*n; p++) {
if(m<n-i-1) {
arr[p] = 0;
m++;
}
else {
arr[p] = i+1-x;
x++;
}
}
}
return arr;
}
My idea is to split the arr into n groups. For each group, count the numbers of 0 (n-i-1), assign 0 to those first arr[p]s, and use m to keep track of how many zeros I have assigned. Then assign i+1-x to arr[p]s at the back of the group and increment x after each round to create decreasing order.
int[] newArray = new int[n * n];
ReplyDeletefor (int i = 1; i <= n; i++) {
for (int j = n * n - i; j >= n * (i - 1); j -= n) {
newArray[j] = i;
}
}
return newArray;
public int[] squareUp(int n) {
ReplyDeleteint a = n;
int[] array = new int[n * n];
int i = array.length - 1;
while (i >= 0) {
for (int j = 1; j <= a; j++) {
if (i >= 0) {
array[i] = j;
i--;
}
}
i=i-(n-a);
a--;
}
return array;
}
def squareUp(n):
ReplyDeleteL = []
c = 0
for i in range(n*n):
if i%n == 0:
c += 1
for j in range(n, 0, -1):
if j > c:
L.append(0)
else:
L.append(j)
return L
public int[] squareUp(int n) {
ReplyDeleteint result[] = new int[n*n];
int len = result.length;
int k = len-1;
for (int i=0; i<n; i++, k=k-n) {
for (int j=1; j<=n-i; j++) { //For 1, 2, 3
result[k-j+1] = j;
}
}
return result;
}
public int[] squareUp(int n) {
ReplyDeleteint[] ans = new int[n * n];
for(int i = 0; i < n; i++){
int count = 1;
for(int j = n * n - (i * n) - 1; j > n * n - (i * n) - 1 - n + i; j--){
ans[j] = count;
count++;
}
}
return ans;
}
Somewhat simpler:
ReplyDeleteint[] arr = new int[n * n];
for(int i = 0; i < n; i++)
for(int j = 1; j <= i + 1; j++)
arr[(i + 1) * n - j] = j;
return arr;
public int[] squareUp(int n) {
ReplyDeleteint[] s=new int[n*n]; int i=0; int j=n-1; int a=1;
if(n==0) return s;
while(i<n){
while(j<s.length){
s[j]=a;
j=j+n;
}
a++;
j=a*n-a;
i++;
}
return s;
}
public int[] squareUp(int n) {
ReplyDeleteint[] arr=new int[n*n];
int x=0;
if(arr.length==1) arr[0]=1;
for(int i=arr.length-1;i>0;i-=n){
int pos=i;
for(int j=1;j<=n-x;j++){
arr[pos] =j;
pos--;
}
x++;
}
return arr;
}