Given an array of size N-1 such that it only contains distinct integers in the range of 1 to N. Find the missing element.
Example 1:
Input:N = 5A[] = {1,2,3,5}Output: 4
Discussion posts and replies are publicly visible
a!localVariables( local!list: {1,2,3,5}, difference(1 + enumerate(max(local!list)), local!list) )
alternate approach ->
a!localVariables( local!list: { 1, 2, 3, 4, 5, 6, 9, 8, 10, 11 }, local!n: length(local!list) + 1, (local!n * (local!n + 1)) / 2 - sum(local!list) )
this code will not work for {1,2,3,4,5} where N = 6 or for similar cases
Just swap max(local!list) with your actual maximum number - try this:
a!localVariables( local!maxNumber: 6, local!list: {1, 2, 3, 4, 5}, difference(1 + enumerate(local!maxNumber), local!list) )
I'd say this is the best approach except that I would go by length of the array +1, rather than by max value. That eliminates the possibility of tripping up on first value or last value. But I was totally about to bust out a!forEach.
Plus I never knew that 1 + enumerate(n) worked; I always did enumerate(n) +1. Still trying to decide which is more readable.
a!localVariables( local!number: { 1, 2, 3,4,5,7}, local!n: length(local!number) , ((local!n +1) *(local!n+2))/2 - sum(local!number), )