Skip to main content
Back to problems
#2562
Easy Algorithms

Find the array concatenation value

Array Two Pointers Simulation
71.8% acceptance
Feb 25, 2026
391
20
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn find_the_array_conc_val(nums: Vec<i32>) -> i64 {
    let mut result = 0i64;
    let mut lo = 0usize;
    let mut hi = nums.len() - 1;
    while lo < hi {
      let concat = format!("{}{}", nums[lo], nums[hi]).parse::<i64>().unwrap();
      result += concat;
      lo += 1;
      hi -= 1;
    }
    if lo == hi {
      result += nums[lo] as i64;
    }
    result
  }
}