SER4
QUESTION DESCRIPTION
the professor is conducting surprise test for Engineering first year students. he has dictated an array of n integers.all the elements in array is obtained by adding either +1 or -1 to previous element. Professor gave mandatory condition for this problem like the difference between any two consecutive elements is 1. The expected outcome of the problem is to search an element index with the minimum number of comparison (less than simple element by element search). If the element is present multiple time, then print the smallest index. If the element is not present print -1.
the professor is conducting surprise test for Engineering first year students. he has dictated an array of n integers.all the elements in array is obtained by adding either +1 or -1 to previous element. Professor gave mandatory condition for this problem like the difference between any two consecutive elements is 1. The expected outcome of the problem is to search an element index with the minimum number of comparison (less than simple element by element search). If the element is present multiple time, then print the smallest index. If the element is not present print -1.
TEST CASE 2
INPUT
INPUT
8
5 4 5 6 4 3 2 3
9
OUTPUT-1
#include <stdio.h>
int main() {
int n;
scanf("%d",&n);
int a[n],k,i,j,f=0,g;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&k);
for(i=0;i<n;i++)
{
if(a[i]==k)
{
f=1;
g=i;
break;
}
}
if(f==0)
{
printf("-1");
}
else if(f==1)
{
printf("%d",g);
}
return 0;
}
Comments
Post a Comment