I solve the following problem in Java:
(Condition clarified after my questions, inspired by communication with users). It is necessary to find the minimum natural number that is not in the input array.
The input is a string containing integers in the range from -10 ^ 9 to 10 ^ 9, separated by a space. The output is expected one number that satisfies the condition of the problem.
The code rewrote a bit:
import java.util.*; public class hh1 { public static void main(String[] args) { //int[] arr = {7, 3, 12, 5}; //int[] arr = {6, 9, 5, 2, 3}; int[] arr = {8, 6, 9, 81, -1000000001}; if (arr==null) System.out.println(""); else { List<Integer> list = new ArrayList<>(); Set<Integer> set = new HashSet<>(); for (int i = 0; i < arr.length; i++) { set.add(arr[i]); } for (Integer integer : set) { list.add(integer); } Collections.sort(list); for (int i = 0; i < list.size(); i++) { if (i + 1 >= list.size()) break; if (list.get(0) > 1 || list.get(0) < 0) { System.out.println(1); break; } if ((list.get(i + 1) - list.get(i)) > 1) { int i1 = (list.get(i + 1) - list.get(i)) - 1; System.out.println(list.get(i + 1) - i1); break; } else { System.out.println(""); break; } } } } } Example:
Array 6, 9, 5, 2, 3, 1 sorted in ascending order - 1, 2, 3, 5, 6, 9. The minimum integer missing element greater than zero is 4.
Corrected taking into account user tips and negative numbers (passes one test more, but 6 of the 29 fails, as it does not show me). Perhaps I did not take into account something.
How else to drive her?
Where is the mistake?
{1, 2, 3, 4, 5}? - Denis