A tail recursive function is a one which has recursive call as the last command of the function. Nothing after the recursive call should be present in the function.
Kindly find below the scala code for given problem:
def arraySortedOrNot(list: List[Int]): Int = {
// list has one or no element or the rest are already checked and approved.
if (list.length == 1 || list.length == 0)
true
// Unsorted pair found (Equal values allowed)
var last = list.last
//removes last element in the list
list.dropRight(1)
if (last < list.last)
false
// Last pair was sorted, Keep on checking
arraySortedOrNot(list);
}
Get Answers For Free
Most questions answered within 1 hours.