In this post, we will solve **Mirko at the Construction Site HackerRank Solution**. This problem **(Mirko at the Construction Site)** is a part of **HackerRank Functional Programming** series.

**Task**

Mirko is monitoring a construction site. He monitorsÂ * N*Â buildings enumerated fromÂ

**1**Â toÂ

*, starting from the left. For each building, he knows the current number of floors and the number of floors built on each day. He needs to know the answer toÂ*

**N***Â queries. The answer to each query is the index of the tallest building afterÂ*

**Q***Â days, as defined by the query. Your task is to help Mirko find the answers to these queries.*

**T****Input Format**

The first line consists of the numbersÂ * N*Â andÂ

*. The second line consists ofÂ*

**Q***Â integers, where theÂ*

**N****Â integer represents the initial height of theÂ**

*i*^{th}**Â building. The third line consists ofÂ**

*i*^{th}*Â integers, where theÂ*

**N****Â integer represents the number of floors erected in one day for theÂ**

*i*^{th}**Â building. The followingÂ**

*i*^{th}*Â lines consist of the integer,Â*

**Q***, representing the day in the query.*

**T****Output Format**

For each query, output one number which represents the index of the tallest building afterÂ * T*Â days. If there is more than one building, output the building with theÂ

*greatest*Â index in the input array (with indexes starting at 1).

**Constraints**

**1 <=***N*<= 10^{5}**1 <=***Q*<= 10^{5}- Every other integer in the input will fit in a 32-bit signed integer. And they will be non-negative integers.

**Sample Input**

```
3 6
7 5 1
1 2 3
0
1
2
3
4
5
```

**Sample Output**

```
1
1
2
2
3
3
```

**Explanation**

*Query #1:*Â The height at the end of theÂ **0 ^{th}**Â day will beÂ

**{7, 5, 1}**. Here, theÂ

**1**Â building is the tallest.

^{st}*Query #2*: The height at the end of theÂ

**1**Â day will beÂ

^{st}**{8, 7, 4}**. Here, theÂ

**1**Â building is the tallest.

^{st}*Query #3:*Â The height at the end ofÂ

**2**Â day will beÂ

^{nd}**{9, 9, 7}**. Here, theÂ

**1**Â andÂ

^{st}**2**Â buildings are the tallest, while theÂ

^{nd}**2**Â is the larger index.

^{nd}*Query #4:*Â The height at the end ofÂ

**3**Â day will beÂ

^{rd}**{10, 11, 10}**. Here, theÂ

**2**Â building is the tallest.

^{nd}*Query #5:*Â The height at the end ofÂ

**4**Â day will beÂ

^{th}**{11, 13, 13}**. Here, theÂ

**2**Â andÂ

^{nd}**3**Â buildings are the tallest, while theÂ

^{rd}**3**Â is the larger index.

^{rd}*Query #6:*Â The height at the end ofÂ

**5**Â day will beÂ

^{th}**{12, 15, 16}**. Here, theÂ

**3**Â building is the tallest.

^{rd}**Solution – Mirko at the Construction Site – HackerRank Solution**

**Scala**

import java.util.Scanner object Solution { def main(args: Array[String]): Unit = { val sc = new Scanner(System.in) sc.nextInt val q = sc.nextInt sc.nextLine val initials = sc.nextLine.split(" ").map(_.toInt) val steps = sc.nextLine.split(" ").map(_.toInt) case class Query(index: Int, time: Int) val queries = (0 until q).map(Query(_, sc.nextInt)).sortBy(_.time) sc.close() case class Building(index: Int, initial: Long, step: Long) extends Ordered[Building] { override def compare(that: Building): Int = { if (this.step < that.step) -1 else if (this.step > that.step) 1 else if (this.initial < that.initial) -1 else if (this.initial > that.initial) 1 else this.index.compareTo(that.index) } def height(time: Long): Long = initial + step * time } val buildings: List[Building] = initials.indices.map(i => Building(i + 1, initials(i), steps(i))) .groupBy(_.step) .map { case (_, list) => list.maxBy(_.initial) } .toList .sorted def intersection(b0: Building, b1: Building) = (b1.initial.toDouble - b0.initial) / (b0.step.toDouble - b1.step) @scala.annotation.tailrec def prepare(buildings: List[Building], acc: List[Building]): List[Building] = buildings match { case Nil => acc case v :: Nil => v :: acc case v0 :: v1 :: vs => val highest = acc.head val inter0 = intersection(highest, v0) val inter1 = intersection(highest, v1) if (inter0 < inter1 || inter0 == inter1 && v0.index > v1.index) prepare(v1 :: vs, v0 :: acc) else if (acc.tail.isEmpty) prepare(v1 :: vs, acc) else prepare(acc.head :: v1 :: vs, acc.tail) } val highBuildings = prepare(buildings.tail, List(buildings.head)).reverse case class Pair(queryIndex: Int, highestIndex: Int) case class Acc(buildings: List[Building], highestIndices: List[Pair]) val answer = queries.foldLeft(Acc(highBuildings, Nil))((acc, q) => { @scala.annotation.tailrec def findHighest(buildings: List[Building]): Acc = buildings match { case b :: Nil => Acc(acc.buildings, Pair(q.index, b.index) :: acc.highestIndices) case b0 :: b1 :: bs => val inter = intersection(b0, b1) if (inter > q.time || inter == q.time && b0.index > b1.index) Acc(buildings, Pair(q.index, b0.index) :: acc.highestIndices) else findHighest(b1 :: bs) case Nil => throw new Exception("Impossible") } findHighest(acc.buildings) }) println(answer.highestIndices .sortBy(_.queryIndex) .map(_.highestIndex) .mkString("\n") ) } }

**Note:** This problem **(Mirko at the Construction Site)** is generated by **HackerRank** but the solution is provided by **CodingBroz**. This tutorial is only for **Educational** and **Learning** purpose.