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.
Input Specification:
input1: L, denoting the number of street lights.
input2: An array of L * 2 elements. For each row i , (Xi, Yi) denote that the
street light i covers the distance from Xi to Yi.
Output Specification:
Your function should return the length of the street covered with light.
Example 1:
input1: 1,
input2: {{5,10}}
Output: 5
Explanation:
Street Light 1:10 – 5 = 5 units covered.
Solution
public int coverage(int Input1, int[][] Input2) {
int length = 0;
for (int i = 0; i < Input2.length; i++) {
length += Input2[i][1] - Input2[i][0];
if (i != Input2.length - 1 && Input2[i][1] > Input2[i + 1][0]) {
length = length - (Input2[i][1] - Input2[i+1][0]);
}
}
return length;
}
Happy Learning – If you require any further information, feel free to contact me.