Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int[] solve(int n, int k, int[] h, int[][] queries) {
- // N = no. heights
- // K = Difference
- // L & R = First and Last fighters
- int[] results = new int[queries.length];
- int count = 1;
- int pairs = 0;
- int l, r =0;
- for(int j=0; j<queries.length; j++) {
- l = queries[j][0];
- r = queries[j][1];
- int[] range = Arrays.copyOfRange(h, l, r+1);
- int[] sortedSubset = sort(range);
- for(int i=0; i<sortedSubset.length-1; i++) {
- count = i+1;
- while((count != sortedSubset.length) && sortedSubset[count] <= (sortedSubset[i]+k)) {
- // While the current number is still in range of the number we are checking i.e. (number + k)
- pairs++;
- count++;
- }
- }
- results[j] = pairs;
- pairs = 0;
- }
- return results;
- }
Add Comment
Please, Sign In to add comment