balaji2104
BAN USER
Comments (3)
Reputation 10
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
1
of 1 vote
Substring will not work since aacn (Sorted word) is not a substring of aabcn (Sorted characters).
- balaji2104 April 29, 2014Comment hidden because of low score. Click to expand.
0
of 0 vote
public static int[] selfExcludingProduct(int[] input) {
if(input == null || input.length == 0)
return null;
int product = 1;
int nonZeroProduct = 1;
int[] prod = new int[input.length];
for(int i : input) {
if(i!=0) {
nonZeroProduct*=i;
}
product*=i;
}
for(int i=0; i< input.length;i++) {
if(input[i] == 0) {
prod[i] = nonZeroProduct;
}
else {
prod[i] = product/input[i];
}
}
return prod;
}
This should cover a case where ONLY one element in array is 0, the self-excluding product of 0 is a non-zero value.
{3,1,4,2,0} ==> {0, 0, 0, 0, 24}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Adding OO Code using PriorityQueue. You can enhance this to use a blocking queue if multiple entrances/exits exist.
- balaji2104 May 01, 2014