Dada una array A[] de tamaño N (1 ≤ N ≤ 10 5 ) , que consta de números enteros positivos, donde la puntuación de un índice i en el rango [0, N – 1] se define como:
Puntuación[i] = A[i] * ((i + A[i] < N) ? Puntuación(i + A[i]) : 1)
La tarea es encontrar el índice con la puntuación mínima.
Ejemplos:
Entrada: A[] = {1, 2, 3, 4, 5}
Salida: 2
Explicación:
- Puntuación[4] = 5 * 1 = 5
- Puntuación[3]. = 4 * 1 = 4
- Puntaje[2] = 3 * 1 = 3 (Mínimo)
- Puntuación[1] = 2 * Puntuación[3] = 2 * 4 = 8
- Puntuación[0] = 1 * Puntuación[1] = 1 * 8 = 8
Entrada: A[] = {9, 8, 1, 2, 3, 6}
Salida : 4
Enfoque: siga los pasos a continuación para resolver el problema
- Atraviese la array a la inversa, es decir, iterar desde i = N – 1 a 0 .
- Para cada i , comprueba si (A[i] + i) es menor que N o no.
- Actualice el puntaje[i] para el índice i como S core[i] = A[i] * ((i + A[i] < N) ? Score(i + A[i]) ? 1)
- Imprime el índice con la puntuación mínima.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; //Function to find the index //with minimum score void Min_Score_Index(int N, vector<int> A){ //Stores the score of current index vector<int> Score(N,0); //Traverse the array in reverse for (int i=N-1;i>=0;i--){ if (A[i] + i < N) Score[i] = A[i] * Score[A[i] + i]; else Score[i] = A[i]; } //Update minimum score int min_value = INT_MAX; for(int i:Score) min_value = min(i,min_value); //Print the index with minimum score int ind = 0; for(int i=0;i<N;i++){ if(Score[i]==min_value) ind = i; } cout<<(ind); } //Driver Code int main(){ int N = 5; vector<int> A ={1, 2, 3, 4, 5}; Min_Score_Index(N, A); }
Java
// Java program for the above approach import java.util.*; class GFG { // Function to find the index // with minimum score static void Min_Score_Index(int N, int []A) { // Stores the score of current index int []Score = new int[N]; // Traverse the array in reverse for (int i = N - 1; i >= 0; i--) { if (A[i] + i < N) Score[i] = A[i] * Score[A[i] + i]; else Score[i] = A[i]; } // Update minimum score int min_value = Integer.MAX_VALUE; for(int i:Score) min_value = Math.min(i, min_value); // Print the index with minimum score int ind = 0; for(int i = 0; i < N; i++) { if(Score[i] == min_value) ind = i; } System.out.print(ind); } // Driver Code public static void main(String[] args) { int N = 5; int []A ={1, 2, 3, 4, 5}; Min_Score_Index(N, A); } } // This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach # Function to find the index # with minimum score def Min_Score_Index(N, A): # Stores the score of current index Score = [0] * N # Traverse the array in reverse for i in range(N - 1, -1, -1): if A[i] + i < N: Score[i] = A[i] * Score[A[i] + i] else: Score[i] = A[i] # Update minimum score min_value = min(Score) # Print the index with minimum score ind = Score.index(min_value) print(ind) # Driver Code if __name__ == "__main__": N = 5 A = [1, 2, 3, 4, 5] Min_Score_Index(N, A) # This code is contributed by mohit kumar 29
C#
// C# program for the above approach using System; class GFG{ // Function to find the index // with minimum score static void Min_Score_Index(int N, int[] A) { // Stores the score of current index int[] Score = new int[N]; // Traverse the array in reverse for(int i = N - 1; i >= 0; i--) { if (A[i] + i < N) Score[i] = A[i] * Score[A[i] + i]; else Score[i] = A[i]; } // Update minimum score int min_value = Int32.MaxValue; foreach(int i in Score) { min_value = Math.Min(i, min_value); } // Print the index with minimum score int ind = 0; for(int i = 0; i < N; i++) { if (Score[i] == min_value) ind = i; } Console.WriteLine(ind); } // Driver Code public static void Main() { int N = 5; int[] A = { 1, 2, 3, 4, 5 }; Min_Score_Index(N, A); } } // This code is contributed by chitranayal
Javascript
<script> // JavaScript program for the above approach // Function to find the index // with minimum score function Min_Score_Index(N, A){ // Stores the score of current index var Score = Array(N).fill(0); // Traverse the array in reverse for (var i=N-1;i>=0;i--){ if (A[i] + i < N) Score[i] = A[i] * Score[A[i] + i]; else Score[i] = A[i]; } // Update minimum score var min_value = 1000000000; Score.forEach(i => { min_value = Math.min(i,min_value); }); // Print the index with minimum score var ind = 0; for(var i=0;i<N;i++){ if(Score[i]==min_value) ind = i; } document.write(ind); } // Driver Code var N = 5; var A =[1, 2, 3, 4, 5]; Min_Score_Index(N, A); </script>
Producción:
2
Complejidad temporal: O(N)
Espacio auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por kashishmishra9911 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA