Given an array of points on the X-Y plane points
where points[i] = [xi, yi]
, return the area of the largest triangle that can be formed by any three different points. Answers within 10-5
of the actual answer will be accepted.
Example 1:
Input: points = [[0,0],[0,1],[1,0],[0,2],[2,0]] Output: 2.00000 Explanation: The five points are shown in the above figure. The red triangle is the largest.
Example 2:
Input: points = [[1,0],[0,0],[0,1]] Output: 0.50000
Constraints:
3 <= points.length <= 50
-50 <= xi, yi <= 50
program main
! Solves the problem:
! Given an array of points on the X-Y plane points where points[i] = [xi, yi], return the area of the largest triangle that can be formed by any three different points.
use, intrinsic :: iso_fortran_env, only : error_unit, DP => REAL64
implicit none
integer, parameter :: n = 50
real(kind=DP), dimension(n) :: points
real(kind=DP) :: area
integer :: i, j, k
call get_points(points)
do i = 1, size(points)-2
do j = i+1, size(points)-1
do k = j+1, size(points)
area = triangle_area(points(i), points(j), points(k))
write (unit=error_unit, fmt='(A,F10.5)') 'Area: ', area
end do
end do
end do
contains
subroutine get_points(points)
! Reads the input points from stdin and stores them in the array points
implicit none
real(kind=DP), dimension(:), intent(out) :: points
integer :: i, n
character(len=100) :: line
read (unit=input_unit, fmt='(I10)') n
if (n > size(points)) then
write (unit=error_unit, fmt='(A)') 'Too many points'
stop 1
end if
do i = 1, n
read (unit=input_unit, fmt='(A)') line
read (unit=line, fmt='(2(I10,1X))') points(i), points(i+n)
end do
end subroutine get_points
function triangle_area(a, b, c) result(area)
! Returns the area of the triangle formed by the points a, b and c
implicit none
real(kind=DP), value :: a(2), b(2), c(2)
real(kind=DP) :: area
area = abs((a(1)*b(2)+b(1)*c(2)+c(1)*a(2)-a(2)*b(1)-b(2)*c(1)-c(2)*a(1)) / 2.0D0)
end function triangle_area
end program main
temp.f95:46:33: 46 | real(kind=DP), value :: a(2), b(2), c(2) | 1 Error: VALUE attribute conflicts with DIMENSION attribute at (1) temp.f95:43:28: 43 | function triangle_area(a, b, c) result(area) | 1 Error: Symbol ‘a’ at (1) has no IMPLICIT type temp.f95:43:31: 43 | function triangle_area(a, b, c) result(area) | 1 Error: Symbol ‘b’ at (1) has no IMPLICIT type temp.f95:43:34: 43 | function triangle_area(a, b, c) result(area) | 1 Error: Symbol ‘c’ at (1) has no IMPLICIT type temp.f95:31:29: 31 | read (unit=input_unit, fmt='(I10)') n | 1 Error: Symbol ‘input_unit’ at (1) has no IMPLICIT type temp.f95:49:76: 49 | area = abs((a(1)*b(2)+b(1)*c(2)+c(1)*a(2)-a(2)*b(1)-b(2)*c(1)-c(2)*a(1)) / 2.0D0) | 1 Error: Function ‘a’ at (1) has no IMPLICIT type temp.f95:16:23: 16 | area = triangle_area(points(i), points(j), points(k)) | 1 Error: Type mismatch in argument ‘a’ at (1); passed REAL(8) to UNKNOWN
module largest_triangle
implicit none
contains
real function largest_triangle(points) result(area)
! Given an array of points on the X-Y plane, return the area of the largest triangle that can be formed by any three different points.
! Input:
! points: array of points on the X-Y plane, where points[i] = [xi, yi]
! Output:
! area: the area of the largest triangle that can be formed by any three different points
real, intent(in) :: points(:, :)
real :: x1, y1, x2, y2, x3, y3, area
! Initialize the largest triangle area to 0
area = 0.0
! Loop through all possible combinations of three points
do i = 1, size(points, 1) - 2
do j = i + 1, size(points, 1) - 1
do k = j + 1, size(points, 1)
! Calculate the area of the current triangle
x1 = points(i, 1)
y1 = points(i, 2)
x2 = points(j, 1)
y2 = points(j, 2)
x3 = points(k, 1)
y3 = points(k, 2)
area = abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0)
! Update the largest triangle area if necessary
if (area > largest_triangle) then
largest_triangle = area
end if
end do
end do
end do
end function
end module
program test_largest_triangle
use largest_triangle
implicit none
! Test case 1:
! Input: [[0,0], [0,1], [1,0], [0,2], [2,0]]
! Output: 2.00000
! Explanation: The five points are shown in the above figure. The red triangle is the largest.
call assert(abs(largest_triangle([[0,0], [0,1], [1,0], [0,2], [2,0]]) - 2.0) < 1e-5)
! Test case 2:
! Input: [[1,0], [0,0], [0,1]]
! Output: 0.50000
call assert(abs(largest_triangle([[1,0], [0,0], [0,1]]) - 0.5) < 1e-5)
contains
subroutine assert(condition)
logical, intent(in) :: condition
integer :: error_code
if (.not. condition) then
error_code = 1
else
error_code = 0
end if
if (error_code /= 0) then
write (*, '(A)') 'Test failed.'
else
write (*, '(A)') 'Test passed.'
end if
end subroutine
end program
temp.f95:7:30: 7 | real function largest_triangle(points) result(area) | 1 Error: MODULE attribute of ‘largest_triangle’ conflicts with PROCEDURE attribute at (1) temp.f95:16:32: 16 | real, intent(in) :: points(:, :) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:17:36: 17 | real :: x1, y1, x2, y2, x3, y3, area | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:20:10: 20 | area = 0.0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:23:29: 23 | do i = 1, size(points, 1) - 2 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:24:37: 24 | do j = i + 1, size(points, 1) - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:25:37: 25 | do k = j + 1, size(points, 1) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:28:29: 28 | x1 = points(i, 1) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:29:29: 29 | y1 = points(i, 2) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:30:29: 30 | x2 = points(j, 1) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:31:29: 31 | y2 = points(j, 2) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:32:29: 32 | x3 = points(k, 1) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:33:29: 33 | y3 = points(k, 2) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:34:80: 34 | area = abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:37:40: 37 | if (area > largest_triangle) then | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:38:33: 38 | largest_triangle = area | 1 Error: Symbol ‘largest_triangle’ at (1) has already been host associated temp.f95:39:15: 39 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:41:11: 41 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:42:7: 42 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:43:3: 43 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:45:3: 45 | end function | 1 Error: Expecting END MODULE statement at (1) temp.f95:51:5: 51 | use largest_triangle | 1 Fatal Error: Cannot open module file ‘largest_triangle.mod’ for reading at (1): No such file or directory compilation terminated.
def is_shifted(s, goal):
if len(s) != len(goal): return False
s = s + s
return goal in s
The algorithm first checks if the length of both input strings is not equal, in which case it returns false as they can't possibly be shifted versions of each other. If they are of the same length, it concatenates the string s
with itself, which creates all possible shifted combinations of s
. Then it checks whether the goal
is present in this concatenated string or not. If it is present, it returns true (as s
can be shifted to obtain the goal
), else it returns false.
bool isShifted(string s, string goal) {
if(s.length() != goal.length()) return false;
s = s + s;
return s.find(goal) != string::npos;
}
The algorithm first checks if the length of both input strings is not equal, in which case it returns false as they can't possibly be shifted versions of each other. If they are of the same length, it concatenates the string s
with itself, which creates all possible shifted combinations of s
. Then it checks whether the goal
is present in this concatenated string or not. If it is present, it returns true (as s
can be shifted to obtain the goal
), else it returns false.