Open In App

Geek’s Plants

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Geek wants to water n plants (numbered from 0 to n-1), where planti requires arr[i] units of water daily. Geek has to use buckets of the same size and can not use a bucket more than once a day. Also, Geek does not waste any water. Find the minimum number of buckets Geek needs to use daily for watering the plants.

Note: Each bucket can be designated for only one plant after receiving a fresh refill of water at the beginning of each day.

Examples:

Input: N = 4, arr[] = {2, 6, 4, 10}
Output: 11
Explanation: Geek will use buckets of size 2 units daily. Then Geek will need 1, 3, 2, and 5 numbers of buckets for plant 0, plant 1, plant 2, and plant 3 respectively daily.

Input: N = 1, arr[] = {5}
Output: 1
Explanation: Geek will use a bucket of size 5 units daily. Then Geek will need 1 bucket for plant 0 daily.

Approach:

Since all bucket sizes should be the same, and one bucket is used for only one plant, we can deduce that the bucket size should be the common divisor of all elements in the array. As we need to determine the minimum number of buckets required for watering all the plants, we will calculate the greatest common divisor (GCD) of all the elements in the array.

Steps to solve this problem:

Initially, we take one variable named gcd_val to store the greatest common divisor of all elements of the array. It is initialized by the value 0.

  • Using a loop, we will traverse the given array and, for each element of the array, we will calculate its GCD using the Euclidean Algorithm. In the C++ language, we can use a built-in function(i.e. __gcd(a,b)) for calculating GCD.
  • Now, we have taken another variable called ans to store the answer to the problem. Initially, the value of the variable is 0.
  • Now, for every element of the array, we will divide each element of the array by the value stored inside the gcdval variable, and the division result will be added to the answer variable, ans.
  • Finally, we will return the ans variable as an answer, i.e., the minimum number of buckets required for watering the plants.

Below is the implementation of the above approach.

C++




#include <bits/stdc++.h>
using namespace std;
// Function to calculate minimum number buckets required for
// watering the plants daily
long long minimumBuckets(int N, int arr[])
{
    // code here
    // This variable is used to store the gcd value of every
    // element of the array
    int gcdval = 0;
    for (int i = 0; i < N; i++) {
        // using Euclidian algorithm we can calculate GCD
        gcdval = __gcd(gcdval, arr[i]);
    }
    // Declared ans variable to store the answer of the
    // problem
    long long ans = 0;
    for (int i = 0; i < N; i++) {
        // For each element of the array we divide the
        // element by their gcd value,gcdval
        ans += (arr[i] / gcdval) + (arr[i] % gcdval);
    }
    // Finally return the answer
    return ans;
}
// Driver function
int main()
{
    // Number of Plants
    int N = 4;
    // arr[i] represent the amount of water required for
    // i'th plants per day
    int arr[] = { 2, 6, 4, 10 };
 
    cout << "Minimum number of buckets required: "
        << minimumBuckets(N, arr) << endl;
    return 0;
}


C




#include <stdio.h>
#include <stdlib.h>
// Function to return gcd of a and b
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
// Function to calculate the minimum number of buckets
// required for watering the plants daily
long long minimumBuckets(int N, int arr[])
{
    // This variable is used to store the GCD value of every
    // element of the array
    int gcdval = 0;
 
    for (int i = 0; i < N; i++) {
        // Using Euclidean algorithm to calculate GCD
        gcdval = gcd(gcdval, arr[i]);
    }
 
    // Declared ans variable to store the answer of the
    // problem
    long long ans = 0;
 
    for (int i = 0; i < N; i++) {
        // For each element of the array, we divide the
        // element by their GCD value, gcdval
        ans += (arr[i] / gcdval) + (arr[i] % gcdval);
    }
 
    // Finally return the answer
    return ans;
}
 
// Driver function
int main()
{
    // Number of Plants
    int N = 4;
 
    // arr[i] represents the amount of water required for
    // i'th plants per day
    int arr[] = { 2, 6, 4, 10 };
 
    long long result = minimumBuckets(N, arr);
    printf("Minimum number of buckets required: %lld\n",
           result);
 
    return 0;
}


Java




import java.util.Arrays;
 
