From 37d25a2bbe8d06cbadbeebd25686a2215ee04a7b Mon Sep 17 00:00:00 2001 From: Prakhar Saxena <91530129+Someone-who-loves-coding@users.noreply.github.com> Date: Wed, 1 Nov 2023 17:44:25 +0530 Subject: [PATCH] Create hashing.cpp --- hashing.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 hashing.cpp diff --git a/hashing.cpp b/hashing.cpp new file mode 100644 index 0000000..0fd7703 --- /dev/null +++ b/hashing.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; +#define MAX 1000 + +bool has[MAX + 1][2]; + +bool search(int X) +{ + if (X >= 0) { + if (has[X][0] == 1) + return true; + else + return false; + } + + X = abs(X); + if (has[X][1] == 1) + return true; + + return false; +} + +void insert(int a[], int n) +{ + for (int i = 0; i < n; i++) { + if (a[i] >= 0) + has[a[i]][0] = 1; + else + has[abs(a[i])][1] = 1; + } +} + +int main() +{ + int a[] = { -1, 9, -5, -8, -5, -2 }; + int n = sizeof(a)/sizeof(a[0]); + insert(a, n); + int X = -5; + if (search(X) == true) + cout << "Present"; + else + cout << "Not Present"; + return 0; +}