Find the Array Concatenation Value — LeetCode 2562 Python Solution

EasyArrayTwo PointersSimulation
Problem
#2562
Reading time
2 min

The problem

You are given a 0-indexed integer array nums. The concatenation of two numbers is the number formed by concatenating their numerals.

Example

Input
nums = [7,52,2,4]
Output
596
Explanation
Before performing any operation, nums is [7,52,2,4] and concatenation value is 0.

Python solution

Python
class Solution:
    def findTheArrayConcVal(self, nums: List[int]) -> int:
        ans = 0
        i, j = 0, len(nums) - 1
        while i < j:
            ans += int(str(nums[i]) + str(nums[j]))
            i, j = i + 1, j - 1
        if i == j:
            ans += nums[i]
        return ans

Complexity

MeasureComplexity
TimeO(n \times \log M)
SpaceO(\log M) auxiliary

Pattern: Two Pointers

Use the order already in the input to discard half the search space at every step. LeetCode 2562. Find the Array Concatenation Value is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Two Pointers.

The two pointers guide has the Python template for the pattern and the 201 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2562. Find the Array Concatenation Value?
LeetCode 2562. Find the Array Concatenation Value is rated Easy on LeetCode.
What is the time complexity of LeetCode 2562. Find the Array Concatenation Value?
The Python solution on this page runs in O(n \times \log M).
What is the space complexity of LeetCode 2562. Find the Array Concatenation Value?
The Python solution on this page uses O(\log M) auxiliary space.
What topics does LeetCode 2562. Find the Array Concatenation Value cover?
LeetCode 2562. Find the Array Concatenation Value is tagged Array, Two Pointers and Simulation on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview