The place of the missing element does not matter. What you are implementing here is the Gaussian addition formula to add numbers from 1..n. which is actually n*(n-1) /2.

In this case, you are using the length of the array as the determinant for N. However, we know that the array is missing one member for sure, which means that the real length if the array was full would be nums.length + 1... hence your formula turns into calculating the total by n * (n+1) /2.

By that token, you are also guaranteed to find the missing value if you added all the numbers in the supposedly full array and subtracted the total from this array.

As someone else also pointed out, you don't need the if statement before the return line. The last line is always going to give you the right answer.