BudiBadu Logo

Even Numbers as String

Sorting Easy 10 views
Like23

You’re compiling a weekly report for a customer service team. You receive a list of integers, but only the Even Numbers correspond to actual processed transactions. Your mission in Even Numbers as String is to pull out these even values in order and format them as a single comma-separated string.

The "secret sauce" is Filtering and Joining. As you scan the numbers, you collect only values where number % 2 == 0. This includes negative even numbers (refunds) and zeroes. Once you have the collection, you join them with a comma. This ensures the accounting team can read the transaction log quickly. If no even numbers are found, return an empty string. This challenge is a great way to practice basic array filtering and string formatting to turn raw data into a clean, human-readable report for efficient auditing!

For text and token tasks, be precise with index movement and substring boundaries. Most hidden failures come from partial-match handling and boundary cuts, so keep comparisons explicit and avoid assumptions about implicit separators or formatting not guaranteed by the input contract.

Examples

Example 1
Input
nums = [0,-2,8,11,14]
Output
"0,-2,8,14"
Explanation

Collect the even values while keeping their order.

Example 2
Input
nums = [5,7,9]
Output
""
Explanation

No even numbers exist, so return an empty string.

Example 3
Input
nums = [1,2,3,4]
Output
"2,4"
Explanation

Only 2 and 4 are even, so the string becomes "2,4".

Algorithm Flow

Recommendation Algorithm Flow for Even Numbers as String - Budibadu
Recommendation Algorithm Flow for Even Numbers as String - Budibadu

Best Answers

java
import java.util.stream.*;class Solution{public String even_to_string(Object n){int[]a=(int[])n;return IntStream.of(a).filter(x->x%2==0).mapToObj(String::valueOf).collect(Collectors.joining(","));}}