Friday, July 12, 2019

Read n integers of sorted array and find the start and last ocuurance of give X integer value

i/p 
10
1 2 2 2 2 3 4 8 8 8
8

o/p
7    9
#include<stdio.h>
#include<conio.h>
void main()
{
    int n,X,a[100]={1,2,2,2,2,3,4,8,8,8},findex=-1,lindex=-1;
    int i;
    clrscr();

    scanf("%d",&n);
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);

    scanf("%d",&X);

    for(i=0;i<n;i++)
    {
if(X==a[i])
{
findex=i;
break;
}
    }
    for(i=n;i>=0;i--)
    {
if(X==a[i])
{
lindex=i;
break;
}
    }
    if(findex!=-1 && lindex!=-1)
    printf("%d   %d",findex,lindex);
    else
    printf("\nNO OCCURANCE");

    getch();
}

No comments:

Post a Comment