What is wrong in my Code? It's working Completely fine in LeetCode but not in CB ide

Code below.

Code Link : https://ide.geeksforgeeks.org/ksLf4bvJPk

import java.util.*;
public class Main {

public static void product(int[] a, int n) {
    int[] left = new int[n];
    int[] right = new int[n];
    int[] res = new int[n];

    left[0] = 1;
    right[n-1] = 1;

    for(int i=1;i<n;i++) {
        left[i] = left[i-1] * a[i-1]; 
    }

    for(int i=n-2;i>=0;i--) {
        right[i] = right[i+1] * a[i+1];
    }

    for(int i=0;i<n;i++) {
        res[i] = left[i] * right[i];
    }

    // print

    for(int i=0;i<n;i++) {
        System.out.print(res[i] + " ");
    }
}

public static void main (String args[]) {
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    int[] a = new int[n];

    for(int i=0;i<n;i++) {
        a[i] = scan.nextInt();
    }

    product(a, n);
}

}

Hey @shivampokhriyal
Numbers are big here so just make a small change

        long[] left = new long[n];
        long[] right = new long[n];
        long[] res = new long[n];

If this resolves your query then please mark it as resolved :slight_smile:

1 Like