Answer:
Explanation:
The following code is written in Java. It first creates a function that takes in the three parameters to create the sequence, if the sequence is valid it returns the created sequence, otherwise it prints -1 and returns an empty array. The second function called compareSequences takes in to sequences as parameters and compares them, printing out the winner. A test case has been provided in the main method and the output can be seen in the image below.
import java.util.Arrays;
class Brainly {
public static void main(String[] args) {
int[] seq1 = createSequence(5, 4, 2);
int[] seq2 = createSequence(5, 3, 3);
System.out.println("Winner: " + Arrays.toString(compareSequences(seq1, seq2)));
}
public static int[] createSequence(int num, int upperEnd, int lowerEnd) {
int[] myArray = new int[num];
int middle = (int) Math.floor(num/2.0);
myArray[0] = upperEnd;
myArray[num-1] = lowerEnd;
int currentValue = upperEnd;
for (int i = 1; i < num-1; i++) {
if (i <= middle) {
myArray[i] = currentValue + 1;
currentValue += 1;
} else {
myArray[i] = currentValue - 1;
currentValue += 1;
}
}
System.out.println(Arrays.toString(myArray));
if (myArray[num-2] < lowerEnd) {
System.out.println("-1");
int[] empty = {};
return empty;
} else {
return myArray;
}
}
public static int[] compareSequences(int[] seq1, int[] seq2) {
int lowestLength;
if (seq1.length > seq2.length) {
lowestLength = seq2.length;
} else {
lowestLength = seq1.length;
}
for (int i = 0; i < lowestLength; i++) {
if (seq1[i] > seq2[i]) {
return seq1;
} else if (seq1[i] < seq2[i]) {
return seq2;
}
}
if (seq1.length > seq2.length) {
return seq1;
} else {
return seq2;
}
}
}