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]
2 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]2 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
X = 0
cricket20 [7]

The missing line would be x+= num

This means that every time a number is selected from the range it is added to the value of x.

3 0
2 years ago
Alex's woodworking shop is trying to design a web page with Cascading Style Sheets (CSS). Alex would like create the new design
guajiro [1.7K]

Answer:

1. C) Embedded Style

2. C) User Agent Style

Explanation:

1. Alex will use Embedded style to create styles that apply only to the HTML document that the style was created. With Embedded styling; the rules can be embedded into the HTML document using the <style> element.

2. Since Alex has forgotten to give any style for all of his pages, the style that will be applied to his pages is User Agent Style. User Agent Style is the default style of a browser. The browser has a basic style sheet that gives a default style to any document and this style is called User Agent.

8 0
2 years ago
What is a motherboard​
aleksandr82 [10.1K]

Answer:

A motherboard is the main printed circuit board in general-purpose computers and other expandable systems. It holds and allows communication between many of the crucial electronic components of a system

Explanation:

8 0
2 years ago
The term “frivolous” implies that the author
tiny-mole [99]
Thank you for posting your question here at brainly. I hope the answer will help you. Feel free to ask more questions.
believes that the average individual does not need a smart phone 
<span>firivolous mean not serious, light, unimportant.</span>

3 0
3 years ago
The numeric keys on a keyboard or calculator are referred to as a:
Anna [14]

Numeric Keypad is the answer

6 0
3 years ago
Other questions:
  • Modern database tools support ________________, which entails separating data from the programs that manipulate data.
    14·1 answer
  • When seeking information on the internet about a variety of subjects, the most useful place to look would be
    8·2 answers
  • A security policy is a
    11·1 answer
  • The function below takes a string argument sentence. Complete the function to return the second word of the sentence. You can as
    5·1 answer
  • PLEASE help me RIGHT NOW!!!!!!!!! I will give a Brainly to anyone who helps me!
    15·1 answer
  • On a piano, a key has a frequency, say f0. Each higher key (black or white) has a frequency of f0 * r^n, where n is the distance
    10·1 answer
  • Someone please tell me please y my ps4 controller won’t charge:(
    12·2 answers
  • The lower band and upper band of integer data type​
    6·1 answer
  • Can somebody PLEASE help me cancel my subscription I have tried so many different things and contacted support - they are no hel
    14·1 answer
  • How do you get lugia in pokemon alpha saphire
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!