All indices Problem

During dry run the output is correct but when i’m compiling it is showing garbage value.

Code =======>

import java.util.*;
public class Main {
public static void main(String args[]) {
// Your Code Here
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] arr = new int[N];
for(int i=0;i<N;i++){
arr[i] = sc.nextInt();
}
int M = sc.nextInt();
int[] ans = fidx(arr,M,0,0);
System.out.println(ans);
}

public static int[] fidx(int[] arr,int M,int idx,int count){
	if(idx == arr.length){
		return new int[count];			
	}
    if(arr[idx] == M){
		int iarr[] = fidx(arr,M,idx+1,count+1);
		iarr[count] = idx;
		return iarr;			
	}
	else{
		int iarr[] = fidx(arr,M,idx+1,count);
		return iarr;
	}
	
}

}

print the ans string only for its length to avoid printing garbage values
here