problem no 23: Missing number inside the array.

QUESTION:

Vaibhav likes to play with numbers and he has n numbers. One day he was placing the numbers on the playing board just to count that how many numbers he have. He was placing the numbers in increasing order i.e. from 1 to n. But when he was putting the numbers back into his bag, some numbers fell down onto the floor. He picked up all the numbers but one number, he couldn't find. Now he have to go somewhere urgently, so he asks you to find the missing number.

Input:
The first of input contains an integer T, then T test cases follow. Each test case contains an integer n i.e. numbers of integers he placed on the board and the second line of each test case contains the array a[] which represents the numbers, he successfully picked up from the floor. 


Output:
For each test case in a new line print the missing number.


Constraints:
1 <= T <= 100
1 <= n <= 104
1 <= a[i] <= 104


Example:
Input
:                    
2                                        
4                                        
1 4 3                           
5
2 5 3 1
Output:
2
4

 

ANSWER:

 #include<bits/stdc++.h>

using namespace std;

int missingnumber(int arr[],int n)
{
    int index;
    for(int i=0;i<n;i++)
    {
        if(arr[i]!=i+1)
        {
            index=i+1;
            return index;
        }
    }
}


int main() {
    int t;cin>>t;
    while(t--)
    {
        int n;cin>>n;
        int arr[n-1];
        for(int i=0;i<n-1;i++)
        {
            cin>>arr[i];
        }
        sort(arr,arr+n-1);
        
        int number=missingnumber(arr,n);
        cout<<number<<"\n";
    }
    return 0;
}

 

 

Comments

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.