You are given a binary array nums
(0-indexed).
We define xi
as the number whose binary representation is the subarray nums[0..i]
(from most-significant-bit to least-significant-bit).
nums = [1,0,1]
, then x0 = 1
, x1 = 2
, and x2 = 5
.Return an array of booleans answer
where answer[i]
is true
if xi
is divisible by 5
.
Example 1:
Input: nums = [0,1,1] Output: [true,false,false] Explanation: The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10. Only the first number is divisible by 5, so answer[0] is true.
Example 2:
Input: nums = [1,1,1] Output: [false,false,false]
Constraints:
1 <= nums.length <= 105
nums[i]
is either 0
or 1
.program main
implicit none
integer, parameter :: n = 3
integer, dimension(n) :: nums
logical, dimension(n) :: answer
! Example 1
nums = [0, 1, 1]
answer = solve(nums)
write (*, '(A, L1)') 'Example 1: ', answer
! Example 2
nums = [1, 1, 1]
answer = solve(nums)
write (*, '(A, L1)') 'Example 2: ', answer
contains
function solve(nums) result(answer)
implicit none
integer, dimension(:), intent(in) :: nums
logical, dimension(size(nums)) :: answer
integer :: i
do i = 1, size(nums)
answer(i) = mod(xi(nums, i), 5) == 0
end do
end function solve
function xi(nums, i) result(xi)
implicit none
integer, dimension(:), intent(in) :: nums
integer, value :: i
integer :: xi
xi = 0
do while (i > 0)
xi = xi * 2 + nums(i)
i = i - 1
end do
end function xi
end program main
temp.f95:31:35: 31 | function xi(nums, i) result(xi) | 1 Error: RESULT variable at (1) must be different than function name temp.f95:32:21: 32 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:33:49: 33 | integer, dimension(:), intent(in) :: nums | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:34:27: 34 | integer, value :: i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:35:21: 35 | integer :: xi | 1 Error: Symbol βxiβ at (1) has already been host associated temp.f95:37:11: 37 | xi = 0 | 1 Error: Symbol βxiβ at (1) has already been host associated temp.f95:38:24: 38 | do while (i > 0) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:39:15: 39 | xi = xi * 2 + nums(i) | 1 Error: Symbol βxiβ at (1) has already been host associated temp.f95:40:21: 40 | i = i - 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:41:11: 41 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:42:7: 42 | end function xi | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:27:28: 27 | answer(i) = mod(xi(nums, i), 5) == 0 | 1 Error: Function βxiβ at (1) has no IMPLICIT type
module is_divisible_by_5
implicit none
contains
function is_divisible_by_5(nums) result(answer)
integer, intent(in) :: nums(:)
logical :: answer(size(nums))
integer :: i, xi
do i = 1, size(nums)
xi = 0
do j = i, size(nums)
xi = xi * 2 + nums(j)
end do
answer(i) = mod(xi, 5) == 0
end do
end function is_divisible_by_5
end module is_divisible_by_5
program test_is_divisible_by_5
use is_divisible_by_5
implicit none
integer, parameter :: nums1(3) = [0, 1, 1]
integer, parameter :: nums2(3) = [1, 1, 1]
integer, parameter :: nums3(3) = [0, 0, 1]
integer, parameter :: nums4(3) = [1, 0, 1]
integer, parameter :: nums5(3) = [0, 1, 0]
integer, parameter :: nums6(3) = [1, 1, 0]
integer, parameter :: nums7(3) = [0, 0, 0]
integer, parameter :: nums8(3) = [1, 0, 0]
integer, parameter :: nums9(3) = [0, 1, 1]
integer, parameter :: nums10(3) = [1, 1, 1]
write (*,*) "Test 1:"
write (*,*) "Input: ", nums1
write (*,*) "Output: ", is_divisible_by_5(nums1)
write (*,*) "Expected: ", .true.
write (*,*) "Test 2:"
write (*,*) "Input: ", nums2
write (*,*) "Output: ", is_divisible_by_5(nums2)
write (*,*) "Expected: ", .false.
write (*,*) "Test 3:"
write (*,*) "Input: ", nums3
write (*,*) "Output: ", is_divisible_by_5(nums3)
write (*,*) "Expected: ", .false.
write (*,*) "Test 4:"
write (*,*) "Input: ", nums4
write (*,*) "Output: ", is_divisible_by_5(nums4)
write (*,*) "Expected: ", .true.
write (*,*) "Test 5:"
write (*,*) "Input: ", nums5
write (*,*) "Output: ", is_divisible_by_5(nums5)
write (*,*) "Expected: ", .false.
write (*,*) "Test 6:"
write (*,*) "Input: ", nums6
write (*,*) "Output: ", is_divisible_by_5(nums6)
write (*,*) "Expected: ", .false.
write (*,*) "Test 7:"
write (*,*) "Input: ", nums7
write (*,*) "Output: ", is_divisible_by_5(nums7)
write (*,*) "Expected: ", .false.
write (*,*) "Test 8:"
write (*,*) "Input: ", nums8
write (*,*) "Output: ", is_divisible_by_5(nums8)
write (*,*) "Expected: ", .true.
write (*,*) "Test 9:"
write (*,*) "Input: ", nums9
write (*,*) "Output: ", is_divisible_by_5(nums9)
write (*,*) "Expected: ", .false.
write (*,*) "Test 10:"
write (*,*) "Input: ", nums10
write (*,*) "Output: ", is_divisible_by_5(nums10)
write (*,*) "Expected: ", .true.
end program test_is_divisible_by_5
temp.f95:5:26: 5 | function is_divisible_by_5(nums) result(answer) | 1 Error: MODULE attribute of βis_divisible_by_5β conflicts with PROCEDURE attribute at (1) temp.f95:6:30: 6 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:7:23: 7 | logical :: answer(size(nums)) | 1 Error: Symbol βnumsβ is used before it is typed at (1) temp.f95:8:16: 8 | integer :: i, xi | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:20: 10 | do i = 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:11:10: 11 | xi = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:12:24: 12 | do j = i, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:13:29: 13 | xi = xi * 2 + nums(j) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:14:7: 14 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:15:31: 15 | answer(i) = mod(xi, 5) == 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:3: 16 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:18:3: 18 | end function is_divisible_by_5 | 1 Error: Expecting END MODULE statement at (1) temp.f95:23:5: 23 | use is_divisible_by_5 | 1 Fatal Error: Cannot open module file βis_divisible_by_5.modβ for reading at (1): No such file or directory compilation terminated.
def largestPerimeter(nums):
nums.sort(reverse=True)
for i in range(len(nums) - 2):
if nums[i] < nums[i + 1] + nums[i + 2]:
return nums[i] + nums[i + 1] + nums[i + 2]
return 0
To find the largest perimeter of a triangle with a non-zero area, we first sort the array in descending order. This allows us to check if a triangle can be formed by taking the longest possible sides first. Then, we iterate through the sorted array and check if the current element and the next two elements form a triangle of non-zero area. To check if a triangle can be formed, we use the triangle inequality theorem, which states that for any triangle with sides a, b, and c, the sum of any two sides must be greater than the third side (a + b > c, a + c > b, b + c > a). Since we have the array sorted in descending order, we only need to check one case of the inequality (nums[i] < nums[i + 1] + nums[i + 2]) for each iteration. If we find a valid triangle, we return the sum of its sides as the largest perimeter. If no valid triangle is found, we return 0.
#include <vector>
#include <algorithm>
int largestPerimeter(std::vector<int>& nums) {
std::sort(nums.begin(), nums.end(), std::greater<int>());
for (size_t i = 0; i < nums.size() - 2; ++i) {
if (nums[i] < nums[i + 1] + nums[i + 2]) {
return nums[i] + nums[i + 1] + nums[i + 2];
}
}
return 0;
}
To find the largest perimeter of a triangle with a non-zero area, we first sort the array in descending order. This allows us to check if a triangle can be formed by taking the longest possible sides first. Then, we iterate through the sorted array and check if the current element and the next two elements form a triangle of non-zero area. To check if a triangle can be formed, we use the triangle inequality theorem, which states that for any triangle with sides a, b, and c, the sum of any two sides must be greater than the third side (a + b > c, a + c > b, b + c > a). Since we have the array sorted in descending order, we only need to check one case of the inequality (nums[i] < nums[i + 1] + nums[i + 2]) for each iteration. If we find a valid triangle, we return the sum of its sides as the largest perimeter. If no valid triangle is found, we return 0.