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 INPUT nums = [7, 52, 2, 4] 7 i=0 52 i=1 2 i=2 4 i=3 First Last Concatenate first + last Remove both elements Add to result concat(a,b) = str(a) + str(b) as int ALGORITHM STEPS 1 Iteration 1 concat(7,4) = "74" = 74 result = 0 + 74 = 74 2 Array Update nums = [52, 2] Remove first and last 3 Iteration 2 concat(52,2) = "522" = 522 result = 74 + 522 = 596 4 Array Empty nums = [] - Done! Return result Running Total Step 1: 0 + 74 = 74 Step 2: 74 + 522 = 596 FINAL RESULT Concatenation Value 596 Breakdown 7 + 4 --> "74" = 74 52 + 2 --> "522" = 522 Total: 74 + 522 = 596 OK - Verified Key Insight: Two-pointer approach: Use left and right pointers to process elements from both ends. String concatenation: Convert numbers to strings, join them, then parse back to integer. Time: O(n) - each element processed once. Space: O(1) - only pointers needed. TutorialsPoint - Find the Array Concatenation Value | Optimal Two-Pointer Approach
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