In this post you will see Sasken Technologies off campus coding question with step by step solution in java
Sasken Technologies off campus coding question
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 a 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:
N – Total number of participants.
K – top scorers
An array of length N with the scores of all ‘N’ participants.
Example:
N=5
K=2
Array=[4,1,2,5,8]
Ouput:
8+5 = 13 (Highest scores in array)
Explanation:
In array we can observe first k highest values are 8 and 5, so output will be the addition of 8 & 5 which is 13
Algorithm:
- In first step we will take value of N, which indicates total number of participents
- Then we will take the value of k which indicates top scorers.
- Further we will take an array of size k inidicating the socres of k paricipants
- Then SORT the array in Decending order
- Next add first k elemets in array.
- congrats you just solved one question, try more all th best
- For more such off campus question Click Here
Java code for k top scorers in exams
<strong>import java.util.Arrays; class problem9 { public static void main(String[] args) { </strong> <strong> System.out.println("The main method is starting here ");</strong> <strong> int array1[] = { 1, 3, 5, 4, 8, 6, 7, 100, 99, 88, 97 }; System.out.println("The original array is : " + Arrays.toString(array1));</strong> // sorting array elemets in decending order <strong> for (int i = 0; i < array1.length - 1; i++) { if (array1[i] < array1[i + 1]) { int temp = array1[i]; array1[i] = array1[i + 1]; array1[i + 1] = temp; // important line of this code i = -1; } } System.out.println("The sorted array is : " + Arrays.toString(array1)); int k = 3; int res = 0;</strong> // Adding k highest elements in sorted array <strong> for (int i = 0; i < k; i++) { res = res + array1[i]; } System.out.println("The sum of top " + k + " scorers is " + res); System.out.println("The main method is ending here "); } }</strong>
To get more Explanation of code you can visit our channel
Pingback: Complete Solution For Rob And Mars Problem In Java - Is It Actually