public class Recursively_find_two_elements_w_given_difference
{
public static void main(String[] args)
{
int[] A = {0, 1, 5, 8, 10, 12, 20, 30, 40, 60};
int i = 0;
int j = 1;
int diff = 20;
int result = findDiff(A, i, j, diff);
System.out.println(result);
}
/**
* @param arr sorted array
* @param i array index, type of int
* @param j array index, type of int
* @param difference given difference
* @return number of cases when given difference occurs in the array
*/
public static int findDiff(int[] arr, int i, int j, int difference)
{
if (j == arr.length)
{
return 0;
}
else
{
if (arr[j] - arr[i] < difference)
{
return findDiff(arr, i, j + 1, difference);
}
else if (arr[j] - arr[i] > difference)
{
return findDiff(arr, i + 1, j, difference);
}
else
{
return 1 + findDiff(arr, i + 1, j + 1, difference);
}
}
}
}
int[] b = {0 1 2 3 5 6 7 8 9 11 12 13 14 15 17 18 19};
int x = 0;
int y = 1;
int diff = 10;
int count = 0;
while (y < b.length)
{
if (b[y] - b[x] < diff)
{
y++;
}
else if (b[y] - b[x] > diff)
{
x++;
}
else
{
count++;
y++;
}
}
У меня тоже 15.10