fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. int[] towns = new int[]{1 , 5 , 7 , 8 , 9 , 11 , 12};
  13. Arrays.sort(towns);
  14. int count = 0;
  15. int d = 3;
  16. int i = towns.length-1;
  17. int j = towns.length-2;
  18. while(!(j==0 && towns[i]-towns[j] < d)){
  19. System.out.printf("%d - %d = %d\n",towns[i],towns[j],towns[i]-towns[j]);
  20. if(towns[i]-towns[j] == d) count++;
  21. if(towns[i]-towns[j] < d) j--;
  22. else i--;
  23. }
  24.  
  25. System.out.println(count);
  26. }
  27. }
Success #stdin #stdout 0.05s 4386816KB
stdin
Standard input is empty
stdout
12 - 11 = 1
12 - 9 = 3
11 - 9 = 2
11 - 8 = 3
9 - 8 = 1
9 - 7 = 2
9 - 5 = 4
8 - 5 = 3
7 - 5 = 2
7 - 1 = 6
5 - 1 = 4
3