#include
using namespace std;
int main()
{
int n;
cin>>n;
int *arr=new int[n];
for(int i=0;i<n;++i)
cin>>arr[i];
unsigned long long int sum=n*(n+1)/2;
for(int i=0;i<n;++i)
sum-=arr[i];
cout<<sum<<endl;
}
#include
using namespace std;
int main()
{
int n;
cin>>n;
int *arr=new int[n];
for(int i=0;i<n;++i)
cin>>arr[i];
unsigned long long int sum=n*(n+1)/2;
for(int i=0;i<n;++i)
sum-=arr[i];
cout<<sum<<endl;
}
An efficient Algorithm to find the missing integer is as :
Using concept of XOR operation we will solve the problem as: Step 1 : Initialize one variable n (int n),vector ‘nums’ of integer type(vector nums).(n is the number of elements to be pushed in the vector ‘nums’) and a variable ‘a’.
Step 2 : Make a function missingNumber and pass ‘nums’ as parameter(call by refrence).
Ie, missingNumber(nums)
Int missingNumber(vector& nums)
Step 3 : Inside the function Initialize two variables x and y both equal to zero initially.
Step 4 : XOR all the numbers from 0 to n, let the result be x. i)Loop from i=0 till i< size of vector ‘nums’ (i=0;i<nums.size(); i++) ,and xor i with value of ‘x’ and store the result in ‘x’ on every iteration.
Step 5 : XOR all the vector elements , let the result y. i)Loop from i=0 till i< size of vector ‘nums’ (i=0;i<nums.size(); i++) ,and xor nums[i] with value of ‘y’ and store the result in ‘y’ on every iteration.
Step 6 : XOR of x and y gives the missing number.
Return the xor of x and y;
Solution link : https://ide.codingblocks.com/s/183412