A common algorithmic puzzle is to compare values across two arrays to see which index positions have elements that sum to a specific **target number** (e.g. 10). What makes this puzzle interesting is that the two arrays can have **different lengths**, so we must handle index out-of-bounds conditions safely to prevent our code from crashing.
Let's look at how we solve this using a simple card flipping game analogy.
Imagine you have two piles of cards lying side-by-side. Pile 1 has **6 cards** and Pile 2 has **10 cards**.
You play a game to count how many index pairs sum to exactly 10:
- You flip the top card from both piles at the same time (index = 0). If their values sum to 10, you record a score!
- You repeat this index-by-index down the decks.
- When Pile 1 runs out of cards after the 6th flip, you don't stop the game. Instead, you pretend the empty slots are **0-value cards** and keep comparing them with the remaining cards in Pile 2 until both decks are exhausted.
Walkthrough of the Main Method Scenario
Let's trace how the program executes step-by-step from the entry point of the main method:
The program declares two arrays and sets the target sum to 10:
int[] firstArr = {8, 2, 6, 4, 13, 10};
int[] secondArr = {2, 8, 13, 5, 9, 4, 9, 9, 9, 5};
int target = 10;
int count = 0;
Since we want to compare all elements, we run the loop up to the length of the larger array (10 elements):
int length = Integer.max(firstArr.length, secondArr.length); // length = 10
The program runs the loop from index 0 to 9:
- Index 0: `getNumber(firstArr, 0) + getNumber(secondArr, 0)` -> `8 + 2 = 10`. Matches! Count becomes 1.
- Index 1: `2 + 8 = 10`. Matches! Count becomes 2.
- Index 2: `6 + 13 = 19` (no).
- Index 3: `4 + 5 = 9` (no).
- Index 4: `13 + 9 = 22` (no).
- Index 5: `10 + 4 = 14` (no).
- Index 6: `firstArr[6]` throws an out-of-bounds error and is caught inside our `getNumber` helper, returning `0`. Result: `0 + 9 = 9` (no).
- Index 7 to 9: Out of bounds. Returns `0`. Sums are not matching target.
At the end, the program prints the total matches found: **2**.
Java Implementation
Below is the complete Java code demonstrating safe index checking across arrays of differing sizes:
package io.practise.accolite;
public class SummingUpToN {
public static void main(String[] args) {
int[] firstArr = {8, 2, 6, 4, 13, 10};
int[] secondArr = {2, 8, 13, 5, 9, 4, 9, 9, 9, 5};
int target = 10;
int count = 0;
int length = Integer.max(firstArr.length, secondArr.length);
for (int index = 0; index < length; ++index) {
if (getNumber(firstArr, index) + getNumber(secondArr, index) == target) {
++count;
}
}
System.out.println(count); // Outputs: 2
}
static int getNumber(int[] arr, int index) {
try {
return arr[index];
} catch (Exception e) {
return 0; // Safe fallback if index is out of bounds
}
}
}
Conclusion
By using a helper function with a try-catch block, we can safely access array elements without worrying about IndexOutOfBoundsException. If an array runs out of elements, we return a fallback value of 0, allowing the game loop to continue running smoothly to check the rest of the cards!