75 views

### Original Question  - here

Consider the following C function in which size is the number of elements in the array E:

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;
} 

The value returned by the function MyX is the

1. maximum possible sum of elements in any sub-array of array E.
2. maximum element in any sub-array of array E.
3. sum of the maximum elements in all possible sub-arrays of array E.
4. the sum of all the elements in the array E.

====================================================================================

Can someone trace the code by taking some arbitrary values in the array and show how to do this? Thank you!

| 75 views
0

take elements of an array with +ve and -ve values both

And then dry run the code

like array $a\left [ i \right ]=\left \{ 5,4,-1,2,-3 \right \}$

Now, if u run the code

 for(i = 0; i< size; i++)
Y = Y + E[i]; 

this part will give value of Y is 7

Now, check how Z is working. .

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; 

here when i=0,j=0 value of Z is 5

Comparing with Y i.e.(7>5)

if it is true? Yes

but for i=0, j=1 see what happens

Z=5+4=9

Comparing with Y (7>9)

So, Y returning 9

Thus the code working

0
what is option $3$ says

$3)$ sum of the maximum elements in all possible sub-arrays of array E.

what is this mean??

+1 vote

What is the meaning of sub-array?

 for(i = 0; i< size; i++)
Y = Y + E[i];

therefore Y is sum of all elements of E 

Z = 0;
for(k = i; k <= j; k++)
Z = Z + E[k];

therefore Z is sum of all the elements on that sub array only 

if(Z > Y)
Y = Z;

therefore updating Y with sum of all the elements on that sub array when it is grater than y,
means Y = maximum possible sum of elements in any sub-array of array E.

your doubt is how got that sub array?

therefore k is a every possible sub array ==> y returns maximum possible ( sum of elements in any sub-array of array E. )

if you didn't get this, then follow this method for understanding...

Let A is array of 5 elements, A=[a,b,c,d,e]

what are the all possible sub-array's ?

1) a, ab, abc, abcd, abcde

2) b, bc, bcd, bcde

3) c, cd, cde

4) d, de

5) e

---------------------------------

i=1 to n -----> getting 5 rows

j=i to n

k=i to j ------> dividing into sub array

by Veteran (61.8k points)
selected by
0
Thank you so much, Shaik.
0
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; 

(OR)

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; 

There is any difference in the code??

In the last code, i don't know how many times $1$st for loop get executed??

0
@lakshman

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

statement1;

statement2;

then it is equivalent to the code

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

{
statement1;

}
statement2;

for your question, yes those are equivalent !
0
Yes thanks

1
2