[go: up one dir, main page]

0% found this document useful (0 votes)
21 views2 pages

Chatgpt Text

The document contains a C++ program that implements a TwoSumSolver class to find two indices of numbers in an array that add up to a specified target sum. It includes methods for logging, finding the two sum, and displaying results, as well as a helper function for user input. The program handles exceptions if no solution is found and logs the program's execution flow.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Chatgpt Text

The document contains a C++ program that implements a TwoSumSolver class to find two indices of numbers in an array that add up to a specified target sum. It includes methods for logging, finding the two sum, and displaying results, as well as a helper function for user input. The program handles exceptions if no solution is found and logs the program's execution flow.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <vector>
#include <unordered_map>
#include <stdexcept>
using namespace std;

// Debug logger
void log(const string& msg) {
cout << "[LOG] " << msg << endl;
}

// TwoSumSolver Class
class TwoSumSolver {
private:
vector<int> nums;
int target;

public:
// Constructor
TwoSumSolver(const vector<int>& numsInput, int targetValue) {
nums = numsInput;
target = targetValue;
log("TwoSumSolver object created.");
}

// Main Logic Function


pair<int, int> findTwoSum() {
unordered_map<int, int> numMap; // value -> index

log("Searching for two elements that sum to target...");

for (int i = 0; i < nums.size(); ++i) {


int complement = target - nums[i];
log("Checking if " + to_string(complement) + " exists...");

if (numMap.find(complement) != numMap.end()) {
log("Match found: " + to_string(complement) + " and " +
to_string(nums[i]));
return {numMap[complement], i};
}

numMap[nums[i]] = i;
}

throw runtime_error("No two sum solution found!");


}

// Display result
void displayResult() {
try {
pair<int, int> result = findTwoSum();
cout << "Indices Found: " << result.first << " and " << result.second
<< endl;
cout << "Numbers are: " << nums[result.first] << " + " <<
nums[result.second] << " = " << target << endl;
}
catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
}
}
};

// Helper function to take input


vector<int> getInputArray() {
int size;
cout << "Enter the size of the array: ";
cin >> size;

vector<int> arr(size);
cout << "Enter " << size << " integers:\n";
for (int i = 0; i < size; ++i) {
cin >> arr[i];
}

return arr;
}

// Main function
int main() {
log("Program Started.");
vector<int> nums = getInputArray();

int target;
cout << "Enter the target sum: ";
cin >> target;

// Create solver object


TwoSumSolver solver(nums, target);
solver.displayResult();

log("Program Ended.");
return 0;
}

You might also like