Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.62 KB | None | 0 0
  1. public static boolean sim(int[] a, int i) {
  2.  
  3.         // check if I reached the middle of the array
  4.         if (i == a.length / 2 || i + 1 == a.length) {
  5.            
  6.             // if array's length is even, need to check the two mid cells
  7.             if (a.length % 2 == 0)     
  8.                 return a[i] == a[i - 1] ? true : false;
  9.            
  10.             // if odd, there's only one cell in the middle, So I don't need to check anything.
  11.             return true;
  12.        
  13.         } else {
  14.            
  15.             return a[i] == a[a.length - i - 1] ? true && sim(a, i + 1) : false && sim(a, i + 1);
  16.         }
  17.  
  18.     }
  19.  
  20.     public static void main(String[] args) {
  21.         int[] a = { 1, 2, 4, 2, 1 };
  22.         System.out.println(sim(a, 0));
  23.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement