Skip to content

Create Water_Jug_Problem.js function in the Recursive section #1751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update WaterJugProblem.js
  • Loading branch information
swappy-2003 authored Oct 24, 2024
commit acd529606c3f8746d904d9f20f7eecfe266cfafa
17 changes: 8 additions & 9 deletions Recursive/WaterJugProblem.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// WaterJugProblem.js

/**
* Function to determine if it is possible to measure exactly targetAmount
* using two jugs of capacity jug1Capacity and jug2Capacity.
*
* @param {number} jug1Capacity - Capacity of the first jug
* @param {number} jug2Capacity - Capacity of the second jug
* @param {number} targetAmount - Target amount of water
* @returns {boolean}
*/
export function canMeasureWater(jug1Capacity, jug2Capacity, targetAmount) {
// Input validation
if (jug1Capacity < 0 || jug2Capacity < 0) {
throw new Error('Invalid input: capacities must be non-negative.');
}
if (targetAmount < 0) {
throw new Error('Invalid input: target amount must be non-negative.');
}

// Base case: If the target amount is greater than the sum of both jugs, it's not possible.
if (targetAmount > jug1Capacity + jug2Capacity) return false;

Expand Down
Loading