C ++ program to find index of first occurrence of x when array is sorted .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findFirst ( int arr [ ] , int n , int x ) { int count = 0 ; bool isX = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) isX = true ; else if ( arr [ i ] < x ) count ++ ; } return ( isX == false ) ? -1 : count ; }