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
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
temp.f95:51:10: 51 | case (0._DP) | 1 Error: Expression in CASE selector at (1) cannot be REAL(8) temp.f95:52:16: 52 | sign = 0 | 1 Error: Expected a CASE or END SELECT statement following SELECT CASE at (1) temp.f95:53:11: 53 | case (< 0._DP) | 1 Error: Expected initialization expression in CASE at (1) temp.f95:54:17: 54 | sign = -1 | 1 Error: Expected a CASE or END SELECT statement following SELECT CASE at (1) temp.f95:50:17: 50 | select case (x) | 1 Error: Argument of SELECT statement at (1) cannot be REAL(8) temp.f95:16:4: 16 | nums = [1._DP, 5._DP, 0._DP, 2._DP, -3._DP] | 1 Error: Different shape for array assignment at (1) on dimension 1 (7 and 5) temp.f95:24:4: 24 | nums = [-1._DP, 1._DP, -1._DP, 1._DP, -1._DP] | 1 Error: Different shape for array assignment at (1) on dimension 1 (7 and 5)
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
temp.f95:5:21: 5 | function signFunc(x) result(y) | 1 Error: MODULE attribute of ‘signfunc’ conflicts with PROCEDURE attribute at (1) temp.f95:6:32: 6 | integer, intent(in) :: x | 1 Error: Unexpected data declaration statement in INTERFACE block at (1) temp.f95:7:20: 7 | integer :: y | 1 Error: Unexpected data declaration statement in INTERFACE block at (1) temp.f95:8:7: 8 | end function signFunc | 1 Error: Expecting END INTERFACE statement at (1) temp.f95:13:17: 13 | function signFunc(x) result(y) | 1 Error: MODULE attribute of ‘signfunc’ conflicts with PROCEDURE attribute at (1) temp.f95:14:28: 14 | integer, intent(in) :: x | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:15:16: 15 | integer :: y | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:17:19: 17 | if (x > 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:18:13: 18 | y = 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:24: 19 | else if (x < 0) then | 1 Error: Unexpected ELSE IF statement in CONTAINS section at (1) temp.f95:20:14: 20 | y = -1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:21:8: 21 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:22:13: 22 | y = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:23:7: 23 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:24:3: 24 | end function signFunc | 1 Error: Expecting END MODULE statement at (1) temp.f95:29:9: 29 | use signFunc | 1 Fatal Error: Cannot open module file ‘signfunc.mod’ for reading at (1): No such file or directory compilation terminated.
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.
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.