Skip to content
Advertisement

find max of an input array

EDIT :: I was assuming there would be 3 array. The problem was not asking for that.

I am trying to write a program from the platform Jutge.org that reads sequences of integer numbers and prints the maximum value of each sequence.

Input consists in the input of sequences. Each sequence begins with its number of elements n > 0, followed by n integer numbers. Given the numbers and the length of the sequence [] (it will be introduced before the sequence of numbers) the input/output sample looks like this:

Input

10 (array length input)  
10 30 40 50 60 30 40 15 10 20

2  (array length input) 
-54 -134

4  (array length input) 
1 1 1 1

Output

60
-54
1

I have the exact output, but I think something is missing, because the Judge.org compiler won’t accept my code as correct.

import java.util.*;
public class problema7 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int max1=Integer.MIN_VALUE;
        int max2=Integer.MIN_VALUE;
        int max3=Integer.MIN_VALUE;

        // sequence 1

        int num1 = in.nextInt();
        int[] seq1 = new int [num1];

        for(int x = 0; x<num1; x++) {
            seq1[x] = in.nextInt();
            if (max1 == 0 || seq1[x] > max1) max1 = seq1[x];
        }

        // sequence 2

        int num2 = in.nextInt();
        int[] seq2 = new int [num2];

        for(int x = 0; x < num2; x++) {
            seq2[x] = in.nextInt();
            if (max2 == 0 || seq2[x] > max2) max2 = seq2[x];
        }

        // sequence 3

        int num3 = in.nextInt();
        int[] seq3 = new int [num3];

        for(int x = 0; x<num3; x++) {
            seq3[x] = in.nextInt();
            if (max3 == 0 || seq3[x] > max3) max3 = seq3[x];
        }

        System.out.println(max1);
        System.out.println(max2);
        System.out.println(max3);
    }
}

problem:

https://jutge.org/problems/P71753_en/pdf

Advertisement

Answer

First of all welcome to SO.

Secondly, Scott’s observation is correct, are you sure your input will always have 3 lines? This seems a bit constrictive. Bellow I’ve made a simple class to illustrate how you could possibly solve this problem:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String line;
        System.out.println("Insert line (empty terminates the program):");

        while(!(line = scanner.nextLine()).isBlank()){
            String[] segments = line.split(" ", 2);

            int population = Integer.parseInt(segments[0]);
            String[] sequence = segments[1].trim().split(" ");
            if(population != sequence.length){
                System.err.println("Population declaration and sequence size do not matchnPopulation: " + population + "nSequence: "+ segments[1] + ", size: " + sequence[1].length());
                System.exit(0);
            }

            int max = Integer.MIN_VALUE;

            for (String number : sequence) {
                max = Math.max(Integer.parseInt(number), max);
            }

            System.out.println(max);
            System.out.println("Insert line (empty terminates the program):");
        }

        System.out.println("Now exiting");
    }

}

It expects input from the keyboard, splits at the first space in a line, checks if the correct number of numbers is inserted after the first number (the one denoting the population of the following sequence) and then just finds the maximum number and prints it. It will exit if you enter a blank line.

I’d suggest you adapt it for your input.

Edit: The condition for the while loop uses Java 11’s isBlank() method. So you’ll need a Java version >= 11 or you can adjust the condition to be compatible with the version you’re using.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement