# Minimum time required to reach a given score

• Difficulty Level : Expert
• Last Updated : 17 Jan, 2023

Given an integer target and an array arr[] consisting of N positive integers where arr[i] denotes the time required to score 1 point for the ith array element, the task is to find the minimum time required to obtain the score target from the given array.

Examples:

Input: arr[] = {1, 3, 3, 4}, target = 10
Output: 6
Explanation:
At time instant, t = 1: Score of the array: {1, 0, 0, 0}
At time instant, t = 2: Score of the array: {2, 0, 0, 0}
At time instant, t = 3: Score of the array: {3, 1, 1, 0}
At time instant, t = 4: Score of the array: {4, 1, 1, 1}
At time instant, t = 5: Score of the array: {5, 1, 1, 1}
At time instant, t = 6: Score of the array: {6, 2, 2, 1}
Total score of the array after t = 5 = 6 + 2 + 2 + 1 = 11 ( > 10). Therefore, the

Input: arr[] = {2, 4, 3}, target = 20
Output: 20

Approach: The idea is to use Hashing to store the frequency of array elements and iterate over the Hashmap and keep updating the score until it reaches target. An important observation is that any element, arr[i] adds t / arr[i] to the score at any time instant t. Follow the steps below to solve the problem:

• Initialize a variable, say time, to store the minimum time required and sum with 0 to store the score at any time instant t.
• Store the frequency of array elements in a Hashmap M.
• Iterate until the sum is less than target and perform the following steps:
• Set sum to 0 and increment the value of time by 1.
• Now, traverse the hashmap M and increment the value of sum by frequency * (time / value) in each iteration.
• After completing the above steps, print the value of time as the result.

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach#include using namespace std; // Function to calculate minimum time// required to achieve given score targetvoid findMinimumTime(int* p, int n,                     int target){    // Store the frequency of elements    unordered_map um;     // Traverse the array p[]    for (int i = 0; i < n; i++) {         // Update the frequency        um[p[i]]++;    }     // Stores the minimum time required    int time = 0;     // Store the current score    // at any time instant t    int sum = 0;     // Iterate until sum is at    // least equal to target    while (sum < target) {         sum = 0;         // Increment time with        // every iteration        time++;         // Traverse the map        for (auto& it : um) {             // Increment the points            sum += it.second                   * (time / it.first);        }    }     // Print the time required    cout << time;} // Driver Codeint main(){    int arr[] = { 1, 3, 3, 4 };    int N = sizeof(arr) / sizeof(arr[0]);    int target = 10;     // Function Call    findMinimumTime(arr, N, target);     return 0;}

## Java

 // Java program for the above approachimport java.util.*;import java.io.*;  class GFG{      // Function to calculate minimum time// required to achieve given score targetstatic void findMinimumTime(int [] p, int n,                            int target){          // Store the frequency of elements    HashMap um = new HashMap<>();                                              // Traverse the array p[]    for(int i = 0; i < n; i++)    {                  // Update the frequency        if (!um.containsKey(p[i]))            um.put(p[i], 1);        else            um.put(p[i],            um.get(p[i]) + 1);    }      // Stores the minimum time required    int time = 0;      // Store the current score    // at any time instant t    int sum = 0;          while (sum < target)    {        sum = 0;                  // Increment time with        // every iteration        time++;          // Traverse the map        for(Map.Entry it : um.entrySet())        {                          // Increment the points            sum += it.getValue() * (time / it.getKey());        }    }      // Print the time required    System.out.println(time);} // Driver Codepublic static void main(String args[]){    int[] arr = { 1, 3, 3, 4 };    int N = arr.length;    int target = 10;          // Function Call    findMinimumTime(arr, N, target);}} // This code is contributed by susmitakundugoaldanga

## Python3

 # Python3 program for the above approach # Function to calculate minimum time# required to achieve given score targetdef findMinimumTime(p, n, target):         # Store the frequency of elements    um = {}     # Traverse the array p[]    for i in range(n):         # Update the frequency        um[p[i]] = um.get(p[i], 0) + 1     # Stores the minimum time required    time = 0     # Store the current score    # at any time instant t    sum = 0     # Iterate until sum is at    # least equal to target    while (sum < target):         sum = 0         # Increment time with        # every iteration        time += 1         # Traverse the map        for it in um:             # Increment the points            sum += um[it] * (time // it)     # Print time required    print(time) # Driver Codeif __name__ == '__main__':    arr = [1, 3, 3, 4]    N = len(arr)    target = 10     # Function Call    findMinimumTime(arr, N, target) # This code is contributed by mohit kumar 29

## C#

 // C# program for the above approachusing System.Collections.Generic;using System;using System.Linq; class GFG{ // Function to calculate minimum time// required to achieve given score targetstatic void findMinimumTime(int [] p, int n,                            int target){         // Store the frequency of elements    Dictionary um = new Dictionary();                                             // Traverse the array p[]    for(int i = 0; i < n; i++)    {                 // Update the frequency        if (um.ContainsKey(p[i]) == true)            um[p[i]] += 1;        else            um[p[i]] = 1;    }     // Stores the minimum time required    int time = 0;     // Store the current score    // at any time instant t    int sum = 0;     // Iterate until sum is at    // least equal to target    var val = um.Keys.ToList();         while (sum < target)    {        sum = 0;                 // Increment time with        // every iteration        time++;         // Traverse the map        foreach(var key in val)        {                         // Increment the points            sum += um[key] * (time / key);        }    }     // Print the time required    Console.WriteLine(time);} // Driver Codepublic static void Main(){    int []arr = { 1, 3, 3, 4 };    int N = arr.Length;    int target = 10;         // Function Call    findMinimumTime(arr, N, target);}} // This code is contributed by Stream_Cipher

## Javascript



Output

6

Time Complexity- 0(target*N)
Auxiliary Space- 0(N)

Optimised Approach:

If we use binary search here for l=1 to h=target until we find the minimum time for which we reach the target score for time t the target score is a[0]/t+a[1]/t+….. if it is greater than equal to target then this time is possible.

## C++

 #include using namespace std;bool possible(int arr[],int n,int target,int mid){  int sum=0;     for(int i=0;i=target) return true;  }     return false;   }int solve(int arr[],int target,int n){  int l=1;  int h=target;  int ans=0;  while(l<=h)  {    int mid=l+(h-l)/2;    if(possible(arr,n,target,mid))    {      ans=mid;      h=mid-1;    }    else      l=mid+1;  }     return ans;}int main() {     int arr[]={1,3,3,4};    int target=10;    int n=sizeof(arr)/sizeof(arr[0]);    cout<

Output

6

Time Complexity: O(log(target)*N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up