Find missing number 1..n
Java
Medium
6 views
Problem Description
Task: array has n-1 numbers from 1..n without duplicates. Return the missing number.
Output Format
Return value
Constraints
Use long for expected sum.
Official Solution
static int missing1ToN(int[] a,int n){long exp=1L*n*(n+1)/2;long sum=0;for(int x:a) sum+=x;return (int)(exp-sum);}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!