6-10PM challenge problem #002 solution

akbhairwal

akbhairwal

Posted on September 26, 2019

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)

💖 💪 🙅 🚩
akbhairwal
akbhairwal

Posted on September 26, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related