Contents

How to get concatenated counts, sort by conditions, and get concatenated counts (with.Java)

   Aug 26, 2023     3 min read

In this article, we learned about How to get the number of concatenations, categorize by conditions, and get the number of concatenations (with.Java)

We’re going to learn by solving a coding test problem, reflecting on the problem we solved, and exploring other ways to solve it. Let’s start with the problem.

Problem

You are given a list num_list containing integers. Complete the solution function so that it returns the sum of the only odd numbers in num_list in order and the only even numbers in order.

Example input and output

num_list : [3, 4, 5, 2, 1] result: 393

The number of odd numbers concatenated is 351, and the number of even numbers concatenated is 42. The sum of the two numbers is 393.

My solution to the problem

class Solution {
    public int solution(int[] num_list) {
        int answer = 0;
        String[] strArray = new String[num_list.length];
        String even = "";
        String odd = "";

        for(int i = 0; i < num_list.length; i++){
            strArray[i] = String.valueOf(num_list[i]);
            if(num_list[i] % 2 == 0){
                even += strArray[i];
            } else{
                odd += strArray[i];
            }
        }
        answer = Integer.parseInt(even) + Integer.parseInt(odd);
        return answer;
    }
}
Explanation of the solution

This code implements a function that processes a given array of integers, num_list, to isolate the even and odd numbers separately, then combines the isolated even and odd numbers into a single string each, and finally converts these two strings to integers and returns the summed value.

Let’s take a look at the key parts one by one

int solution(int[] num_list): this is a method that takes an array of integers as input and returns an integer sum.

String[] strArray = new String[num_list.length];: Creates an array of strings called strArray with a length equal to the length of the input num_list. This array will be used to convert each integer element to a string and store it.

for(int i = 0; i < num_list.length; i++) { … }: Executes a loop for each element of the given array num_list.

strArray[i] = String.valueOf(num_list[i]);: Convert the integer that is the current element to a string and store it at the corresponding index in strArray.

if(num_list[i] % 2 == 0) { … } else { … }: Determine whether the current element is an even or odd number.

even += strArray[i]; and odd += strArray[i];: Add the value of the current element converted to a string to the strings even and odd, respectively. Thus, the string even, representing even numbers, will contain even numbers, and the string odd, representing odd numbers, will contain odd numbers, in that order.

answer = Integer.parseInt(even) + Integer.parseInt(odd);: Convert the even and odd numbers stored as strings to integers and store their added values in answer.

return answer;: Returns answer, which is the final calculated sum.

The purpose of this code is to separate the even and odd numbers of a given array of integers and calculate their sum.