Guest User

Untitled

a guest
Dec 11th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. static int[] solve(int n, int k, int[] h, int[][] queries) {
  2. // N = no. heights
  3. // K = Difference
  4. // L & R = First and Last fighters
  5. int[] results = new int[queries.length];
  6. int count = 1;
  7. int pairs = 0;
  8. int l, r =0;
  9.  
  10. for(int j=0; j<queries.length; j++) {
  11. l = queries[j][0];
  12. r = queries[j][1];
  13. int[] range = Arrays.copyOfRange(h, l, r+1);
  14.  
  15. int[] sortedSubset = sort(range);
  16.  
  17. for(int i=0; i<sortedSubset.length-1; i++) {
  18. count = i+1;
  19. while((count != sortedSubset.length) && sortedSubset[count] <= (sortedSubset[i]+k)) {
  20.  
  21. // While the current number is still in range of the number we are checking i.e. (number + k)
  22. pairs++;
  23. count++;
  24. }
  25. }
  26. results[j] = pairs;
  27. pairs = 0;
  28. }
  29.  
  30. return results;
  31. }
Add Comment
Please, Sign In to add comment