C++ code to find minimum time needed to do all tasks

Suppose we have an array A with n elements, and two other arrays k and x. The ith task takes A[i] time to complete. The given A is sorted in non-decreasing fashion. Amal takes at most k tasks and do each of them in x units of time instead of A[i]. (x

So, if the input is like A = [3, 6, 7, 10]; k = 2; x = 2, then the output will be 13, because the best option would be to do the third and the fourth tasks, spending x = 2 time on each instead of A[2] and A[3]. Then the answer is 3 + 6 + 2 + 2 = 13.

Steps

To solve this, we will follow these steps −

x := k * x
n := size of A
for initialize i := 0, when i 

Example

Let us see the following implementation to get better understanding −

#include 
using namespace std;
int solve(vector A, int k, int x){
   x = k * x;
   int n = A.size();
   for (int i = 0; i  A = { 3, 6, 7, 10 };
   int k = 2;
   int x = 2;
   cout 

Input

{ 3, 6, 7, 10 }, 2, 2

Output

13
Updated on: 2022-03-30T14:14:31+05:30

551 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements