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 concatenating their numerals.
- For example, the concatenation of
15,49is1549.
The concatenation value of nums is initially equal to 0. Perform this operation until nums becomes empty:
- If
numshas a size greater than one, add the value of the concatenation of the first and the last element to the concatenation value ofnums, and remove those two elements fromnums. - If only one element exists in
nums, add its value to the concatenation value ofnums, then remove it.
Return the concatenation value of nums.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [7,52,2,4]
›
Output:
596
💡 Note:
First iteration: concatenate 7 and 4 to get 74. Second iteration: concatenate 52 and 2 to get 522. Total: 74 + 522 = 596
Example 2 — Odd Length Array
$
Input:
nums = [5,14,13,8,12]
›
Output:
673
💡 Note:
Pair 5+12=512, pair 14+8=148, middle element 13 remains. Total: 512 + 148 + 13 = 673
Example 3 — Single Element
$
Input:
nums = [15]
›
Output:
15
💡 Note:
Only one element exists, so return it as is: 15
Constraints
- 1 ≤ nums.length ≤ 1000
- 1 ≤ nums[i] ≤ 104
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Array with elements [7,52,2,4]
2
Pair & Concatenate
Pair first+last, then move inward
3
Sum Results
Add all concatenated values: 596
Key Takeaway
🎯 Key Insight: Use two pointers to efficiently access elements from both ends without array modifications
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code