DEV Community

Cover image for TechMahindra "Student Report"Coding Question
Gourav Kadu
Gourav Kadu

Posted on

TechMahindra "Student Report"Coding Question

Statement :-

Given a list of N students, every student is marked for M subjects. Each student is denoted by an index value. Their teacher Ms. Margaret must ignore the marks of any 1 subject for every student. For this she decides to ignore the subject which has the lowest class average.
Your task is to help her find that subject, calculate the total marks of each student in all the other subjects and then finally return the array of the total marks scored by each student.

Input Specification:
input1:
An integer value N denoting number of students

input2:
An integer value M denoting number of subjects

input3:
A 2-D integer array of size N'M containing the marks of all students in each subject.

Output Specification:

Return an integer array of size N containing the total marks of each student afte deducting the score for that one subject.

Example 1:

INPUT

3 5
75 76 65 87 87
78 76 68 56 89
67 87 78 77 65
Enter fullscreen mode Exit fullscreen mode

OUTPUT

325 299 296
Enter fullscreen mode Exit fullscreen mode

Example 2:

INPUT

3 3
50 30 70 
30 70 99 
99 20 30
Enter fullscreen mode Exit fullscreen mode

OUTPUT

120 129 129
Enter fullscreen mode Exit fullscreen mode

Explanation:

Out of these subjects, the students average was lowest in subject 2
i.e 30+70+20= 120/3=40

So the teacher will ignore marks of this subject and will consider the title other two subjects for each of the three students i.e. 120 129 129 respectively

Hence (120 129 129) is returned as the final output.

Solution:-

import java.util.*;
public class MyClass {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);  

        int row = sc.nextInt();   
        int col = sc.nextInt();
        int avg[]= new int[row];
        int ans[] = new int[row];
        int arr[][]= new int[row][col];
        int res[]= new int[row];
        for(int i = 0; i <row; i++) {
            for(int j=0;j<col;j++) {
                arr[i][j]= sc.nextInt();
            }
        }

        /*//verifying i/p
        for(int i = 0; i <row; i++) {
            for(int j=0;j<col;j++) {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println("");
        }*/

        for(int i = 0; i <row; i++) { //calculating and storing average of each column
            for(int j=0;j<row;j++) {
            ans[i] += arr[j][i];

            }
        avg[i] = ans[i]/row; 

        }

    int min = avg[0];
        int eleminate_Index =0;
        for(int i = 1; i < row; i++) { // minimum average calculated and ..
            if(avg[i]<min) {
                min = avg[i];
                eleminate_Index = i; //..index of it stored.
            }
        }

        /*
        System.out.println(min);
        System.out.println(p);
        */
        for(int i = 0 ; i<row; i++) {
            for(int j=0; j<col; j++){
                if(eleminate_Index!=j) {//calculating sum of remaining subjects
                    res[i]+=arr[i][j];
                }
        }

        }
        for(int j=0; j<row; j++){
            System.out.print(res[j]+" ");
            }



        sc.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (6)

Collapse
 
ashish_dts_19ee9f8b014ac7 profile image
Ashish DTS

Need same solution in swift 5

Collapse
 
gouravmpk profile image
Gourav Kadu • Edited

import Foundation

let sc = Scanner.standardInput

let row = sc.nextInt()!
let col = sc.nextInt()!
var avg = Int
var ans = Int
var arr = [Int]
var res = Int

for i in 0..<row {
for j in 0..<col {
arr[i][j] = sc.nextInt()!
}
}

///verifying i/p
for i in 0..<row {
for j in 0..<col {
print(arr[i][j], terminator: " ")
}
print("")
}
/

for i in 0..<row { //calculating and storing average of each column
for j in 0..<row {
ans[i] += arr[j][i]
}
avg[i] = ans[i]/row
}

var min = avg[0]
var eleminate_Index = 0

for i in 1..<row { // minimum average calculated and ..
if avg[i] < min {
min = avg[i]
eleminate_Index = i //..index of it stored.
}
}

