Find the Array Concatenation Value - Problem
You are given a 0-indexed integer array nums. Your task is to calculate the concatenation value by repeatedly combining elements from both ends of the array.
What is concatenation?
The concatenation of two numbers is formed by joining their digits together. For example:
- Concatenating
15and49gives1549 - Concatenating
7and23gives723
The Process:
- Start with a concatenation value of
0 - While the array is not empty:
- If the array has more than one element: take the first and last elements, concatenate them (first + last), add this value to your result, then remove both elements
- If the array has exactly one element: add its value to your result and remove it
Example walkthrough: For [1, 2, 4, 5, 6]:
• Take 1 and 6 → concatenate to get 16, add to result
• Take 2 and 5 → concatenate to get 25, add to result
• Take 4 (only one left) → add 4 to result
• Final result: 16 + 25 + 4 = 45
Input & Output
example_1.py — Basic Array
$
Input:
[1,2,4,5,6]
›
Output:
45
💡 Note:
Step 1: Take 1 and 6, concatenate to get 16. Array becomes [2,4,5]. Step 2: Take 2 and 5, concatenate to get 25. Array becomes [4]. Step 3: Only 4 remains, add it. Result: 16 + 25 + 4 = 45.
example_2.py — Single Element
$
Input:
[7]
›
Output:
7
💡 Note:
Only one element in the array, so we simply add it to the result and return 7.
example_3.py — Two Elements
$
Input:
[15,49]
›
Output:
1549
💡 Note:
Take the first element 15 and last element 49, concatenate them to get 1549.
Constraints
- 1 ≤ nums.length ≤ 1000
- 1 ≤ nums[i] ≤ 104
- The concatenation value will fit in a long long integer
Visualization
Tap to expand
Understanding the Visualization
1
Start at Ends
Place left pointer at index 0 and right pointer at last index
2
Concatenate & Add
Join the numbers at both pointers and add to result
3
Move Inward
Move left pointer right and right pointer left
4
Repeat Until Meet
Continue until pointers meet or cross
Key Takeaway
🎯 Key Insight: Use two pointers to avoid expensive array modifications while maintaining the same logical flow
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code