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

This obviously is incorrect and it something I wrote but I want to know if using

ID: 3638899 • Letter: T

Question

This obviously is incorrect and it something I wrote but I want to know if using what I already have is there a way to check for duplicates and display them this is the problem: (Same-number subsequence) Write an O(n) program that prompts users to enter a sequence of integers ending with 0's and finds the longest subsequence with the same number. For example lets say I entered 122220 it starts at index 1 and has four values of 2

import java.util.Collections;
import java.util.Scanner;
public class Longest_Same_SubSequence {
    public static void main(String[] args){
       
        Scanner scan = new Scanner(System.in);
       
       
        System.out.println("Enter a series of numbers ending with 0");
        int n = scan.nextInt();
       
        java.util.List<Integer> list = new java.util.ArrayList<Integer>(n);
        list.add(n);
        int e = list.get(list.size()-1);
       
       
       
        for(int i = list.indexOf(0); i<e; i++){
            list.listIterator(list.indexOf(i));
          
            System.out.println("" + i);
           
        }
       
       
    }
}

Explanation / Answer

Since you are passing in multiple numbers, you cannot use int. Either one int is entered at a time, or you enter all numbers at once as a String. To keep O(n), you must do the latter (otherwise it's O(2n)). Once the numbers are passed in as a string, that string can be parsed. I did limited error checking, ie I did not make sure everything typed in is a number (123B0 should not be allowed), but since that was not a requirement, I figured would be okay. If there is a tie (ie, 112230), the first sequence is used. import java.util.Collections; import java.util.Scanner; public class Longest_Same_SubSequence { public static void main(String[] args){ Scanner scan = new Scanner(System.in); String intList = null; boolean valid = false; while (!valid) { System.out.println("Enter a series of numbers ending with 0"); intList = scan.next(); valid = intList.charAt(intList.length() - 1) == '0'; if (!valid) { System.out.println("Number list must end in 0. Please try again. "); } } int startingMaxIndex = 0; int currentStartingIndex = 0; int maxLength = 0; int currentLength = 0; int numberUsed = Integer.parseInt(intList.substring(0,1)); for (int x = 0; x maxLength) { numberUsed = number; maxLength = currentLength; startingMaxIndex = currentStartingIndex; } } else { currentLength = 1; currentStartingIndex = x; } } System.out.println("Number Used In Longest Sequence: " + numberUsed); System.out.println("Sequence Length: " + maxLength); System.out.println("Sequence Starting Index: "+startingMaxIndex); } }
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