Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- #include<bits/stdc++.h>
- using namespace std;
- // } Driver Code Ends
- class Solution
- {
- public:
- //Function to find the smallest positive number missing from the array.
- // -4 -3 -2 -2 -1 0 1 1 2
- int missingNumber(int arr[], int n)
- {
- // Your code here
- set<int> s;
- for(int i=0;i<n;i++)
- {
- s.insert(arr[i]);
- }
- int ans=*max_element(arr,arr+n);
- ans+=1;
- int j=1;
- set<int>::iterator itr;
- for (itr = s.begin();itr != s.end(); itr++)
- {
- if(*itr<=0)
- {
- continue;
- }
- else
- {
- if(*itr!=j)
- {
- ans=j;
- break;
- }
- }
- j++;
- }
- if(ans<0)
- {
- return 1;
- }
- return ans;
- }
- };
- //{ Driver Code Starts.
- int missingNumber(int arr[], int n);
- int main() {
- //taking testcases
- int t;
- cin>>t;
- while(t--){
- //input number n
- int n;
- cin>>n;
- int arr[n];
- //adding elements to the array
- for(int i=0; i<n; i++)cin>>arr[i];
- Solution ob;
- //calling missingNumber()
- cout<<ob.missingNumber(arr, n)<<endl;
- }
- return 0;
- }
- // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment