Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that finds an prints to the screen all of the increasing sequenc

ID: 3672778 • Letter: W

Question

Write a program that finds an prints to the screen all of the increasing sequences of numbers of maximumal length from a given array. The shortest printed sequence should have length 2. For example, if the array contains the following values then the output of the program should be Your program should generate an array of 15 pseudo-random integers, display the array and then print the sequences should be printed one per line with a single space separating each pair of numbers. If your array is called list, then prints the arrays as shown in the above example. NOTE: this program is short, but requires some algorithm development to come up with a proper way of determining the increasing sequences. Call your file: IncreasingSequence.java.

Explanation / Answer

import java.util.ArrayList;
import java.util.Scanner;

public class HelloWorld {

   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
      
       String integersString = scanner.nextLine();
       String[] numbers = integersString.split(" ");
      
       ArrayList<String> increasingSequence = new ArrayList<String>();
       ArrayList<String> currentSequence = new ArrayList<String>();
      
       increasingSequence.add(numbers[0]);
       currentSequence.add(numbers[0]);
       System.out.printf("%s ", numbers[0]);
      
       for (int i = 1; i < numbers.length; i++) {
               int currentNum = Integer.parseInt(numbers[i]);
               int prevNum = Integer.parseInt(numbers[i - 1]);
              
               if (currentNum <= prevNum) {
                  
                   currentSequence.clear();
                   System.out.println();
               }
              
               currentSequence.add(numbers[i]);
               System.out.printf("%s ", numbers[i]);
              
               if (currentSequence.size() > increasingSequence.size()) {
                  
                   increasingSequence = new ArrayList<String>(currentSequence);      
               }
       }
      
       System.out.printf(" Longest: ");
       for (String s : increasingSequence) {
          
           System.out.printf("%s ", s);
       }
   }

}

output

-7 3 0 99 120 50 13 0 1 5 56 77 3 1 5                                                                                                                       
-7 3                                                                                                                                                        
0 99 120                                                                                                                                                    
50                                                                                                                                                          
13                                                                                                                                                          
0 1 5 56 77                                                                                                                                                 
3                                                                                                                                                           
1 5                                                                                                                                                         
Longest: 0 1 5 56 77                                                                                                                                

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote