Problem Statement: There are two jugs (suppose capacity of 3 and 5) and we need to fill the jug in such a way that 5 litres capacity jug should contain 4 litres of water.

import java.util.*;

class

**WaterJug**{
public static void main(String sap[]){

Scanner sc = new Scanner(System.in);

// j1 is capacity of small tank

System.out.print("\nEnter odd capacity of small tank: ");

int j1 = sc.nextInt();

// j2 is capacity of large tank

System.out.print("\nEnter odd capacity of large tank: ");

int j2 = sc.nextInt();

// count takes care of number of iterations

int count = j1 + j2;

/* jug1 array would hold the values for smaller tank and jug2 array would hold the values for larger tank */

int jug1[] = new int[count];

int jug2[] = new int[count];

int i=0;

// initialzing jug1 and jug2 array

jug1[i] = j1;

jug2[i] = 0;

i++;

jug1[i] = 0;

jug2[i] = j1;

i++;

while(i < count){

if(jug1[i-1] > 0){

// if jug1 has any amount of water i.e. it is not empty

jug1[i] = jug1[i-1];

jug2[i] = 0;

}

else{

// jug1 is fully empty

jug1[i] = j1;

jug2[i] = jug2[i-1];

}

i++;

if(jug2[i-1] > 0){

// if jug2 has any amount of water i.e. it is not empty

if(jug1[i-1] + jug2[i-1] < j2){

// final result obtained

jug2[i] = jug1[i-1] + jug2[i-1];

jug1[i] = 0;

}

else{

int temp = jug2[i-1];

temp = j2 - temp;

jug2[i] = temp + jug2[i-1];

jug1[i] = jug1[i-1] - temp;

}

}

else{

// jug2 is fully empty

jug2[i] = jug1[i-1];

jug1[i] = 0;

}

i++;

}

// display final result

for(i=0; i<count; i++){

System.out.print("\nJUG1: "+jug1[i]+"\tJUG2: "+jug2[i]);

}

System.out.println();

}

}

**Output:**

Enter odd capacity of small tank: 3

Enter odd capacity of large tank: 5

JUG1: 3 JUG2: 0

JUG1: 0 JUG2: 3

JUG1: 3 JUG2: 3

JUG1: 1 JUG2: 5

JUG1: 1 JUG2: 0

JUG1: 0 JUG2: 1

JUG1: 3 JUG2: 1

JUG1: 0 JUG2: 4

wow.....you are GENIUS....great coder

ReplyDeleteThanks vinay raghuvanshi

DeleteDesign and implement the solution for water jug problem using two jugs, which is stated as follows:

ReplyDelete“You are given two jugs, a 4‐litre one and a 3‐litre one. Neither have any measuring markers on it. There is a pump that can be used to fill the jugs with water. How can you get exactly 2 litres of water into 4‐litre jug.”

Program in java solutions

myTectra Placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance

ReplyDeleteInformative Article… I want to read your articles in light of the fact that your composition style is too great... Customer Reconciliation

ReplyDeleteWarehouse Audit

Chartered Accountant

Its really informative

ReplyDeletejava training in Marathahalli

spring training in Marathahalli

java training institute in Marathahalli

spring and hibernate training in Marathahalli

Nice Blog keep on sharing

ReplyDeletejava training in Bangalore

spring training in Bangalore

java training institute in Bangalore

spring and hibernate training in Bangalore

Nice post..

ReplyDeletejava training in BTM

spring training in BTM

java training institute in btm

spring and hibernate training in btm

Nice Post

ReplyDeleteangularjs training in Bangalore

angularjs training institutes in Bangalore

best angularjs training in Bangalore

Amazing & Useful post.Thanks for sharing this post.Keep in blogging. Continuous Monitoring

ReplyDeleteDuplicate Payment Audit

AR Customer Helpdesk