LeetCode-in-Java

2562. Find the Array Concatenation Value

Easy

You are given a 0-indexed integer array nums.

The concatenation of two numbers is the number formed by concatenating their numerals.

The concatenation value of nums is initially equal to 0. Perform this operation until nums becomes empty:

Return the concatenation value of the nums.

Example 1:

Input: nums = [7,52,2,4]

Output: 596

Explanation: Before performing any operation, nums is [7,52,2,4] and concatenation value is 0.

Example 2:

Input: nums = [5,14,13,8,12]

Output: 673

Explanation: Before performing any operation, nums is [5,14,13,8,12] and concatenation value is 0.

Constraints:

Solution

public class Solution {
    public long findTheArrayConcVal(int[] nums) {
        long sum = 0;
        int idxLeft = 0;
        int idxRight = nums.length - 1;
        while (idxLeft < idxRight) {
            sum += getConcatenationValue(nums[idxLeft], nums[idxRight]);
            ++idxLeft;
            --idxRight;
        }
        if (idxLeft == idxRight) {
            sum += nums[idxLeft];
        }
        return sum;
    }

    private long getConcatenationValue(int leftVal, int rightVal) {
        if ((long) rightVal == 10000) {
            return leftVal * 100000L + rightVal;
        }
        if ((long) rightVal >= 1000) {
            return leftVal * 10000L + rightVal;
        }
        if ((long) rightVal >= 100) {
            return leftVal * 1000L + rightVal;
        }
        if ((long) rightVal >= 10) {
            return leftVal * 100L + rightVal;
        }
        return leftVal * 10L + rightVal;
    }
}