Contents

About Merge Larger(with.Java)

   Aug 18, 2023     3 min read

In this article, we learned about comparing two numbers (with.Java)

We’re going to go through the Coding Test questions, reflecting on the problems we solved, and learning about other ways to solve them. Let’s start with the problem.

Problem

The operation ⊕ is an operation on two integers that returns the value written by concatenating the two integers. For example

12 ⊕ 3 = 123 3 ⊕ 12 = 312 Given two positive integers a and b, complete the solution function that returns the greater of a ⊕ b and b ⊕ a.

However, if a ⊕ b and b ⊕ a are equal, return a ⊕ b.

Example input and output

a: 9 b: 91 result: 991

That is, a ⊕ b = 991, and b ⊕ a = 919. The larger of the two is 991, so we return 991.

My solution to the problem

import java.util.*;

class Solution {
    public int solution(int a, int b) {
        int answer = 0;

        ArrayList<String> arrList = new ArrayList<>();
        arrList.add(Integer.toString(a));
        arrList.add(Integer.toString(b));

        for(int i = 0; i<arrList.size(); i++) {
            String sum = arrList.get(i%2) + arrList.get((i+1)%2);
            if(answer < Integer.parseInt(sum)){
                answer = Integer.parseInt(sum);
            }
        }
        } return answer;
    }
}

We’ve declared an ArrayList of type ArrayList and added values to it by converting Integer a and b to String. Then we’ve used a loop to assign sum of type String to the element arrList[0]+arrList[1] and reassigns the sum value to answer if it is greater than the value in answer. The same is done for subsequent iterations of the form arrList[1]+arrList[0].

Now let’s solve the problem using Math, because it’s simpler.

First of all, what is Math in Java?

The Math class is a utility class used to perform mathematical operations in Java.

Let’s see some of the important methods to use the Math class.

Solving problems with Math
class Solution {
    public int solution(int a, int b) {
        String strA = String.valueOf(a);
        String strB = String.valueOf(b);
        String strSum1 = strA + strB;
        String strSum2 = strB + strA;
        return Math.max(Integer.valueOf(strSum1), Integer.valueOf(strSum2));
    }
}

I’ve inserted a and b, which are int types, into String.valueOf. In strSum1, 2, I’ve added a and b, which are String types, and stored them, and used the Math.max() function to return the larger value. The overall logic is similar to my solution, but I’m impressed that it can be solved with an internal Java module without using a loop.