Rotating a 2D Array by 90 Degrees (Java)

Implement a function that takes a square 2D array (# columns = # rows = n) and rotates it by 90 degrees. Do not create a separate 2D array for the rotation, it rotates in the 2D array.


int a1[][] = {{1, 2, 3},
              {4, 5, 6},
              {7, 8, 9}};
 // rotate(a1, 3) should return:
 // [[7, 4, 1],
 // [8, 5, 2],
 // [9, 6, 3]]


  int a2[][] = {{1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
                {13, 14, 15, 16}};
 // rotate(a2, 4) should return:
 // [[13, 9, 5, 1],
 // [14, 10, 6, 2],
 // [15, 11, 7, 3],
 // [16, 12, 8, 4]]

To understand this problem, we need to analyze what is really going on in the rotation.

a. each element in the rotation is changing position with 3 other elements in the 2D array: a->b, b->c, c->d, d->a

 [?, a, ?, ?]  
 [?, ?, ?, b]
 [d, ?, ?, ?]
 [?, ?, c, ?]

If a={i,j}, then b={j,n-1-i}, c={n-1-i, n-1-j},d={n-1-j,i}

b. In each rotation happens in a “ring” (example, a1, a2, a2, b1, b2 ….d2, d2), we only swap the positions of n-2 elements in the first row. We do not swape n-1th element again because it is a duplicate (We swapped a1 with b1, and b1 does not need to be swapped again. So we only swap a1, a2, a3. ).

 [a1, a2, a3, b1]  
 [d3, ??, ??, b2]
 [d2, ??, ??, b3]
 [d1, c3, c2, c1]

So the outer loop should be for (int i=0; i < n-1; i++):

c. We swap the positions from the outside ring (1’s) to the inner rings (2’s). Each time the iteration decrements by 2 elements. At ith row, we swap from ith element to n-2-i th elements.

 [1, 1, 1, 1]  
 [1, 2, 2, 1]
 [1, 2, 2, 1]
 [1, 1, 1, 1]

So the inner loop is for (int j=i; j<n-1-i; j++)

Put everything together:

 public static int[][] rotate(int[][] a, int n) {

 for (int i=0; i< n-1; i++){
     for (int j=i; j<n-1-i; j++){
         int temp = a[i][j];
         a[i][j] = a[n-1-j][i];
         a[n-1-j][i] = a[n-1-i][n-1-j];
         a[n-1-i][n-1-j] = a[j][n-1-i];
         a[j][n-1-i] = temp;
     return a;


PLAY Pong Plus


Basic features:

  • Health Bars: Each time player fails to reflect a ball, the health bar decreases in length. If the health reaches zero, the game is over.
  • POW bars: It allows the player to use Power Ups, such as Bubble Shields and Rainbow Shots. Each use of the Power Ups consumes a certain amount of POWs.
  • Slippery Paddles: Paddles do not stop immediately if the player stops pressing UP/Down buttons. The paddles decelerate and cushion in. This feature makes the paddles harder to control and challenges players to plan the moves ahead.
  • The ball: The ball will shoot from the center with a random color, size, speed, and angle.
  • Sound: added sound effects and music (Enjoy it!).



Advanced features:

  • Bubble Shield: Players can consume POWs to generate a bubble shield. It reflects the balls back.
  • Rainbow Shot: Players can consume POWs to activate the paddles into rainbow mode. Any ball hits a paddle with rainbow mode will be reflected and move at a doubled speed.
  • Reflection: Balls will bounce back from each other.
  • Exchange of Momentum: If a rainbow ball hits a normal ball, they will exchange the momentum. Meaning the normal ball will become a rainbow ball and vice versa.
  • Particles: A particle system is built in this game. If a ball hits the left or right boundary, it will explode and generate a particle cluster, like fireworks.


Music License:

ロッコ by RoccoW is licensed under a Attribution-ShareAlike License.
Based on a work at
Permissions beyond the scope of this license may be available contact artist via email.

Prime number algorithm

1 st method (very inefficient):

get value for an number n
if n =2, output “It is a prime number”
set the value i =2;
set the value isPrime = True;
if i if n % i == 0, isPrime=False;
else i + 1;
output “It is a prime number” if isPrime is true
output “It is a not prime number” if isPrime is False.

The above method is inefficient. If the input is a very big prime number, it will make division from 2 all the way to n-1.

2nd method (slightly faster):

If :
an integer x < another integer n,
√n % x != 0 (n is not divisible by x )
then: n² % x !=0 (n² is not divisible by x)

Vise versa, n % x == 0, then n² % x ==0 ;

So, we can implement the 1st method, by looping only from 2 to n. we just have to modify the above algorithm “i<n” into “i<√n”. it optimized (n ) algorithm into (√n) algorithm.

3rd method (Sieve of Eratosthene):
This method works when given a number n and get all the prime numbers that is smaller than n. It filters out all the composite numbers, and left the prime number.

It works in this way:

  1. given a number n,
  2. in the range of 2 to n, find the prime number one by one.
    • the first prime number is 2, then filter out all the numbers that is multiple of 2.
    • find the next prime number, which is 3, and filter out all the multiples of 3.
    • find the next prime number 5, and filter out all the multiples of 5.
    • continue the above process. until the searched prime number reaches n.
  3. output all the remaining numbers.

The pseudo code would be:

  1. given a number n, n>1
  2. define a list IsPrime[], indexed from 2 to n, initially all items are set to True
  3. i start from 2, if i < n, repeat the following steps:

if IsPrime[i] == true:

set IsPrime[ i2, i2+i, i2+2i, i2+3i, …] to False (not exceeding n)

      4.output the indices in IsPrime[] ‘s item that is true.

Run the Script in CodeSkulptor


Simple Automation for Maya Rendering

Maya Batch Render doesn’t allow user to render multiple shots with one click. What if I want to let the computer create a render queue and automate the rendering process? You may install backburner to solve this. or there is another simple way of doing so without setting up the Manager and Server.

First of all, create a .bat file from Windows desktop.

The Template is like this:

C:\Progra~1\Autodesk\Maya2014\bin\render -r file -s <starting_frame> -e <ending_frame> -cam <camera> -rd “<render_location>” “<maya_file_directory>”

here under are the possible settings that you can modify

-r file: render from scene setting.

-s : starting frame

-e: ending frame

-cam: camera Name

-rd: where you want to save rendered image / where is the maya file.

For more info for rendering command, refer to this link.