Find the index with minimum score from a given array
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: 2
Explanation:
- Score[4] = 5 * 1 = 5
- Score[3]. = 4 * 1 = 4
- Score[2] = 3 * 1 = 3 (Minimum)
- Score[1] = 2 * Score[3] = 2 * 4 = 8
- Score[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++
#include <bits/stdc++.h>
using namespace std;
void Min_Score_Index( int N, vector< int > A){
vector< int > Score(N,0);
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];
}
int min_value = INT_MAX;
for ( int i:Score) min_value = min(i,min_value);
int ind = 0;
for ( int i=0;i<N;i++){
if (Score[i]==min_value) ind = i;
}
cout<<(ind);
}
int main(){
int N = 5;
vector< int > A ={1, 2, 3, 4, 5};
Min_Score_Index(N, A);
}
|
Java
import java.util.*;
class GFG
{
static void Min_Score_Index( int N, int []A)
{
int []Score = new int [N];
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];
}
int min_value = Integer.MAX_VALUE;
for ( int i:Score) min_value = Math.min(i, min_value);
int ind = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (Score[i] == min_value)
ind = i;
}
System.out.print(ind);
}
public static void main(String[] args)
{
int N = 5 ;
int []A ={ 1 , 2 , 3 , 4 , 5 };
Min_Score_Index(N, A);
}
}
|
Python3
def Min_Score_Index(N, A):
Score = [ 0 ] * N
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]
min_value = min (Score)
ind = Score.index(min_value)
print (ind)
if __name__ = = "__main__" :
N = 5
A = [ 1 , 2 , 3 , 4 , 5 ]
Min_Score_Index(N, A)
|
C#
using System;
class GFG{
static void Min_Score_Index( int N, int [] A)
{
int [] Score = new int [N];
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];
}
int min_value = Int32.MaxValue;
foreach ( int i in Score)
{
min_value = Math.Min(i, min_value);
}
int ind = 0;
for ( int i = 0; i < N; i++)
{
if (Score[i] == min_value)
ind = i;
}
Console.WriteLine(ind);
}
public static void Main()
{
int N = 5;
int [] A = { 1, 2, 3, 4, 5 };
Min_Score_Index(N, A);
}
}
|
Javascript
<script>
function Min_Score_Index(N, A){
var Score = Array(N).fill(0);
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];
}
var min_value = 1000000000;
Score.forEach(i => {
min_value = Math.min(i,min_value);
});
var ind = 0;
for ( var i=0;i<N;i++){
if (Score[i]==min_value) ind = i;
}
document.write(ind);
}
var N = 5;
var A =[1, 2, 3, 4, 5];
Min_Score_Index(N, A);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
14 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...