Code Logo

Even Numbers as String

Published at05 Jan 2026
Easy 16 views
Like23

This one is about reading carefully and then following a clear rule. In Even Numbers as String, you are trying to work toward the right answer by following one clear idea.

Here, you start with one piece of information and turn it into something cleaner or more useful. You might keep only certain items, change their shape, or fix how text looks. The important part is to follow the steps in the right order. If you do that carefully, the final result comes out just the way the problem expects.

For example, if the input is nums = [0,-2,8,11,14], the answer is "0,-2,8,14". Collect the even values while keeping their order. Another example is nums = [5,7,9], which gives "". No even numbers exist, so return an empty string.

This is a friendly practice problem, but it still rewards careful reading. The key is doing the steps in the right order and not changing things you should keep.

Example Input & Output

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
Recommendation Algorithm Flow for Even Numbers as String

Solution Approach

A good way to solve this problem is to walk through the array once, collect every even number in the order it appears, convert those even values to strings, and finally join them with commas. The problem is not asking you to sort anything or change the order of the numbers. It only wants you to filter the array by one rule and then format the kept values as text.

The key rule is parity. A number is even when num % 2 === 0. That works for positive values, zero, and negative values too, so examples like -2 should still be included. If a number is odd, you simply skip it. The order matters, so you should append evens exactly as you encounter them instead of collecting them into a structure that changes their position.

In JavaScript, a very direct version looks like this:

const evens = [];

for (const num of nums) {
  if (num % 2 === 0) {
    evens.push(String(num));
  }
}

return evens.join(',');

This does the whole job in three clear stages. First, scan the array. Second, keep only the even values and convert them to strings. Third, use join(',') to build the final comma-separated result. If there are no even numbers, then evens stays empty and join(',') naturally returns an empty string, which is exactly what the problem expects.

This also handles edge cases cleanly. If the input is empty, the answer is an empty string. If there is one even number, the output is just that number with no extra commas. If all numbers are even, all of them appear in the output in their original order.

The time complexity is O(n) because you inspect each element once. The extra space is O(n) in the worst case if all numbers are even and all get stored in the result list before joining.

So the full strategy is: scan the array, keep the even numbers in order, convert them to strings, and join them with commas to produce the final formatted string.

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(","));}}