|
| 1 | +/* |
| 2 | +Mrs. Jhunjhunwala has taught Programming in Java to the second-year students in ICE college and conducted 3 academic tasks for all the (N) students. Marks are store in a 2-D array but now Mrs. Jhunjhunwala is willing to implement a java program which can accept the 2-D array of marks and return the average marks of Best 2 Academic Tasks of each student. Help her to implement a method public double[] average_marks(double [][] marks) |
| 3 | + |
| 4 | +Input Format |
| 5 | + |
| 6 | +First line reads the number of students N |
| 7 | +Next N lines read the marks of each student seperated by space |
| 8 | + |
| 9 | +Constraints |
| 10 | + |
| 11 | +n>0 |
| 12 | + |
| 13 | +Output Format |
| 14 | + |
| 15 | +Prints the Avaerage marks of best 2 Academic tasks of N students separated by space |
| 16 | + |
| 17 | +Sample Input 0 |
| 18 | + |
| 19 | +2 |
| 20 | +10.5 2.5 15 |
| 21 | +5.25 6.75 11.25 |
| 22 | +Sample Output 0 |
| 23 | + |
| 24 | +12.75 9.0 |
| 25 | +*/ |
| 26 | + |
| 27 | +import java.io.*; |
| 28 | +import java.util.*; |
| 29 | + |
| 30 | +public class Solution { |
| 31 | + public double[] average_marks(double [][]marks) |
| 32 | + { |
| 33 | + double []result = new double[marks.length]; |
| 34 | + for(int i=0;i<marks.length;i++) |
| 35 | + { |
| 36 | + double min = marks[i][0]; |
| 37 | + for(int j=1;j<marks[i].length;j++) |
| 38 | + { |
| 39 | + if(min>marks[i][j]) |
| 40 | + min = marks[i][j]; |
| 41 | + } |
| 42 | + double sum = 0; |
| 43 | + for(int j=0;j<marks[i].length;j++) |
| 44 | + { |
| 45 | + if(marks[i][j]!=min) |
| 46 | + sum += marks[i][j]; |
| 47 | + } |
| 48 | + double avg = sum/(marks[i].length-1); |
| 49 | + result[i] = avg; |
| 50 | + } |
| 51 | + return result; |
| 52 | + } |
| 53 | + |
| 54 | + public static void main(String[] args) { |
| 55 | + /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ |
| 56 | + Scanner sc = new Scanner(System.in); |
| 57 | + int n = sc.nextInt();//number of student |
| 58 | + double [][]marks = new double[n][3]; |
| 59 | + //take input for marks |
| 60 | + for(int i=0;i<n;i++) |
| 61 | + { |
| 62 | + for(int j=0;j<3;j++) |
| 63 | + { |
| 64 | + marks[i][j] = sc.nextDouble(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + //calling required method by creating sutable object |
| 69 | + Solution ans = new Solution(); |
| 70 | + double []result = ans.average_marks(marks); |
| 71 | + for(double i : result) |
| 72 | + { |
| 73 | + System.out.print(i+" "); |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | +} |
0 commit comments