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 15 and 49 gives 1549
  • Concatenating 7 and 23 gives 723

The Process:

  1. Start with a concatenation value of 0
  2. 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
Two Pointers Array Concatenation ProcessStep 1: Initialize pointers at both ends12456LEFTRIGHT1 + 6 = 16Step 2: Move pointers inward12456LEFTRIGHT2 + 5 = 25Step 3: Pointers meet at middle element12456L=RAdd 4Total: 16 + 25 + 4 = 45
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
Asked in
Google 25 Amazon 18 Microsoft 12 Meta 8
24.8K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen