-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ3_Lab11.cpp
More file actions
83 lines (70 loc) · 1.88 KB
/
Q3_Lab11.cpp
File metadata and controls
83 lines (70 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
using namespace std;
class HashMap {
static const int TABLE_SIZE = 1000;
int keys[TABLE_SIZE];
int values[TABLE_SIZE];
public:
HashMap() {
for (int i = 0; i < TABLE_SIZE; i++) {
keys[i] = -1;
values[i] = 0;
}
}
int hashFunction(int key) {
return (key % TABLE_SIZE + TABLE_SIZE) % TABLE_SIZE;
}
void insert(int key) {
int index = hashFunction(key);
while (keys[index] != -1 && keys[index] != key) {
index = (index + 1) % TABLE_SIZE;
}
if (keys[index] == -1) {
keys[index] = key;
}
values[index]++;
}
bool exists(int key) {
int index = hashFunction(key);
while (keys[index] != -1) {
if (keys[index] == key) {
return true;
}
index = (index + 1) % TABLE_SIZE;
}
return false;
}
};
int findTripletWithZeroSum(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
HashMap hashMap;
for (int j = i + 1; j < size; j++) {
int target = -(arr[i] + arr[j]);
if (hashMap.exists(target)) {
return 1;
}
hashMap.insert(arr[j]);
}
}
return 0;
}
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
if (size < 3) {
cout << "An array must have at least three elements to check for a triplet." << endl;
return 0;
}
int arr[size];
cout << "Enter " << size << " elements of the array: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
if (findTripletWithZeroSum(arr, size)) {
cout << "The array contains a triplet with a sum of zero." << endl;
} else {
cout << "No triplet with a sum of zero was found in the array." << endl;
}
return 0;
}