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
, pick the first element and last element in nums
respectively and add the value of their concatenation to the concatenation value of nums
, then delete the first and last element from nums
.nums
, then delete it.Return the concatenation value of the 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 <= 1000
1 <= nums[i] <= 104
.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0; } .spoiler {overflow:hidden;} .spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;} .spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;} .spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
program concatenation_of_numbers
implicit none
integer :: i, j, n, nums(1000), concat_val
! read input
read(*,*) n
do i = 1, n
read(*,*) nums(i)
end do
! solve problem
concat_val = 0
do while (n > 0)
if (n >= 2) then
! concatenate first and last elements
concat_val = concat_val + concatenate(nums(1), nums(n))
! remove first and last elements
nums(1:n-1) = nums(2:n)
n = n - 1
else
! concatenate single element
concat_val = concat_val + nums(1)
! remove single element
nums(1:n-1) = nums(2:n)
n = n - 1
end if
end do
! print result
write(*,*) concat_val
contains
function concatenate(a, b) result(concat)
implicit none
integer, intent(in) :: a, b
integer :: concat
concat = a * 10 + b
end function concatenate
end program concatenation_of_numbers
At line 7 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x78415e9fd960 in ??? #1 0x78415e9fe4d9 in ??? #2 0x78415ec5217b in ??? #3 0x78415ec4b684 in ??? #4 0x78415ec4c2aa in ??? #5 0x5d387510f246 in MAIN__ #6 0x5d387510f470 in main
PROGRAM concatenation
IMPLICIT NONE
INTEGER, DIMENSION(:), ALLOCATABLE :: nums
INTEGER :: n, i, j, concatenation_value
! Read the input
READ (*,*) n
ALLOCATE (nums(n))
DO i = 1, n
READ (*,*) nums(i)
END DO
! Initialize the concatenation value
concatenation_value = 0
! Loop until the nums array is empty
DO WHILE (SIZE(nums) > 0)
! Check if there are more than one number in the nums array
IF (SIZE(nums) > 1) THEN
! Pick the first and last element in the nums array
i = 1
j = SIZE(nums)
! Add the concatenation of the first and last element to the concatenation value
concatenation_value = concatenation_value + CONCATENATE(nums(i), nums(j))
! Delete the first and last element from the nums array
nums(i:j-1) = nums(i+1:j)
ELSE
! Add the value of the only element in the nums array to the concatenation value
concatenation_value = concatenation_value + nums(1)
! Delete the only element from the nums array
nums = nums(2:)
END IF
END DO
! Output the concatenation value
WRITE (*,*) concatenation_value
END PROGRAM concatenation
FUNCTION CONCATENATE(a, b) RESULT(c)
IMPLICIT NONE
INTEGER, INTENT(IN) :: a, b
INTEGER :: c
! Concatenate the two numbers
c = a*10 + b
END FUNCTION CONCATENATE
temp.f95:26:67: 26 | concatenation_value = concatenation_value + CONCATENATE(nums(i), nums(j)) | 1 Error: Return type mismatch of function ‘concatenate’ at (1) (UNKNOWN/INTEGER(4)) temp.f95:26:67: 26 | concatenation_value = concatenation_value + CONCATENATE(nums(i), nums(j)) | 1 Error: Function ‘concatenate’ at (1) has no IMPLICIT type
def concatenationValue(nums):
value = 0
while nums:
n = len(nums)
if n == 1:
value += nums[0]
nums.pop()
else:
value += int(str(nums[0]) + str(nums[-1]))
nums.pop(0)
nums.pop()
return value
The algorithm goes as follows: 1. Initialize the concatenation value to 0. 2. Check if the nums list is empty. If it is not: a. Check if the length of the nums list is 1. If it is, add the only element in the list to the concatenation value and remove it from the list. b. If there is more than one element in the nums list, concatenate the first and last elements and add this value to the concatenation value. Then, remove both elements from the list. 3. Repeat step 2 until the nums list is empty. 4. Return the concatenation value.
int concatenationValue(vector<int>& nums) {
int value = 0;
while (!nums.empty()) {
int n = nums.size();
if (n == 1) {
value += nums[0];
nums.pop_back();
} else {
value += stoi(to_string(nums[0]) + to_string(nums[n - 1]));
nums.erase(nums.begin());
nums.pop_back();
}
}
return value;
}
The algorithm goes as follows: 1. Initialize the concatenation value to 0. 2. Check if the nums list is empty. If it is not: a. Check if the length of the nums list is 1. If it is, add the only element in the list to the concatenation value and remove it from the list. b. If there is more than one element in the nums list, concatenate the first and last elements and add this value to the concatenation value. Then, remove both elements from the list. 3. Repeat step 2 until the nums list is empty. 4. Return the concatenation value.