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

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 ...

Sales by Match HackerRank Solution | Java Solution

HackerRank Sales by Match problem solution in Java   Problem Description : Alex works at a clothing store. There is a large pile of socks that must be paired by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are n=7 socks with colors socks = [1,2,1,2,1,3,2]. There is one pair of color 1 and one of color 2 . There are three odd socks left, one of each color. The number of pairs is 2 .   Example 1 : Input : n = 6 arr = [1, 2, 3, 4, 5, 6] Output : 0 Explanation : We have 6 socks with all different colors, So print 0. Example 2 : Input : n = 10 arr = [1, 2, 3, 4, 1, 4, 2, 7, 9, 9] Output : 4 Explanation : We have 10 socks. There is pair of color 1, 2, 4 and 9, So print 4. This problem easily solved by HashMap . Store all pair of socks one by one in Map and check if any pair is present in Map or not. If pair is present then increment ans variable by 1 ...