public class MinimumBuckets {
    // Function to calculate the minimum number of buckets required for watering the plants daily
    static long minimumBuckets(int N, int[] arr) {
        // This variable is used to store the gcd value of every element of the array
        int gcdVal = 0;
        for (int i = 0; i < N; i++) {
            // Using Euclidean algorithm to calculate GCD
            gcdVal = gcd(gcdVal, arr[i]);
        }
 
        // Declared ans variable to store the answer of the problem
        long ans = 0;
        for (int i = 0; i < N; i++) {
            // For each element of the array, divide the element by their gcd value (gcdVal)
            ans += (arr[i] / gcdVal) + (arr[i] % gcdVal);
        }
 
        // Finally, return the answer
        return ans;
    }
 
    // Function to calculate GCD using Euclidean algorithm
    static int gcd(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
 
    // Driver function
    public static void main(String[] args) {
        // Number of Plants
        int N = 4;
        // arr[i] represents the amount of water required for i'th plants per day
        int[] arr = {2, 6, 4, 10};
 
        System.out.println("Minimum number of buckets required: " + minimumBuckets(N, arr));
    }
}
 
// This code is contributed by shivamgupta310570


Python3




def minimum_buckets(N, arr):
    # Function to calculate GCD using Euclidean algorithm
    def gcd(a, b):
        while b != 0:
            temp = b
            b = a % b
            a = temp
        return a
 
    # This variable is used to store the gcd value of every element of the array
    gcd_val = 0
    for i in range(N):
        # Using Euclidean algorithm to calculate GCD
        gcd_val = gcd(gcd_val, arr[i])
 
    # Declared ans variable to store the answer of the problem
    ans = 0
    for i in range(N):
        # For each element of the array, divide the element by their gcd value (gcd_val)
        ans += (arr[i] // gcd_val) + (arr[i] % gcd_val)
 
    # Finally, return the answer
    return ans
 
# Driver function
if __name__ == "__main__":
    # Number of Plants
    N = 4
    # arr[i] represents the amount of water required for i'th plant per day
    arr = [2, 6, 4, 10]
 
    print("Minimum number of buckets required:", minimum_buckets(N, arr))


C#




using System;
 
public class GFG {
    // Function to calculate the minimum number of buckets
    // required for watering the plants daily
    static long MinimumBuckets(int N, int[] arr)
    {
        // This variable is used to store the gcd value of
        // every element of the array
        int gcdVal = 0;
        for (int i = 0; i < N; i++) {
            // Using Euclidean algorithm to calculate GCD
            gcdVal = GCD(gcdVal, arr[i]);
        }
 
        // Declared ans variable to store the answer of the
        // problem
        long ans = 0;
        for (int i = 0; i < N; i++) {
            // For each element of the array, divide the
            // element by their gcd value (gcdVal)
            ans += (arr[i] / gcdVal) + (arr[i] % gcdVal);
        }
 
        // Finally, return the answer
        return ans;
    }
 
    // Function to calculate GCD using Euclidean algorithm
    static int GCD(int a, int b)
    {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
 
    // Driver function
    public static void Main(string[] args)
    {
        // Number of Plants
        int N = 4;
        // arr[i] represents the amount of water required
        // for i'th plants per day
        int[] arr = { 2, 6, 4, 10 };
 
        Console.WriteLine(
            "Minimum number of buckets required: "
            + MinimumBuckets(N, arr));
    }
}


Javascript




// Function to calculate the minimum number of buckets required for watering the plants daily
function minimumBuckets(N, arr) {
    // This variable is used to store the gcd value of every element of the array
    let gcdVal = 0;
 
    // Using Euclidean algorithm to calculate GCD
    for (let i = 0; i < N; i++) {
        gcdVal = gcd(gcdVal, arr[i]);
    }
 
    // Declared ans variable to store the answer of the problem
    let ans = 0;
 
    for (let i = 0; i < N; i++) {
        // For each element of the array, divide the element by their gcd value (gcdVal)
        ans += Math.floor(arr[i] / gcdVal) + (arr[i] % gcdVal);
    }
 
    // Finally, return the answer
    return ans;
}
 
// Function to calculate GCD using Euclidean algorithm
function gcd(a, b) {
    while (b !== 0) {
        let temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}
 
// Driver function
function main() {
    // Number of Plants
    const N = 4;
 
    // arr[i] represents the amount of water required for i'th plants per day
    const arr = [2, 6, 4, 10];
 
    console.log("Minimum number of buckets required: " + minimumBuckets(N, arr));
}
 
// Invoke the main function
main();


Output

Minimum number of buckets required: 11

Time Complexity: O(N*logP)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads