Made Easy Test Series:Programming Test4-Function Call

1 vote
351 views

Consider the following function

int fun(int a[ ],int l, int target){
int i=0,j=0,sum=0,count=0;
while(j<l){
if(sum<target){
sum=sum+a[j];
j++;
}
else if(sum>target){
sum=sum-a[i];
i++;
}
else{
count++;
sum=sum-a[i];
i++;
}
}
if(sum==target)
count++;
return count;
}

If $a[ ]$ states the element

$a\left [ \right ]=\left \{ 2,3,3,2,.5,4,1,3,6,8,2,3,4,4,2,2 \right \}$

What will be return value of function call $fun\left ( a,16,8 \right )=$_______________

Given ans $6,$ but I got $4.$ Which one correct?? Any shortcut to evaluate??

0
The answer is 7. Note that since its an integer array, the element  "0.5" will become 0.
0
ok, can u elaborate more??
5
When a[]={2,3,3,2,0.5,4,1,3,6,8,2,3,4,4,2,2} is passed
since a is an integer array the floating value 0.5 becomes 0
And the array becomes {2,3,3,2,0,4,1,3,6,8,2,3,4,4,2,2}
Initially, sum=0,j=0,i=0 and COUNT =0
and following are their values after each iteration
1 ) sum=2 , j=1,i=0 and COUNT=0  sum+=2
2 ) sum=5 , j=2,i=0 and COUNT=0 sum+=3
3 ) sum=8 , j=3,i=0 and COUNT=0 sum+=3
4 ) sum=6 , j=3,i=1 and COUNT=1 since sum=8 is equal to target and hence count is incremented
5 ) sum=8 , j=4,i=1 and COUNT=1 sum+=2
6 ) sum=5 , j=4,i=2 and COUNT=2 since sum=8==target
7 ) sum=5 , j=5,i=2 and COUNT=2 sum+=0
8 ) sum=9 , j=6,i=2 and COUNT=2  sum+=4
9 ) sum=6 , j=6,i=3 and COUNT=2  sum-=3
10 ) sum=7 , j=7,i=3 and COUNT=2 sum+=1
11 ) sum=10 , j=8,i=3 and COUNT=2 sum+=3
12 ) sum=8 , j=8,i=4 and COUNT=2
13 ) sum=8 , j=8,i=5 and COUNT=3 since sum=8==target
14 ) sum=4 , j=8,i=6 and COUNT=4 since sum=8==target
15 ) sum=10 , j=9,i=6 and COUNT=4 sum+=6
16 ) sum=9 , j=9,i=7 and COUNT=4
17 ) sum=6 , j=9,i=8 and COUNT=4
18 ) sum=14 , j=10,i=8 and COUNT=4 sum+=8
19 ) sum=8 , j=10,i=9 and COUNT=4
20 ) sum=0 , j=10,i=10 and COUNT=5  since sum=8==target
21 ) sum=2 , j=11,i=10 and COUNT=5 sum+=2
22 ) sum=5 , j=12,i=10 and COUNT=5 sum+=3
23 ) sum=9 , j=13,i=10 and COUNT=5 sum+=4
24 ) sum=7 , j=13,i=11 and COUNT=5
25 ) sum=11 , j=14,i=11 and COUNT=5 sum+=4
26 ) sum=8 , j=14,i=12 and COUNT=5
27 ) sum=4 , j=14,i=13 and COUNT=6  since sum=8==target
28 ) sum=6 , j=15,i=13 and COUNT=6 sum+=2
29 ) sum=8 , j=16,i=13 and COUNT=6 sum+=2


Now the next time program won't enter loop and since sum=target=8, we increment count once again

Hence finally count=7 is returned

1
thanks for details explanation. U can make it ans too :)
0
The output is 7

Related questions

1 vote
1
340 views
Consider the following program: void A(int n){ printf("*"); if(n>1){ A(n/4); A(n/4); A(n/4); A(n/4); } } Let $X$ be number of asterisks printed by above function when $n=1024.$ Then the value of $X$ will be ____________ How ans will be $\frac{4^{k+1}-1}{3}$ , where $k=5?$ .
1 vote
Consider the following $C$ implementation which when given $3$ numbers a,b,c as input, find the maximum of $3$ numbers $a,b,c.$ int kickstart(int a,int b,int c) { if(B1) return a; if(a>=b) return B2; return kickstart(c,a,b); } How the boxes filled up correctly? $I)B1:a\geq b$ ... $IV)B1:a\geq b$ && $a\geq c, B2:kickstart\left ( b,c,a \right );$ Is it $I) and II)$ or $I) and IV)$