Given input as HashMap, value consists of marks and roll no as key. Find the sum of the lowest three subject marks from the HashMap. The return type of the output is the sum of the lowest three subject marks.
Input and Output Format
- The first line of the input corresponds to the HashMap size.
- Input consists of a HashMap with marks and roll no.
- The output is an integer which is the sum of the lowest three subject marks.
Refer sample output for formatting specifications
Sample Input 1:
5
1
54
2
85
3
74
4
59
5
57
Sample Output 1:
170
Sample Input 2:
4
10
56
20
58
30
87
40
54
Sample Output 2:
168
Program to find the sum of lowest marks in hashmap in Java
Following are the steps to find the sum of the lowest marks in hashmap in Java:
- Input the size of the Hashmap.
- Create an empty HashMap with type integers both key and value. (to store marks and roll no.)
- Add elements to the HashMap using the put() method. And, pass it to the getvalues() method.
- Inside the method, create an array list and initialize variable ‘m’ to 0;
- Iterate over the map, and get each value from the map and add it to the array list.
- Now, sort the list using the Collection.sort() method and you will get the list in ascending order. Next, just add the lowest three marks by using the get() method and add them.
- At last, print the addition.
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); HashMap<Integer, Integer> h1 = new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++) { h1.put(sc.nextInt(), sc.nextInt()); } System.out.println(getvalues(h1)); } public static int getvalues(HashMap<Integer, Integer> h1) { ArrayList<Integer> a1 = new ArrayList<Integer>(); int m = 0; Iterator<Integer> it = h1.keySet().iterator(); while (it.hasNext()) { int x = it.next(); a1.add(h1.get(x)); } Collections.sort(a1); m = a1.get(0) + a1.get(1) + a1.get(2); return m; } }
Output
Find the sum of the lowest three subject marks in Array
Given input as Array, value consists of marks. Find the sum of the lowest three subject marks from the Array. The return type of the output is the sum of the lowest three subject marks.
- Input the size of an array.
- Create an empty array of size ‘n’. Add marks to an array.
- Pass them to getvalues() method.
- Inside this method, First use the Arrays.sort() to sort the marks into ascending order. Then, we will get the marks starting from lowest to highest.
- Next, just fetch the first three marks which are the lowest, and add them.
- At last, print the addition.
package com.demo2; import java.util.Arrays; import java.util.Scanner; public class LowestMarksUsingArray { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int marks[] =new int[n]; for(int i =0;i<marks.length;i++){ marks[i] = scanner.nextInt(); } getvalues(marks); } private static void getvalues(int[] marks) { //first sort the input marks Arrays.sort(marks); for(int i =0;i<marks.length;i++){ System.out.println(marks[i]); } int lowestMarks = marks[0] + marks[1] + marks[2]; System.out.println(lowestMarks); } }
Output
Thus, in this way, we learn how to find the sum of the lowest marks using HashMap and Array in Java.