Find the Array Concatenation Value

To solve this problem, iterate over the array by popping out the first and last elements, concatenate them, convert them back to an integer, and keep adding this value until the array is empty. When only one element is left, just add it to the sum. Here is how you could do it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution:
    def findTheArrayConcVal(self, nums: List[int]) -> int:
        conc_value = 0
        while nums:
            # when more than one number is present
            if len(nums) > 1:
                first_num = nums.pop(0)
                last_num = nums.pop(-1)
                conc_num = int(str(first_num) + str(last_num))
                conc_value += conc_num
            # when only one number is present
            else:
                conc_value += nums.pop(0)
        return conc_value

In this solution:

  1. conc_value is initialized as 0, which will hold the sum of all concatenated numbers.
  2. In the loop, it’s checked if nums has more than one element. If yes, then the first and last elements are popped out, concatenated to form a new number, and this number is added to conc_value.
  3. If only one element is left in nums, then this element is popped out and added to conc_value.
  4. This loop will continue until nums becomes empty.
  5. The final value of conc_value is returned as the answer.