Minimum time required to reach a given score | C ++ program for the above approach ; Function to calculate minimum time required to achieve given score target ; Store the frequency of elements ; Traverse the array p [ ] ; Update the frequency ; Stores the minimim time required ; Store the current score at any time instant t ; Iterate until sum is at least equal to target ; Increment time with every iteration ; Traverse the map ; Increment the points ; Print the time required ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE 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 ; }