← All Exercises

Java Loop Control Flow

basicjava~15min

Background

Java labeled break and continue provide control over nested loops, useful for search and matrix traversal.

Problem

Write a Java method that finds the first pair of numbers in an array that sum to a target, using nested loops with labeled break.

Input

An array of integers and a target sum.

Output

An int array of the two indices, or null if no pair exists.

Sample tests

Sample 1

Input: [2, 7, 11, 15], target=9
Expected: [0, 1]

Your solution

Learning objective

Learn labeled loops, break/continue with labels, and nested loop search patterns.