Breaking

Saturday, 26 August 2017

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!

1 comment:

  1. //c code

    #include
    #include

    int main()
    {
    int n,temp,c=0,c1=0;
    scanf("%d",&n);
    int arr[n];
    for(int i=0;itemp;i--)
    {
    if(arr[i]==x)
    {
    printf("%d ",i+1);
    c1++;
    break;
    }
    }
    if(c==0&&c1==0)
    printf("-1 -1");
    if(c==1&&c1==0)
    printf("%d ",temp+1);
    return 0;

    }

    ReplyDelete

Like