Given an array arr[] of N integers where ith element represents the amount of water required by the plant at ith index and an integer K, the task is to calculate the count of operations required to water all the plants using a container that can hold at most K liters of water wherein each operation,
- You can move to the adjacent plant to the left or to the right.
- Also, there is a river at index -1 from where the container can be refilled any number of times.
- Note that initially, at index -1 and any plant can not be watered partially during any step.
Examples:
Input: arr[] = {2, 2, 3, 3}, K = 5
Output: 14
Explanation: For the above example, during the first 2 operations: the plants at index 0 and 1 can be watered.
Since we do not have enough water for the 3rd plant, return to the river in 2 operations.
Refill the container and return to the 3rd plant in 3 operations.
Similarly, we do not have enough water for the 4th plant.
So refill the container and come back to the 4th plant in a total of 7 operations.
Therefore, a total of 14 operations are required.
Input: arr[] = {1, 2, 3, 4}, K = 3
Output: -1
Explanation: It is not possible to fully water the 4th plant using a container of capacity 3.
Approach: The given problem is an implementation-based problem. It can be solved using the following steps:
- Create a variable current_capacity to store the current quantity of water in the container. Initially, current_capacity = K.
- Traverse the given array arr[] using a variable i and perform the following operations:
- If arr[i] > K, return -1.
- If arr[i] > current_capacity, add 2*i + 1 into the operation count and set current_capacity = K – arr[i].
- Otherwise, add 1 to the operation count and set current_capacity = current_capacity – arr[i].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int reqOperationCnt(vector< int >& arr, int K)
{
int current_capacity = K;
int cnt = 0;
for ( int i = 0; i < arr.size(); i++) {
if (arr[i] > K) {
return -1;
}
if (current_capacity < arr[i]) {
cnt += 2 * i + 1;
current_capacity = K - arr[i];
}
else {
cnt++;
current_capacity -= arr[i];
}
}
return cnt;
}
int main()
{
vector< int > arr{ 2, 2, 3, 3 };
int K = 5;
cout << reqOperationCnt(arr, K);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int reqOperationCnt( int []arr, int K)
{
int current_capacity = K;
int cnt = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
if (arr[i] > K) {
return - 1 ;
}
if (current_capacity < arr[i]) {
cnt += 2 * i + 1 ;
current_capacity = K - arr[i];
}
else {
cnt++;
current_capacity -= arr[i];
}
}
return cnt;
}
public static void main (String args[]) {
int []arr = { 2 , 2 , 3 , 3 };
int K = 5 ;
System.out.println(reqOperationCnt(arr, K));
}
}
|
C#
using System;
class GFG {
static int reqOperationCnt( int []arr, int K)
{
int current_capacity = K;
int cnt = 0;
for ( int i = 0; i < arr.Length; i++) {
if (arr[i] > K) {
return -1;
}
if (current_capacity < arr[i]) {
cnt += 2 * i + 1;
current_capacity = K - arr[i];
}
else {
cnt++;
current_capacity -= arr[i];
}
}
return cnt;
}
public static void Main () {
int []arr = { 2, 2, 3, 3 };
int K = 5;
Console.Write(reqOperationCnt(arr, K));
}
}
|
Python3
def reqOperationCnt(arr, K):
current_capacity = K
cnt = 0
for i in range ( len (arr)):
if (arr[i] > K):
return - 1
if (current_capacity < arr[i]):
cnt + = 2 * i + 1
current_capacity = K - arr[i]
else :
cnt + = 1
current_capacity - = arr[i]
return cnt
arr = [ 2 , 2 , 3 , 3 ]
K = 5
print (reqOperationCnt(arr, K))
|
Javascript
<script>
function reqOperationCnt(arr, K) {
let current_capacity = K;
let cnt = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > K) {
return -1;
}
if (current_capacity < arr[i]) {
cnt += 2 * i + 1;
current_capacity = K - arr[i];
}
else {
cnt++;
current_capacity -= arr[i];
}
}
return cnt;
}
let arr = [2, 2, 3, 3];
let K = 5;
document.write(reqOperationCnt(arr, K));
</script>
|
Time Complexity: O(N) since one traversal of the array is required to complete all operations hence the overall time required by the algorithm is linear
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant