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

asked in Algorithms by Loyal (7.9k points) | 71 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)

Answer here is No

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 Answer

+1 vote
Best answer

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

answered by Veteran (60.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

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,541 questions
54,083 answers
187,207 comments
70,992 users