Largest Positive Integer That Exists With Its Negative

🏠 ⬅️ ➡️

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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
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
Compiled
Executed
Correct
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
🌐 Data from online sources
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`.
🌐 Data from online sources
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`.