Amazon Interview Question
Java DevelopersCountry: United States
//in javascript
let a=[1,2,5,8]
for(let i=1;;i++){
let j=i;
while(a.indexOf(j)<0){
console.log(j+' la so ko co trong arr')
let k=a.reduce((a,b)=>{
return b>=j?a:a+b
});
console.log('tong la: '+k)
if(j<=k/2) break;
else j=k-j;
if(a.indexOf(j)>=0||j==0) break;
if(j<Math.min(...a)) return i;
}
console.log('khong tim thay voi: '+i)
}
There are 2 scenarios to consider,
- Test April 18, 20181. You cannot create new coins
2. You cannot break down existing coins
The solution you provide addressed #1 but not #2.
Eg. {1,4} answer should be 2 not 5