Answered! I appreciate any help with the following Java problem, pleae only use Java format for the program….

I appreciate any help with the following Java problem, pleae only use Java format for the program.

Consider an array data of n numerical values in sorted order and a list of two numerical target values. Your goal is to compute the smallest range of array indices that contains both of the target values. If a target value is smaller than data[0], the range should start with a -1. If a target value is larger than data[n-1], the range should end with n.

For example, given the array

581013 15 20 22 26 72 62 52 41 31 21 18 05

The following table illustrates target values and their corresponding ranges:

— Devise and implement an algorithm that solves this problem —

Expert Answer

 Here is your answer: –

Indeces.java

import java.util.Scanner;

public class Indeces {
public static void main(String[] args) {
System.out.println(“Enter 8 numbers of the array in sorted order”);
Scanner sc = new Scanner(System.in);
Integer[] array = new Integer[8];
String line;
int in = 0;
while (in <= 7) {
line = sc.next();
array[in] = Integer.parseInt(line);
in++;
}
Integer[][] targets = new Integer[4][2];
System.out.println(“Enter the 4 target values separated by comma.”);
in = 0;
String word[];

while (in <= 3) {
line = sc.next();
word = line.split(“,”);
targets[in][0] = Integer.parseInt(word[0]);
targets[in][1] = Integer.parseInt(word[1]);
in++;
}
Integer[][] range = new Integer[4][2];
int lowerT, upperT, minRange, maxRange, j;
for (int i = 0; i < targets.length; i++) {
j = -1;
lowerT = targets[i][0];
upperT = targets[i][1];
while (j < array.length && array[j + 1] <= lowerT) {
j++;
}
minRange = j;
if (minRange == -1) {
j++;
}
while (j < array.length && array[j] < upperT) {
j++;
}
maxRange = j;

range[i][0] = minRange;
range[i][1] = maxRange;
}

String[][] table = { { “Target Values”, “Smallest Range of Indices” },
{ targets[0][0] + “, ” + targets[0][1], “[” + range[0][0] + “, ” + range[0][1] + “]” },
{ targets[1][0] + “, ” + targets[1][1], “[” + range[1][0] + “, ” + range[1][1] + “]” },
{ targets[2][0] + “, ” + targets[2][1], “[” + range[2][0] + “, ” + range[2][1] + “]” },
{ targets[3][0] + “, ” + targets[3][1], “[” + range[3][0] + “, ” + range[3][1] + “]” } };

printTable(table);
}

public static void printTable(String data[][]) {
int col = data[0].length;

int maxWidth[] = new int[col];

for (String[] rowD : data)
for (int i = 0; i < col; i++) {
if (maxWidth[i] < rowD[i].length())
maxWidth[i] = rowD[i].length();
}

String format = “”;

for (int x : maxWidth)
format += “%-” + (x + 2) + “s “;

format += “%n”;

for (String[] rowD : data) {
System.out.printf(format, rowD);
}
}
}

Sample output: –

Enter 8 numbers of the array in sorted order
5
8
10
13
15
20
22
26
Enter the 4 target values separated by comma.
2,8
9,14
12,21
14,30
Target Values Smallest Range of Indices
2, 8 [-1, 1]
9, 14 [1, 4]
12, 21 [2, 6]
14, 30 [3, 8]

Still stressed from student homework?
Get quality assistance from academic writers!