CodingCoding
0 0
Read Time:2 Minute, 0 Second

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

Example

The minimum sum is  and the maximum sum is . The function prints

16 24

Function Description

Complete the miniMaxSum function in the editor below.

miniMaxSum has the following parameter(s):

  • arr: an array of  integers

Print

Print two space-separated integers on one line: the minimum sum and the maximum sum of  of  elements.

Input Format

A single line of five space-separated integers.

Constraints

Output Format

Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than a 32 bit integer.)

Sample Input

1 2 3 4 5

Sample Output

10 14


Java Solution

2021
package hackerrank1;
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result1 {

    /*
     * Complete the 'miniMaxSum' function below.
     *
     * The function accepts INTEGER_ARRAY arr as parameter.
     */

    public static void miniMaxSum(List<Integer> arr) {
    int size= arr.size();
    int[] sum_arr= new int[size];
    int index=0;
   
    for(int z=1;z<=arr.size();z++) {
    	 int index_sum_counter=0;
    	    
    for(int i=z;index_sum_counter<arr.size()-1;i++) {
    			if(i>arr.size()-1) {
    					i=0;
    				}
    			 
    			sum_arr[index] +=arr.get(i);
    	index_sum_counter++;
  
    }
  
 	index++;
 	
    }
  
    int minvalue=sum_arr[0];
	for(int i=1;i<sum_arr.length;i++) {
		if(minvalue>sum_arr[i]) {
			minvalue=sum_arr[i];
		}
	}
	 int maxvalue=sum_arr[0];
		for(int i=1;i<sum_arr.length;i++) {
			if(maxvalue<sum_arr[i]) {
				maxvalue=sum_arr[i];
			}
		}
		
	System.out.print(minvalue+" "+maxvalue);	

    }

}

public class Minmax {
	 public static void main(String[] args) throws IOException {
	        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

	        List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
	            .map(Integer::parseInt)
	            .collect(toList());

	        Result1.miniMaxSum(arr);

	        bufferedReader.close();
	    }
}

Eclipse

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

About Author

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

One thought on “Mini-Max Sum

Leave a Reply

Your email address will not be published. Required fields are marked *