sadensmol / test_threatrix

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Write a function:

that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.

For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.

Given A = [1, 2, 3], the function should return 4.

Given A = [−1, −3], the function should return 1.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [1..100,000];


*Solutions*

**Solution 1 - simples one, on the live coding**
just iterations. performance :  700 ns


**Solution 2 - set based**
temp set collection.

performance :  500 ns
space complexity: O(n) - need to create additional collection
time complexity: O(n^2) - need to go through all collection n times in the most bad case

**Solution 2 - bubble sort**

performance :  100 ns
space complexity: O(1) - working with the same collection
time complexity: O(n) - go over collection once


About


Languages

Language:Java 100.0%