+ 2
not able to console the val? odd(N,arr,val=0,i=index,sum=0);
Given an integer array, return the count of all the subsets of the array, where the sum of all the elements in the subset is odd let N = 3 let arr = [1,2,3] here is the code function subset(N,arr,index,count){ if(index === N){ return; } odd(N,arr,val=0,i=index,sum=0); subset(N,arr,index+1,count+=val); } function odd(N,arr,val,i,sum){ if(i === N){ // console.log(val) return val; } sum+=arr[i]; if(sum%2 !== 0){ odd(N,arr,val+1,i+1,sum); }else{ odd(N,arr,val,i+1,sum); } } console.log(subset(N,arr,index=0,count=0))
2 Answers
+ 2
You need to return the results of the recursive calls made inside of odd() function.
function odd(N,arr,val,i,sum){
if(i === N){
// console.log(val)
return val;
}
sum+=arr[i];
if(sum%2 !== 0){
return odd(N,arr,val+1,i+1,sum);
}else{
return odd(N,arr,val,i+1,sum);
}
}
+ 1
Given an integer array, return the count of all the subsets of the array, where the sum of all the elements in the subset is odd
this is what it ask
i/p = N = 3
arr = [1,2,3]
O/P = 4