I tried brute force method:
#include <stdio.h> int sum(int a [],int b[], int m); int main (void) { int a [] = {1,2,3,4,5}; int b [] = {4,3,5,2,6}; int i; printf("Enter to find a given number:\n"); scanf("%d",&i); printf("%s\n",sum(a,b,i) ? "True":"False"); return 0; } int sum(int a[], int b[],int m) { int i=0,j=0; for (i=0;i<=sizeof(a)/sizeof(int)+1;i++) for(j=0;j<=sizeof(b)/sizeof(int)+1;j++) if (a[i]+b[j]==m) return 1; return 0; }
since you can see that the runtime is O (n ^ 2), is there any smart way to minimize this?
source share