Problem Statement : In an array of numbers, the difference between two consecutive numbers is 1, ( i.e. can be +1 or -1). You have to find a key value from the array without implementing linear search.
Anónimo
int main() { int a[10]={5,6,7,6,5,6,7,8,9,8}; int i,n,found=0,diff; scanf("%d",&n); i=0; while(i<10) { diff=n-a[i]; if(diff==0) { printf("Index: %d\n",i); found=1; break; } else { i=i+abs(diff); } } if(found==0) printf("Element Not found\n"); return 0; }