Company Hiring

Found this question on web

An array consists of number of employees required for each month. ‘Hire’ is the cost of hiring an employee and ‘serv’ is the cost of removing an employee and ‘sal’ is the cost of retaining an employee for a month.Cost for each month will be (no_hires * hire + no_fired * serv + no_retain * sal) Given, the requirement for any month will strictly lie between 1 and 41. What is the minimum possible total cost for the ‘n’ months.


How to start ,i think there are many possibilities here ,How to approach??

@vijju123 Can u help in this?

I dont think there are that many possibilities.
Simply for each day calculate the value for all possible hiring,take the max value and add to max value of previous day.

Ie on first day u have only 1 choice,do arr[1] hiring.

On second day , u can do arr[2]-arr[1] < k < arr[2]. Hirings

And so on…

With number of hirings ,u can easily calculate nofired and retain.

Hope this helps.Correct me if i am wrong as the question is not that clear.

@vivek_1998299 it depends on the cost of hire,serv and sal isn’t it? so u can’t directly do arr[2]-arr[1]<k<arr[2] hirings.

First let me say what i am assuming.
We can fire only people from prev day.

I mean lets say there are 5 people on day1.
I can fire only on day2 these people.
So on day2,i can fire 0,5 people.

Now regarding what u are saying,i am actually calculating it for all possible combinations for a day and taking maximum among them.

It would help if u make the question a bit clear.

"Calculating it for all possible combinations for a day " Can u elaborate more how you are calculating?
By the way yes u can fire only the people who are there in the previous month ,so u can fire only 0-5 people yes u r correct in that

Lets say no_of_employee arr is

So on first day ,5 hire (no other option)
So cost1=5*50=250

On 2nd day

Hire. Fired. Retain. Cost

  1. |…| 0. |…| 5. |…| 1 * 50+0 * 40+5 * 30=200

  2. |…| 1. |…| 4. |…| 2 * 50+1 * 40+4 * 30=260

  3. |…| 2. |…| 3. |…| 3 *50+2 * 40+3 * 30=320

  4. |…| 3. |…| 2. |…| 4* 50+3* 40+2* 30=380

  5. |…| 4. |…| 1. |…| 5* 50+4* 40+1 *30=440

  6. |…| 5. |…| 0. |…| 6* 50+5* 40+0* 30=500

So the first option is more feasible


If i am doing anything wrong,plz correct me.

I could reach to the deduction that for 1 hire only 1 combination exist by:
Let x be no_of employee in prev day
Let y be no.of hired in current day
Let z be req. No of employee in current day

So we have following

For all valid y’s:



Retain =z-y

Since z and y are constant. So retain is constant

Since retain is constant,fired is constant,ie there is unique solution

y is not a constant ,only z is constant.Please explain why y is a constant??

As i said previously i am calculting for all y.
So lets say if i hire 1 people so y=1 so constant

Similarly for y=2

I mean y is constant for 1 particular combination.

You have hire cost for hiring, serv cost for firing and sal cost for retaining

There are two cases in it

  • hire+serv < sal

Then you should hire new employees every month as paying salary will cost more then firing and hiring them again. So find the total number of employees required for n months and calculate the hiring cost and firing cost for n-1 months. Sum will be the answer

  • sal < hire+serv

In this case, pay salary for the number of emplyees which are required in next month. if more employees are required pay hire price for them and if less are required, remove remaining

There may be few more cases, if i am not wrong

Sorry I didnt see this until now. I will look at it right away.

The question is extremely unclear. What do you mean by Given, the requirement for any month will strictly lie between 1 and 41. ? Also, whats the constraints for number of months?

If its available on net, can you please give the Q link?

@vijju123 question link was given in the question itself

that means that the number of employees required for a month will never exceed 41