Range Start and End Position

                    Range Start and End Position

N integers are passed as input. X which is an integer is also passed as the input. The program must print the start S and end E positions of X in these N integers. -1 must be printed as the start and end positions when X is not present in these N numbers.
Input Format:
The first line contains N.
The second line contains N integer values separated by a space.
The third line contains X.
Output Format:
The first line contains S and E separated by a space.
Boundary Conditions:
2 <= N <= 1000
-9999 <= X <= 9999
Example Input/Output 1:
Input:
5
1 2 3 1 3
3
Output:
3 5
Example Input/Output 2:
Input:
7
10 20 10 20 30 40 50
60
Output:

-1 -1 

Code:

#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int n,key,i,first=0,last=0,j;
cin>>n;
int arr[n];
for(i=0;i<n;i++)
    cin>>arr[i];
cin>>key;
for(i=0,j=n-1;i<n;i++,j--)
{
    if(arr[i]==key)
        first=i+1;
    if(arr[j]==key)
        last=j+1;
}
if(first==0)
    cout<<"-1 -1";
else
    cout<<last<<" "<<first;
}

Please do comment If u have any Queries!

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *