Finds the smallest positive integer that does not appear in an array (the array element can be duplicated)

Source: Internet
Author: User

Title Description

Description

Given nn non-negative integers, please find the least non-negative integer This doesn ' t occur in the Nn Numbers.

Input

The first line was an integer T, representing the number of test cases. (T≤10)
The first line of all test case was an integer n (n≤2?10?^5??).
The second line of all test case is nn non-negativeintegers a? i?? (a? i?? <2?^31??).

Output

For each test case, output a linewith the answer.

Input sample

2

4

4 0 1 3

2

1 1

Output sample

2

0

Analysis: Violence in the range of numbers given, violence search is not not, but certainly will time out, online there are many solutions to the array elements are not duplicated, reference value is not very large, and then I think of a method, need to sort, then from the beginning, need to record the repetition number of repeated numbers, Based on the relationship between the subscript and the number of repetitions of the array, the solution is obtained. Time complexity O (n), Spatial complexity O (1)

Code:

1#include <algorithm>2#include <iostream>3#include <string>4#include <vector>5 using namespacestd;6 intMain ()7 {8     intN;9CIN >>N;Ten     //Use the GCC compiler when playing a game, and be sure to place two > between them to avoid miscalculation Onevector<vector<int> > V (1001,vector<int> (200001, Int_max)); Avector<int> Len (1001); -      for(inti =0; I < n; i++) -     { the         //int m; -CIN >>Len[i]; -          for(intj =0; J < Len[i]; J + +) -CIN >>V[i][j]; +     } -      for(inti =0; I < n; i++) +     { A         intres =0; at         intRepnum =0; - sort (V[i].begin (), V[i].end ()); -         if(v[i][0] >0) -         { -cout <<0<<Endl; -             Continue; in         } -         Else to         { +             intj =1; -              for(; J < Len[i]; J + +) the             { *                 if(V[i][j] = = V[i][j-1]) $                 {Panax Notoginsengrepnum++; -                     Continue; the                 } +                 if(V[i][j]! = J-repnum) A                 { thecout << J-repnum <<Endl; +                      Break; -                 } $             } $             if(J = =Len[i]) -cout << Len[i] <<Endl; -         } the     } -System"Pause");Wuyi     return 0;

Finds the smallest positive integer that does not appear in an array (the array element can be duplicated)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.