Given an integer n, add a dot ( ". ") as the thousands separator and return it in string format.
Example 1:
Input: n = 987 Output: "987 "
Example 2:
Input: n = 1234 Output: "1.234 "
Constraints:
0 <= n <= 231 - 1program main
implicit none
integer, parameter :: n = 987
character(len=:), allocatable :: result
result = add_dot(n)
write (*, '(A)') trim(result)
contains
function add_dot(n) result(res)
implicit none
integer, value :: n
character(len=:), allocatable :: res
if (n < 1000) then
allocate(character(len=len(trim(str(n)))+1) :: res)
res = str(n) // " "
else
allocate(character(len=len(trim(add_dot(n / 1000))) + 4) :: res)
res = add_dot(n / 1000) // ". " // str(mod(n, 1000))
end if
end function add_dot
end program main
temp.f95:21:44:
21 | allocate(character(len=len(trim(add_dot(n / 1000))) + 4) :: res)
| 1
Error: Function โadd_dotโ at (1) cannot be called recursively, as it is not RECURSIVE
temp.f95:18:44:
18 | allocate(character(len=len(trim(str(n)))+1) :: res)
| 1
Error: Function โstrโ at (1) has no IMPLICIT type
temp.f95:19:18:
19 | res = str(n) // " "
| 1
Error: Function โstrโ at (1) has no IMPLICIT type
temp.f95:22:46:
22 | res = add_dot(n / 1000) // ". " // str(mod(n, 1000))
| 1
Error: Function โstrโ at (1) has no IMPLICIT type
```
program main
implicit none
integer :: n
character(len=20) :: result
n = 987
result = add_thousands_separator(n)
write (*,*) result
n = 1234
result = add_thousands_separator(n)
write (*,*) result
contains
function add_thousands_separator(n) result(result)
implicit none
integer, intent(in) :: n
character(len=20) :: result
integer :: i, j, k
i = 1
j = 1
k = 0
do while (i <= len(n))
if (i == 4) then
result(j:j) = "."
j = j + 1
end if
result(j:j) = n(i:i)
i = i + 1
j = j + 1
end do
end function add_thousands_separator
end program main
```
temp.f95:1:2:
1 | ```
| 1
Error: Invalid character in name at (1)
temp.f95:31:30:
31 | result(j:j) = n(i:i)
| 1
Error: Syntax error in argument list at (1)
temp.f95:37:2:
37 | ```
| 1
Error: Invalid character in name at (1)
temp.f95:26:27:
26 | do while (i <= len(n))
| 1
Error: โstringโ argument of โlenโ intrinsic at (1) must be CHARACTER
def can_be_equal(target, arr):
target.sort()
arr.sort()
return target == arr
The basic idea behind the solution is to compare the sorted versions of the input arrays `target` and `arr`. If they are equal after sorting, it means that we can obtain `target` from `arr` by reversing any number of sub-arrays, since we can sort `arr` by reversing sub-arrays of length 2.
target and arr.true; otherwise, return false.The time complexity is O(n log n) as the solution involves sorting the arrays, and the space complexity is O(log n) for the sorting operations.
#include <vector>
#include <algorithm>
bool canBeEqual(std::vector<int>& target, std::vector<int>& arr) {
std::sort(target.begin(), target.end());
std::sort(arr.begin(), arr.end());
return target == arr;
}
The basic idea behind the solution is to compare the sorted versions of the input arrays `target` and `arr`. If they are equal after sorting, it means that we can obtain `target` from `arr` by reversing any number of sub-arrays, since we can sort `arr` by reversing sub-arrays of length 2.
target and arr.true; otherwise, return false.The time complexity is O(n log n) as the solution involves sorting the arrays, and the space complexity is O(log n) for the sorting operations.