Which of the following is true for MyX function?

int MyX(int *E, unsigned int size)

{

    int Y = 0;

    int Z;

    int i, j, k;

    for(i = 0; i < size; i++)

        Y = Y + E[i];

    for(i = 0; i < size; i++)

        for(j = i; j < size; j++)

        {

            Z = 0;

            for(k = i; k <= j; k++)

                Z = Z + E[k];

   @)        if (Z > Y)

                Y = Z;

        }

    return Y;

}

 

(A) maximum possible sum of elements in any sub-array of array E.
(B) maximum element in any sub-array of array E.
(C) sum of the maximum elements in all possible sub-arrays of array E
(D) the sum of all the elements in the array E.
 

 

A) is given as answer but Y is the sum  all no. inside the array E  so how a sum of sub array number (Z) greater than Y see line number

see the @ mark line.It will never true so how A will true answer may be D????

1Comment
Sumit Verma @sumitverma
12 May 2017 01:14 am
I think you are not considering negative numbers.
Consider an array with elements like, 3, 5, -1, -7 .
Here you can see that sum of sub array 3, 5 is greater than sum of all the elements.

Pages