Java > Array-3 > squareUp (CodingBat Solution)

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


38 comments :

  1. Declare initially the first n and as only last element is 1 just declare it.
    now 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;
    }

    ReplyDelete
  2. I found a shorter solution:

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

    ReplyDelete
    Replies
    1. solutions like this makes my code look so dumb. It's just brilliant

      Delete
    2. I think I found a good solution

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

      Delete
  3. Here's my solution. Only one for loop!
    public 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;
    }

    ReplyDelete
  4. Check it pls)

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

    ReplyDelete
  5. It also possible to decrease index in loops:
    public 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;
    }

    ReplyDelete
  6. public int[] squareUp(int n) {
    int[]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;
    }

    ReplyDelete
  7. static int[] squareUp(int n) {
    int[] 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;
    }

    ReplyDelete
  8. shortest solution so far


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

    ReplyDelete
  9. public int[] squareUp(int n) {
    int[] 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?

    ReplyDelete
  10. public int[] squareUp(int n) {
    int[] 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;
    }

    ReplyDelete
  11. public int[] squareUp(int n) {
    int[] 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;
    }

    ReplyDelete
  12. Maybe is the clearest solution

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

    ReplyDelete
  13. public int[] squareUp(int n) {
    int [] 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;
    }

    ReplyDelete
  14. public int[] squareUp(int n) {

    int[] 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;
    }

    ReplyDelete
  15. If you increment i and decrement j, you can compare the two (if(j<=i)} and add 0 or j accordingly:
    public 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;
    }

    ReplyDelete
  16. 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) {
    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;
    }

    ReplyDelete
  17. public int[] squareUp(int n) {
    int[] 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;
    }

    ReplyDelete
  18. I found a short solution:
    public 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;
    }

    ReplyDelete
  19. public int[] squareUp(int n) {
    //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;
    }

    ReplyDelete
  20. public int[] squareUp(int n) {
    int[] arr = new int[n*n];

    for(int i=0; in-2) arr[n*i+j] = n-j;
    return arr;
    }

    ReplyDelete
  21. It is not posting the whole code. It is missing these two line above if statement
    for(int i=0; i<n; i++ )
    for(int j=0; j<n; j++ )

    ReplyDelete
    Replies
    1. it is totally messed up, sorry.It is not letting me to post all Here is the last two lines





      if(i+j>n-2) arr[n*i+j] = n-j;
      return arr;
      }

      Delete
  22. public int[] squareUp(int n) {
    int[] 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.

    ReplyDelete
  23. int[] newArray = new int[n * n];
    for (int i = 1; i <= n; i++) {
    for (int j = n * n - i; j >= n * (i - 1); j -= n) {
    newArray[j] = i;
    }
    }
    return newArray;

    ReplyDelete
  24. public int[] squareUp(int n) {
    int 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;
    }

    ReplyDelete
  25. def squareUp(n):
    L = []
    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

    ReplyDelete
  26. public int[] squareUp(int n) {
    int 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;
    }

    ReplyDelete
  27. public int[] squareUp(int n) {
    int[] 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;
    }

    ReplyDelete
  28. Somewhat simpler:

    int[] 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;

    ReplyDelete
  29. public int[] squareUp(int n) {
    int[] 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;
    }

    ReplyDelete
  30. public int[] squareUp(int n) {
    int[] 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;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact