Total number of days taken to complete the task if after certain days one person leaves
Last Updated :
30 May, 2022
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.
Examples:
Input: a = 10, b = 20, n = 5
Output: 10
Input: a = 5, b = 15, n = 3
Output: 6
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:
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfDays( int a, int b, int n)
{
int Days = b * (n + a) / (a + b);
return Days;
}
int main()
{
int a = 10, b = 20, n = 5;
cout << numberOfDays(a, b, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int numberOfDays( int a, int b, int n)
{
int Days = b * (n + a) / (a + b);
return Days;
}
public static void main (String[] args)
{
int a = 10 , b = 20 , n = 5 ;
System.out.println (numberOfDays(a, b, n));
}
}
|
Python3
def numberOfDays(a, b, n):
Days = b * (n + a) / / (a + b)
return Days
if __name__ = = "__main__" :
a = 10
b = 20
n = 5
print (numberOfDays(a, b, n))
|
C#
using System;
class GFG
{
static int numberOfDays( int a, int b, int n)
{
int Days = b * (n + a) / (a + b);
return Days;
}
public static void Main ()
{
int a = 10, b = 20, n = 5;
Console.WriteLine(numberOfDays(a, b, n));
}
}
|
Javascript
<script>
function numberOfDays(a , b , n)
{
var Days = b * (n + a) / (a + b);
return Days;
}
var a = 10, b = 20, n = 5;
document.write(numberOfDays(a, b, n));
</script>
|
Time Complexity: O(1), since there is only basic arithmetic operation.
Auxiliary Space: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...