Search code examples
javaarraysalgorithmserverclient

The longest sequence problem on server to client programming


//Hey all the algorithm I created is working the way I designed it but I want to add and new functionality to it.

If you input for example the following six numbers: 5 2 12 4 3 9 You will get back: Length of longest Consecutive Sequence = 4 Longest Consecutive Sequence values are: [2, 3, 4, 5, 9, 12]

What I want it to print out is:

If you input for example the following six numbers: 5 2 12 4 3 9 You will get back: Length of longest Consecutive Sequence = 4 Longest Consecutive Sequence values are: [2, 3, 4, 5]

Where it only includes the numbers of the longest sequence. Any help or tips will be appreciated, promise I won't be posting for a while after this.

                String ws = is.readUTF();
                String[] wordSequence = ws.split(" ");

                // decide the response

                int size = wordSequence.length;
                int[] arr = new int[size];
                for (int i = 0; i < size; i++) {
                    arr[i] = Integer.parseInt(wordSequence[i]);
                }
                int n = arr.length;
                HashSet<Integer> a = new HashSet<>();
                int ans = 0;
                for (int i = 0; i < n; ++i) {
                    a.add(arr[i]);
                }

                // check each possible sequence from the start
                // then update optimal length                
                for (int i = 0; i < n; ++i) {

                    // if current element is the starting
                    // element of a sequence
                    if (!a.contains(arr[i] - 1)) {

                        // Then check for next elements in the
                        // sequence
                        int j = arr[i];
                        while (a.contains(j)) {
                            j++;
                        }

                        // update optimal length if this length
                        // is more
                        if (ans < j - arr[i]) {
                            ans = j - arr[i];
                        }
                    }
                }


                System.out.println("The largest consecutive subsequence is = " + ans);
                os.write(ans + "\n");
                os.flush();
                System.out.println("Displaying the subsequence " + a.toString());
                String hashtag = a.toString();
                os.write(hashtag + "\n" );
                os.flush();

Solution

  • If you want to print the values of the longest sequence, just remember the start value as well as the current longest.

    int ans = 0;
    int start = 0; // add
    
    if (ans < j - arr[i]) {
       ans = j - arr[i];
       start = arr[i]; // add
    }
    

    Then something like:

    System.out.println(ans);
    for(int i=0; i<ans-1; i++)
        System.out.format("%d, ", start+i);
    System.out.println(start+ans-1);
    

    Output:

    4
    2, 3, 4, 5