Find the Array Concatenation Value

easy array two pointers simulation

Problem

You are given a 0-indexed integer array nums. The concatenation of two numbers is the number formed by joining their digits (for example, the concatenation of 15 and 49 is 1549). The concatenation value is found by repeatedly taking the first and last elements, adding their concatenation to the total, and removing both. If only one element remains, add its value and remove it. Return the concatenation value.

Inputnums = [7,52,2,4]
Output596
concat(7,4)=74, then concat(52,2)=522; total 74 + 522 = 596.

def find_concatenation_value(nums):
    total = 0
    i, j = 0, len(nums) - 1
    while i < j:
        total += int(str(nums[i]) + str(nums[j]))
        i += 1
        j -= 1
    if i == j:
        total += nums[i]
    return total
function findConcatenationValue(nums) {
  let total = 0;
  let i = 0, j = nums.length - 1;
  while (i < j) {
    total += Number(String(nums[i]) + String(nums[j]));
    i++;
    j--;
  }
  if (i === j) total += nums[i];
  return total;
}
class Solution {
    public long findTheArrayConcVal(int[] nums) {
        long total = 0;
        int i = 0, j = nums.length - 1;
        while (i < j) {
            total += Long.parseLong("" + nums[i] + nums[j]);
            i++;
            j--;
        }
        if (i == j) total += nums[i];
        return total;
    }
}
long long findTheArrayConcVal(vector<int>& nums) {
    long long total = 0;
    int i = 0, j = nums.size() - 1;
    while (i < j) {
        total += stoll(to_string(nums[i]) + to_string(nums[j]));
        i++;
        j--;
    }
    if (i == j) total += nums[i];
    return total;
}
Time: O(n) Space: O(1)