/*
print(min)
print(p)
*/
for i in 0..<row {
for j in 0..<col {
if eleminate_Index != j {//calculating sum of remaining subjects
res[i] += arr[i][j]
}
}
}

for j in 0..<row {
print(res[j], terminator: " ")
}

sc.close()

Collapse
 
diprajshahane profile image
Dipraj Shahane • Edited

Same problem statement for Nagarro.

import java.util.Scanner;
public class TestSample {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);

        int students = scanner.nextInt();
        int subjects = scanner.nextInt();

        int marks[][] = new int[students][subjects];

        for (int row = 0; row < students; row++) {
            for (int col = 0; col < subjects; col++) {
                marks[row][col] = scanner.nextInt();
            }
        }
        scanner.close();


        int avg[] = new int[subjects];
        int min = -1;
        int skipIndex = -1;

        for (int sub = 0; sub < subjects; sub++) {
            for (int stu = 0; stu < students; stu++) {
                avg[sub] += marks[stu][sub];
            }
            avg[sub] /= students;

            if (sub == 0) {
                min = avg[0];
                skipIndex = 0;
            } else if (avg[sub] < min) {
                min = avg[sub];
                skipIndex = sub; 
            }
        }

        int result[] = new int[students];
        for (int row = 0; row < students; row++) {
            for (int col = 0; col < subjects; col++) {
                if (skipIndex != col)
                    result[row] += marks[row][col];
            }
        }

        for (int mark : result) {
            System.out.print(mark + " ");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
gyanconnected profile image
gyanconnected

Need same solution in C#

Collapse
 
vineysagar profile image
Lalit S

using System;
using System.Linq;

class Program {
static void Main() {
int N = 3; // number of students
int M = 4; // number of subjects

    int[,] marks = { { 90, 80, 70, 60 }, // marks of student 1
                     { 85, 75, 65, 55 }, // marks of student 2
                     { 80, 70, 60, 50 }  // marks of student 3
                   };

    int[] sum = new int[N]; // array to store total marks of each student
    int minSubject = 0;
    double minAvg = double.MaxValue;

    for (int j = 0; j < M; j++) {
        double avg = 0;
        for (int i = 0; i < N; i++) {
            avg += marks[i,j];
            sum[i] += marks[i,j];
        }
        avg /= N;
        if (avg < minAvg) {
            minAvg = avg;
            minSubject = j;
        }
    }

    for (int i = 0; i < N; i++) {
        sum[i] -= marks[i,minSubject];
    }

    Console.WriteLine("Subject to ignore: " + (minSubject+1));
    Console.WriteLine("Total marks of each student in all other subjects:");
    for (int i = 0; i < N; i++) {
        Console.WriteLine("Student " + (i+1) + ": " + sum[i]);
    }
}
Enter fullscreen mode Exit fullscreen mode

}

Collapse
 
vineysagar profile image
Lalit S

C#

using System;
using System.Linq;

class Program {
static void Main() {
int N = 3; // number of students
int M = 4; // number of subjects

    int[,] marks = { { 90, 80, 70, 60 }, // marks of student 1
                     { 85, 75, 65, 55 }, // marks of student 2
                     { 80, 70, 60, 50 }  // marks of student 3
                   };

    int[] sum = new int[N]; // array to store total marks of each student
    int minSubject = 0;
    double minAvg = double.MaxValue;

    for (int j = 0; j < M; j++) {
        double avg = 0;
        for (int i = 0; i < N; i++) {
            avg += marks[i,j];
            sum[i] += marks[i,j];
        }
        avg /= N;
        if (avg < minAvg) {
            minAvg = avg;
            minSubject = j;
        }
    }

    for (int i = 0; i < N; i++) {
        sum[i] -= marks[i,minSubject];
    }

    Console.WriteLine("Subject to ignore: " + (minSubject+1));
    Console.WriteLine("Total marks of each student in all other subjects:");
    for (int i = 0; i < N; i++) {
        Console.WriteLine("Student " + (i+1) + ": " + sum[i]);
    }
}
Enter fullscreen mode Exit fullscreen mode

}