Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmMinimum time required to fill a cistern using N pipes

Minimum time required to fill a cistern using N pipes

Given the time required by a total of N+1 pipes where N pipes are used to fill the Cistern and a single pipe is used to empty the Cistern. The task is to Calculate the amount of time in which the Cistern will get filled if all the N+1 pipes are opened together.

Examples

Input: n = 2, 
pipe1 = 12 hours, pipe2 = 14 hours,
emptypipe = 30 hours
Output: 8 hours

Input: n = 1, 
pipe1 = 12 hours
emptypipe = 18 hours
Output: 36 hours 

Approach: 

  • If a pipe1 can fill a cistern in ‘n’ hours, then in 1 hour, the pipe1 will able to fill ‘1/n’ Cistern.
  • Similarly If a pipe2 can fill a cistern in ‘m’ hours, then in one hour, the pipe2 will able to fill ‘1/m’ Cistern.
  • So on…. for other pipes.

So, total work done in filling a Cistern by N pipes in 1 hours is 

1/n + 1/m + 1/p…… + 1/z
Where n, m, p ….., z are the number of hours taken by each pipes respectively.

The result of the above expression will be the part of work done by all pipes together in 1 hours, let’s say a / b.
To calculate the time taken to fill the cistern will be b / a.

Consider an example of two pipes: 

Time taken by 1st pipe to fill the cistern = 12 hours 
Time taken by 2nd pipe to fill the cistern = 14 hours 
Time taken by 3rd pipe to empty the cistern = 30 hours
Work done by 1st pipe in 1 hour = 1/12 
Work done by 2nd pipe in 1 hour = 1/14 
Work done by 3rd pipe in 1 hour = – (1/30) as it empty the pipe. 
So, total work done by all the pipes in 1 hour is 
=> ( 1 / 12 + 1/ 14 ) – (1 / 30) 
=> ((7 + 6 ) / (84)) – (1 / 30) 
=> ((13) / (84)) – (1 / 30) 
=> 51 / 420
So, to Fill the cistern time required will be 420 / 51 i.e 8 hours Approx.

Below is the implementation of above approach:  

C++




// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the time
float Time(float arr[], int n, int Emptypipe)
{
 
    float fill = 0;
    for (int i = 0; i < n; i++)
        fill += 1 / arr[i];
 
    fill = fill - (1 / (float)Emptypipe);
 
    return 1 / fill;
}
 
// Driver Code
int main()
{
    float arr[] = { 12, 14 };
    float Emptypipe = 30;
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << floor(Time(arr, n, Emptypipe)) << " Hours";
 
    return 0;
}


Java




// Java implementation of
// above approach
import java.io.*;
 
class GFG
{
     
// Function to calculate the time
static float Time(float arr[], int n,
                  float Emptypipe)
{
    float fill = 0;
    for (int i = 0; i < n; i++)
        fill += 1 / arr[i];
 
    fill = fill - (1 / (float)Emptypipe);
 
    return 1 / fill;
}
 
// Driver Code
public static void main (String[] args)
{
    float arr[] = { 12, 14 };
    float Emptypipe = 30;
    int n = arr.length;
     
    System.out.println((int)(Time(arr, n,
                        Emptypipe)) + " Hours");
}
}
 
// This code is contributed
// by inder_verma.


Python3




# Python3 implementation of
# above approach
 
# Function to calculate the time
def Time(arr, n, Emptypipe) :
 
    fill = 0
    for i in range(0,n) :
        fill += (1 / arr[i])
 
    fill = fill - (1 / float(Emptypipe))
 
    return int(1 / fill)
 
 
# Driver Code
if __name__=='__main__':
    arr = [ 12, 14 ]
    Emptypipe = 30
    n = len(arr)
    print((Time(arr, n, Emptypipe))
          , "Hours")
 
# This code is contributed by
# Smitha Dinesh Semwal


C#




// C# implementation of
// above approach
using System;
 
class GFG
{
     
// Function to calculate the time
static float Time(float []arr, int n,
                  float Emptypipe)
{
    float fill = 0;
    for (int i = 0; i < n; i++)
        fill += 1 / arr[i];
 
    fill = fill - (1 / (float)Emptypipe);
 
    return 1 / fill;
}
 
// Driver Code
public static void Main ()
{
    float []arr = { 12, 14 };
    float Emptypipe = 30;
    int n = arr.Length;
     
    Console.WriteLine((int)(Time(arr, n,
                             Emptypipe)) +
                                " Hours");
}
}
 
// This code is contributed
// by inder_verma.


PHP




<?php
// PHP implementation of above approach
 
// Function to calculate the time
function T_ime($arr, $n, $Emptypipe)
{
    $fill = 0;
    for ($i = 0; $i < $n; $i++)
        $fill += 1 / $arr[$i];
 
    $fill = $fill - (1 / $Emptypipe);
 
    return 1 / $fill;
}
 
// Driver Code
$arr = array( 12, 14 );
$Emptypipe = 30;
$n = count($arr);
 
echo (int)T_ime($arr, $n,
                $Emptypipe) . " Hours";
 
// This code is contributed
// by inder_verma.
?>


Javascript




<script>
 
// Javascript implementation of above approach
 
// Function to calculate the time
function Time(arr, n, Emptypipe)
{
    var fill = 0;
    for(var i = 0; i < n; i++)
        fill += 1 / arr[i];
 
    fill = fill - (1 / Emptypipe);
 
    return 1 / fill;
}
 
// Driver Code
var arr = [ 12, 14 ];
var Emptypipe = 30;
var n = arr.length;
 
document.write(Math.floor(
    Time(arr, n, Emptypipe)) + " Hours");
     
// This code is contributed by itsok
 
</script>


Output: 

8 Hours

 

Time Complexity: O(1)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments