Find the minimum positive integer (positive, integer, greater than 0 ) that is not in the original array A
For example:
For A = [1, 3] answer: 2
For A = [1, 4, 6, 1, 3, 2] answer is: 5
Find the minimum positive integer (positive, integer, greater than 0 ) that is not in the original array A
For example:
For A = [1, 3] answer: 2
For A = [1, 4, 6, 1, 3, 2] answer is: 5
My compact and beautiful solution without unnecessary sorting
int mas[] = {1, -1, 7, 0, 1, 2, 4}; int length = sizeof(mas) / sizeof(mas[0]); int res = 1; for (int j = 0; j < length; j++) for (int i = 0; i < length; i++) { if (mas[i] == res) { res++; break; } } cout<<res; C++ not a writer, but I can offer two options:
Baseline :
int a[ 6 ] = { 1, 4, 6, 1, 3, 2 }; int size = sizeof( a ) / sizeof( a[ 0 ] ); Option 1:
because By the condition the number must be greater than 0 , then starting from 1 check all the numbers that are not included in the array. The first will be that.
int min = 1; while (true) { bool isContain = false; for (int i = 0; i < size; i++) { if (min == a[ i ]) { isContain = true; break; } } if (isContain) { min++; } else { std::cout << min << "\n"; break; } } Option 2:
First, we sort the original array (in ascending order in this case), and check the difference of neighboring elements. If we get:
0 - elements are equal;1 - elements go in order;2 or more means that the minimum element is in this range.We add 1 to the element that is to the left in the pair - this value will be minimal.
for (int j = 1; j < size; j++) { for (int i = 0; i < size - j; i++) { if (a[ i ] > a[ i + 1 ]) { int b = a[ i ]; a[ i ] = a[ i + 1 ]; a[ i + 1 ] = b; } } } for (int i = 1; i < size; i++) { if (a[ i ] - a[ i - 1 ] > 1) { std::cout << a[ i - 1 ] + 1 << "\n"; break; } } a[] = {-1}; expected: 1 output, but nothing output - jfstake the number n = 1 ; Look in the array for this number. If it is, then ++n ; and repeat the loop until it is in the array of such a number.
Or sort the array, and let's say it has a size of 10 :
const int sz = 10; int m[10]; // инициализация, потом сортировка int k = 1; for (int i = 0; i < sz; ++i) { if ( m[i] == k ) { if (m[i] == m[i +1]) continue; ++k; } else { cout << "это число: " << k; break; } } int m[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; . Expected output: 2 . Your code displays: это число: 1 . - jfsLinear algorithm:
[1, n] that are in the input array, where n is the size of the array #include <algorithm> #include <iostream> #include <iterator> #include <vector> int main() { // read numbers std::istream_iterator<int> numbers {std::cin}, eof; std::vector<int> arr(numbers, eof); // mark numbers in [1, n] range that are in the array size_t n = arr.size(); std::vector<bool> m(n+1, false); for (int x : arr) if (1 <= x && x <= n) m[x-1] = true; // find the first absent number (position + 1) auto it = std::find(std::begin(m), std::end(m), false); std::cout << (std::distance(std::begin(m), it) + 1) << std::endl; } Source: https://ru.stackoverflow.com/questions/813579/
All Articles