Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given a 0-indexed integer array nums.
The concatenation of two numbers is the number formed by concatenating their numerals.
15, 49 is 1549.The concatenation value of nums is initially equal to 0. Perform this operation until nums becomes empty:
nums has a size greater than one, add the value of the concatenation of the first and the last element to the concatenation value of nums, and remove those two elements from nums. For example, if the nums was [1, 2, 4, 5, 6], add 16 to the concatenation value.nums, add its value to the concatenation value of nums, then remove it.Return the concatenation value of 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.
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 104Problem summary: 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 nums has a size greater than one, add the value of the concatenation of the first and the last element to the concatenation value of nums, and remove those two elements from nums. For example, if the nums was [1, 2, 4, 5, 6], add 16 to the concatenation value. If only one element exists in nums, add its value to the concatenation value of nums, then remove it. Return the concatenation value of nums.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers
[7,52,2,4]
[5,14,13,8,12]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2562: Find the Array Concatenation Value
class Solution {
public long findTheArrayConcVal(int[] nums) {
long ans = 0;
int i = 0, j = nums.length - 1;
for (; i < j; ++i, --j) {
ans += Integer.parseInt(nums[i] + "" + nums[j]);
}
if (i == j) {
ans += nums[i];
}
return ans;
}
}
// Accepted solution for LeetCode #2562: Find the Array Concatenation Value
func findTheArrayConcVal(nums []int) (ans int64) {
i, j := 0, len(nums)-1
for ; i < j; i, j = i+1, j-1 {
x, _ := strconv.Atoi(strconv.Itoa(nums[i]) + strconv.Itoa(nums[j]))
ans += int64(x)
}
if i == j {
ans += int64(nums[i])
}
return
}
# Accepted solution for LeetCode #2562: Find the Array Concatenation Value
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
// Accepted solution for LeetCode #2562: Find the Array Concatenation Value
impl Solution {
pub fn find_the_array_conc_val(nums: Vec<i32>) -> i64 {
let n = nums.len();
let mut ans = 0;
let mut i = 0;
let mut j = n - 1;
while i < j {
ans += format!("{}{}", nums[i], nums[j]).parse::<i64>().unwrap();
i += 1;
j -= 1;
}
if i == j {
ans += nums[i] as i64;
}
ans
}
}
// Accepted solution for LeetCode #2562: Find the Array Concatenation Value
function findTheArrayConcVal(nums: number[]): number {
const n = nums.length;
let ans = 0;
let i = 0;
let j = n - 1;
while (i < j) {
ans += Number(`${nums[i]}${nums[j]}`);
i++;
j--;
}
if (i === j) {
ans += nums[i];
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.