Given that person A takes a days to do a certain piece of work while person B takes b days to do the same work. If A and B started the work together and n days before the completion of work, A leaves the work. Find the total number of days taken to complete work.
Input: a = 10, b = 20, n = 5
Input: a = 5, b = 15, n = 3
Approach: Let D be the total number of days to complete the work. A and B work together for D – n days. So,
(D – n) * (1/a + 1/b) + (n) * (1/b) = 1
D * (1/a + 1/b) – (n/a) – (n/b) + (n/b) = 1
D * (1/a + 1/b) = (n + a) / a
D = b * (n + a) / (a + b)
Below is the implementation of the above approach:
Time Complexity: O(1 )
Auxiliary Space: O(1)
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live