Question

Java problem In this problem, the first input is a positive integer called n that will...

Java problem

In this problem, the first input is a positive integer called n that will represent the number of lines to process. The lines to be processed have one or more integers separated by whitespaces. For each of these lines, you must output:

The minimum value of the integers

The maximum value of the integers

The sum of the integers

It is worth to mention that the number of integers of each line is not known a priori and can varry from line to line. Write a program that solve this requirement.

Input Format

The first line contains n, the number of lines to be processed. The remaining n lines are the lines to be processed.

Constraints

n must be a positive integer.
The lines to be processed must have at least one integer.

Output Format

For each line to be processed, the output must be formed by 3 space-separated integers: min max sum
min: the minimum value of the integers of the line.
max: the maximum value of the integers of the line.
sum: the sum of the integers of the line.

Sample Input 0

4
-9 17 77 -18
55 0 42 11 7 90 -2 -15 30
-8 0 2
8 12 22 -1 5

Sample Output 0

-18 77 67
-15 90 218
-8 2 -6
-1 22 46
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*The whole answer is well commented so please create a class SumMinMax.java and copy whole answer and execute

*

*Here I will use BufferedReader to read input from console because it is fastest and

*best read lines from console but It reads lines in string format you need to process it

*

*So You can use Integer.parseInt(str) method to convert a string to integer

*str.split() method to split string

*

* So after reading input as string i will convert the string to integer of array then

* We can find min max easily in array of integer

*

* How to find min?

* Declare a variable min;

* assign arr[0] means first element of array to it min=arr[0]

* now iterate over array from index 1 to n-1 and check if(arr[i]<min) then

* modify the value of min as min=arr[i]

*

* ex- arr=[5,2,1]

* min=arr[0]=5

* if(arr[1]<min) => 2<5 => min=2

* if(arr[2]<min) => 2<1 => min=1

* So same procedure for max

*

*

* Sample Input:

4

-9 17 77 -18

55 0 42 11 7 90 -2 -15 30

-8 0 2

8 12 22 -1 5

*Sample Output:

-18 77 67

-15 90 218

-8 2 -6

-1 22 46

*If you are copy pasting input then press Enter to see last output

*/

import java.io.*;

import java.util.*;

public class SumMinMax {

public static void main(String[] args) throws IOException {

//Instantiating BufferedReader

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

//Now Reading n as it is single integer we can directly convert to integer

//ReadLine method reads a complete line from console

int n=Integer.parseInt(br.readLine());

//Below code of block will read n lines and find min -max -sum

for(int t=1;t<=n;t++)

{

//Below line will read a line and split it and will create array of Strings

String[] strs=br.readLine().split(" ");

//Finding number of elements in line

int len=strs.length;

//Creating integer array

int[] numbers=new int[len];

//Now Converting array of strings to array of integers

for(int i=0;i<len;i++)

{

numbers[i]=Integer.parseInt(strs[i]);

}

//Declaring min, max, sum variable

int min=numbers[0];

int max=numbers[0];

int sum=numbers[0];

for(int i=1;i<len;i++)

{

//Checking for min

if(numbers[i]<min)

min=numbers[i];

//Checking for max

if(numbers[i]>max)

max=numbers[i];

//Adding to sum

sum =sum +numbers[i];

}

//Printing out

System.out.println(min+" "+max+" "+sum);

}

}

}

Add a comment
Know the answer?
Add Answer to:
Java problem In this problem, the first input is a positive integer called n that will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Input Format For Custom Testing The first line contains an integer, n, that denotes the...

    Java Input Format For Custom Testing The first line contains an integer, n, that denotes the number of elements in s. Each line i of the n subsequent lines (where 0 si<n) contains a string that describes s[i]. Sample Case 0 Sample Input For Custom Testing 4 code aaagmnrs anagrams doce Sample Output aaagmnrs code Explanation aaagmnrs and anagrams are anagrams, code and doce are anagrams. After sorting aaagmnrs comes first. Sample Case 1 Sample Input For Custom Testing 4...

  • Preferably in python but java is good too Task 1: Minimum Spanning Trees For this warm-up...

    Preferably in python but java is good too Task 1: Minimum Spanning Trees For this warm-up task you are to implement any efficient minimum spanning tree algorithm that takes a sequence of edge-weighted graphs and outputs the minimum cost weight of a spanning tree of each Input Format For this assignment we use adjacency matrices with positive integer weights. Here a zero entry at row i and column J indicates that no edge i] exists in the graph. The first...

  • Python Script format please! 1. Write a script that takes in three integer numbers from the...

    Python Script format please! 1. Write a script that takes in three integer numbers from the user, calculates, and displays the sum, average, product, smallest, and largest of the numbers input. Important things to note: a. You cannot use the min() or max() functions, you must provide the logic yourself b. The calculated average must be displayed as an integer value (ex. If the sum of the three values is 7, the average displayed should be 2, not 2.3333). Example:...

  • You are given a positive integer n, break it into the sum of at least two...

    You are given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum 2 product you can get. (Example Input: 10, Output: 36, Explanation: 10 = 3 3 4 = 36). What is your answer for n = 82. Write a python code for this

  • Homework 2 Description This homework will test the use of arrays and control structures. In summary,...

    Homework 2 Description This homework will test the use of arrays and control structures. In summary, you will read 10 integers from standard input, store them into an array and then produce some statistics. Details You need to compute Array Statistics. Create a class called Hwk2 that implements a main to solve your homework. At the beginning display a line with the following text: Type 10 integer values: Then, read 10 integers from standard input, one by one and store...

  • The input consists of n numbers a1, a2, . . . , an and a target...

    The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...

  • Please write code in java You’re given a chess board with dimension n x n. There’s...

    Please write code in java You’re given a chess board with dimension n x n. There’s a king at the bottom right square of the board marked with s. The king needs to reach the top left square marked with e. The rest of the squares are labeled either with an integer p (marking a point) or with x marking an obstacle. Note that the king can move up, left and up-left (diagonal) only. Find the maximum points the king...

  • Problem Description proving program correctness Consider the following program specification: Input: An integer n > 0...

    Problem Description proving program correctness Consider the following program specification: Input: An integer n > 0 and an array A[0..(n - 1)] of n integers. Output: The smallest index s such that A[s] is the largest value in A[0..(n - 1)]. For example, if n = 9 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 1, since the largest value in...

  • Complete the Python program below that performs the following operations. First prompt the user to input...

    Complete the Python program below that performs the following operations. First prompt the user to input two integers, n and m. If n<m, then print the odd positive integers that are less than m (in order, on a single line, separated by spaces). If man, then print the even positive integers that are less than n (in order, on a single line, separated by spaces). If neem, then print nothing. For instance, if the user enters 5 followed by 10,...

  • Please submit only Python source code. 1. Arithmetic trees 50 marks You are given an input file with multiple pairs of...

    Please submit only Python source code. 1. Arithmetic trees 50 marks You are given an input file with multiple pairs of input lines. The first line of each pair is a tree given as a predecessor array. The second line is the value at the corresponding node. Values at leaf nodes (nodes with no children) are integers. At non-leaf nodes, the two possible values are + or *. The tree represents an arithmetic expression where the value at a non-leaf...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT