Last Index of Number
Send Feedback
Line 1 : An Integer N i.e. size of array
Line 2 : N integers which are elements of the array, separated by spaces
Line 3 : Integer x
last index or -1
4
9 8 10 8
8
3
SOLUTION:-
#include<iostream>
using namespace std;
int lastIndex(int input[], int size, int x) {
/* Don't write main().
Don't read input, it is passed as function argument.
Return output and don't print it.
Taking input and printing output is handled automatically.
*/
if(size==0)
return -1;
static int i=0,pos=-1;
if(input[0]==x)
{
lastIndex(input+1,size-1,x);
i++;
pos++;
}
else
{
lastIndex(input+1,size-1,x);
if(i==0)
return -1;
pos++;
}
return pos;
}
int main(){ int n; cin >> n; int *input = new int[n]; for(int i = 0; i < n; i++) { cin >> input[i]; } int x; cin >> x; cout << lastIndex(input, n, x) << endl; }
This is dummy text. It is not meant to be read. Accordingly, it is difficult to figure out when to end it. But then, this is dummy text. It is not meant to be read. Period.
If you have any doubts then please let me know... ConversionConversion EmoticonEmoticon