Created
May 26, 2020 02:08
-
-
Save niroj-office/54748b99d847d004059a00e483a81b65 to your computer and use it in GitHub Desktop.
Program to find longest sequence in Scala. Example: input- List(0,1,5,6,2), Output - 0,1,2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val longestSequence = (numList: List[Int]) => { | |
val list = numList | |
.map(x => { | |
var n = 0; var list = List[Int]() | |
while (numList.sorted.distinct.containsSlice(x to x + n)) { | |
list = (x to x + n).toList; n += 1; | |
} | |
list | |
}) | |
.map(x => (x, x.length)) | |
.sortBy(_._2)(Ordering.Int.reverse)(0) | |
._1 | |
.sorted(Ordering.Int.reverse) | |
var n = 1 | |
var finalList = list | |
while (numList.filter(_ == list.max - n).length > 1) { | |
finalList = list.max - n :: finalList; n += 1 | |
} | |
finalList.map(_.toString).reduce(_ + "," + _) | |
} | |
//Author: Niroj Pattnaik |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment