Skip to main content

Mini-Max Sum HackerRank solution in Java

Find the minimum and maximum values that can be calculated by summing exactly four of the five integers

Mini-Max Sum HackerRank solution in Java

Problem Description :

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 1 :

arr = [1, 3, 5, 7, 9]
The minimum sum is 1 + 3 + 5 + 7 = 16 and the maximum sum is 3 + 5 + 7 + 9 = 24.
Answer is 16 24

Example 2 :

arr = [396285104, 573261094, 759641832, 819230764, 364801279]
The minimum sum is 364801279 + 396285104 + 573261094 + 759641832 = 2093989309 and the maximum sum is 396285104 + 573261094 + 759641832 + 819230764 = 2548418794.
Answer is 2093989309 2548418794

Lets see solution.

Solution 1

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MiniMaxSum {

    public static void main(String[] args) {
       
        List<Integer> arr = new ArrayList<>() {{
            add(5);
            add(4);
            add(2);
            add(3);
            add(1);
        }};   
       
        long max = 0, min = 0;
        Collections.sort(arr);
        
        for(int i = 0, j = arr.size()-1; i < arr.size()-1; i++, j--){
            min += arr.get(i);
            max += arr.get(j);
        }
        
        System.out.print(min+" "+max);
    }
}

Output :

10 14

Solution explanation :

  • Define 2 long variable and initialize with 0.
  • Sort given List using Collections.sort() method.
  • Traverse through given list from 0 to list size-1. We are define two variable i and j in loop. i variable used for traverse from 0 to 3 (means minimum value to maximum value) and j variable used for traverse from 4 value to 1 (max value to min value).
  • last, print both min and max value.

Output explanation :

after sorting list becomes :

list = [1, 2, 3, 4, 5]

  • i = 0,  j = 4, min = 0, max = 0
    • min = 0 + arr.get(0) | 0 + 1 = 1
    • max = 0 + arr.get(4) | 0 + 5 = 5

  • i = 1,  j = 3, min = 1, max =5
    • min = 1 + arr.get(0) | 1 + 2 = 3
    • max = 5 + arr.get(4) | 5 + 4 = 9

  • i = 2,  j = 3, min = 3, max = 9
    • min = 3 + arr.get(2) | 3 + 3 = 6
    • max = 9 + arr.get(3) | 9 + 3 = 12

  • i = 3, j = 2, min = 6, max = 12
    • min = 6 + arr.get(3) | 6 + 4 = 10
    • max = 12 + arr.get(2) | 12 + 2 = 14

  • i = 4 is not less than 4.
  • min  = 10, max = 14 

Solution 2

import java.util.ArrayList;
import java.util.List;

public class MiniMaxSum {

    public static void main(String[] args) {
       
        List<Integer> arr = new ArrayList<>() {{
            add(1);
            add(2);
            add(5);
            add(7);
            add(9);
        }};   
       
        long min = arr.get(0);
        long max = min;
        long sum = min;
       
        for (int i = 1; i < arr.size(); i++) {
            sum += arr.get(i);
            if (arr.get(i) < min) {
                min = arr.get(i);
            }
           
            if (arr.get(i) > max) {
                max = arr.get(i);
            }
        }
       
        System.out.print((sum - max) + " " + (sum - min));
    }

}

Output :

15 23

Solution explanation :

  • Define three long variables and initialize with 0th index value of given list.
  • Traverse through given list from 0 to list size. 
    • Add sum plus current list index value in sum variable.
    • Check condition, if current list value is less than min variable then store that value to min variable.
    • In another if, check if current list value is greater than max variable then store in max variable.
  • At last, we will get sum of all list value in sum variable, minimum value in min and maximum value in max variable. 
  • So for get minimum 4 integer sum, minus the max value from total sum of all list value. and for get maximum 4 integer sum, minus the min value from total sum of all list value.

 

Other hackerrank problem and its solution in java :

Comments

Popular posts from this blog

Queen's Attack II HackerRank Solution in Java with Explanation

Queen's Attack II Problem's Solution in Java (Chessboard Problem)   Problem Description : You will be given a square chess board with one queen and a number of obstacles placed on it. Determine how many squares the queen can attack.  A queen is standing on an n * n chessboard. The chess board's rows are numbered from 1 to n, going from bottom to top. Its columns are numbered from 1 to n, going from left to right. Each square is referenced by a tuple, (r, c), describing the row r and column c, where the square is located. The queen is standing at position (r_q, c_q). In a single move, queen can attack any square in any of the eight directions The queen can move: Horizontally (left, right) Vertically (up, down) Diagonally (four directions: up-left, up-right, down-left, down-right) The queen can move any number of squares in any of these directions, but it cannot move through obstacles. Input Format : n : The size of the chessboard ( n x n ). k : The number of obstacles...

Java Hashset HackerRank Solution | Programming Blog

Java Hashset HackerRank Solution with Explanation   Problem Statement :- In computer science, a set is an abstract data type that can store certain values, without any particular order, and no repeated values. {1,2,3} is an example of a set, but {1,2,2} is not a set. Today you will learn how to use sets in java by solving this problem. You are given n pairs of strings. Two pairs (a,b) and (c,d) are identical if a = c and b = d. That also implies (a,b) is not same as (b,a). After taking each pair as input, you need to print number of unique pairs you currently have. See full problem description in HackerRank Website :- https://www.hackerrank.com/challenges/java-hashset/problem Let's see solution of problem. import java.util.HashSet; import java.util.Scanner; public class Solution {     public static void main(String[] args) {         Scanner s = new Scanner(System.in);         System.out.println("Enter tot...