File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ This repository contains all the popular Competitive Programming questions and I
26
26
<li ><a href =" https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/Numbers " >Numbers</a ></li >
27
27
<li ><a href =" https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/OOPs " >OOPs</a ></li >
28
28
<li ><a href =" https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/Passwords " >Passwords</a ></li >
29
+ <li ><a href =" https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/Recursion " >Recursion</a ></li >
29
30
<li ><a href =" https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/Searching%20Algorithms " >Searching Algorithms</a ></li >
30
31
<li ><a href =" https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/Sorting%20Algorithms " >Sorting Algorithms</a ></li >
31
32
<li ><a href =" https://github.com/smv1999/CompetitiveProgrammingQuestionBank/tree/master/Standard%20Template%20Library " >Standard Template Library</a ></li >
Original file line number Diff line number Diff line change
1
+ /* Program to find first index of a number in array*/
2
+ #include < iostream>
3
+ using namespace std ;
4
+ int firstIndex (int arr[],int n,int x){
5
+ if (n==0 ){ // if size of array is 0
6
+ return -1 ;
7
+ }
8
+ if (arr[0 ]==x){ // if the element is found on first index
9
+ return 0 ;
10
+ }
11
+ int ans=firstIndex (arr+1 ,n-1 ,x);// recursion
12
+ if (ans==-1 ){
13
+ return -1 ;
14
+ }
15
+ else {
16
+ return ans+1 ;
17
+ }
18
+ }
19
+
20
+ int main () {
21
+ int n,x,a[200 ];
22
+ cin>>n;
23
+ for (int i=0 ;i<n;i++){
24
+ cin>>a[i];
25
+ }
26
+ cin>>x;
27
+ cout<<firstIndex (a,n,x);
28
+
29
+ }
30
+ /* Example
31
+ Input:
32
+ 5
33
+ 5 5 6 7 9
34
+ 5
35
+ Output:
36
+ 0
37
+ */
You can’t perform that action at this time.
0 commit comments