Day 24 - Java

Barbie's Gold and Silver Collection

Barbie is standing under a tree. When she shakes the tree, several gold coins and silver coins fall. The coins fall in a straight line relative to Barbie's position.

You need to determine how many gold coins and silver coins land within a specific range where Barbie can collect them. The range is defined by the starting and ending points relative to Barbie's position.

Input Format

  1. An integer, s, the starting point of Barbie's collection range.
  2. An integer, t, the ending point of Barbie's collection range.
  3. An integer, x, the position of Barbie under the tree.
  4. Two integers, m and n, the number of gold coins and silver coins that fall from the tree, respectively.
  5. An array of m integers, where each element represents the distance a gold coin falls relative to the tree position.
  6. An array of n integers, where each element represents the distance a silver coin falls relative to the tree position.

Output Format

Print two integers:

  1. The number of gold coins collected by Barbie.
  2. The number of silver coins collected by Barbie.

Constraints

  • 1s<t10
  • 1x105
  • 1m,n1051 \leq m, n \leq 10^5
  • 105distance of coins105-10^5 \leq \text{distance of coins} \leq 10^5

Sample Input 1

7 10 5 3 3 2 3 -4 1 -2 2

Sample Output 1

2 1

Explanation

  • The collection range is [7, 10].
  • The tree is at position 5.
  • Gold coins fall at positions: 5+2=75 + 2 = 7, 5+3=85 + 3 = 8, 5+(4)=1.
    • Tow coins coin (at position 7 and 8) are within Barbie's range.
  • Silver coins fall at positions: 5+1=65 + 1 = 6, 5+(2)=35 + (-2) = 3, 5+2=75 + 2 = 7.
    • One coin (at position 7) iswithin Barbie's range.

Sample Input 2

15 20 10 4 5 5 3 2 -1 10 12 -5 -3

Sample Output 2

1 1

Solution Code 

import java.util.Scanner; public class BarbieCoins { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int s = scanner.nextInt(); // Start of range int t = scanner.nextInt(); // End of range int x = scanner.nextInt(); // Position of the tree int m = scanner.nextInt(); // Number of gold coins int n = scanner.nextInt(); // Number of silver coins int[] goldCoins = new int[m]; int[] silverCoins = new int[n]; for (int i = 0; i < m; i++) { goldCoins[i] = scanner.nextInt(); } for (int i = 0; i < n; i++) { silverCoins[i] = scanner.nextInt(); } int goldCount = 0, silverCount = 0; for (int distance : goldCoins) { int position = x + distance; if (position >= s && position <= t) { goldCount++; } } for (int distance : silverCoins) { int position = x + distance; if (position >= s && position <= t) { silverCount++; } } System.out.println(goldCount); System.out.println(silverCount); scanner.close(); } }

Insights

  1. Input Parsing:
    The program uses the Scanner class to sequentially read inputs, adhering to a clean input format.

  2. Range Validation:
    The condition if (position >= s && position <= t) ensures only coins landing within Barbie's collectible range [s, t] are counted.

  3. Position Calculation:
    Each coin’s final position is calculated using position = x + distance, where x is the tree's position, and distance is the relative fall of the coin.

  4. Efficient Storage:
    The program utilizes arrays to store gold and silver coin distances, which is efficient for handling up to 10^elements.

  5. Separate Counting:
    Gold and silver coin counts are calculated in separate loops, maintaining clarity and simplicity in logic.

  6. Scalability:
    The program is designed to handle large inputs efficiently, adhering to the constraints of competitive programming.

  7. Single Responsibility:
    The main function handles input, logic, and output in a streamlined manner, making it easy to follow.

  8. Zero-Based Indexing:
    Arrays in Java are zero-based, and the loops iterate from 0 to m-1 and 0 to n-1 for gold and silver coins, respectively.

  9. Intuitive Output:
    The program prints results on separate lines, directly matching the format expected in competitive programming problems.

  10. Flexibility for Constraints:
    The program handles both negative and positive distances, ensuring that coins falling in all directions are accounted for correctly.

"Like Barbie collecting coins within her range, focus on writing code that efficiently filters only what’s relevant—clarity and precision are golden in programming!"

Comments