Find the Array Concatenation Value
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.
nums = [7,52,2,4]596def 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;
}
Explanation
The process always pairs the current first and last elements, then peels them off. That “work from both ends inward” pattern is exactly what two pointers model: i starting at the front and j at the back.
While i < j we concatenate nums[i] and nums[j]. Concatenation is a string operation — join the two number strings and parse the result back to an integer — which we add to the running total. Then both pointers step inward.
When the array length is odd, the pointers eventually meet at one shared middle element (i == j). That lone element is added by itself, not concatenated with anything, matching the “if only one element remains” rule.
No actual deletion is needed: moving the pointers inward simulates removing the outer pair each round, so the whole thing runs in a single pass without modifying the array.
Example: [7,52,2,4]. Round 1 concatenates 7 and 4 → 74. Round 2 concatenates 52 and 2 → 522. Pointers cross, so total is 74 + 522 = 596.