Find the index with minimum score from a given array Last Updated : 14 Jun, 2021 Comments Improve Suggest changes Like Article Like Report Given an array A[] of size N(1 ? N ? 105), consisting of positive integers, where the score of an index i in range [0, N - 1] is defined as: Score[i] = A[i] * ((i + A[i] < N) ? Score(i + A[i]) : 1) The task is to find the index with minimum score. Examples: Input: A[] = {1, 2, 3, 4, 5}Output: 2Explanation: Score[4] = 5 * 1 = 5Score[3]. = 4 * 1 = 4Score[2] = 3 * 1 = 3 (Minimum)Score[1] = 2 * Score[3] = 2 * 4 = 8Score[0] = 1 * Score[1] = 1 * 8 = 8 Input: A[] = {9, 8, 1, 2, 3, 6}Output : 4 Approach: Follow the steps below to solve the problem Traverse the array in reverse, i.e. iterate from i = N - 1 to 0.For every i, check if (A[i] + i) is less than N or not.Update score[i] for index i as Score[i] = A[i] * ((i + A[i] < N) ? Score(i + A[i]) ? 1)Print the index with minimum score. Below is the implementation of above approach: 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> Output: 2 Time Complexity: O(N)Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Find the index with minimum score from a given array K kashishmishra9911 Follow Improve Article Tags : Dynamic Programming Pattern Searching Searching Python DSA Arrays +2 More Practice Tags : ArraysDynamic ProgrammingPattern SearchingpythonSearching +1 More Similar Reads Queries for the minimum element in an array excluding the given index range Given an array arr[] of N integers and Q queries where each query consists of an index range [L, R]. For each query, the task is to find the minimum element in the array excluding the elements from the given index range. Examples: Input: arr[] = {3, 2, 1, 4, 5}, Q[][] = {{1, 2}, {2, 3}} Output: 3 2 15+ min read Find all good indices in the given Array Given an array A[] of integers. The task is to print all indices i of this array such that after removing the ith element from the array, the array becomes a good array. Note: An array is good if there is an element in the array that equals to the sum of all other elements.1-based indexing is consid 6 min read Minimum steps to reach a given index in the Array based on given conditions Given an array arr[ ] of size N consisting of integers -1, 0, 1 only and an array q[ ] consisting of queries. In the array arr[ ], -1 signifies that any index to the left of it is reachable and 1 signifies that any index to the right of it is reachable from that index. The index in a particular quer 10 min read Minimum in an array which is first decreasing then increasing Given an array of N integers where array elements form a strictly decreasing and increasing sequence. The task is to find the smallest number in such an array. Constraints: N >= 3 Examples: Input: a[] = {2, 1, 2, 3, 4}Output: 1Input: a[] = {8, 5, 4, 3, 4, 10}Output: 3 A naive approach is to linea 12 min read Find a number with least sum of bit differences from given Array Given an array arr[] and an integer L which represents the number of bits to be considered in bit representation of an array element. The task is to find a positive integer X, such that the sum of the bit difference of all the elements in arr[] with X is minimum. Examples: Input: N = 3, L = 5, arr[] 8 min read Minimum operations to make Array sum at most S from given Array Given an array arr[], of size N and an integer S, the task is to find the minimum operations to make the sum of the array less than or equal to S. In each operation: Any element can be chosen and can be decremented by 1, orCan be replaced by any other element in the array. Examples: Input: arr[]= {1 10 min read Find minimum value of arr[j] + abs(j - i) for all indices Given an array arr[] of size N, the task is for each index i (1â¤iâ¤n) find it's minimum value, that can be calculated by the formula (arr[j] + abs(jâi)), for 0<=j<n and j != i. Examples: Input: arr = { 1, 3, 11, 2, 15, 7, 5 }Output: 4 2 3 4 3 4 5Explanation: For i = 0, the minimum value is for 9 min read Maximize score of same-indexed subarrays selected from two given arrays Given two arrays A[] and B[], both consisting of N positive integers, the task is to find the maximum score among all possible same-indexed subarrays in both the arrays such that the score of any subarray over the range [L, R] is calculated by the maximum of the values (AL*BL + AL + 1*BL + 1 + ... + 15+ min read Find index with minimum difference between prefix and suffix average of given Array Given an array arr[] of size N, the task is to find an index i such that the difference between prefix average and suffix average is minimum, i.e. average of elements till i (in range [0, i]) and the average of elements after i (in range [i+1, N)) is minimum. Examples: Input: arr[] = {2, 9, 3, 5}Out 8 min read Index with Minimum sum of prefix and suffix sums in an Array Given an array of integers. The task is to find the index i in the array at which the value of prefixSum(i) + suffixSum(i) is minimum.Note: PrefixSum(i) = The sum of first i numbers of the array.SuffixSum(i) = the sum of last N - i + 1 numbers of the array.1-based indexing is considered for the arra 10 min read Like