Given an array of integers, find the numbers such that they add up to a specific target number.
The function twosum should return indices of the numbers such that they add up to the target, where index1 must is Les S than Index2. Please note that your returned answers (both Index1 and INDEX2) is not zero-based.
You may assume this each input would has exactly one solution.
Input:numbers={2, 7, one, A, target=9
Output:index1=1, index2=2
//auxiliary data structures, preserving values and indexes of the original arraystructnode{intVal; intpos;};BOOLcmpConstNode &a,ConstNode &b) { returna.val<B.val; }classSolution { Public: Vector<int> Twosum (vector<int> &numbers,inttarget) { intI=0, j=numbers.size (); Node*nodearray=Newnode[j+1];//note the initial declaration here for(; i<j;i++) {Nodearray[i].val=Numbers[i]; Nodearray[i].pos=i+1; } sort (Nodearray,nodearray+J,CMP);//using the library function sort, note the parametersI=0; J=numbers.size ()-1; while(i<j) { intsum=nodearray[i].val+Nodearray[j].val; if(sum==target) {Vector<int>result; if(nodearray[i].pos>Nodearray[j].pos) {Result.push_back (Nodearray[j].pos); Result.push_back (Nodearray[i].pos); }Else{result.push_back (nodearray[i].pos); Result.push_back (Nodearray[j].pos); } returnresult; }Else if(sum>target) J--; ElseI++; } }};
The Sum of