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.  
  13. int diff = 0;
  14. int count = 0;
  15. int arr[] = {1,2,3,1,2,1,1};
  16. Arrays.sort(arr);
  17. int k =5;
  18. int n = arr.length;
  19. for(int i = 0,j=0; j<n;j++){
  20. diff = arr[j] - arr[i];
  21. while(diff > k){
  22. diff -= arr[i];
  23. i++;
  24. }
  25. count+= (j-i+1);
  26. }
  27.  
  28. System.out.println(count - n); // For the pairs where i==j
  29.  
  30. }
  31. }
Success #stdin #stdout 0.09s 52504KB
stdin
Standard input is empty
stdout
21