Code giving some weird Output

public class Main
{

public static int[] Find_all_indices(int arr[] , int find_no , int count ,int si){
    
    if(si == arr.length){
        int[] base = new int[count];
        return base ; 
    }
    
    int[] result_arr = null;
    
    if(arr[si] == find_no){
         result_arr = Find_all_indices(arr , find_no , count+1 , si+1);
    
    }
   
    else{
        result_arr = Find_all_indices(arr , find_no , count , si+1);
     
    }
   
    
    if(arr[si] == find_no){
        result_arr[count] = si ; 
    }
    
    return result_arr ; 
    
    
}
public static void main(String[] args) {
int arr[] = {3 , 8 , 1 , 8 , 8 , 4};
int find_no = 8 ;
int count = 0 ;
int si = 0 ;
int[] ans = Find_all_indices(arr , find_no , 0 , 0);
for(int i = 0 ; i < ans.length ; i++){
    System.out.print(arr[i]+"  ");
}
}

}

OUTPUT - 3 , 8 , 1

Sir when I am running my code it is giving me this output I tried debugging also but couldn’t find the issue and sir please tell me the use of base array .

Hey @rahul1402 Refer my code :
public class AllIndices{

public int [] AllIndexesRecursive( int input[],

int x, int start)

{

// If the start index reaches the

// length of the array, then

// return empty array

if (start == input.length) {

int [] ans = new int [ 0 ]; // empty array

return ans;

}

// Getting the recursive answer in

// smallIndex array

int [] smallIndex = AllIndexesRecursive(input, x,

start + 1 );

// If the element at start index is equal

// to x then

// (which is the answer of recursion) and then

// (which came through recursion)

if (input[start] == x) {

int [] myAns = new int [smallIndex.length + 1 ];

// Put the start index in front

// of the array

myAns[ 0 ] = start;

for ( int i = 0 ; i < smallIndex.length; i++) {

// Shift the elements of the array

// one step to the right

// and putting them in

// myAns array

myAns[i + 1 ] = smallIndex[i];

}

return myAns;

}

else {

// If the element at start index is not

// equal to x then just simply return the

// answer which came from recursion.

return smallIndex;

}

}

public int [] AllIndexes( int input[], int x)

{

return AllIndexesRecursive(input, x, 0 );

}

// Driver Code

public static void main(String args[])

{

AllIndice g = new AllIndice();

int arr[] = { 1 , 2 , 3 , 2 , 2 , 5 }, x = 2 ;

int output[] = g.AllIndexes(arr, x);

// Printing the output array

for ( int i = 0 ; i < output.length; i++) {

System.out.print(output[i] + " " );

}

}

}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.