smilelearner
BAN USERWhy there is an need to compute using double and cast again it to int and returning it?
- smilelearner September 20, 2012i don't get it, can you give some code or explain it? why dividing is not efficient?
- smilelearner September 20, 2012Yeah, thats right. and is there any other cases we need to concern? what is NPE and corner cases because as scorpionking mentioned for shiva. (not checking NPE and corner cases) so what else?
- smilelearner September 20, 2012In java, you can just create Hashmap or ( Tree Hashmap [sorted : so more efficient] ). you can give a key in it and values associated with it. for generating a key you can create your own hash methods, or use standard hash function like "MD5" store those Values with its key. so to search it, we can compute its hash value and getValue(Key) somewhat like O(1) search and thats it .
- smilelearner September 18, 2012i guess using Hash methods 'll be more effiicient than Binary search (if database already stored with hash values)
- smilelearner September 18, 2012for each element you can compute the product value of all other numbers
(if elt[index] !=elt[i])
but the optimal way is to compute the product initially and for each no divide the product value by it.
Java code :
public class Product {
public static void main(String[] args) {
int num[]={1,2,3,4,5};
int result[];
result=prod(num);
System.out.println(result);
}
private static int[] prod(int[] num) {
int product[]=new int[num.length],p=1;
for(int x:num)
{
p*=x;
}
System.out.println("total product: "+p);
for(int i=0;i<num.length;i++)
{
product[i]=p/num[i];
System.out.println(product[i]);
}
return product;
}
}
There may even more optimal way, but my idea is this. But i didn't get this, what is a corner case?
- smilelearner September 18, 2012
sample code for indexing?
- smilelearner September 20, 2012