Cho một mảng arr[] có kích thước N, nhiệm vụ của mọi chỉ số mảng là tìm số nguyên không âm bị thiếu nhỏ nhất cho đến chỉ số đó của mảng đã cho
ví dụ
Đầu vào. mảng[] = {1, 3, 0, 2}
Đầu ra. 0 0 2 4
Giải thích.
Số nguyên không âm bị thiếu nhỏ nhất từ chỉ số 0 đến 0 là 0.
Số nguyên không âm nhỏ nhất bị thiếu từ chỉ số 0 đến 1 là 0.
Số nguyên không âm nhỏ nhất bị thiếu từ chỉ số 0 đến 2 là 2.
Số nguyên không âm nhỏ nhất bị thiếu từ chỉ số 0 đến 3 là 4.Đầu vào. mảng[] = {0, 1, 2, 3, 5}
Đầu ra. 1 2 3 4 4
Cách tiếp cận. Vấn đề này có thể được giải quyết bằng Hashing. Thực hiện theo các bước dưới đây để giải quyết vấn đề
- Khởi tạo một biến, giả sử smNonNeg để lưu trữ các số nguyên không âm nhỏ nhất bị thiếu giữa chỉ mục bắt đầu và chỉ mục hiện tại của mảng đã cho
- Khởi tạo một mảng, giả sử hash[N] để kiểm tra xem smNonNeg có xuất hiện giữa chỉ mục bắt đầu và chỉ mục hiện tại hay không
- Duyệt qua mảng đã cho và kiểm tra xem hash[smNonNeg] có bằng 0 hay không. Nếu tìm thấy là đúng, sau đó in giá trị của smNonNeg
- Mặt khác, tăng giá trị của smNonNeg trong khi hash[smNonNeg] không bằng 0
Dưới đây là việc thực hiện các phương pháp trên
C++
// C++ program to implement
// the above approach
#include
using
namespace
std;
// Function to print the smallest
// missing non-negative integer
________số 8
// C++ program to implement
0 // C++ program to implement
1// C++ program to implement
2 // C++ program to implement
3// C++ program to implement
2 // C++ program to implement
5
// C++ program to implement
6
// C++ program to implement
7// C++ program to implement
8
// C++ program to implement
7// the above approach
0
// C++ program to implement
7// the above approach
2
// C++ program to implement
7____02 // the above approach
5
// C++ program to implement
7// the above approach
7
// C++ program to implement
7// the above approach
9
// C++ program to implement
7#include
1
// C++ program to implement
7#include
3 #include
4
// C++ program to implement
7#include
6
// C++ program to implement
7#include
8 #include
9// C++ program to implement
2 using
1
using
2____33
using
2____35
using
2using
7 using
8
using
9namespace
0____41namespace
2
using
2____44
using
2namespace
6
using
2____48
using
2____50
using
2std;
2 std;
3
using
9std;
5
using
2____44
using
2____59
using
2____61
using
2// Function to print the smallest
3// Function to print the smallest
4namespace
2
// C++ program to implement
7namespace
4
namespace
4
// Function to print the smallest
9
// C++ program to implement
2 // missing non-negative integer
1
// C++ program to implement
6
// C++ program to implement
7// C++ program to implement
2 // missing non-negative integer
5
// C++ program to implement
7// C++ program to implement
2 // missing non-negative integer
8// missing non-negative integer
9// up to every array indices
0// missing non-negative integer
9// up to every array indices
2
// C++ program to implement
7// up to every array indices
4
namespace
4
Java
// up to every array indices
6
// the above approach
// up to every array indices
8 // up to every array indices
9
// up to every array indices
8 // C++ program to implement
01
// C++ program to implement
02 // C++ program to implement
03
// C++ program to implement
04
// Function to print the smallest
// missing non-negative integer
________số 8
// C++ program to implement
08 // C++ program to implement
0 // C++ program to implement
1// C++ program to implement
2 // C++ program to implement
3// C++ program to implement
2 // C++ program to implement
5
// C++ program to implement
6
// C++ program to implement
7
// C++ program to implement
7// C++ program to implement
8
// C++ program to implement
7// the above approach
0
// C++ program to implement
7// the above approach
2
// C++ program to implement
7____02 // C++ program to implement
25____026namespace
2
// C++ program to implement
04
// C++ program to implement
7// the above approach
7
// C++ program to implement
7// the above approach
9
// C++ program to implement
7#include
1
// C++ program to implement
7// C++ program to implement
36// C++ program to implement
37 // C++ program to implement
38// C++ program to implement
39// C++ program to implement
40
// C++ program to implement
7____042____043// C++ program to implement
44
// C++ program to implement
7#include
6
// C++ program to implement
7#include
8#include
9// C++ program to implement
2 // C++ program to implement
51// C++ program to implement
26// C++ program to implement
53
// C++ program to implement
7// C++ program to implement
6
using
2
using
2____33
using
2____35
using
2____37 // C++ program to implement
63____026 // C++ program to implement
65
using
2____06
using
9namespace
0____41namespace
2
using
2____44
// C++ program to implement
04
using
2namespace
6
using
2____48
using
2____50
using
2std;
2 // C++ program to implement
83
using
2____06
using
9std;
5
using
2____44
// C++ program to implement
04
using
2____59
using
2____61
using
2______096____64// C++ program to implement
44
// C++ program to implement
7namespace
4
namespace
4
// C++ program to implement
04
// Function to print the smallest
9
________ 104 ________ 008 ________ 00 ________ 107
// C++ program to implement
6
// C++ program to implement
7// C++ program to implement
2 // the above approach
11// C++ program to implement
26// the above approach
13// C++ program to implement
39// the above approach
13// the above approach
16// the above approach
13// the above approach
18// the above approach
13// the above approach
20 // the above approach
21
// C++ program to implement
7____02 // the above approach
24
// C++ program to implement
7
// C++ program to implement
7// up to every array indices
4
namespace
4
namespace
4
// the above approach
30
Python3
// the above approach
31
// the above approach
32
// the above approach
33
// the above approach
34
// the above approach
35
// the above approach
36 // the above approach
37
// C++ program to implement
7
// C++ program to implement
7____140
// C++ program to implement
7____142
// C++ program to implement
7____144
// C++ program to implement
7____146// the above approach
47 // C++ program to implement
26
// C++ program to implement
7____150
// C++ program to implement
7____152
// C++ program to implement
7____154
// C++ program to implement
7__// the above approach
56 // the above approach
47 // the above approach
58// C++ program to implement
26// the above approach
60// the above approach
61 // the above approach
62// the above approach
63 // C++ program to implement
39// the above approach
65
// C++ program to implement
7// the above approach
67
// C++ program to implement
7#include
8 // the above approach
70// the above approach
71 // the above approach
72// the above approach
73
using
2____175
using
2____177
using
2using
7 ________ 180// the above approach
47 ________ 026 ________ 183 ________ 184
using
9// the above approach
56// the above approach
87// the above approach
47 // the above approach
89
using
2____191
using
2____193
using
2____195
using
2____52 #include
9____156#include
00
using
9// the above approach
46____163// the above approach
47 // C++ program to implement
39
using
2____207
using
2____209
using
2______211#include
12// the above approach
47 // Function to print the smallest
4// the above approach
65
#include
16
using
7 #include
18____147// the above approach
47 #include
21#include
22
// C++ program to implement
7
// C++ program to implement
7_______225// the above approach
47 // the above approach
58// C++ program to implement
26// the above approach
13// C++ program to implement
39// the above approach
13// the above approach
16// the above approach
13// the above approach
18// the above approach
13// the above approach
20 // the above approach
60