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?

  • if I correctly understood your task, then what result should be with the array {1, 2, 3, 4, 5} ? - Denis
  • What should be the conclusion? Just the number 5? - Anton Sorokin
  • Perhaps zero is also considered positive. - Enikeyschik September
  • Output one number. The minimum missing element of the sequence. In the array 8, 6, 9, 81 is 1. The sequence here begins with one. 6, 9, 5, 2, 3, 1 - Conclusion 4. - Jonny Shopkins
  • How can output 4 be? - Anton Sorokin

3 answers 3

I don’t know the java badly;

 Создать массив содержащий все элементы входного массива без дубликатов и превышающие 0, отсортированные по возрастанию. //Насколько я понял, первая половина вашей программы делает примерно это Если массив пуст, то Вывести 1 Завершить. Перебрать все элементы массива по очереди. Если значение элемента больше чем индекс + 1 то Вывести индекс + 1 Завершить. Если массив закончился, то вывести кол-во элементов + 1 
  • Made an edit about items less than or equal to zero. - German Borisov

The mistake is that if the input array has a negative X, then your algorithm will produce X + 1.

 int[] arr = {8, 6, 9, 81, -5}; 

Result: -4. And should be 1.

  • array elements are natural numbers. - Jonny Shopkins
  • In the condition of this is not. - Enikeyschik September
  • yes you are right .... - Jonny Shopkins September

import java.util.Scanner; import java.util.TreeSet;

public class min3 {

 public static void main(String[] args) { TreeSet<Long> hs=new TreeSet <>(); Scanner in = new Scanner(System.in); boolean f= false; long count=1; try { while (in.hasNextLong()) { long a=in.nextLong(); if(a>0) hs.add(a); } ; //int count=hs.iterator().next(); for(Long e: hs) { if (count!=e) { System.out.print(count); f=true; break; }; count++; }; } catch(Exception e) {}; if(!f)System.out.print(count); //System.out.print(hs); } 

}