The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
88 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??

asked in Programming by Veteran (112k points) | 88 views
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??
+3
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 :)

Please log in or register to answer this question.

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,588 questions
54,197 answers
187,535 comments
71,152 users