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 Water_Jug_Problem.js
formated the code with prettier
  • Loading branch information
swappy-2003 authored Oct 24, 2024
commit ff16d60673820a2b31f17c15e30393dfafc28dd1
61 changes: 35 additions & 26 deletions Recursive/Water_Jug_Problem.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
function canMeasureWater(jug1Capacity, jug2Capacity, targetAmount) {
// Base case: If the target amount is greater than the sum of both jugs, it's not possible.
if (targetAmount > jug1Capacity + jug2Capacity) return false;
// Base case: If the target amount is greater than the sum of both jugs, it's not possible.
if (targetAmount > jug1Capacity + jug2Capacity) return false;

// Use BFS to explore possible states.
let visited = new Set(); // To keep track of visited states.
let queue = [[0, 0]]; // Starting with both jugs empty.
// Use BFS to explore possible states.
let visited = new Set(); // To keep track of visited states.
let queue = [[0, 0]]; // Starting with both jugs empty.

while (queue.length > 0) {
let [jug1, jug2] = queue.shift();
while (queue.length > 0) {
let [jug1, jug2] = queue.shift();

// If we've reached the target amount in either jug.
if (jug1 === targetAmount || jug2 === targetAmount || jug1 + jug2 === targetAmount) {
return true;
}
// If we've reached the target amount in either jug.
if (
jug1 === targetAmount ||
jug2 === targetAmount ||
jug1 + jug2 === targetAmount
) {
return true;
}

// If this state has been visited, skip it.
let state = `${jug1},${jug2}`;
if (visited.has(state)) continue;
visited.add(state);
// If this state has been visited, skip it.
let state = `${jug1},${jug2}`;
if (visited.has(state)) continue;
visited.add(state);

// Add all possible next states to the queue:
queue.push([jug1Capacity, jug2]); // Fill Jug 1
queue.push([jug1, jug2Capacity]); // Fill Jug 2
queue.push([0, jug2]); // Empty Jug 1
queue.push([jug1, 0]); // Empty Jug 2
queue.push([Math.max(0, jug1 - (jug2Capacity - jug2)), Math.min(jug2Capacity, jug1 + jug2)]); // Pour Jug 1 into Jug 2
queue.push([Math.min(jug1Capacity, jug1 + jug2), Math.max(0, jug2 - (jug1Capacity - jug1))]); // Pour Jug 2 into Jug 1
}
// Add all possible next states to the queue:
queue.push([jug1Capacity, jug2]); // Fill Jug 1
queue.push([jug1, jug2Capacity]); // Fill Jug 2
queue.push([0, jug2]); // Empty Jug 1
queue.push([jug1, 0]); // Empty Jug 2
queue.push([
Math.max(0, jug1 - (jug2Capacity - jug2)),
Math.min(jug2Capacity, jug1 + jug2),
]); // Pour Jug 1 into Jug 2
queue.push([
Math.min(jug1Capacity, jug1 + jug2),
Math.max(0, jug2 - (jug1Capacity - jug1)),
]); // Pour Jug 2 into Jug 1
}

// If no solution is found
return false;
// If no solution is found
return false;
}

Loading
Oops, something went wrong.