search
Log In
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??

in Programming 351 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??
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

Please log in or register to answer this question.

Related questions

1 vote
0 answers
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?$ .
asked May 4, 2019 in Programming srestha 340 views
1 vote
0 answers
2
327 views
Consider the following C code: int getNextGap(int gap){ gap=(gap*10)/13; if(gap<1)return 1; return gap; } void mystery(int a[ ],int n){ int gap=n; bool red=true; while(gap!=1||red==true){ gap=getNextGap(gap); red=false; for(int i=0;i<(n-gap ... n is number of array elements, then what output will print at last? I mostly stuck how bool function working here. Plz help me out, how program executing:(
asked May 10, 2019 in Programming srestha 327 views
4 votes
3 answers
3
401 views
#include<stdio.h> #include<iostream> int bar(int m, int n){ if(m==0)return n; if(n==0)return m; return bar(n%m,m); } int foo(int m,int n){ return(m*n/bar(m,n)); } int main(){ int x=foo(1000,1500); printf("%d",x); return 0; } Output of the program is ___________
asked May 20, 2019 in Programming srestha 401 views
2 votes
1 answer
4
435 views
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)$
asked May 19, 2019 in Programming srestha 435 views
...