Question :
A petrol tank has infinite capacity it can store infinite litres of petrol .
There are Four petrolpumps say 1 , 2 , 3 , 4. which are in circular manner.
PetrolTank should start from one petrol pump and should reach other petrolpump and should complete the circle with out any break.
Given input is a two dimensional array first column is petrol capacity of petrolpump and second column is distance to next petrol pump.
If petroltank can start from two petrol pumps means then you should start from least petrol pump number.
Given input array is int [][] array1 ={{3,3},
{7,4},
{7,8},
{9,8}}
For this input petrol tank can start from petrolpump 2 and petrolpump 4 but as we have to start from least number of petrolpump it will start from petrolpump 2 and will finish the circle.
Hints : You have to check whether input is two dimensional array or not .
Solution :
PetrolProblem.java
/**
*
*/
package com.techyield.blogspot.in;
/**
* @author SATYA
*
*/
public class PetrolProblem {
public int startPointofPetrolTank(int [][] input){
int petrolPumpnumber=0;
try{
int row= input.length;
int totaldistance=0;
int totalpetrol=0;
for(int i=0;i<row;i++){
int col=input[i].length;
if(col!=2){
throw new UnsupportedException("Input is not a two dimentional array.");
}
}
for(int i =0; i<row;i++){
totaldistance+=input[i][1];
totalpetrol+=input[i][0];
}
if(totalpetrol<totaldistance){
return 0;
}
for(int i =0; i<row;i++){
if(input[i][0]>input[i][1]){
petrolPumpnumber=i+1;
break;
}
}
}
catch(UnsupportedException e){
System.out.println(e.getMessage());
}
catch(Exception e){
System.out.println(e.getMessage());
}
return petrolPumpnumber;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int [][] array1 ={{3,3},
{7,4},
{7,8},
{9,8}
};
PetrolProblem object = new PetrolProblem();
int petrolPumpindex =object.startPointofPetrolTank(array1);
if(petrolPumpindex >=1)
System.out.println("Petrol Tank will start from PetrolPump "+petrolPumpindex+"");
else
System.out.println("PetrolTank can't start from any petrol pump");
}
}
UnsupportedException.java
package com.techyield.blogspot.in;
public class UnsupportedException extends Exception {
public UnsupportedException(String s){
super(s);
}
}
Output :
Petrol Tank will start from PetrolPump 2
A petrol tank has infinite capacity it can store infinite litres of petrol .
There are Four petrolpumps say 1 , 2 , 3 , 4. which are in circular manner.
PetrolTank should start from one petrol pump and should reach other petrolpump and should complete the circle with out any break.
Given input is a two dimensional array first column is petrol capacity of petrolpump and second column is distance to next petrol pump.
If petroltank can start from two petrol pumps means then you should start from least petrol pump number.
Given input array is int [][] array1 ={{3,3},
{7,4},
{7,8},
{9,8}}
For this input petrol tank can start from petrolpump 2 and petrolpump 4 but as we have to start from least number of petrolpump it will start from petrolpump 2 and will finish the circle.
Hints : You have to check whether input is two dimensional array or not .
Solution :
PetrolProblem.java
/**
*
*/
package com.techyield.blogspot.in;
/**
* @author SATYA
*
*/
public class PetrolProblem {
public int startPointofPetrolTank(int [][] input){
int petrolPumpnumber=0;
try{
int row= input.length;
int totaldistance=0;
int totalpetrol=0;
for(int i=0;i<row;i++){
int col=input[i].length;
if(col!=2){
throw new UnsupportedException("Input is not a two dimentional array.");
}
}
for(int i =0; i<row;i++){
totaldistance+=input[i][1];
totalpetrol+=input[i][0];
}
if(totalpetrol<totaldistance){
return 0;
}
for(int i =0; i<row;i++){
if(input[i][0]>input[i][1]){
petrolPumpnumber=i+1;
break;
}
}
}
catch(UnsupportedException e){
System.out.println(e.getMessage());
}
catch(Exception e){
System.out.println(e.getMessage());
}
return petrolPumpnumber;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int [][] array1 ={{3,3},
{7,4},
{7,8},
{9,8}
};
PetrolProblem object = new PetrolProblem();
int petrolPumpindex =object.startPointofPetrolTank(array1);
if(petrolPumpindex >=1)
System.out.println("Petrol Tank will start from PetrolPump "+petrolPumpindex+"");
else
System.out.println("PetrolTank can't start from any petrol pump");
}
}
UnsupportedException.java
package com.techyield.blogspot.in;
public class UnsupportedException extends Exception {
public UnsupportedException(String s){
super(s);
}
}
Petrol Tank will start from PetrolPump 2
No comments:
Post a Comment