Given a Binary Tree, find vertical sum of the nodes that are in same vertical line. Print all sums through different vertical lines. Examples: 1 / \ 2 3 / \ / \ 4 5 6 7 The tree has 5 vertical lines Vertical-Line-1 has only one node 4 => vertical sum is 4 Vertical-Line-2: has only one node 2=> vertical sum is 2 Vertical-Line-3: has three nodes: 1,5,6 => vertical sum is 1+5+6 = 12 Vertical-Line-4: has only one node 3 => vertical sum is 3 Vertical-Line-5: has only one node 7 => vertical sum is 7 So expected output is 4, 2, 12, 3 and 7
int count_coins(int * coin,int len,int money)
ReplyDelete{
int arr[100];
for(int i=0;i<100;i++)
{
arr[i]=999;
}
arr[0]=0;
for(int i=0;i<=money;i++)
{
for(int j=0;j=0)
{
arr[i]=min(arr[i],1+arr[i-coin[j]]);
}
}
}
return arr[money];
}
typo error for 2nd loop: for(int j=0;j<len;j++)
ReplyDelete@ankit why initially you have taken array of 100 elements...
ReplyDelete@priyaranjan ... ummmm lazyness i dint want to write
ReplyDeleteint * arr=(int *)malloc(money*sizeof(int));
;)
so i assumed that maximum money that user wud input is 99
Btw..is the code correct?
Hi
DeleteTry for S=3. array[5,2,1]
answer shd be 2, it gives 1.
I think before
arr[i]=min(arr[i],1+arr[i-coin[j]]);
you shd add
if(i>=coin[j])
that wud do!
@anonymous please see the below comment, it depicts the same logic..:)
Delete@ankit yeah rest logic is correct..:)
ReplyDeleteSet Min[i] equal to Infinity for all of i
ReplyDeleteMin[0]=0
For i = 1 to S
For j = 0 to N - 1
If (Vj<=i AND Min[i-Vj]+1<Min[i])
Then Min[i]=Min[i-Vj]+1
Output Min[S]