Given an integer array nums
that does not contain any zeros, find the largest positive integer k
such that -k
also exists in the array.
Return the positive integer k
. If there is no such integer, return -1
.
Example 1:
Input: nums = [-1,2,-3,3] Output: 3 Explanation: 3 is the only valid k we can find in the array.
Example 2:
Input: nums = [-1,10,6,7,-7,1] Output: 7 Explanation: Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
Example 3:
Input: nums = [-10,8,6,7,-2,-3] Output: -1 Explanation: There is no a single valid k, we return -1.
Constraints:
1 <= nums.length <= 1000
-1000 <= nums[i] <= 1000
nums[i] != 0
program main
implicit none
integer, parameter :: n = 5
integer, dimension(n) :: nums
integer :: k
! Example 1
nums = [-1, 2, -3, 3]
print '(I0)', solve(nums)
! Example 2
nums = [-1, 10, 6, 7, -7, 1]
print '(I0)', solve(nums)
! Example 3
nums = [-10, 8, 6, 7, -2, -3]
print '(I0)', solve(nums)
contains
function solve(nums) result(k)
implicit none
integer, intent(in) :: nums(:)
integer :: k
logical :: found
k = -1
found = .false.
do i = 1, size(nums)
if (nums(i) == -k) then
found = .true.
exit
end if
end do
if (found) then
k = abs(k)
end if
end function solve
end program main
temp.f95:31:12: 31 | do i = 1, size(nums) | 1 Error: Symbol ‘i’ at (1) has no IMPLICIT type temp.f95:9:4: 9 | nums = [-1, 2, -3, 3] | 1 Error: Different shape for array assignment at (1) on dimension 1 (5 and 4) temp.f95:13:4: 13 | nums = [-1, 10, 6, 7, -7, 1] | 1 Error: Different shape for array assignment at (1) on dimension 1 (5 and 6) temp.f95:17:4: 17 | nums = [-10, 8, 6, 7, -2, -3] | 1 Error: Different shape for array assignment at (1) on dimension 1 (5 and 6)
module largest_positive_integer_k
implicit none
private
public :: largest_positive_integer_k
contains
function largest_positive_integer_k(nums) result(k)
integer, intent(in) :: nums(:)
integer :: k
integer :: i, j
k = -1
do i = 1, size(nums)
if (nums(i) < 0) then
do j = 1, size(nums)
if (nums(j) == -nums(i)) then
k = max(k, abs(nums(i)))
exit
end if
end do
end if
end do
end function largest_positive_integer_k
end module largest_positive_integer_k
program test_largest_positive_integer_k
use largest_positive_integer_k
implicit none
integer :: nums(4)
nums = [-1, 2, -3, 3]
write (*, '(A, I0)') 'Example 1: ', largest_positive_integer_k(nums)
nums = [-1, 10, 6, 7, -7, 1]
write (*, '(A, I0)') 'Example 2: ', largest_positive_integer_k(nums)
nums = [-10, 8, 6, 7, -2, -3]
write (*, '(A, I0)') 'Example 3: ', largest_positive_integer_k(nums)
end program test_largest_positive_integer_k
temp.f95:4:40: 4 | public :: largest_positive_integer_k | 1 Error: PUBLIC attribute applied to MODULE largest_positive_integer_k at (1) temp.f95:8:39: 8 | function largest_positive_integer_k(nums) result(k) | 1 Error: MODULE attribute of ‘largest_positive_integer_k’ conflicts with PROCEDURE attribute at (1) temp.f95:9:38: 9 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:20: 10 | integer :: k | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:23: 11 | integer :: i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:14: 13 | k = -1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:14:28: 14 | do i = 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:15:33: 15 | if (nums(i) < 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:16:36: 16 | do j = 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:17:49: 17 | if (nums(j) == -nums(i)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:18:48: 18 | k = max(k, abs(nums(i))) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:28: 19 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:20:23: 20 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:21:19: 21 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:22:15: 22 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:23:11: 23 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:24:7: 24 | end function largest_positive_integer_k | 1 Error: Expecting END MODULE statement at (1) temp.f95:28:9: 28 | use largest_positive_integer_k | 1 Fatal Error: Cannot open module file ‘largest_positive_integer_k.mod’ for reading at (1): No such file or directory compilation terminated.
def find_K(nums):
s = set(nums)
k = -1
for num in nums:
if -num in s:
k = max(k, abs(num))
return k
The algorithm puts all numbers in `nums` into a set `s` and then iterates through the numbers in the array. For each of the numbers, it checks whether `-k` is present in the set. If it is, it updates `k` to be the maximum of the absolute values of `k` and the current element (`num`). The maximum positive integer `k` is returned if it exists, otherwise `-1` is returned. This way, it finds the largest positive `k` such that both `k` and `-k` exist in the input array `nums`.
int findK(vector<int>& nums) {
set<int> s(nums.begin(), nums.end());
int k = -1;
for (const int& num : nums) {
if (s.find(-num) != s.end()) {
k = max(k, abs(num));
}
}
return k;
}
The algorithm puts all numbers in `nums` into a set `s` and then iterates through the numbers in the array. For each of the numbers, it checks whether `-k` is present in the set. If it is, it updates `k` to be the maximum of the absolute values of `k` and the current element (`num`). The maximum positive integer `k` is returned if it exists, otherwise `-1` is returned. This way, it finds the largest positive `k` such that both `k` and `-k` exist in the input array `nums`.