GeeksforGeeks » Interview Questions
Question in recruitment examination of Microsoft for SDE
(5 posts)-
Multiply all the elements of A[] and store the output in an aray out[], and the element at out[i] should be the output of all elements of A[] except A[i]
-
calculate product of all element of given array and store it in a variable called Prod, the start a loop from 0 to n and subtract the encountered element from the sum and store it again in the same array
-
lets say nums are 1 2 3 4 5 create 2 arrays such that the element contains product of all nums till n-1, traversing front and reverse so 1 1 2 6 24 120 60 20 5 1 Now multiply them and store it back in array 120 60 40 30 24
-
#include <stdio.h> #include <stdlib.h> void findProduct(int *in,int len, int prod, int *out) { int i; if(!in || len <= 0) return; for(i = 0; i < len; i++) out[i] = prod/in[i]; } void display(int *a, int len) { if(!a || len <= 0) return; printf("%d\n", *a); display(++a, --len); } int main() { int a[]={1,2,3,4,5}; int *out, prod = 1, i; int len = sizeof(a)/sizeof(a[0]); out = (int*)malloc(sizeof(int) * len); for(i = 0; i < len; i++) prod *= a[i]; findProduct(a, len, prod, out); display(out, len); free(out); out = NULL; return 0; }O(n)
Reply
You must log in to post.