Skip to content

Commit 77d90eb

Browse files
authored
Update Last_index_of_element_in_array.cpp
1 parent 313cfc1 commit 77d90eb

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Recursion/Last_index_of_element_in_array.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
using namespace std;
44
int lastIndex(int arr[],int n,int x){
55
if(n==0){
6-
return -1;
6+
return -1; // if size of array is 0 then we return -1
77
}
8-
int ans=lastIndex(arr+1,n-1,x);
9-
if(ans==-1){
10-
if(arr[0]==x){
8+
int ans=lastIndex(arr+1,n-1,x);//recursion call to the next element of array.
9+
if(ans==-1){ //if we cannot find the element in the remaining part of array
10+
if(arr[0]==x){//We check for 1st element.
1111
return 0;
1212
}
1313
else{
14-
return -1;
14+
return -1;//else we return -1
1515
}
1616
}
1717

@@ -37,4 +37,4 @@ int main() {
3737
5
3838
Output:
3939
0
40-
*/
40+
*/

0 commit comments

Comments
 (0)