Geek’s Plants
Last Updated :
29 Jan, 2024
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;
long long minimumBuckets( int N, int arr[])
{
int gcdval = 0;
for ( int i = 0; i < N; i++) {
gcdval = __gcd(gcdval, arr[i]);
}
long long ans = 0;
for ( int i = 0; i < N; i++) {
ans += (arr[i] / gcdval) + (arr[i] % gcdval);
}
return ans;
}
int main()
{
int N = 4;
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>
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
long long minimumBuckets( int N, int arr[])
{
int gcdval = 0;
for ( int i = 0; i < N; i++) {
gcdval = gcd(gcdval, arr[i]);
}
long long ans = 0;
for ( int i = 0; i < N; i++) {
ans += (arr[i] / gcdval) + (arr[i] % gcdval);
}
return ans;
}
int main()
{
int N = 4;
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 {
static long minimumBuckets( int N, int [] arr) {
int gcdVal = 0 ;
for ( int i = 0 ; i < N; i++) {
gcdVal = gcd(gcdVal, arr[i]);
}
long ans = 0 ;
for ( int i = 0 ; i < N; i++) {
ans += (arr[i] / gcdVal) + (arr[i] % gcdVal);
}
return ans;
}
static int gcd( int a, int b) {
while (b != 0 ) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static void main(String[] args) {
int N = 4 ;
int [] arr = { 2 , 6 , 4 , 10 };
System.out.println( "Minimum number of buckets required: " + minimumBuckets(N, arr));
}
}
|
Python3
def minimum_buckets(N, arr):
def gcd(a, b):
while b ! = 0 :
temp = b
b = a % b
a = temp
return a
gcd_val = 0
for i in range (N):
gcd_val = gcd(gcd_val, arr[i])
ans = 0
for i in range (N):
ans + = (arr[i] / / gcd_val) + (arr[i] % gcd_val)
return ans
if __name__ = = "__main__" :
N = 4
arr = [ 2 , 6 , 4 , 10 ]
print ( "Minimum number of buckets required:" , minimum_buckets(N, arr))
|
C#
using System;
public class GFG {
static long MinimumBuckets( int N, int [] arr)
{
int gcdVal = 0;
for ( int i = 0; i < N; i++) {
gcdVal = GCD(gcdVal, arr[i]);
}
long ans = 0;
for ( int i = 0; i < N; i++) {
ans += (arr[i] / gcdVal) + (arr[i] % gcdVal);
}
return ans;
}
static int GCD( int a, int b)
{
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static void Main( string [] args)
{
int N = 4;
int [] arr = { 2, 6, 4, 10 };
Console.WriteLine(
"Minimum number of buckets required: "
+ MinimumBuckets(N, arr));
}
}
|
Javascript
function minimumBuckets(N, arr) {
let gcdVal = 0;
for (let i = 0; i < N; i++) {
gcdVal = gcd(gcdVal, arr[i]);
}
let ans = 0;
for (let i = 0; i < N; i++) {
ans += Math.floor(arr[i] / gcdVal) + (arr[i] % gcdVal);
}
return ans;
}
function gcd(a, b) {
while (b !== 0) {
let temp = b;
b = a % b;
a = temp;
}
return a;
}
function main() {
const N = 4;
const arr = [2, 6, 4, 10];
console.log( "Minimum number of buckets required: " + minimumBuckets(N, arr));
}
main();
|
Output
Minimum number of buckets required: 11
Time Complexity: O(N*logP)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...