Problem Description
Take an input array.The array consists of list of numbers, where num[i] is not equal to num[i+1].The mission is to print the peak element and also print its index value.INPUT FORMAT: Get an input array with a list of numbers. OUTPUT FORMAT:Print the peak element (largest element) with its index value. EXPLANATION: Get an input array which must contain a series of numbers. And now,the doer has to find out the peak element in that array. So, finally print the peak element (largest element in the array).Along with its index number as shown in the format.CODING ARENA
#include<stdio.h>
int main()
{
int a,peak,ind=0,i;
scanf("%d",&a);
int ar[a];
for(i=0;i<a;i++)
scanf("%d",&ar[i]);
peak=ar[0];
for(i=0;i<a;i++)
{
if(peak<ar[i])
{
peak=ar[i];
ind=i;
}
}
printf("Peak Element=%d\n",peak);
printf("Index Value=%d",ind);
return 0;
}
Test Case 1
Input (stdin)4 1 2 3 1
Expected OutputPeak Element=3 Index Value=2
Test Case 2
Input (stdin)6 6 2 5 8 9 7
Expected OutputPeak Element=9 Index Value=4
No comments:
Post a Comment