|
| 1 | +// https://www.hackerrank.com/challenges/apple-and-orange/problem |
| 2 | + |
| 3 | +package implimentation; |
| 4 | + |
| 5 | +import java.util.Scanner; |
| 6 | + |
| 7 | +public class AppleAndOrange { |
| 8 | + private static final Scanner scanner = new Scanner(System.in); |
| 9 | + |
| 10 | + public static void main(String[] args) { |
| 11 | + int start = scanner.nextInt(); |
| 12 | + int end = scanner.nextInt(); |
| 13 | + int appleTree = scanner.nextInt(); |
| 14 | + int orangeTree = scanner.nextInt(); |
| 15 | + int apples = scanner.nextInt(); |
| 16 | + int oranges = scanner.nextInt(); |
| 17 | + int[] appleDistances = getArray(apples); |
| 18 | + int[] orangeDistances = getArray(oranges); |
| 19 | + System.out.println(inRange(appleDistances, start, end, appleTree)); |
| 20 | + System.out.println(inRange(orangeDistances, start, end, orangeTree)); |
| 21 | + } |
| 22 | + |
| 23 | + private static int[] getArray(int length) { |
| 24 | + int[] array = new int[length]; |
| 25 | + for (int index = 0 ; index < array.length ; index++) { |
| 26 | + array[index] = scanner.nextInt(); |
| 27 | + } |
| 28 | + return array; |
| 29 | + } |
| 30 | + |
| 31 | + private static int inRange(int[] distances, int start, int end, int tree) { |
| 32 | + int count = 0; |
| 33 | + for (int distance : distances) { |
| 34 | + int position = tree + distance; |
| 35 | + count += position <= end && position >= start ? 1 : 0 ; |
| 36 | + } |
| 37 | + return count; |
| 38 | + } |
| 39 | +} |
0 commit comments