Link to home
Start Free TrialLog in
Avatar of gnanagowthaman sankar
gnanagowthaman sankar

asked on

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]
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Are you going to tell us what particular part of the question you don't understand?
You have to return an array which has n sets of n, arranged in n down to 1, left-to-right, order.
Avatar of gnanagowthaman sankar
gnanagowthaman sankar

ASKER

how the 001 021 321 getting printed.  if its squareUp(3). it changes for squareUp(2) and squareUp(4).
I don't understand your last comment.
squareUp(3) means 001 021 321 how ? we want to use 2 loops or 1 loop.
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forget programming. Describe in words what pattern you see, between n and what numbers are shown
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.
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.
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
thanks for your help sir @CEHJ
How are you progressing on this one gnanagowthaman sankar ?

Hope you haven't given up.
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.
Which source code did you use? Can you provide a link or post the code here?
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;
}

Open in new window

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?
yes sir
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 ?
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.
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;
  }
}

Open in new window

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

Follow ?
Thank you sir. Thanks a lot. Your notes and codes are helpful thank u sir.
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;
    
  }

Open in new window


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

Open in new window

Super sir
:)

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. ;)
It's a kind of respect to the teachers sir.