I am solving a simple problem on one of the coding challenge websites where I need to find the missing element from the array.
Problem statement is that you have an input array from 1 to N+1 and you have to find the missing element from that array.
For example, given array A such that:
A[0] = 2
A[1] = 3
A[2] = 1
A[3] = 5
the function should return 4, as it is the missing element.
For solving this I wrote a simple function that computed the result
def sol(a: Array[Int]): Int = {
((a.length+1)*(a.length+2)/2)-a.sum
}
However on submitting the solution I got the correctness of the solution as 100% but was shocked to see the performance results at 60%. Can anybody point out any part of this one line code that can be optimised or is there any other way to achieve 100% performance for this ?
Note : I am looking out for functional style code.