help | forum home | logged in as: guest | login/register
link mingle
interview_questions
Bookmarks
Google Interview Question : Product of other Elements in an Array in O(n)
-2
Votes

There is an array A[N+1] of N integers. You have to compose an array Output[N+1] such that Output[i] will be equal to the productof all the elements of A[] except A[i].
Example:
INPUT:[4, 3, 2, 1, 2]
OUTPUT:[12, 16, 24, 48, 24]

SSolve it without division operator and in O(n) with out using division


saved under Google Interview Questions by interview_questions

 
import java.util.Arrays;

public class PArray {

    public int[] PSub(int[] inp){
     int[] out = new int[inp.length];
     out[0]=1;out[1]=inp[0];
     for (int i = 2; i <inp.length ; i++)
         out[i] = inp[i-1]*out[i-1];
     int P = 1;
     for(int i=inp.length-2;i>=0;i--){
         P*=inp[i+1];
         out[i]=out[i]*P;
     }
      return out;
    }
    public static void main(String[] args) {
        PArray pArray = new PArray();
        int in[] = new int[]{4,3,2,1,2};
        System.out.println("INPUT:"+
            Arrays.toString(in));
        System.out.println("OUTPUT:"+
            Arrays.toString(pArray.PSub(in)));
    }
}

comment by fabia on 2008-06-04 22:17:30
// O(n) import java.util.Arrays; public class PArray2 { public int[] PSub(int[] inp){ int[] outL = new int[inp.length]; int[] outR = new int[inp.length]; int[] ret = new int[inp.length]; outL[0]=1;outL[1]=inp[0]; outR[inp.length-1]=1; outR[inp.length-2]=inp[inp.length-1]; for (int i = 2; i <inp.length ; i++) { outL[i] = inp[i-1]*outL[i-1]; outR[inp.length-1-i] = inp[inp.length-i]*outR[inp.length-i]; if (i >= inp.length-1-i) { ret[i] = outL[i]*outR[i]; ret[inp.length-1-i] = outL[inp.length-1-i]*outR[inp.length-1-i]; } } return ret; } public static void main(String[] args) { PArray2 pArray = new PArray2(); int in[] = new int[]{4,3,2,1,2}; System.out.println("INPUT:"+ Arrays.toString(in)); System.out.println("OUTPUT:"+ Arrays.toString(pArray.PSub(in))); } }
comment by Pokmajom on 2008-06-09 17:01:02
import java.util.Arrays;

public class PArray2 {
	    
	    public int[] PSub(int[] inp){

	    	 int[] outL = new int[inp.length];
		     int[] outR = new int[inp.length];
		     int[] ret = new int[inp.length];
		     outL[0]=1;outL[1]=inp[0];
		     outR[inp.length-1]=1; outR[inp.length-2]=inp[inp.length-1];
		     for (int i = 2; i <inp.length ; i++)
		     {
		    	 outL[i] = inp[i-1]*outL[i-1];
		    	 outR[inp.length-1-i] = inp[inp.length-i]*outR[inp.length-i];
		    	 if (i >= inp.length-1-i) {
		    		 ret[i] = outL[i]*outR[i];
	    			 ret[inp.length-1-i] = outL[inp.length-1-i]*outR[inp.length-1-i];
		    	 }
		     }
		      return ret;
	    }
	    public static void main(String[] args) {
	    	PArray2 pArray = new PArray2();
	    	 int in[] = new int[]{4,3,2,1,2};
	        System.out.println("INPUT:"+
	            Arrays.toString(in));
	        System.out.println("OUTPUT:"+
	            Arrays.toString(pArray.PSub(in)));
	    }
	}

comment by Pokmajom on 2008-06-09 17:06:29
 



Enter the string above
 
Thumbnails by Thumbshots.com