Python không âm

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 implement0 // C++ program to implement1// C++ program to implement2 // C++ program to implement3// C++ program to implement2 // C++ program to implement5

// C++ program to implement6

// C++ program to implement7// C++ program to implement8

// C++ program to implement7// the above approach0

// C++ program to implement7// the above approach2

// C++ program to implement7____02 // the above approach5

 

// C++ program to implement7// the above approach7

// C++ program to implement7// the above approach9

// C++ program to implement7#include 1

// C++ program to implement7#include 3 #include 4

 

// C++ program to implement7#include 6

// C++ program to implement7#include 8 #include 9// C++ program to implement2 using1

 

using2____33

using2____35

using2using7 using8

using9namespace0____41namespace2

using2____44

 

using2namespace6

using2____48

using2____50

using2std;2 std;3

using9std;5

using2____44

 

using2____59

using2____61

using2// Function to print the smallest3// Function to print the smallest4namespace2

// C++ program to implement7namespace4

namespace4

 

// Function to print the smallest9

// C++ program to implement2 // missing non-negative integer1

// C++ program to implement6

// C++ program to implement7// C++ program to implement2 // missing non-negative integer5

// C++ program to implement7// C++ program to implement2 // missing non-negative integer8// missing non-negative integer9// up to every array indices0// missing non-negative integer9// up to every array indices2

// C++ program to implement7// up to every array indices4

namespace4

Java




// up to every array indices6

// the above approach

// up to every array indices8 // up to every array indices9

// up to every array indices8 // C++ program to implement01

 

// C++ program to implement02 // C++ program to implement03

// C++ program to implement04

// Function to print the smallest

// missing non-negative integer

________số 8

// C++ program to implement08 // C++ program to implement0 // C++ program to implement1// C++ program to implement2 // C++ program to implement3// C++ program to implement2 // C++ program to implement5

// C++ program to implement6

// C++ program to implement7

// C++ program to implement7// C++ program to implement8

// C++ program to implement7// the above approach0

// C++ program to implement7// the above approach2

// C++ program to implement7____02 // C++ program to implement25____026namespace2

// C++ program to implement04

// C++ program to implement7// the above approach7

// C++ program to implement7// the above approach9

// C++ program to implement7#include 1

// C++ program to implement7// C++ program to implement36// C++ program to implement37 // C++ program to implement38// C++ program to implement39// C++ program to implement40

// C++ program to implement7____042____043// C++ program to implement44

 

// C++ program to implement7#include 6

// C++ program to implement7#include 8#include 9// C++ program to implement2 // C++ program to implement51// C++ program to implement26// C++ program to implement53

// C++ program to implement7// C++ program to implement6

using2

using2____33

using2____35

using2____37 // C++ program to implement63____026 // C++ program to implement65

using2____06

using9namespace0____41namespace2

using2____44

// C++ program to implement04

using2namespace6

using2____48

using2____50

using2std;2 // C++ program to implement83

using2____06

using9std;5

using2____44

// C++ program to implement04

using2____59

using2____61

using2______096____64// C++ program to implement44

// C++ program to implement7namespace4

namespace4

// C++ program to implement04

// Function to print the smallest9

________ 104 ________ 008 ________ 00 ________ 107

// C++ program to implement6

// C++ program to implement7// C++ program to implement2 // the above approach11// C++ program to implement26// the above approach13// C++ program to implement39// the above approach13// the above approach16// the above approach13// the above approach18// the above approach13// the above approach20 // the above approach21

// C++ program to implement7____02 // the above approach24

// C++ program to implement7

// C++ program to implement7// up to every array indices4

namespace4

namespace4

 

// the above approach30

Python3




// the above approach31

// the above approach32

 

// the above approach33

// the above approach34

// the above approach35

// the above approach36 // the above approach37

// C++ program to implement7

// C++ program to implement7____140

// C++ program to implement7____142

// C++ program to implement7____144

// C++ program to implement7____146// the above approach47 // C++ program to implement26

 

// C++ program to implement7____150

// C++ program to implement7____152

// C++ program to implement7____154

// C++ program to implement7__// the above approach56 // the above approach47 // the above approach58// C++ program to implement26// the above approach60// the above approach61 // the above approach62// the above approach63 // C++ program to implement39// the above approach65

 

// C++ program to implement7// the above approach67

// C++ program to implement7#include 8 // the above approach70// the above approach71 // the above approach72// the above approach73

 

using2____175

using2____177

using2using7 ________ 180// the above approach47 ________ 026 ________ 183 ________ 184

using9// the above approach56// the above approach87// the above approach47 // the above approach89

 

using2____191

using2____193

using2____195

using2____52 #include 9____156#include 00

using9// the above approach46____163// the above approach47 // C++ program to implement39

 

using2____207

using2____209

using2______211#include 12// the above approach47 // Function to print the smallest4// the above approach65

 

#include 16

using7 #include 18____147// the above approach47 #include 21#include 22

// C++ program to implement7

// C++ program to implement7_______225// the above approach47 // the above approach58// C++ program to implement26// the above approach13// C++ program to implement39// the above approach13// the above approach16// the above approach13// the above approach18// the above approach13// the above approach20 // the above approach60

Điều gì là tiêu cực trong Python?

Như vậy, các số lớn hơn 0 là số dương, các số nguyên nhỏ hơn 0 được gọi là số âm. Đây là khái niệm được sử dụng trong Python là chương trình phủ định.

Số nguyên Python có thể âm không?

Int hoặc số nguyên là một số nguyên, dương hoặc âm , không có số thập phân, có độ dài không giới hạn.