Relative Sort Array

🏠 ⬅️ ➡️

Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.

Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that do not appear in arr2 should be placed at the end of arr1 in ascending order.

Example 1:

Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6] Output: [2,2,2,1,4,3,3,9,6,7,19]

Example 2:

Input: arr1 = [28,6,22,8,44,17], arr2 = [22,28,8,6] Output: [22,28,8,6,17,44]

Constraints:

  • 1 <= arr1.length, arr2.length <= 1000
  • 0 <= arr1[i], arr2[i] <= 1000
  • All the elements of arr2 are distinct.
  • Each arr2[i] is in arr1.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 10
    integer, dimension(n) :: arr1, arr2, sorted_arr
    integer :: i, j

    ! Example 1
    arr1 = [2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19]
    arr2 = [2, 1, 4, 3, 9, 6]
    call sort_array(arr1, arr2, sorted_arr)
    write(*,*) 'Example 1:'
    write(*,'(10I5)') sorted_arr

    ! Example 2
    arr1 = [28, 6, 22, 8, 44, 17]
    arr2 = [22, 28, 8, 6]
    call sort_array(arr1, arr2, sorted_arr)
    write(*,*) 'Example 2:'
    write(*,'(10I5)') sorted_arr

contains

    subroutine sort_array(arr1, arr2, sorted_arr)
        implicit none
        integer, intent(in) :: arr1(:), arr2(:)
        integer, intent(out) :: sorted_arr(:)
        integer :: i, j, k

        do i = 1, size(arr1)
            do j = 1, size(arr2)
                if (arr1(i) == arr2(j)) then
                    exit
                end if
            end do
            if (j > size(arr2)) then
                sorted_arr(i) = arr1(i)
            else
                do k = i+1, size(arr1)
                    if (arr1(k) < arr1(i)) then
                        sorted_arr(i) = arr1(k)
                        exit
                    end if
                end do
            end if
        end do

        do i = 1, size(arr2)
            do j = 1, size(arr1)
                if (arr2(i) == arr1(j)) then
                    exit
                end if
            end do
            if (j > size(arr1)) then
                sorted_arr(i) = arr2(i)
            end if
        end do

    end subroutine sort_array

end program main
Compiled
Executed
Correct
program sort_array
implicit none

integer, dimension(:), allocatable :: arr1, arr2, sorted_arr
integer :: i, j, k, n, m

! Read the input arrays
read(*,*) n
allocate(arr1(n), arr2(n))
do i = 1, n
    read(*,*) arr1(i), arr2(i)
end do

! Sort the elements of arr1 according to the relative ordering of items in arr2
sorted_arr = arr1
do i = 1, n
    do j = 1, n
        if (arr2(j) == sorted_arr(i)) then
            exit
        end if
    end do
    do k = i, n - 1
        if (arr2(j) > sorted_arr(k + 1)) then
            sorted_arr(k) = sorted_arr(k + 1)
            sorted_arr(k + 1) = arr2(j)
        end if
    end do
end do

! Print the sorted array
do i = 1, n
    write(*,*) sorted_arr(i)
end do

end program sort_array
🌐 Data from online sources
def longest_dup_substring(s):
    n = len(s)
    longest_duplicate = ""
    for length in range(n - 1, 0, -1):
        for i in range(n - length + 1):
            substring = s[i:i + length]
            if s.find(substring, i + 1) != -1:
                longest_duplicate = substring
                return longest_duplicate
    return longest_duplicate
The algorithm starts with defining the longest possible duplicate string length which is the length of the input string minus one. Then, we iterate through all possible substring lengths from the longest to the shortest.

For each substring length, we iterate through the input string to check if there's a duplicate substring. We obtain a substring with the starting position i and the current length, then find if there's another occurrence of this substring in the input string. If there's one, we return the current substring as the longest duplicate substring.

If we finish iterating through all possible substring lengths without finding any duplicate substrings, we return an empty string.

🌐 Data from online sources
#include <string>

std::string longestDupSubstring(std::string s) {
    int n = s.length();
    std::string longest_duplicate;
    for (int length = n - 1; length > 0; --length) {
        for (int i = 0; i <= n - length; ++i) {
            std::string substring = s.substr(i, length);
            if (s.find(substring, i + 1) != std::string::npos) {
                longest_duplicate = substring;
                return longest_duplicate;
            }
        }
    }
    return longest_duplicate;
}
The algorithm starts with defining the longest possible duplicate string length which is the length of the input string minus one. Then, we iterate through all possible substring lengths from the longest to the shortest.

For each substring length, we iterate through the input string to check if there's a duplicate substring. We obtain a substring with the starting position i and the current length, then find if there's another occurrence of this substring in the input string. If there's one, we return the current substring as the longest duplicate substring.

If we finish iterating through all possible substring lengths without finding any duplicate substrings, we return an empty string.