From b2a73a7d1eaa24b94cd9ed0eacb6213839844443 Mon Sep 17 00:00:00 2001 From: Aashna Chourasia <57864526+bitaashna@users.noreply.github.com> Date: Wed, 13 Oct 2021 19:08:28 +0530 Subject: [PATCH] Two Sum Problem --- TwoSum.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 TwoSum.cpp diff --git a/TwoSum.cpp b/TwoSum.cpp new file mode 100644 index 0000000..06d806c --- /dev/null +++ b/TwoSum.cpp @@ -0,0 +1,41 @@ +/* Given an array of integers, find two numbers such that they add up to a specific target number. + +Your function should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. + +Assume that each input would have exactly one solution. + +Input: numbers={2, 7, 11, 15}, target=9 +Output: index1=1, index2=2 + +*/ +#include +using namespace std; + +vector twoSum(vector& nums, int target) { + vectorans; + unordered_mapmp; + for(int i=0;i>n; + int k; + cout<<"Enter the target sum: "; + cin>>k; + cout<<"Enter the array elements: "; + vectorarr(n); + for(int i=0;i>arr[i]; + vectorres=twoSum(arr,k); + cout<<"index1="<