Given an array of numbers, nums, return an array of numbers products, where products[i] is the product of all nums[j], j != i. Input : [1, 2, 3, 4, 5] Output: [(2*3*4*5), (1*3*4*5), (1*2*4*5), (1*2*3*5), (1*2*3*4)] = [120, 60, 40, 30, 24] You must do this in O(N) without using division. 13 AnswersWell my answer was to divide the array in 2 different parts perform some logic and in end multiply both the arrays One solution would probably be to go through entire array once, keep a track of total product, and then in another loop populate result array by setting each index as totalProduct / (value at index in argument array) oh.. but without division. Didn't see that part with my tiny browser Show More Responses If you were not supposed to divide the array into two, for processing.... import java.util.*; class aNum2{ public static void main(String[] args){ int[] a= new int[5]; int[] b= new int[5]; a[0]=1; a[1]=2; a[2]=3; a[3]=4; a[4]=5; int j; for(j=0;j<5;j++){ int i; int p=1; for(i=0; i<5; i++){ p=a[i]*p; } b[j]=p/j; } System.out.println(Arrays.toString(b)); } } if you werent supposed to use the division operator : import java.util.*; class aNum{ public static void main(String[] args){ int[] a= new int[5]; int[] b= new int[5]; a[0]=1; a[1]=2; a[2]=3; a[3]=4; a[4]=5; int j; for(j=0;j<5;j++){ int i; int p=1; for(i=0; i Please ignore the very first response .. there is a correction on line 20 If you were not supposed to divide the array into two, for processing.... import java.util.*; class aNum2{ public static void main(String[] args){ int[] a= new int[5]; int[] b= new int[5]; a[0]=1; a[1]=2; a[2]=3; a[3]=4; a[4]=5; int j; for(j=0;j<5;j++){ int i; int p=1; for(i=0; i<5; i++){ p=a[i]*p; } b[j]=p/a[j]; } System.out.println(Arrays.toString(b)); } } trick is to construct the arrays (in the case for 4 elements) for example { 1, a[0], a[0]*a[1], a[0]*a[1]*a[2], } // productsBelow { a[1]*a[2]*a[3], a[2]*a[3], a[3], 1, } // productsAbove then multiply productsBelow and productsAbove http://stackoverflow.com/questions/2680548/interview-q-given-an-array-of-numbers-return-array-of-products-of-all-other-nu #include using namespace std; int main(){ int a[4] = {1,2,3,4}; int b[4] = {1,1,1,1}; for(int i = 1 ; i =0; i--){ prod *= a[i+1]; b[i] *= prod; } for(int i = 0; i < 4; i++){ cout << b[i] << " " << endl; } } public class Example2 { static int[] Numarray={1,2,3,4,5}; public static void main(String[] args) { ArrayList arr1=new ArrayList(); int first=0; int count=1; for(int j=0;j<=Numarray.length-1;j++) { for(int i=0;i There should be no divsion of array not even numbers so the solution goes like this //SIMPLIFIED APPROACH class amazon2 { public static void main(String rags[]) { int []a={1,2,3,4,5};//any digits as per approach int []b=new int[10]; int []c=new int[10];//dummy integer array for(int i=0;i public void printProduct(int[] a) { int[] b= new int[a.length]; for(int j=0;j public void printProduct(int[] a) { int[] b= new int[a.length]; for(int j=0;j prodSum = function (arr, memo, index) { if(index >= arr.length){ return; } for(var i=0; i |