Answer to Question #338111 in Java | JSP | JSF for mahi

Question #338111

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​


1
Expert's answer
2022-05-08T14:33:01-0400
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);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS