# Compute the Area of a Polygon – HackerRank Solution

In this post, we will solve Compute the Area of a Polygon HackerRank Solution. This problem (Compute the Area of a Polygon) is a part of HackerRank Functional Programming series.

You are given the cartesian coordinates of a set of points in aÂ 2DÂ plane. When traversed sequentially, these points form a Polygon,Â P, which is not self-intersecting in nature. Can you compute the area of polygonÂ P?

## Input Format

The first line contains an integer,Â N, denoting the number of points.
TheÂ NÂ subsequent lines each containÂ 2Â space-separated integers denoting the respectiveÂ xÂ andÂ yÂ coordinates of a point.

## Constraints

• NoÂ 2Â points areÂ coincident, and polygonÂ PÂ is obtained by traversing the points in a counter-clockwise direction.
• 4 <= N <= 1000
• 0 <= x, y <= 1000

## Output Format

For each test case, print the area ofÂ PÂ (correct to a scale of one decimal place).

Note:Â Do not add any leading/trailing spaces or units; it is assumed that your result is in square units.

Sample Input

``````4
0 0
0 1
1 1
1 0``````

Sample Output

``1``

Explanation

The given polygon is a square, and each of its sides areÂ 1Â unit in length.
area(P) = length x width = 1 x 1 = 1, so we printÂ Â on a new line.

## Solution – Compute the Area of a Polygon – HackerRank Solution

Scala

```import java.util.Scanner

object Solution {
def main(args: Array[String]): Unit = {
val sc = new Scanner(System.in)
val n = sc.nextInt

case class Coord(x: Double, y: Double)

val coords = (0 until n).map(_ => Coord(sc.nextInt, sc.nextInt))

val square = math.abs((coords.last +: coords).sliding(2)