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, 49 is 1549.

The concatenation value of nums is initially equal to 0. Perform this operation until nums becomes empty:

  • If nums has a size greater than one, add the value of the concatenation of the first and the last element to the concatenation value of nums, and remove those two elements from nums.
  • If only one element exists in nums, add its value to the concatenation value of nums, 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
Array Concatenation Value ProcessInput: [7, 52, 2, 4]75224Concatenate: 7 + 4 = 74522Concatenate: 52 + 2 = 522Final Result: 74 + 522 = 596
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
Asked in
Google 15 Amazon 12 Microsoft 8
25.0K Views
Medium Frequency
~15 min Avg. Time
850 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