In this post, we will solve **Order exercises HackerRank Solution**. This problem **(Order exercises)** is a part of **HackerRank Functional Programming** series.

**Task**

During a math class, a teacher wanted to practice ordering with students. He gave an array of * N* integers,

**to the students along with following definitions:**

*a*= {*a*_{1},*a*_{2}, . . . ,*a*_{N}}- Subarray is a contiguous segment of array. For example
is a subarray, where*a*[*l*,*r*] = {*a*_{l},*a*_{l+1}, . . . ,*a*_{r}} - We say that a sum of a subarray
is a sum of elements in this subarray*X*(=*a*[*xl*,*xr*] = {*a*_{xl},*a*_{xl+1}, . . . ,*a*_{xr}}) - We say that subarray is greater than subarray if and only if:
has a greater sum than**X****Y**and**X**has the same sum and**Y**begins earlier**X**and**X**has the same sum, they start in the same place and the length of**Y**is smaller than the length of**X****Y**

Since the teacher doesn’t like number 0, there is no 0 in the array * a*. Other than array

*, the teacher also gave an integer*

**a***. The task is to lists as many as possible, but not more than*

**K***, subarrays with a*

**K***positive sum*in the following order.

- The first subarray is the greatest subarray in the array according to above definition.
- The
subarray is the greatest subarray disjoint to any of the*i*^{th}subarray, where*j*^{th}(disjoint means that they have no common elements).*j*<*i*

Of course in order to win with others, you have to solve the problem first.

**Input**

In the first line there are two integers * N* and

*separated by a single space.*

**K**In the second line there are

*integers separated by single space denoting the array*

**N***.*

**arr****Output**

Print no more than * K* lines. In the

**line print the sum of the**

*i*^{th}**sequence in the above order.**

*i*^{th}**Constraints**

**1 <=***N*<= 10^{5}**1 <=***K*<=*N***0 < |***a*| <= 104, where_{i}*i*∈ [1,*N*]

**Sample Input 00**

```
5 3
2 4 -10 2 -2
```

**Sample Output 00**

```
6
2
```

**Explanation**

Subarray ** a[1, 2] = {a1, a2}** has sum 6 and this is the greatest value in the whole array. Next disjoint greatest subarray is

**with sum = 2. There are no more subsequences with a positive value disjoint with the first and the second subsequence.**

*a*[4, 4] =*a*_{4}**Sample Input 01**

```
4 2
-2 5 -1 -8
```

**Sample Output 01**

`5 `

**Explanation 01**

Subarray ** a[2, 2] = {a2}** has sum 5 and this is the greatest value in the whole array. There are no more subsequences with a positive value disjoint with the first one, so even if K = 2, we print out just one value.

**Solution – Order exercises – HackerRank Solution**

**Scala**

import java.util.Scanner import scala.collection.immutable.Queue object Solution { def main(args: Array[String]): Unit = { val sc = new Scanner(System.in) sc.nextInt val k = sc.nextInt sc.nextLine val a = sc.nextLine.split(" ").map(_.toInt) sc.close() case class Acc(list: List[Int]) val sentinel = Int.MaxValue / 2 val temp = a.foldLeft(Acc(List(-sentinel, sentinel)))((acc, v) => acc.list match { case Nil if v < 0 => acc case Nil => Acc(v :: Nil) case list@x :: xs => if ((v < 0) == (x < 0)) Acc(x + v :: xs) else Acc(v :: list) }).list val compressed = if (temp.head < 0) temp.tail else temp println(solve(compressed) .sortBy(-_) .take(k) .mkString("\n") ) } def solve(seq: List[Int]): List[Int] = { case class Accumulator(totalSum: Int, queue: Queue[Int], answer: List[Int]) @scala.annotation.tailrec def inner(seq: List[Int], acc: Accumulator): List[Int] = { seq match { case Nil => acc.answer case xPos :: xs if acc.queue.isEmpty => //empty queue inner(xs, Accumulator(xPos, Queue(xPos), acc.answer)) case xNeg :: xPos :: xs => val (nextSeq, nextAcc) = { val sNeg = acc.totalSum + xNeg if (sNeg <= 0) { val (firstPos, tempQueue) = acc.queue.dequeue if (tempQueue.isEmpty) { //All items in queue are processed. (xs, Accumulator(xPos, Queue(xPos), firstPos :: acc.answer)) } else { if (acc.totalSum >= firstPos) { //Glue items in queue. (xs, Accumulator(xPos, Queue(xPos), acc.totalSum :: acc.answer)) } else { //Separate first item in queue. val (_, nextQueue) = tempQueue.dequeue (nextQueue.toList ++ seq, Accumulator(0, Queue(), firstPos :: acc.answer)) } } } else { val sPos = sNeg + xPos if (sPos >= acc.queue.head) { //Glue (xs, Accumulator(sPos, Queue(sPos), acc.answer)) } else { //Uncertain (xs, Accumulator(sPos, acc.queue.enqueue(xNeg).enqueue(xPos), acc.answer)) } } } inner(nextSeq, nextAcc) } } inner(seq, Accumulator(0, Queue(), Nil)) } }

**Note:** This problem **(Order exercises)** is generated by **HackerRank** but the solution is provided by **CodingBroz**. This tutorial is only **Educational** and **Learning** purpose.