# Super Digit – HackerRank Solution

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

Contents

We define super digit of an integerÂ xÂ using the following rules:

• IfÂ xÂ has onlyÂ 1Â digit, then its super digit isÂ x.
• Otherwise, the super digit ofÂ xÂ is equal to the super digit of the digit-sum ofÂ x. Here, digit-sum of a number is defined as the sum of its digits.

For example, super digit ofÂ 9875Â will be calculated as:

``````super_digit(9875) = super_digit(9+8+7+5)
= super_digit(29)
= super_digit(2+9)
= super_digit(11)
= super_digit(1+1)
= super_digit(2)
= 2.``````

You are given two numbersÂ nÂ andÂ k. You have to calculate the super digit ofÂ P.

PÂ is created when numberÂ nÂ is concatenatedÂ kÂ times. That is, ifÂ n = 123Â andÂ k = 3, thenÂ P = 123123123.

## Input Format

Output the super digit ofÂ P, whereÂ PÂ is created as described above.

Sample Input 0

``148 3``

Sample Output 0

``3``

Explanation 0

HereÂ n = 148Â andÂ k = 3, soÂ P = 148148148.

``````super_digit(P) = super_digit(148148148)
= super_digit(1+4+8+1+4+8+1+4+8)
= super_digit(39)
= super_digit(3+9)
= super_digit(12)
= super_digit(1+2)
= super_digit(3)
= 3.``````

## Solution – Super Digit – HackerRank Solution

Scala

```import java.util.Scanner

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

val n = sc.next
val k = sc.nextInt

sc.close()

val nDigit = superOnce(n)
println(superDigit((nDigit.toLong * k).toString))
}

def superDigit(s: Seq[Char]): Int = {
val sum = superOnce(s)
if (sum < 10) sum else superOnce(sum.toString)
}

def superOnce(s: Seq[Char]): Int = s.foldLeft(0)((acc, c) => acc + c - '0')
}
```

Note: This problem (Super Digit) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.