Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <unordered_map>
- using namespace std;
- class PairFinder {
- private:
- vector<int> arr;
- int target;
- pair<int, int> indices;
- bool found;
- public:
- PairFinder() : target(0), found(false) {}
- void input() {
- int n;
- cout << "Введите количество элементов и число k: ";
- cin >> n >> target;
- arr.resize(n);
- cout << "Введите элементы: ";
- for (int i = 0; i < n; i++) {
- cin >> arr[i];
- }
- }
- // Функция поиска пары индексов
- bool findPair() {
- unordered_map<int, int> hash;
- for (int i = 0; i < arr.size(); i++) {
- int complement = target - arr[i];
- if (hash.find(complement) != hash.end()) {
- indices = make_pair(hash[complement], i);
- found = true;
- return true;
- }
- hash[arr[i]] = i;
- }
- return false;
- }
- void output() const {
- if (found) {
- cout << "Найденные индексы: " << indices.first << " и " << indices.second << endl;
- } else {
- cout << "Пара, удовлетворяющая условию, не найдена." << endl;
- }
- }
- };
- int main() {
- PairFinder pf;
- pf.input();
- pf.findPair();
- pf.output();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement