Dadas tres arrays nombres[] , marcas[] y actualizaciones[] donde:
- names[] contiene los nombres de los estudiantes.
- notes[] contiene las notas de los mismos estudiantes.
- actualizaciones[] contiene los números enteros por los cuales se actualizarán las calificaciones de estos estudiantes.
La tarea es encontrar el nombre del estudiante con las máximas calificaciones después de la actualización y el salto en el rango del estudiante, es decir, rango anterior – rango actual .
Nota: Los detalles de los estudiantes están en orden descendente de sus calificaciones y si más de dos estudiantes obtuvieron calificaciones iguales (también la más alta), entonces elija el que tuvo una calificación más baja anteriormente.
Ejemplos:
Entrada: nombres[] = {“sam”, “ram”, “geek”, “sonu”},
marcas[] = {99, 75, 70, 60},
actualizaciones[] = {-10, 5, 9, 39}
Salida: Nombre: sonu, Salto: 3
Las marcas actualizadas son {89, 80, 79, 99}, está claro que sonu tiene las marcas más altas con un salto de 3Entrada: nombres[] = {“sam”, “ram”, “geek”},
marcas[] = {80, 79, 75},
actualizaciones[] = {0, 5, -9}
Salida: Nombre: ram, Saltar: 1
Enfoque: Cree una estructura struct Student para almacenar la información de cada estudiante, cada uno de los cuales tendrá 3 atributos nombre del estudiante, calificaciones del estudiante, prev_rank del estudiante.
Ahora, actualice las calificaciones de acuerdo con el contenido de las actualizaciones [] luego, con un solo recorrido de la array, encuentre al estudiante que tiene las calificaciones más altas después de la actualización.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Structure to store the information of // students struct Student { string name; int marks = 0; int prev_rank = 0; }; // Function to print the name of student who // stood first after updation in rank void nameRank(string names[], int marks[], int updates[], int n) { // Array of students Student x[n]; for (int i = 0; i < n; i++) { // Store the name of the student x[i].name = names[i]; // Update the marks of the student x[i].marks = marks[i] + updates[i]; // Store the current rank of the student 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]; } // Print the name and jump in rank cout << "Name: " << highest.name << ", Jump: " << abs(highest.prev_rank - 1) << endl; } // Driver code int main() { // Names of the students string names[] = { "sam", "ram", "geek" }; // Marks of the students int marks[] = { 80, 79, 75 }; // Updates that are to be done int updates[] = { 0, 5, -9 }; // Number of students int n = sizeof(marks) / sizeof(marks[0]); nameRank(names, marks, updates, n); }
Java
// Java implementation of the approach import java.util.*; class GFG{ static class Student { String name; int marks, prev_rank; Student() { this.marks = 0; this.prev_rank = 0; } } // Function to print the name of student who // stood first after updation in rank static void nameRank(String []names, int []marks, int []updates, int n) { // Array of students Student []x = new Student[n]; for(int i = 0; i < n; i++) { x[i] = new Student(); // Store the name of the student x[i].name = names[i]; // Update the marks of the student x[i].marks = marks[i] + updates[i]; // Store the current rank of the student 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]; } // Print the name and jump in rank System.out.print("Name: " + highest.name + ", Jump: " + Math.abs(highest.prev_rank - 1)); } // Driver code public static void main(String[] args) { // Names of the students String []names = { "sam", "ram", "geek" }; // Marks of the students int []marks = { 80, 79, 75 }; // Updates that are to be done int []updates = { 0, 5, -9 }; // Number of students int n = marks.length; nameRank(names, marks, updates, n); } } // This code is contributed by pratham76
Python3
# Python3 implementation of the approach # Function to print the name of student who # stood first after updation in rank def nameRank(names, marks, updates, n): # Array of students x = [[0 for j in range(3)] for i in range(n)] for i in range(n): # Store the name of the student x[i][0] = names[i] # Update the marks of the student x[i][1]= marks[i] + updates[i] # Store the current rank of the student x[i][2] = i + 1 highest = x[0] for j in range(1, n): if (x[j][1] >= highest[1]): highest = x[j] # Print the name and jump in rank print("Name: ", highest[0], ", Jump: ", abs(highest[2] - 1), sep="") # Driver code # Names of the students names= ["sam", "ram", "geek"] # Marks of the students marks = [80, 79, 75] # Updates that are to be done updates = [0, 5, -9] # Number of students n = len(marks) nameRank(names, marks, updates, n) # This code is contributed by SHUBHAMSINGH10
C#
// C# implementation of the approach using System; class GFG{ public class Student { public string name; public int marks, prev_rank; public Student() { this.marks = 0; this.prev_rank = 0; } } // Function to print the name of student who // stood first after updation in rank static void nameRank(string []names, int []marks, int []updates, int n) { // Array of students Student []x = new Student[n]; for(int i = 0; i < n; i++) { x[i] = new Student(); // Store the name of the student x[i].name = names[i]; // Update the marks of the student x[i].marks = marks[i] + updates[i]; // Store the current rank of the student 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]; } // Print the name and jump in rank Console.Write("Name: " + highest.name + ", Jump: " + Math.Abs(highest.prev_rank - 1)); } // Driver code public static void Main(string[] args) { // Names of the students string []names = { "sam", "ram", "geek" }; // Marks of the students int []marks = { 80, 79, 75 }; // Updates that are to be done int []updates = { 0, 5, -9 }; // Number of students int n = marks.Length; nameRank(names, marks, updates, n); } } // This code is contributed by rutvik_56
Javascript
<script> // Javascript implementation of the approach // Function to print the name of student who // stood first after updation in rank function nameRank(names, marks, updates, n) { // Array of students 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++) { // Store the name of the student x[i][0] = names[i] // Update the marks of the student x[i][1] = marks[i] + updates[i] // Store the current rank of the student 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] } // Print the name and jump in rank document.write("Name: ", highest[0], ", Jump: ", Math.abs(highest[2] - 1), sep = "") } // Driver code // Names of the students let names = [ "sam", "ram", "geek" ]; // Marks of the students let marks = [ 80, 79, 75 ]; // Updates that are to be done let updates = [ 0, 5, -9 ]; // Number of students let n = marks.length; nameRank(names, marks, updates, n) // This code is contributed by unknown2108 </script>
Name: ram, Jump: 1
Complejidad temporal: O(N)
Espacio auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por mohit kumar 29 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA