one of the streets in your city has a total of l street lights. Each light i covers the street from Xi to yi distance. Find the length of street covered with light
Answers
Answered by
1
Answer:
import java.util.Scanner;
class main
{
public static void main(String args[])
{
int [][] myarray = {{1,4},{7,10},{11,13},{16,17},{17,18}};
int sum=0,diff=0;
for(int i =0;i<myarray.length;i++){
sum = sum +(myarray[i][1]-myarray[i][0]);
if(i<myarray.length-1){
if(myarray[i][1] > myarray[i+1][0] || (myarray[i][1] - myarray[i+1][0]) == -1){
diff = diff +(myarray[i][1] - myarray[i+1][0]);
}
}
}
System.out.println(sum-diff);
}
}
Explanation:
Similar questions