Skip to content
Related Articles

Related Articles

Minimum time required to reach a given score
  • Difficulty Level : Expert
  • Last Updated : 14 Dec, 2020

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 <bits/stdc++.h>
using namespace std;
 
// Function to calculate minimum time
// required to achieve given score target
void findMinimumTime(int* p, int n,
                     int target)
{
    // Store the frequency of elements
    unordered_map<int, int> um;
 
    // Traverse the array p[]
    for (int i = 0; i < n; i++) {
 
        // Update the frequency
        um[p[i]]++;
    }
 
    // Stores the minimim 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 Code
int 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 approach
import java.util.*;
import java.io.*;
  
class GFG{
      
// Function to calculate minimum time
// required to achieve given score target
static void findMinimumTime(int [] p, int n,
                            int target)
{
      
    // Store the frequency of elements
    HashMap<Integer,
            Integer> 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 minimim 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<Integer,
                 Integer> it : um.entrySet())
        {
              
            // Increment the points
            sum += it.getValue() * (time / it.getKey());
        }
    }
  
    // Print the time required
    System.out.println(time);
}
 
// Driver Code
public 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 target
def 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 minimim 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)
 
    # Prthe time required
    print(time)
 
# Driver Code
if __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 approach
using System.Collections.Generic;
using System;
using System.Linq;
 
class GFG{
 
// Function to calculate minimum time
// required to achieve given score target
static void findMinimumTime(int [] p, int n,
                            int target)
{
     
    // Store the frequency of elements
    Dictionary<int,
               int> um = new Dictionary<int,
                                        int>();
                                         
    // 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 minimim 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 Code
public 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
Output
6

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :