Jump in rank of a student after updating marks
Given three arrays names[], marks[] and updates[] where:
- names[] contains the names of students.
- marks[] contains the marks of the same students.
- updates[] contains the integers by which the marks of these students are to be updated.
The task is find the name of the student with maximum marks after updation and the jump in the student’s rank i.e. previous rank – current rank.
Note: The details of the students are in descending order of their marks and if more than two students scored equal marks (also the highest) then choose the one who had a lower rank previously.
Examples:
Input: names[] = {“sam”, “ram”, “geek”, “sonu”},
marks[] = {99, 75, 70, 60},
updates[] = {-10, 5, 9, 39}
Output: Name: sonu, Jump: 3
Updated marks are {89, 80, 79, 99}, its clear that sonu has the highest marks with jump of 3
Input: names[] = {“sam”, “ram”, “geek”},
marks[] = {80, 79, 75},
updates[] = {0, 5, -9}
Output: Name: ram, Jump: 1
Approach: Create a structure struct Student to store the information of each student, each of which will have 3 attributes name of the student, marks of the student, prev_rank of the student.
Now, updated the marks according to the content of the updates[] then with a single traversal of the array, find the student who has the highest marks after updation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Student {
string name;
int marks = 0;
int prev_rank = 0;
};
void nameRank(string names[], int marks[],
int updates[], int n)
{
Student x[n];
for ( int i = 0; i < n; i++) {
x[i].name = names[i];
x[i].marks = marks[i] + updates[i];
x[i].prev_rank = i + 1;
}
Student highest = x[0];
for ( int j = 1; j < n; j++) {
if (x[j].marks >= highest.marks)
highest = x[j];
}
cout << "Name: " << highest.name
<< ", Jump: "
<< abs (highest.prev_rank - 1)
<< endl;
}
int main()
{
string names[] = { "sam" , "ram" , "geek" };
int marks[] = { 80, 79, 75 };
int updates[] = { 0, 5, -9 };
int n = sizeof (marks) / sizeof (marks[0]);
nameRank(names, marks, updates, n);
}
|
Java
import java.util.*;
class GFG{
static class Student
{
String name;
int marks, prev_rank;
Student()
{
this .marks = 0 ;
this .prev_rank = 0 ;
}
}
static void nameRank(String []names, int []marks,
int []updates, int n)
{
Student []x = new Student[n];
for ( int i = 0 ; i < n; i++)
{
x[i] = new Student();
x[i].name = names[i];
x[i].marks = marks[i] + updates[i];
x[i].prev_rank = i + 1 ;
}
Student highest = x[ 0 ];
for ( int j = 1 ; j < n; j++)
{
if (x[j].marks >= highest.marks)
highest = x[j];
}
System.out.print( "Name: " + highest.name +
", Jump: " +
Math.abs(highest.prev_rank - 1 ));
}
public static void main(String[] args)
{
String []names = { "sam" , "ram" , "geek" };
int []marks = { 80 , 79 , 75 };
int []updates = { 0 , 5 , - 9 };
int n = marks.length;
nameRank(names, marks, updates, n);
}
}
|
Python3
def nameRank(names, marks, updates, n):
x = [[ 0 for j in range ( 3 )] for i in range (n)]
for i in range (n):
x[i][ 0 ] = names[i]
x[i][ 1 ] = marks[i] + updates[i]
x[i][ 2 ] = i + 1
highest = x[ 0 ]
for j in range ( 1 , n):
if (x[j][ 1 ] > = highest[ 1 ]):
highest = x[j]
print ( "Name: " , highest[ 0 ], ", Jump: " ,
abs (highest[ 2 ] - 1 ), sep = "")
names = [ "sam" , "ram" , "geek" ]
marks = [ 80 , 79 , 75 ]
updates = [ 0 , 5 , - 9 ]
n = len (marks)
nameRank(names, marks, updates, n)
|
C#
using System;
class GFG{
public class Student
{
public string name;
public int marks, prev_rank;
public Student()
{
this .marks = 0;
this .prev_rank = 0;
}
}
static void nameRank( string []names, int []marks,
int []updates, int n)
{
Student []x = new Student[n];
for ( int i = 0; i < n; i++)
{
x[i] = new Student();
x[i].name = names[i];
x[i].marks = marks[i] + updates[i];
x[i].prev_rank = i + 1;
}
Student highest = x[0];
for ( int j = 1; j < n; j++)
{
if (x[j].marks >= highest.marks)
highest = x[j];
}
Console.Write( "Name: " + highest.name +
", Jump: " +
Math.Abs(highest.prev_rank - 1));
}
public static void Main( string [] args)
{
string []names = { "sam" , "ram" , "geek" };
int []marks = { 80, 79, 75 };
int []updates = { 0, 5, -9 };
int n = marks.Length;
nameRank(names, marks, updates, n);
}
}
|
Javascript
<script>
function nameRank(names, marks, updates, n)
{
let x = new Array(n);
for (let i = 0; i < n; i++)
{
x[i] = new Array(3);
for (let j = 0; j < 3; j++)
{
x[i][j] = 0;
}
}
for (let i = 0; i < n; i++)
{
x[i][0] = names[i]
x[i][1] = marks[i] + updates[i]
x[i][2] = i + 1
}
let highest = x[0];
for (let j = 1; j < n; j++)
{
if (x[j][1] >= highest[1])
highest = x[j]
}
document.write( "Name: " , highest[0], ", Jump: " ,
Math.abs(highest[2] - 1), sep = "" )
}
let names = [ "sam" , "ram" , "geek" ];
let marks = [ 80, 79, 75 ];
let updates = [ 0, 5, -9 ];
let n = marks.length;
nameRank(names, marks, updates, n)
</script>
|
Output
Name: ram, Jump: 1
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(N)
Last Updated :
13 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...