3 minute read

2562. Find the Array Concatenation Value

Difficulty: Easy

Related Topics: Array, Two Pointers, Simulation

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 the nums.

Example 1:

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:

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.

First Attempt:

Code:

class Solution:
    def findTheArrayConcVal(self, nums: List[int]) -> int:
        summ = 0
        while nums:
            if len(nums) == 1:
                summ += nums[0]
                return summ
            newnum = int(str(nums[0]) + str(nums[-1]))
            summ += newnum
            nums = nums[1:-1]
        return summ

Feedbacks:

I first approached to this question by changing elements in the list to int -> str -> int to get a sum of first and last elements. Then, I removed those already added elements until the list is empty. If list has odd number of elements, I just simply added it at the last loop. However, it showed quite slow performance, and I found that this problem could be solved with two pointers more efficiently.

Second Attempt:

Code:

class Solution:
    def findTheArrayConcVal(self, nums: List[int]) -> int:
        ptr1 = 0
        ptr2 = len(nums)-1
        summ = 0
        while ptr1 < ptr2:
            newnum = int(str(nums[ptr1])+str(nums[ptr2]))
            summ += newnum
            ptr1+=1
            ptr2-=1
        if ptr1 == ptr2:
            summ += nums[ptr1]
        return summ

Feedbacks:

I set two pointers one as 0 and another as last index of the list. While pointer 1 is less than pointer 2, I sum up two numbers that pointers are pointing. And if there’s odd number of elements which results in same value to two pointers, I add that number at the last. This algorithm using two pointers showed way better performance than previous code.