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, target=9
Output: index1=1, index2=2
#include <stdio.h> #include <stdlib.h>int cmp (void *a,void *b) {return * (int *) a-* (int *) b;} int* twosum (int numbers[], int n, int target) {int i,j; int tmp,tmp1,tmp2; int* res= (int*) malloc (sizeof (int) * *); int array[100000]; for (i=0;i<n;i++) {tmp=numbers[i]; Array[tmp]=i; }/*for (i=0;i<n-1;i++) {for (j=i+1;j<n;j++) {if (Numbers[i]>numbers[j]) {Tmp=nu Mbers[i]; NUMBERS[I]=NUMBERS[J]; numbers[j]=tmp; }}}*/Qsort (numbers,n,sizeof (numbers[0]), CMP); for (i=0,j=n-1;i!=j;) {if (numbers[i]+numbers[j]==target) {tmp1=numbers[i]; tmp1=array[tmp1]+1; TMP2=NUMBERS[J]; tmp2=array[tmp2]+1; res[0]= (TMP1>TMP2) tmp2:tmp1; res[1]= (TMP1>TMP2) tmp1:tmp2; return res; } else if (numbers[i]+numbers[j]>target) {j--; } else i++; }}void Main () {int arr[]={3,2,4}; int* a= (int*) malloc (sizeof (int) * *); int array[100000]; A=twosum (arr,3,6); printf ("index1=%d,index2=%d\n", A[0],a[1]);}
The Sum of