while(start!=end)//T(n)=n/2 { if(*start+*end==k) { printf("\nFound a pair (%d,%d)",*start,*end); start++; end--; } else if(*start+*end<k) { start++; } else end--; } } int main() { int a[10],n,k,i;
printf("Enter the Number of values in an array <=10\n"); scanf("%d",&n); printf("\nEnter the Array elements in sorted order\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter the k value\n"); scanf("%d",&k); cal_ab(a,n,k); return 0; }
#include < stdio.h >
ReplyDeletevoid cal_ab(int a[],int n,int k)
{
int *start=a,*end=a;
end=end+(n-1);//o(1)
while(start!=end)//T(n)=n/2
{
if(*start+*end==k)
{
printf("\nFound a pair (%d,%d)",*start,*end);
start++;
end--;
}
else if(*start+*end<k)
{
start++;
}
else
end--;
}
}
int main()
{
int a[10],n,k,i;
printf("Enter the Number of values in an array <=10\n");
scanf("%d",&n);
printf("\nEnter the Array elements in sorted order\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the k value\n");
scanf("%d",&k);
cal_ab(a,n,k);
return 0;
}