Given three integer arrays nums1
, nums2
, and nums3
, return a distinct array containing all the values that are present in at least two out of the three arrays. You may return the values in any order.
Example 1:
Input: nums1 = [1,1,3,2], nums2 = [2,3], nums3 = [3] Output: [3,2] Explanation: The values that are present in at least two arrays are: - 3, in all three arrays. - 2, in nums1 and nums2.
Example 2:
Input: nums1 = [3,1], nums2 = [2,3], nums3 = [1,2] Output: [2,3,1] Explanation: The values that are present in at least two arrays are: - 2, in nums2 and nums3. - 3, in nums1 and nums2. - 1, in nums1 and nums3.
Example 3:
Input: nums1 = [1,2,2], nums2 = [4,3,3], nums3 = [5] Output: [] Explanation: No value is present in at least two arrays.
Constraints:
1 <= nums1.length, nums2.length, nums3.length <= 100
1 <= nums1[i], nums2[j], nums3[k] <= 100
program main
! Solve the problem described in the task
implicit none
integer, parameter :: n = 3
integer, dimension(n) :: nums1 = [1, 1, 3, 2]
integer, dimension(n) :: nums2 = [2, 3]
integer, dimension(n) :: nums3 = [3]
integer, dimension(:), allocatable :: result
call solve(nums1, nums2, nums3, result)
write (*, '(*(g0, :, ", "))') result
contains
subroutine solve(nums1, nums2, nums3, result)
implicit none
integer, dimension(:), intent(in) :: nums1
integer, dimension(:), intent(in) :: nums2
integer, dimension(:), intent(in) :: nums3
integer, dimension(:), allocatable, intent(out) :: result
logical, dimension(size(nums1)) :: mask1
logical, dimension(size(nums2)) :: mask2
logical, dimension(size(nums3)) :: mask3
integer :: i, j
! Initialize the masks
mask1 = .false.
mask2 = .false.
mask3 = .false.
! Set the masks for the elements that are present in at least two arrays
do i = 1, size(nums1)
if (any(nums1(i) == nums2)) then
mask1(i) = .true.
end if
if (any(nums1(i) == nums3)) then
mask1(i) = .true.
end if
end do
do i = 1, size(nums2)
if (any(nums2(i) == nums1)) then
mask2(i) = .true.
end if
if (any(nums2(i) == nums3)) then
mask2(i) = .true.
end if
end do
do i = 1, size(nums3)
if (any(nums3(i) == nums1)) then
mask3(i) = .true.
end if
if (any(nums3(i) == nums2)) then
mask3(i) = .true.
end if
end do
! Allocate the result array
allocate(result(count(mask1 .or. mask2 .or. mask3)))
! Fill the result array
j = 1
do i = 1, size(nums1)
if (mask1(i)) then
result(j) = nums1(i)
j = j + 1
end if
end do
do i = 1, size(nums2)
if (mask2(i)) then
result(j) = nums2(i)
j = j + 1
end if
end do
do i = 1, size(nums3)
if (mask3(i)) then
result(j) = nums3(i)
j = j + 1
end if
end do
end subroutine solve
end program main
temp.f95:6:34: 6 | integer, dimension(n) :: nums1 = [1, 1, 3, 2] | 1 Error: Different shape for array assignment at (1) on dimension 1 (3 and 4) temp.f95:7:34: 7 | integer, dimension(n) :: nums2 = [2, 3] | 1 Error: Different shape for array assignment at (1) on dimension 1 (3 and 2) temp.f95:8:34: 8 | integer, dimension(n) :: nums3 = [3] | 1 Error: Different shape for array assignment at (1) on dimension 1 (3 and 1) temp.f95:11:20: 11 | call solve(nums1, nums2, nums3, result) | 1 Error: Symbol ‘nums1’ at (1) has no IMPLICIT type temp.f95:11:27: 11 | call solve(nums1, nums2, nums3, result) | 1 Error: Symbol ‘nums2’ at (1) has no IMPLICIT type temp.f95:11:34: 11 | call solve(nums1, nums2, nums3, result) | 1 Error: Symbol ‘nums3’ at (1) has no IMPLICIT type
module solve_problem
implicit none
contains
function solve(nums1, nums2, nums3) result(result)
integer, intent(in) :: nums1(:), nums2(:), nums3(:)
integer :: result(size(nums1))
integer :: i, j, k, count
! Initialize the result array
result = 0
! Loop through the arrays
do i = 1, size(nums1)
! Check if the current value is present in at least two arrays
count = 0
do j = 1, size(nums2)
if (nums1(i) == nums2(j)) then
count = count + 1
end if
end do
do k = 1, size(nums3)
if (nums1(i) == nums3(k)) then
count = count + 1
end if
end do
! If the value is present in at least two arrays, add it to the result array
if (count >= 2) then
result(i) = nums1(i)
end if
end do
end function solve
end module solve_problem
program test_solve_problem
use solve_problem
implicit none
integer, parameter :: nums1(4) = [1, 1, 3, 2]
integer, parameter :: nums2(2) = [2, 3]
integer, parameter :: nums3(1) = [3]
integer :: result(size(nums1))
result = solve(nums1, nums2, nums3)
write (*,*) "Result: ", result
end program test_solve_problem
Result: 0 0 3 0
def largest_odd(num: str) -> str:
for i in range(len(num)):
if (int(num[i]) % 2) == 1:
return num[:i + 1]
return ""
The algorithm used iterates through the input string num
. For each character, it checks if it's an odd digit (by subtracting '0' and mod-ing 2 to check if it's equal to 1).
If an odd digit is found, it returns the substring from the beginning of the string to the odd digit (inclusive) as the largest odd integer substring.
If no odd digit is found, it returns an empty string.
std::string largestOdd(std::string num) {
for (int i = 0; i < num.length(); ++i) {
if ((num[i] - '0') % 2 == 1) {
return num.substr(0, i + 1);
}
}
return "";
}
The algorithm used iterates through the input string num
. For each character, it checks if it's an odd digit (by subtracting '0' and mod-ing 2 to check if it's equal to 1).
If an odd digit is found, it returns the substring from the beginning of the string to the odd digit (inclusive) as the largest odd integer substring.
If no odd digit is found, it returns an empty string.