ganeshjwhr
BAN USERWinner( piles[], n){
totalattempts=0;
for(i=1; i<=n; i++)
totalattempts += piles[i] / i;
if(totalattempts &1)
print Bob winner
else print Alice winner
}
// This was my solution during my Vmware interview.
// where they asked us to remove the repeating elements
removeDuplicates(int arr[], int size, int n){
HashMap<Integer,Integer> hashmap=new HashMap<Integer,Integer>(n);
for(i=0;i<size;i++){
if(hashmap.get(arr[i])==null) hashmap.put(arr[i],1);
else hashmap.put(arr[i], hashmap.get(arr[i]) + 1);
}
leftindex=0;
for(i=0;i<size;i++){
if(hashmap.get(arr[i])>0){
arr[leftindex++]=arr[i];
hashmap.put(arr[i],0); //mark the no. as seen.
}
}
size=leftindex;
}
//Code for n-length linked list with result list of size 'n' preallocated
void findSUM( Node l1, Node l2, Node result){
if( (l1==NULL && l2==NULL) || result==NULL) return;
if(l1!=NULL) {result=l2; return;}
else {result=l1; return;}
carry=sumHelper(l1, l2, result);
if(carry>0)
result.data=result.data+(carry*10);
}
int sumHelper(Node l1, Node l2, Node result){
if(l1==NULL || l2==NULL)
return 0;
carry=sumHelper(l1.next, l2.next, result.next);
result.data=(l1.data + l2.data+ carry)%10;
return (l1.data + l2.data + carry)/10;
}
Your code is not a generic one. Please do consider the case where the arrays are unsorted.
- ganeshjwhr August 02, 2013