Answer to Question #338109 in Java | JSP | JSF for mahi

Question #338109

N number of people participated in a coding marathon where they were asked to solve some problems. Each problem carried 1 mark and at the end of the marathon, 

the total marks that each person achieved was calculated. 

As an organizer, you have the list of the total marks that each person achieved. You have to calculate the sum of the marks of top K scorers from the list. 

Input Specification: 

input1: N, Total number of participants 

input2: K, Top scorers 

input3: An array of length N with the scores of all N participants 

Output Specification: 

Return S, sum of the marks of top K scorers from the list. 

Example 1: 

input1: 4 

input2: 2 

input3: {4, 1, 2, 5} 

Output: 9 

Explanation: Top 2 scores are 5 and 4. Sum = 5+4=9. 

Example 2: 

input1: 4 

input2: 3 

input3: {4, 3, 6, 1} 

Output: 13 

Explanation: Top 3 scores are 6, 4 and 3. Sum = 6+4+3=13.


1
Expert's answer
2022-05-08T14:33:04-0400
    import java.util.Arrays;
import java.util.Scanner;

    public class Answer {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Enter total number of participants: ");
            int n = scanner.nextInt();
            System.out.println("Enter top scorers: ");
            int top = scanner.nextInt();
            System.out.println("Enter n scores: ");
            int[] score = new int[n];
            for (int i = 0; i < n; i++) {
                score[i] = scanner.nextInt();
            }
            int ans = 0;
            Arrays.sort(score);
            for (int i = n - 1; i >= n - top; i--) {
                ans = ans + score[i];
            }
            System.out.println("Sum: " + ans);
        }
    }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS