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

Plus Minus HackerRank Solution in Java | Programming Blog

Java Solution for HackerRank Plus Minus Problem Given an array of integers, calculate the ratios of its elements that are positive , negative , and zero . Print the decimal value of each fraction on a new line with 6 places after the decimal. Example 1 : array = [1, 1, 0, -1, -1] There are N = 5 elements, two positive, two negative and one zero. Their ratios are 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000. Results are printed as:  0.400000 0.400000 0.200000 proportion of positive values proportion of negative values proportion of zeros Example 2 : array = [-4, 3, -9, 0, 4, 1]  There are 3 positive numbers, 2 negative numbers, and 1 zero in array. Following is answer : 3/6 = 0.500000 2/6 = 0.333333 1/6 = 0.166667 Lets see solution Solution 1 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.st

Flipping the Matrix HackerRank Solution in Java with Explanation

Java Solution for Flipping the Matrix | Find Highest Sum of Upper-Left Quadrant of Matrix Problem Description : Sean invented a game involving a 2n * 2n matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times. The goal of the game is to maximize the sum of the elements in the n *n submatrix located in the upper-left quadrant of the matrix. Given the initial configurations for q matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix's upper-left quadrant is maximal.  Input : matrix = [[1, 2], [3, 4]] Output : 4 Input : matrix = [[112, 42, 83, 119], [56, 125, 56, 49], [15, 78, 101, 43], [62, 98, 114, 108]] Output : 119 + 114 + 56 + 125 = 414 Full Problem Description : Flipping the Matrix Problem Description   Here we can find solution using following pattern, So simply we have to find Max of same number of box like (1,1,1,1). And last