All Indices of Number
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
indexes where x is present in the array (separated by space)
Constraints :
1 <= N <= 10^3
5
9 8 10 8 8
8
Sample Output :
1 3 4
SOLUTION:-
#include<iostream>
using namespace std;
int allIndexes(int input[],int size,int x,int output[]){
static int index=-1;
if(size==0)
{
return 0;
}
else{
if(input[0]==x)
{
output[0]=++index;
cout<<index<<" ";
allIndexes(input+1,size-1,x,output+1);
}
else{
++index;
allIndexes(input+1,size-1,x,output);
}
}
}
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;
int *output = new int[n];
int size = allIndexes(input, n, x, output);
for(int i = 0; i < size; i++) {
cout << output[i] << " ";
}
delete [] input;
delete [] output;
}