Coders Packet

Positive Missing Number From Unsorted Array In C++

By Aditi Mishra

This project resolves the problem of positive missing numbers from unsorted arrays in C++ language . We can search any numbers entered by the user from the array if it is present.

Searching Positive Missing Numbers from unsorted arrays is the main task of my program which is done in C++ language.

An array "a" is there with size "n" is taken, then first sorting is done and after that, for searching the element, logic has been applied in the program.

 

Code->

#include<bits/stdc++.h>
using namespace std;


    int missingNumber(int arr[], int n) { 
        
    sort(arr, arr+n);
    int i,k=0;
    for(i=0; i<n; i++)
        {
            if(arr[i]>0)
            {
                break;
                
            }
        }
    int max1=1;
    for(k=i; k<n; k++)
        {
            if(arr[k]>max1){
                
                return max1;
                
            }
            if(arr[k] == arr[k-1])
                continue;
            max1= max1+1;    
        }
      return (max1);
    
}



int main(){
    int t;
    cout<<"\nEnter the number of testcases:-> ";
    cin>>t;
    while(t--){
        int n;
        cout<<"\nEnter the size of array:-> ";
        cin>>n;
    
    int a[n];
    cout<<"\nEnter the elements of array:-> ";
    for(int i=0;i<n;i++){
        cin>>a[i];
        
    }
    cout<<"\nMissing number is:-> ";
    cout<<missingNumber(a,n);
    cout<<"\n";
}
return 0;
}

Input- Enter the number of testcases:-> 2

          Enter the size of array:-> 5

          Enter the elements of array:-> 1 2 3 4 5

Output- Missing number is:-> 6

 


Input- Enter the size of array:-> 7

          Enter the elements of array:-> 4 5 8 2 -5 0 6

Output- Missing number is:-> 1

Download project

Reviews Report

Submitted by Aditi Mishra (aditi1310)

Download packets of source code on Coders Packet