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.
![[Solved] Street Lights with Java [Solved] Street Lights with Java](https://realcoder.techss24.com/wp-content/uploads/2022/11/Solved-Street-Lights-with-Java.png)
![[Solved] You are given two strings str1 and str2. Find the minimum number of edits (operations) that can be performed on str1 to transform it into str2 with Java](https://realcoder.techss24.com/wp-content/uploads/2022/08/Solved-You-are-given-two-strings-str1-and-str2.-Find-the-minimum-number-of-edits-operations-that-can-be-performed-on-str1-to-transform-it-into-str2-with-Java-300x200.png)
![[Solved] Sum of digits with Java](https://realcoder.techss24.com/wp-content/uploads/2022/07/Solved-Sum-of-digits-with-Java-300x200.png)
![[Solved] The big number difference with Java, C++, Python](https://realcoder.techss24.com/wp-content/uploads/2023/02/Solved-The-big-number-difference-with-Java-C-Python-300x169.png)