eniallator

Bitonic Checker | Java

Nov 15th, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. public boolean checkBitonic(int[] inArray) {
  2.     if (inArray.length < 3 || inArray[0] > inArray[1])
  3.         return false;
  4.  
  5.     boolean decreasing = false;
  6.  
  7.     for (int i = 1; i < inArray.length - 1; i++) {
  8.         if (inArray[i] == inArray[i + 1] || decreasing && inArray[i] < inArray[i + 1])
  9.             return false;
  10.         else if (!decreasing)
  11.             decreasing = inArray[i] > inArray[i + 1];
  12.     }
  13.  
  14.     return decreasing;
  15. }
  16.  
  17. System.out.println("Should be true: " + checkBitonic(new int[]{1, 2, 1}));
  18. System.out.println("Should be true: " + checkBitonic(new int[]{1, 5, 7, 9, 8, 2}));
  19. System.out.println("Should be true: " + checkBitonic(new int[]{3, 4, 5, 1}));
  20.  
  21. System.out.println("Should be false: " + checkBitonic(new int[]{}));
  22. System.out.println("Should be false: " + checkBitonic(new int[]{1, 2}));
  23. System.out.println("Should be false: " + checkBitonic(new int[]{2, 1}));
  24. System.out.println("Should be false: " + checkBitonic(new int[]{1, 3, 5, 7}));
  25. System.out.println("Should be false: " + checkBitonic(new int[]{7, 5, 3, 1}));
  26. System.out.println("Should be false: " + checkBitonic(new int[]{1, 5, 5, 2}));
  27. System.out.println("Should be false: " + checkBitonic(new int[]{5, 2, 1, 3}));
Advertisement
Add Comment
Please, Sign In to add comment