# Issue

This Content is from Stack Overflow. Question asked by developer

Question:

Given a sorted array having N elements, find the indices of the first and last occurrences of an element X in the given array.

``````Input : arr[] = {1, 3, 5, 5, 5, 5, 7, 123, 125 },
x = 7
Output : First Occurrence = 6
``````

but they will give output what will I do wrong :

``````5
``````

code:

``````#include<iostream>
using namespace std;
int main(){

int arr[]={1, 3, 5, 5, 5, 5, 7, 123, 125};
int start=0;
int end=(sizeof(arr)/sizeof(arr))-1;
int x=7;
int mid;
int result;
while(start<end){
mid=(start+end)/2;
if(arr[mid]<x){
start=mid+1;
}
else if(arr[mid]==x){
end=mid-1;
}
}
cout<<"first occuerence"<<start;
}
``````

# Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

```
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.```