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
import java.util.Scanner;
public class Answer {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter length: ");
int l = scanner.nextInt();
System.out.println("Enter array: ");
int[] array = new int[l * 2];
for (int i = 0; i < array.length; i++) {
array[i] = scanner.nextInt();
}
int ans = 0;
for (int i = 0; i < array.length; i = i + 2) {
ans = ans + (array[i + 1] - array[i]);
}
System.out.println("Street covered with light: " + ans);
}
}
Comments
Leave a comment