1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
aalyn [17]
3 years ago
6

Construct sequence using a specified number of integers within a range. The sequence must be strictly increasing at first and th

en strictly decreasing. The goal is to maximize the sequence array elements starting from the beginning. For example, [4, 5, 4, 3, 2] beats [3,4,5,4,3] because its first element is larger, and [4, 5, 6, 5, 4] beats [4,5,4,3,2] because its third element is larger. Given the length of the sequence and the range of integers, return the winning sequence. If it is not possible to construct such a sequence, return -1. Write an algorithm that returns a winning sequence and -1 if the sequence is not possible. Input The input to the function/method consists of three arguments: num : an integer representing the size of sequence to create lower End : an integer representing the lower end of integer range upperEnd : an integer representing the upper end of integer range.
Computers and Technology
1 answer:
natulia [17]3 years ago
7 0

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;

       }

   }

}

You might be interested in
A coworker asks your opinion about how to minimize ActiveX attacks while she browses the Internet using Internet Explorer. The c
uysha [10]

Answer:

Security Tab is the correct answer.

Explanation:

The Security Tab will enable the users to authorize anyone to use that file or directory.Many clients deleted the security tab because After that nobody can alter the security privileges on the device.

  • With the help of the Security tab, we will minimize the ActiveX attacks on Internet Explorer. This option is available in the Internet Options of the dialog box.
  • Other options are incorrect because they are not related to a given scenario.
8 0
3 years ago
What are the three main desktop operating systems used today
Norma-Jean [14]

Answer:The three most common operating systems for personal computers are Microsoft Windows, macOS, and Linux.

Explanation:

3 0
2 years ago
Que coño son los resistores?
iragen [17]

Answer:

puede hacer su pregunta un poco más clara por favor.

Explanation:puede hacer su pregunta un poco más clara por favor.

4 0
4 years ago
Which of the following best describes your sequence of actions when developing a web page?
Anettt [7]

Answer:

I think D

Explanation:

5 0
2 years ago
Mha ship what yo fave
Diano4ka-milaya [45]

Answer:

umm the first one I think..? lol

4 0
3 years ago
Read 2 more answers
Other questions:
  • TRUE/FALSE: In order to use an object in a program, its class must be defined.
    10·1 answer
  • Structured walk-throughs take place throughout the SDLC and are called _____ reviews, depending on the phase in which they occur
    9·1 answer
  • Within the Differentiated Services (DiffServ) architecture, how many classes of service (CoS) are there?
    7·1 answer
  • What is one expectation of open-source software?
    15·1 answer
  • While Joe's wife Molly and daughter Sunny did not mind moving,his son Daniel was not too happy with the sudden change in their l
    12·1 answer
  • Its an HTML5 anyone kwons
    15·1 answer
  • Which format has the largest file size?
    10·1 answer
  • A series of instructions coded in a language to instruct the computer to perform a task is
    5·1 answer
  • A numeric literal that is written with a decimal point is called a ________. real number floating-point value decimal literal do
    6·1 answer
  • A ____ risk assessment evaluates threats to and vulnerabilities of the network.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!