Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1.  
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6. public static int keraaLuvut(int[] luvut) {
  7. int kierros = 1;
  8. boolean[] pieru = new boolean[luvut.length + 1];
  9. pieru[luvut[0]] = true;
  10. for (int k = 1; k < luvut.length; k++) {
  11. if (!pieru[luvut[k] - 1]) {
  12. kierros++;
  13. }
  14. pieru[luvut[k]] = true;
  15. }
  16. return kierros;
  17. }
  18.  
  19. public static void main(String[] args) {
  20. System.out.println(keraaLuvut(new int[]{1, 2, 3, 4, 5}));
  21. System.out.println(keraaLuvut(new int[]{5, 1, 2, 3, 4}));
  22. System.out.println(keraaLuvut(new int[]{5, 4, 3, 2, 1}));
  23. System.out.println(keraaLuvut(new int[]{1, 5, 2, 4, 3}));
  24. }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement