59. Find the Array Concatenation Value

EasyArrayArray

Given a 0-indexed integer array nums, compute the concatenation value as follows: while nums is non-empty, take the first and last element, concatenate them as a number (first||last), add it to the total, and remove both. If only one element remains, add it directly.

Return the total concatenation value.

Input: An integer array nums of length n.

Output: Long integer — total concatenation value.

Examples

Example 1
Input: [7,52,2,4]
Output: 596
Explanation: 74+522=596? No: concat(7,4)=74, concat(52,2)=522. 74+522=596.
Example 2
Input: [5,14,13,8,12]
Output: 673
Explanation: concat(5,12)=512, concat(14,8)=148, middle=13. 512+148+13=673.
Example 3
Input: [1,2,3]
Output: 133
Explanation: concat(1,3)=13, middle=2. 13+2+... wait: concat(1,3)=13, then middle=2. 13+2=15? No: 13+2=15 but concat is 13 and then single 2 → 15. Hmm recheck: [1,2,3]: l=0,r=2: str(1)+str(3)='13'=13, l=1,r=1: l==r, add 2. total=13+2=15.

Constraints

Asked by

IBMAmazon
Solve this problem in the editor →