DEV Community

akbhairwal
akbhairwal

Posted on

6-10PM challenge problem #002 solution

Problem#002

Array of Array Products

public  int[] solve(int[] arr) {
       int [] out = new int[arr.length];
       int product =1;

       for(int i=0;i<arr.length;i++){
           out[i]=product;
           product = product*arr[i];
        }

        product=1;
        for(int j=arr.length-1;j>=0;j--){
            out[j] = out[j]*product;
            product = product* arr[j];
        }
      return out;
   }

n is size of an array
Time complexity is O(n)
Space complexity is O(n)

Top comments (0)