Given an integer array nums
, the array concatenation value is the sum of the concatenated value of all pairs of integers in the array. A concatenated value is calculated by concatenating two integers.
For example, if nums = [1, 2, 3, 4]
, the concatenated values of the pairs (1, 2)
, (1, 3)
, (1, 4)
, (2, 3)
, (2, 4)
, and (3, 4)
would be 12
, 13
, 14
, 23
, 24
, and 34
respectively. The array concatenation value would be their sum: 12 + 13 + 14 + 23 + 24 + 34
.
Write a function find_array_concatenation_value(nums: List[int]) -> int
that computes the array concatenation value of nums
.
nums
always be non-negative?(nums[i], nums[j])
, compute the concatenated value by converting each integer to string, concatenating them, and converting back to integer.from typing import List
def find_array_concatenation_value(nums: List[int]) -> int:
concatenation_value = 0
n = len(nums)
for i in range(n):
for j in range(i+1, n):
concatenated_value = int(str(nums[i]) + str(nums[j]))
concatenation_value += concatenated_value
return concatenation_value
# Example usage
nums = [1, 2, 3, 4]
print(find_array_concatenation_value(nums)) # Expected output: 146
n
times, where n
is the length of the array.(n-1)/2
times.O(n^2)
, since we iterate over each pair of elements exactly once.This algorithm efficiently computes the concatenation value by leveraging string operations and nested loops to sum the concatenated values of all unique pairs.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?