Advertisement
Guest User

proximity.java

a guest
Dec 4th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. public class proximity {
  4.  
  5. public static void main(String[] args) throws IOException {
  6. Scanner in = new Scanner(new File("proximity.in"));
  7. PrintWriter out = new PrintWriter(new FileWriter("proximity.out"));
  8.  
  9. int n = in.nextInt();
  10. int k = in.nextInt();
  11. int[] breeds = new int[n];
  12. for(int i=0; i<n; i++) {
  13. breeds[i] = in.nextInt();
  14. }
  15.  
  16. ArrayList<Integer> breedIDs = new ArrayList<Integer>();
  17.  
  18. // 1. Set up window
  19. ArrayList<Integer> window = new ArrayList<Integer>(k);
  20. for(int i=0; i<=k; i++) {
  21. if(window.contains(breeds[i])) breedIDs.add(breeds[i]);
  22. window.add(breeds[i]);
  23. }
  24.  
  25. // 2. Move window
  26. int fj = 0;
  27.  
  28. while(fj+k+1 < n) {
  29. window.remove(0);
  30. fj++;
  31. if(window.contains(breeds[fj+k])) breedIDs.add(breeds[fj+k]);
  32. window.add(breeds[fj+k]);
  33. }
  34.  
  35. int result = 0;
  36. for(int i=0; i<breedIDs.size(); i++) {
  37. if(breedIDs.get(i) > result) {
  38. result = breedIDs.get(i);
  39. }
  40. }
  41.  
  42. System.out.print(result);
  43. out.print(result);
  44.  
  45.  
  46. in.close();
  47. out.close();
  48.  
  49. }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement