Test case 2 and 3 show wrong ans even though the output is right

public static void main(String[] args) {

	Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();
	int[] arr = new int[n];
	for (int i = 0; i < n; i++) {
		arr[i] = scn.nextInt();
	}
	int[] ans = productOfArray(arr);
	for(int i=0;i<n;i++) {
		System.out.print(ans[i]+" ");
	}
}

public static int[] productOfArray(int[] arr) {
	int n=arr.length;

    if(arr==null || n==0) {
		return new int[0];
	}
    
	int[] left=new int[n];
	int[] right=new int[n];
	 
	left[0]=1;
	
	for(int i=1;i<n;i++) {
		left[i]=left[i-1]*arr[i-1];
	}
	
	right[n-1]=1; 
	
	for(int i=n-2;i>=0;i--) {
		right[i]=right[i+1]*arr[i+1];
	}
	
	int[] ans=new int[n];
	for(int i=0;i<n;i++) {
		ans[i]=left[i]*right[i];
	}
	
	return ans;
}

@Utkarsh-Patiyal-2067329909961088 take care of integer overflow!
rest logic seems fine

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.