In: Computer Science
Software Decode: Write a function that accepts an in-order array of unsigned integer values. The function shall then scan the array for a specific pattern: Three values contained within the array equally spaced 20 units apart. The function shall return the index position within the original array where the pattern begins or -1 if not present.
Given the input array: data[] = {10,20,31,40,55,60,65525} The function shall return: 1
IN JAVA PLEASE
public class TwentUnits {
public static void main(String[] args) {
int data[] = { 10, 20, 3, 40, 55,
60, 65525 };
System.out.println(findIt(data));
}
private static int findIt(int[] arr) {
// using nested loops to iterate
all elements
for (int i = 0; i < arr.length -
2; i++) {
int sum =
0;
// iterating 3
set of elemtns
for (int j = i;
j < i + 2; j++) {
// finding the difference
sum += Math.abs(arr[j] - arr[j + 1]);
}
// checking if
it equals 20 than return i
if (sum ==
20)
return i;
}
return -1;
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me