Skip to content

Commit f2b590e

Browse files
authored
Find Last Element Solution Added
1 parent 6ae1249 commit f2b590e

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Hacker Blocks/Find Last Element.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* Hacker Blocks */
2+
/* Title - Find Last Element */
3+
/* Created By - Akash Modak */
4+
/* Date - 06/07/2020 */
5+
6+
// Take as input N, the size of array. Take N more inputs and store that in an array. Take as input M, a number. Write a recursive function which returns the last index at which M is found in the array and -1 if M is not found anywhere. Print the value returned.
7+
8+
// Input Format
9+
// There will be three lines of input:
10+
11+
// N - the size of the array
12+
// N space separated integers that make up the array
13+
// M
14+
// Constraints
15+
// 1 < N < 1000
16+
// -10^9 < i,M < 10^9 , where i is any number within the array
17+
18+
// Output Format
19+
// For each case, print the integer value of the last index that M is found at within the given array.
20+
// If it is not found, print '-1' (without the quotes).
21+
22+
// Sample Input
23+
// 7
24+
// 86 -16 77 65 45 77 28
25+
// 77
26+
// Sample Output
27+
// 5
28+
// Explanation
29+
// Last occurence of 77 is found at index = 5.
30+
31+
#include<iostream>
32+
using namespace std;
33+
int fun(int *a,int n,int k){
34+
if(n==0)
35+
return -1;
36+
int i=fun(a+1,n-1,k);
37+
if(i==-1){
38+
if(a[0]==k)
39+
return 0;
40+
else
41+
return -1;
42+
}
43+
return i+1;
44+
}
45+
int main() {
46+
int n;
47+
cin>>n;
48+
int a[n];
49+
for(int i=0;i<n;i++)
50+
cin>>a[i];
51+
int m;
52+
cin>>m;
53+
cout<<fun(a,n,m)<<endl;
54+
return 0;
55+
}

0 commit comments

Comments
 (0)