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 there exists more than one number in nums, pick the first element and last element in nums respectively and add the value of their concatenation to the concatenation value of nums, then delete the first and last element from nums.
  • If one element exists, add its value to the concatenation value of nums, then delete it.

Return the concatenation value of thenums.

Examples

Example 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Input: nums = [7,52,2,4]
Output: 596
Explanation: Before performing any operation, nums is [7,52,2,4] and concatenation value is 0.
 - In the first operation:
We pick the first element, 7, and the last element, 4.
Their concatenation is 74, and we add it to the concatenation value, so it becomes equal to 74.
Then we delete them from nums, so nums becomes equal to [52,2].
 - In the second operation:
We pick the first element, 52, and the last element, 2.
Their concatenation is 522, and we add it to the concatenation value, so it becomes equal to 596.
Then we delete them from the nums, so nums becomes empty.
Since the concatenation value is 596 so the answer is 596.

Example 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Input: nums = [5,14,13,8,12]
Output: 673
Explanation: Before performing any operation, nums is [5,14,13,8,12] and concatenation value is 0.
 - In the first operation:
We pick the first element, 5, and the last element, 12.
Their concatenation is 512, and we add it to the concatenation value, so it becomes equal to 512.
Then we delete them from the nums, so nums becomes equal to [14,13,8].
 - In the second operation:
We pick the first element, 14, and the last element, 8.
Their concatenation is 148, and we add it to the concatenation value, so it becomes equal to 660.
Then we delete them from the nums, so nums becomes equal to [13].
 - In the third operation:
nums has only one element, so we pick 13 and add it to the concatenation value, so it becomes equal to 673.
Then we delete it from nums, so nums become empty.
Since the concatenation value is 673 so the answer is 673.

Constraints

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 10^4

Solution

Method 1 – Two Pointers and String Concatenation

Intuition

Use two pointers to pick the first and last elements, concatenate them as strings, and add to the answer. If only one element remains, add it directly.

Approach

  1. Initialize two pointers, one at the start and one at the end of the array.
  2. While start <= end:
    • If start == end, add nums[start] to the answer and break.
    • Otherwise, concatenate nums[start] and nums[end] as strings, convert to integer, and add to the answer.
    • Move start forward and end backward.
  3. Return the answer.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
public:
    long long findTheArrayConcVal(vector<int>& nums) {
        int l = 0, r = nums.size() - 1;
        long long ans = 0;
        while (l <= r) {
            if (l == r) ans += nums[l++];
            else {
                string s = to_string(nums[l++]) + to_string(nums[r--]);
                ans += stoll(s);
            }
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
func findTheArrayConcVal(nums []int) int64 {
    l, r := 0, len(nums)-1
    var ans int64
    for l <= r {
        if l == r {
            ans += int64(nums[l])
            l++
        } else {
            s := fmt.Sprintf("%d%d", nums[l], nums[r])
            var v int64
            fmt.Sscan(s, &v)
            ans += v
            l++
            r--
        }
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    public long findTheArrayConcVal(int[] nums) {
        int l = 0, r = nums.length - 1;
        long ans = 0;
        while (l <= r) {
            if (l == r) ans += nums[l++];
            else {
                String s = String.valueOf(nums[l++]) + nums[r--];
                ans += Long.parseLong(s);
            }
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
    fun findTheArrayConcVal(nums: IntArray): Long {
        var l = 0
        var r = nums.size - 1
        var ans = 0L
        while (l <= r) {
            if (l == r) ans += nums[l++].toLong()
            else {
                val s = "${nums[l++]}${nums[r--]}"
                ans += s.toLong()
            }
        }
        return ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
    def findTheArrayConcVal(self, nums: list[int]) -> int:
        l, r = 0, len(nums) - 1
        ans = 0
        while l <= r:
            if l == r:
                ans += nums[l]
                break
            ans += int(str(nums[l]) + str(nums[r]))
            l += 1
            r -= 1
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
impl Solution {
    pub fn find_the_array_conc_val(nums: Vec<i32>) -> i64 {
        let (mut l, mut r) = (0, nums.len() - 1);
        let mut ans = 0i64;
        while l <= r {
            if l == r {
                ans += nums[l] as i64;
                break;
            }
            let s = format!("{}{}", nums[l], nums[r]);
            ans += s.parse::<i64>().unwrap();
            l += 1;
            r -= 1;
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
    findTheArrayConcVal(nums: number[]): number {
        let l = 0, r = nums.length - 1, ans = 0;
        while (l <= r) {
            if (l === r) {
                ans += nums[l++];
            } else {
                ans += parseInt(`${nums[l++]}${nums[r--]}`);
            }
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n), since each element is processed at most once.
  • 🧺 Space complexity: O(1), only a few variables are used for computation.