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 - 1

Note: This problem is from LeetCode.
โŒ Compiled
โŒ Executed
โŒ Correct
program 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
โŒ Compiled
โŒ Executed
โŒ Correct
```
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
```
๐ŸŒ Data from online sources
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.
  1. Sort the input arrays target and arr.
  2. Compare the sorted arrays for equality.
  3. If the sorted arrays are equal, return 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.

๐ŸŒ Data from online sources
#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.
  1. Sort the input arrays target and arr.
  2. Compare the sorted arrays for equality.
  3. If the sorted arrays are equal, return 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.