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.

Contents

**Task**

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Â

*Â subsequent lines each containÂ*

**N****2**Â space-separated integers denoting the respectiveÂ

*Â andÂ*

**x***Â coordinates of a point.*

**y****Constraints**

- NoÂ
**2**Â points areÂ*coincident*, and polygonÂÂ is obtained by traversing the points in a counter-clockwise direction.**P** **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) .map(list => (list.head, list.last)) .map { case (c0, c1) => c0.x * c1.y - c0.y * c1.x } .sum / 2) println(square) } }

**Note:** This problem **(Compute the Area of a Polygon)** is generated by **HackerRank** but the solution is provided by **CodingBroz**. This tutorial is only for **Educational** and **Learning** purpose.