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

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