Question: In the provided MinDistance.java file, complete the distance method. For a given target value, the method returns the minimal distance between any two instances of
In the provided MinDistance.java file, complete the distance method. For a given target value, the method returns the minimal distance between any two instances of the target in the input array of numbers.
The distance between two numbers in the array is defined to be the number of array cells between them. Consider the following array of numbers
The distance between the two 5s in the array is 12. The distance between the first two 4s in the array is 0. The minimal distance between any two 3s is 1. The minimal distance between any two 7s is 4.
public static int distance(byte target, byte[] numbers){ /* purpose: determine the minimal distance between any * two instances of target in the numbers array * input : target is the number we are looking for * numbers is an array of numbers * output : the method returns
* o) the minimal distance between any two instances
* of target in the numbers array, if there are
* at least two instances of target present, or
* o) -2 if target is not in the numbers array, or
* o) -1 if target appears only once in the numbers array */
Examples: Let N denote the array shown above.returns
| 5 | 3 | 7 | 2 | 3 | 4 | 4 | 7 | 3 | -1 | 3 | 4 | 7 | 5 |
MinDistance.distance( (byte)5, N)
????? 12returns
MinDistance.distance( (byte)4, N)
????? 0returns
MinDistance.distance( (byte)7, N)
????? 4returns
MinDistance.distance( (byte)3, N)
????? 1returns
MinDistance.distance( (byte)2, N)
????? -1
returns
MinDistance.distance( (byte)13, N)
????? -2
1: Arrays In the provided MinDistance.java file, complete the distance method. For a given target value, the method returns the minimal distance between any two instances of the target in the input array of numbers The distance between two numbers in the array is defined to be the number of array cells between them. Consider the following array of numbers 537234 47 3 13475 The distance between the two 5's in the array is 12. The distance between the first two 4's in the array is 0. The minimal distance between any two 3's is 1. The minimal distance bet ween any two 7's is 4 public static int distance (byte target, byte [] numbers) /*purpose: determine the minimal distance between any two instances of target in the numbers array * input: target is the number we are looking for numbers is an array of numbers * output : the method returns o) the minimal distance between any two instances of target in the numbers array, if there are at least two instances of target present, or o) -2 if target is not in the numbers array, or o) -1 if target appears only once in the numbers array Examples: Let N denote the array shown above MinDistance.distance( (byte)5, N)12 MinDistance.distance (byte)4, o MinDistance.di stance ( (byte)7, N) MinDistance.distance( (byte)3, N) 1 MinDistance.distance (byte)2, 1 MinDistance.distance (byte)13, N) et2 returns returns returns ur ns
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts

