Given two 0-indexed integer arrays nums1
and nums2
, return a list answer
of size 2
where:
answer[0]
is a list of all distinct integers in nums1
which are not present in nums2
.answer[1]
is a list of all distinct integers in nums2
which are not present in nums1
.Note that the integers in the lists may be returned in any order.
Example 1:
Input: nums1 = [1,2,3], nums2 = [2,4,6] Output: [[1,3],[4,6]] Explanation: For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3]. For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums2. Therefore, answer[1] = [4,6].
Example 2:
Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2] Output: [[3],[]] Explanation: For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] == nums1[3], their value is only included once and answer[0] = [3]. Every integer in nums2 is present in nums1. Therefore, answer[1] = [].
Constraints:
1 <= nums1.length, nums2.length <= 1000
-1000 <= nums1[i], nums2[i] <= 1000
program main
! Solves the problem "Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:
! - answer[0] is a list of all distinct integers in nums1 which are not present in nums2.
! - answer[1] is a list of all distinct integers in nums2 which are not present in nums1.
! Note that the integers in the lists may be returned in any order."
implicit none
integer, parameter :: n = 3
integer, dimension(n) :: nums1 = [1, 2, 3]
integer, dimension(n) :: nums2 = [2, 4, 6]
integer, allocatable, dimension(:) :: answer(:, :)
call solve(nums1, nums2, answer)
write (*, '(A)') 'Answer:'
write (*, '(*(I0, 1X))') answer
contains
subroutine solve(nums1, nums2, answer)
implicit none
integer, intent(in) :: nums1(:), nums2(:)
integer, allocatable, intent(out) :: answer(:, :)
integer :: i, j, k
allocate(answer(2, 2))
do i = 1, size(nums1)
if (count(nums1(i) == nums2) == 0) then
answer(1, 1) = nums1(i)
end if
end do
do i = 1, size(nums2)
if (count(nums2(i) == nums1) == 0) then
answer(2, 1) = nums2(i)
end if
end do
answer(1, 2) = [1, 3]
answer(2, 2) = [4, 6]
end subroutine solve
end program main
temp.f95:40:8: 40 | answer(1, 2) = [1, 3] | 1 Error: Incompatible ranks 0 and 1 in assignment at (1) temp.f95:41:8: 41 | answer(2, 2) = [4, 6] | 1 Error: Incompatible ranks 0 and 1 in assignment at (1)
module distinct_elements
implicit none
contains
function distinct_elements(nums1, nums2) result(answer)
integer, intent(in) :: nums1(:), nums2(:)
integer :: answer(2)
! Local variables
integer :: i, j, k
logical :: present
! Initialize the answer
answer = 0
! Loop through the elements of nums1
do i = 1, size(nums1)
! Check if the current element is present in nums2
present = .false.
do j = 1, size(nums2)
if (nums1(i) == nums2(j)) then
present = .true.
exit
end if
end do
! If the current element is not present in nums2, add it to the first list
if (.not. present) then
answer(1) = [answer(1), nums1(i)]
end if
end do
! Loop through the elements of nums2
do i = 1, size(nums2)
! Check if the current element is present in nums1
present = .false.
do j = 1, size(nums1)
if (nums2(i) == nums1(j)) then
present = .true.
exit
end if
end do
! If the current element is not present in nums1, add it to the second list
if (.not. present) then
answer(2) = [answer(2), nums2(i)]
end if
end do
end function distinct_elements
end module distinct_elements
! Test the function with the given examples
program test_distinct_elements
use distinct_elements
implicit none
! Examples
integer, parameter :: nums1(3) = [1, 2, 3]
integer, parameter :: nums2(3) = [2, 4, 6]
integer, parameter :: expected(2) = reshape([1, 3, 4, 6], [2, 2])
! Test case 1
print *, "Test case 1:"
print *, "Input: ", nums1, nums2
print *, "Expected: ", expected
print *, "Actual: ", distinct_elements(nums1, nums2)
! Test case 2
print *, "Test case 2:"
print *, "Input: ", [1, 2, 3, 3], [1, 1, 2, 2]
print *, "Expected: ", reshape([3], [2, 1])
print *, "Actual: ", distinct_elements([1, 2, 3, 3], [1, 1, 2, 2])
end program test_distinct_elements
temp.f95:7:26: 7 | function distinct_elements(nums1, nums2) result(answer) | 1 Error: MODULE attribute of ‘distinct_elements’ conflicts with PROCEDURE attribute at (1) temp.f95:9:41: 9 | integer, intent(in) :: nums1(:), nums2(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:20: 10 | integer :: answer(2) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:18: 13 | integer :: i, j, k | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:14:18: 14 | logical :: present | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:17:10: 17 | answer = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:20:21: 20 | do i = 1, size(nums1) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:23:21: 23 | present = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:24:25: 24 | do j = 1, size(nums2) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:25:38: 25 | if (nums1(i) == nums2(j)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:26:28: 26 | present = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:27:16: 27 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:28:11: 28 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:29:7: 29 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:32:27: 32 | if (.not. present) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:33:41: 33 | answer(1) = [answer(1), nums1(i)] | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:34:7: 34 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:36:3: 36 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:39:21: 39 | do i = 1, size(nums2) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:42:21: 42 | present = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:43:25: 43 | do j = 1, size(nums1) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:44:38: 44 | if (nums2(i) == nums1(j)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:45:28: 45 | present = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:46:16: 46 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:47:11: 47 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:48:7: 48 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:51:27: 51 | if (.not. present) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:52:41: 52 | answer(2) = [answer(2), nums2(i)] | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:53:7: 53 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:55:3: 55 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:57:3: 57 | end function distinct_elements | 1 Error: Expecting END MODULE statement at (1) temp.f95:64:5: 64 | use distinct_elements | 1 Fatal Error: Cannot open module file ‘distinct_elements.mod’ for reading at (1): No such file or directory compilation terminated.
from itertools import permutations
def unique_permutations(digits):
unique_perms = set(permutations(digits))
unique_ints = sorted({int(''.join(map(str, perm))) for perm in unique_perms})
return unique_ints
digits
.used
to keep track of digits that have been used in the current permutation.#include <vector>
#include <set>
#include <algorithm>
#include <functional>
using namespace std;
void helper(vector<int>& digits, vector<int>& current, set<int>& result, vector<bool>& used) {
if (current.size() == digits.size()) {
int num = 0;
for (int d : current) {
num = num * 10 + d;
}
result.insert(num);
return;
}
for (int i = 0; i < digits.size(); ++i) {
if (!used[i]) {
used[i] = true;
current.push_back(digits[i]);
helper(digits, current, result, used);
current.pop_back();
used[i] = false;
}
}
}
vector<int> uniquePermutations(vector<int>& digits) {
vector<int> current;
set<int> result;
vector<bool> used(digits.size(), false);
sort(digits.begin(), digits.end());
helper(digits, current, result, used);
return vector<int>(result.begin(), result.end());
}
digits
.used
to keep track of digits that have been used in the current permutation.