Skip to content
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

Create Water_Jug_Problem.js function in the Recursive section #1751

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
53 changes: 53 additions & 0 deletions Recursive/WaterJugProblem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// WaterJugProblem.js

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;

// 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();

// 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);

// 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;
}
36 changes: 36 additions & 0 deletions Recursive/test/WaterJugProblem.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// WaterJugProblem.test.js
import { canMeasureWater } from "../WaterJugProblem"; // Adjust the path as necessary

describe("Water Jug Problem", () => {
it("should return true for values jug1=3, jug2=5, target=4", () => {
expect(canMeasureWater(3, 5, 4)).toBe(true);
});

it("should return false for values jug1=2, jug2=6, target=5", () => {
expect(canMeasureWater(2, 6, 5)).toBe(false);
});

it("should return true for values jug1=5, jug2=3, target=5", () => {
expect(canMeasureWater(5, 3, 5)).toBe(true);
});

it("should return true for values jug1=3, jug2=5, target=0", () => {
expect(canMeasureWater(3, 5, 0)).toBe(true);
});

it("should return true for values jug1=3, jug2=5, target=8", () => {
expect(canMeasureWater(3, 5, 8)).toBe(true);
});

it("should throw an error for invalid input", () => {
expect(() => canMeasureWater(-1, 5, 3)).toThrow(
"Invalid input: capacities must be non-negative."
);
expect(() => canMeasureWater(3, -2, 1)).toThrow(
"Invalid input: capacities must be non-negative."
);
expect(() => canMeasureWater(3, 5, -1)).toThrow(
"Invalid input: target amount must be non-negative."
);
});
});
Loading