From b5612a1f9e88e3ea2b28a79e6a6eced99ef902c2 Mon Sep 17 00:00:00 2001 From: Ituub Date: Wed, 27 Nov 2024 20:17:14 +0500 Subject: [PATCH] added two-sum.cpp --- src/two-sum.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/two-sum.cpp diff --git a/src/two-sum.cpp b/src/two-sum.cpp new file mode 100644 index 0000000..6ef5f95 --- /dev/null +++ b/src/two-sum.cpp @@ -0,0 +1,23 @@ +#define ARRAY_SIZE 5 +#include + +bool two_sum( + const int nums[ARRAY_SIZE], + const int target, + std::size_t& index0, + std::size_t& index1) +{ + for (int i = 0; i < ARRAY_SIZE-1; i++){ + if (nums[i] >= target) continue; + for (int j = i+1; j < ARRAY_SIZE; j++){ + if (nums[i] + nums[j] == target){ + index0 = i; + index1 = j; + return true; + } + } + } + return false; +} + +