The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+17 votes
1.3k views

Consider the C program given below : 

#include <stdio.h>
int main ()    {
    int sum = 0, maxsum = 0,  i,  n = 6;
    int a [] = {2, -2, -1, 3, 4, 2};
    for (i = 0; i < n; i++)    {
            if (i == 0 || a [i]  < 0  || a [i] < a [i - 1])  {
                     if (sum > maxsum) maxsum = sum;
                     sum = (a [i] > 0) ? a [i] : 0;
            }
            else sum += a [i];
    }
    if (sum > maxsum) maxsum = sum ;
    printf ("%d\n", maxsum);

}

What is the value printed out when this program is executed?

  1. $9$
  2. $8$
  3. $7$
  4. $6$
asked in Programming by Boss (19.1k points)
edited ago by | 1.3k views

3 Answers

+19 votes
Best answer

Answer is C.

I have tried to explain this question in a better way and the explanation is here...

$i$

$A[i]$

for $\rightarrow$ if -----satisfied?

maxsum

sum

-

-

-

$0$

$0$

$0$

$A[0]= 2$

Yes $(i == 0)$

$0$

$2$

$1$

$A[1]= -2$

Yes $(a[i] < 0)$

$2$

$0$

$2$

$A[2]= -1$

Yes $(a[i] < 0)$

--

(for $\rightarrow$ if $\rightarrow$ if -----not satisfied)

$0$

$3$

$A[3]= 3$

No (else executed)

--

(for $\rightarrow$ if $\rightarrow$ if -----not satisfied)

$3$

$4$

$A[4]= 4$

No (else executed)

--

(for $\rightarrow$ if $\rightarrow$ if -----not satisfied)

$7$

$5$

$A[5]= 2$

Yes $(a[i] < a[i - 1])$

$7$

$2$

[End of for loop]

If (sum (i.e. $2$) $ >$ maxsum(i.e. $7$))   // No

                maxsum $=$ sum;   // Not Executed

printf will output maxsum $= 7$

answered by (189 points)
edited ago by
0
ans is c
0
Thanks, I have written it now.
+1
Great !
+12 votes
The algorithm is finding the maximum sum of the monotonically increasing continuous sequence of positive numbers in the array. So, output would be 3+4 = 7.
answered by Veteran (347k points)
+1
Sir but here in array  the no. is 2,-2,-1,3,4,2. How is it monotonically increasing? I couldn't understand? Please explain a bit about it. Thanks.
+2
How did you know that it is monotonically increasing by looking into question.Is there any standard approach to do such kinds of problems ?
0
monotonically incresing means to find of set of contigous vaues which has maximum sum

in ques ,

{-2,-1,3,4}  SUM=4

{-1,3,4} SUM=6

{3,4} SUM=7

here maximum sum is 7

HEnce monotonically incresing value is 7
0
@Arjun Sir,  if the input array contains only negative integers in increasing order (example--9, -8,-7,-3,-2,-1) then output will be 0, is the algorithm  correct in that case?
0
yes, if there are no positive values sum must be 0.
+10 votes
first of  all  some  synatax  error  in  above  code segment .  right code given  below

# include <stdio.h>
int main ()    {
    int sum = 0, maxsum = 0,  i,  n = 6;
    int a [] = {2, -2, -1, 3, 4, 2};
    for (i = 0; i < n; i++)    {
            if (i == 0 || a [i]  < 0  || a [i] < a [i - 1])  {
                     if (sum > maxsum) maxsum = sum;
                     sum = (a [i] > 0) ? a [i] : 0;
            }
            else
                sum += a [i];
            }
            if (sum > maxsum) maxsum = sum ;
            printf ("%d\n", maxsum);
}

 

 

 

ans  is  C .

simply  execute  and  trace  value of  sum and  maxsum for i=0 to 5

i  -->      initially  0   1   2   3   4     5

sum            0     0   2   0   3    7    2

maxsum     0     0   2   2   2   2    7

so  maxsum  =  7
answered by Junior (679 points)
0
Your answer is correct, but the traced values of sum at i = 0 and i = 1 have been written wrong here. These should be opposite (you may have miss typed).
0
general doubt:

when we are inside the function ,the value of sum is retain back just like static.but once if we out from the for loop the value of sum and mxsum wil be zero?
Answer:

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

36,075 questions
43,521 answers
123,666 comments
42,747 users