Sign of the Product of an Array

🏠 ⬅️ ➡️

There is a function signFunc(x) that returns:

  • 1 if x is positive.
  • -1 if x is negative.
  • 0 if x is equal to 0.

You are given an integer array nums. Let product be the product of all values in the array nums.

Return signFunc(product).

Example 1:

Input: nums = [-1,-2,-3,-4,3,2,1] Output: 1 Explanation: The product of all values in the array is 144, and signFunc(144) = 1

Example 2:

Input: nums = [1,5,0,2,-3] Output: 0 Explanation: The product of all values in the array is 0, and signFunc(0) = 0

Example 3:

Input: nums = [-1,1,-1,1,-1] Output: -1 Explanation: The product of all values in the array is -1, and signFunc(-1) = -1

Constraints:

  • 1 <= nums.length <= 1000
  • -100 <= nums[i] <= 100

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    use, intrinsic :: iso_fortran_env, only : error_unit, DP => REAL64
    implicit none
    integer, parameter :: n = 7
    real(kind=DP), dimension(n) :: nums = [-1._DP, -2._DP, -3._DP, &
                                           -4._DP, 3._DP, 2._DP, 1._DP]
    integer :: i
    write (unit=error_unit, fmt='(*(g0))') 'Example 1:'
    do i = 1, size(nums)
        write (unit=error_unit, fmt='(*(g0))') 'nums(', i, ') = ', nums(i)
    end do
    write (unit=error_unit, fmt='(*(g0))') 'signFunc(product) = ', signFunc(product(nums))
    write (unit=error_unit, fmt='(*(g0))')

    write (unit=error_unit, fmt='(*(g0))') 'Example 2:'
    nums = [1._DP, 5._DP, 0._DP, 2._DP, -3._DP]
    do i = 1, size(nums)
        write (unit=error_unit, fmt='(*(g0))') 'nums(', i, ') = ', nums(i)
    end do
    write (unit=error_unit, fmt='(*(g0))') 'signFunc(product) = ', signFunc(product(nums))
    write (unit=error_unit, fmt='(*(g0))')

    write (unit=error_unit, fmt='(*(g0))') 'Example 3:'
    nums = [-1._DP, 1._DP, -1._DP, 1._DP, -1._DP]
    do i = 1, size(nums)
        write (unit=error_unit, fmt='(*(g0))') 'nums(', i, ') = ', nums(i)
    end do
    write (unit=error_unit, fmt='(*(g0))') 'signFunc(product) = ', signFunc(product(nums))
    write (unit=error_unit, fmt='(*(g0))')

contains

function product(array) result(prod)
    implicit none
    real(kind=DP), intent(in) :: array(:)
    real(kind=DP) :: prod
    integer :: i

    prod = 1._DP
    do i = 1, size(array)
        prod = prod * array(i)
    end do
end function product

function signFunc(x) result(sign)
    implicit none
    real(kind=DP), value :: x
    integer :: sign

    select case (x)
    case (0._DP)
        sign = 0
    case (< 0._DP)
        sign = -1
    case default
        sign = 1
    end select
end function signFunc

end program main
Compiled
Executed
Correct
module signFunc
implicit none

interface
    function signFunc(x) result(y)
        integer, intent(in) :: x
        integer :: y
    end function signFunc
end interface

contains

function signFunc(x) result(y)
    integer, intent(in) :: x
    integer :: y

    if (x > 0) then
        y = 1
    else if (x < 0) then
        y = -1
    else
        y = 0
    end if
end function signFunc

end module signFunc

program main
    use signFunc
    implicit none

    integer, parameter :: nums(7) = [-1, -2, -3, -4, 3, 2, 1]
    integer :: product
    integer :: result

    product = product(nums)
    result = signFunc(product)

    write (*,*) "Product: ", product
    write (*,*) "Result: ", result

contains

function product(nums) result(product)
    integer, intent(in) :: nums(:)
    integer :: product
    integer :: i

    product = 1
    do i = 1, size(nums)
        product = product * nums(i)
    end do
end function product

end program main
🌐 Data from online sources
def signFunc(x):
    if x > 0:
        return 1
    elif x < 0:
        return -1
    else:
        return 0

def arraySign(nums):
    product = 1
    for num in nums:
        product *= signFunc(num)
    return product

For the algorithm, we first define a function signFunc that returns 1 if a number is positive, -1 if it's negative, and 0 if it's 0. Then, we initialize a variable product with the value 1. We iterate through the array nums and for each number, we multiply the value of product by the return value of signFunc called with that number as its argument. Finally, we return the value of product.

Thanks to this approach, we don't compute the actual product, which may cause an overflow. Instead, we directly calculate the result using the signs of the integers in the array, making the solution efficient and safe for big arrays with large integers.

🌐 Data from online sources
int signFunc(int x) {
    if(x > 0) return 1;
    if(x < 0) return -1;
    return 0;
}

int arraySign(vector<int>& nums) {
    int product = 1;
    for(int i = 0; i < nums.size(); i++) {
        product *= signFunc(nums[i]);
    }
    return product;
}

For the algorithm, we first define a function signFunc that returns 1 if a number is positive, -1 if it's negative, and 0 if it's 0. Then, we initialize a variable product with the value 1. We iterate through the array nums and for each number, we multiply the value of product by the return value of signFunc called with that number as its argument. Finally, we return the value of product.

Thanks to this approach, we don't compute the actual product, which may cause an overflow. Instead, we directly calculate the result using the signs of the integers in the array, making the solution efficient and safe for big arrays with large integers.