Amazon Interview Question: Given an array of numbers, nu... | Glassdoor.co.in

# 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.

0

Well my answer was to divide the array in 2 different parts perform some logic and in end multiply both the arrays

Interview Candidate on 24-Sep-2012
0

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)

Anonymous on 11-Oct-2012
0

oh.. but without division. Didn't see that part with my tiny browser

Anonymous on 11-Oct-2012
0

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&lt;5;j++){
int i;
int p=1;
for(i=0; i&lt;5; i++){
p=a[i]*p;
}
b[j]=p/j;
}
System.out.println(Arrays.toString(b));
}
}

Rajesh T on 20-Jan-2013
4

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&lt;5;j++){
int i;
int p=1;
for(i=0; i

Rajesh T on 20-Jan-2013
0

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&lt;5;j++){
int i;
int p=1;
for(i=0; i&lt;5; i++){
p=a[i]*p;
}
b[j]=p/a[j];
}
System.out.println(Arrays.toString(b));
}
}

Rajesh T on 20-Jan-2013
1

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

Anonymous on 03-Mar-2013
0

#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 &lt; 4; i++){
cout &lt;&lt; b[i] &lt;&lt; " " &lt;&lt; endl;
}
}

Niketh on 24-Sep-2013
0

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&lt;=Numarray.length-1;j++)
{
for(int i=0;i

Anonymous on 13-May-2015
0

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

0

public void printProduct(int[] a)
{
int[] b= new int[a.length];
for(int j=0;j

Nishant on 22-Jun-2015
0

public void printProduct(int[] a)
{
int[] b= new int[a.length];
for(int j=0;j

Nishant on 22-Jun-2015
0

prodSum = function (arr, memo, index) {
if(index &gt;= arr.length){
return;
}
for(var i=0; i

Ashish Chaturvedi on 31-Aug-2017