Minimum time required to reach a given score
Last Updated :
16 Mar, 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++
#include <bits/stdc++.h>
using namespace std;
void findMinimumTime( int * p, int n,
int target)
{
unordered_map< int , int > um;
for ( int i = 0; i < n; i++) {
um[p[i]]++;
}
int time = 0;
int sum = 0;
while (sum < target) {
sum = 0;
time ++;
for ( auto & it : um) {
sum += it.second
* ( time / it.first);
}
}
cout << time ;
}
int main()
{
int arr[] = { 1, 3, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
int target = 10;
findMinimumTime(arr, N, target);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
static void findMinimumTime( int [] p, int n,
int target)
{
HashMap<Integer,
Integer> um = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
if (!um.containsKey(p[i]))
um.put(p[i], 1 );
else
um.put(p[i],
um.get(p[i]) + 1 );
}
int time = 0 ;
int sum = 0 ;
while (sum < target)
{
sum = 0 ;
time++;
for (Map.Entry<Integer,
Integer> it : um.entrySet())
{
sum += it.getValue() * (time / it.getKey());
}
}
System.out.println(time);
}
public static void main(String args[])
{
int [] arr = { 1 , 3 , 3 , 4 };
int N = arr.length;
int target = 10 ;
findMinimumTime(arr, N, target);
}
}
|
Python3
def findMinimumTime(p, n, target):
um = {}
for i in range (n):
um[p[i]] = um.get(p[i], 0 ) + 1
time = 0
sum = 0
while ( sum < target):
sum = 0
time + = 1
for it in um:
sum + = um[it] * (time / / it)
print (time)
if __name__ = = '__main__' :
arr = [ 1 , 3 , 3 , 4 ]
N = len (arr)
target = 10
findMinimumTime(arr, N, target)
|
C#
using System.Collections.Generic;
using System;
using System.Linq;
class GFG{
static void findMinimumTime( int [] p, int n,
int target)
{
Dictionary< int ,
int > um = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
{
if (um.ContainsKey(p[i]) == true )
um[p[i]] += 1;
else
um[p[i]] = 1;
}
int time = 0;
int sum = 0;
var val = um.Keys.ToList();
while (sum < target)
{
sum = 0;
time++;
foreach ( var key in val)
{
sum += um[key] * (time / key);
}
}
Console.WriteLine(time);
}
public static void Main()
{
int []arr = { 1, 3, 3, 4 };
int N = arr.Length;
int target = 10;
findMinimumTime(arr, N, target);
}
}
|
Javascript
<script>
function findMinimumTime( p, n,
target)
{
let um = new Map();
for (let i = 0; i < n; i++) {
if (um[p[i]])
um[p[i]]++;
else
um[p[i]] = 1
}
let time = 0;
let sum = 0;
while (sum < target) {
sum = 0;
time++;
for (let it in um) {
sum += um[it]
* Math.floor(time / it);
}
}
document.write(time);
}
let arr = [1, 3, 3, 4];
let N = arr.length;
let target = 10;
findMinimumTime(arr, N, target);
</script>
|
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 <iostream>
using namespace std;
bool possible( int arr[], int n, int target, int mid){
int sum=0;
for ( int i=0;i<n;i++)
{
sum+=mid/arr[i];
if (sum>=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<<solve(arr,target,n)<<endl;
return 0;
}
|
Java
public class Main {
public static boolean possible( int [] arr, int n, int target, int mid) {
int sum = 0 ;
for ( int i = 0 ; i < n; i++) {
sum += mid / arr[i];
if (sum >= target) {
return true ;
}
}
return false ;
}
public static 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;
}
public static void main(String[] args) {
int [] arr = { 1 , 3 , 3 , 4 };
int target = 10 ;
int n = arr.length;
System.out.println(solve(arr, target, n));
}
}
|
Python3
def possible(arr, n, target, mid):
sum = 0
for i in range (n):
sum + = mid / / arr[i]
if sum > = target:
return True
return False
def solve(arr, target, n):
l = 1
h = target
ans = 0
while l < = h:
mid = l + (h - l) / / 2
if possible(arr, n, target, mid):
ans = mid
h = mid - 1
else :
l = mid + 1
return ans
if __name__ = = "__main__" :
arr = [ 1 , 3 , 3 , 4 ]
target = 10
n = len (arr)
print (solve(arr, target, n))
|
C#
using System;
class GFG {
public static bool Possible( int [] arr, int n,
int target, int mid)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += mid / arr[i];
if (sum >= target)
return true ;
}
return false ;
}
public static 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;
}
static void Main( string [] args)
{
int [] arr = { 1, 3, 3, 4 };
int target = 10;
int n = arr.Length;
Console.WriteLine(Solve(arr, target, n));
}
}
|
Javascript
function possible(arr, n, target, mid)
{
let sum = 0;
for (let i = 0; i < n; i++) {
sum += Math.floor(mid / arr[i]);
if (sum >= target) {
return true ;
}
}
return false ;
}
function solve(arr, target, n)
{
let l = 1;
let h = target;
let ans = 0;
while (l <= h) {
let mid = l + Math.floor((h - l) / 2);
if (possible(arr, n, target, mid)) {
ans = mid;
h = mid - 1;
} else {
l = mid + 1;
}
}
return ans;
}
let arr = [1, 3, 3, 4];
let target = 10;
let n = arr.length;
console.log(solve(arr, target, n));
|
Time Complexity: O(log(target)*N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...