edited by
21,626 views
62 62 votes

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.

6 Answers

Best answer
59 59 votes

Answer is (A) maximum possible sum of elements in any sub-array of array E.

int MyX ( int * E, unsigned int size )
{
    int Y= 0;
    int z;
    int i, j,k;
   
//calculate sum of the elements of the array E and stores it in Y
    for i 0;i<size;i++)   
      Y = Y+E[i]; 
      
//calculate the sum of all possible subaarays 
//(starting from postion 0..n-1)  
    for (i=0;i<size;i++)        
        for(j=i;j<size ;j++)       
        {                                    
            z = 0;                    
            for(k=i; k<=j;k++)  
                z=z+E[k]; 
                
//checks whether sum of elements of each subarray is greater
//than the current max, if so, then assign it to currentmax
            if(z>Y)           
                Y = z;
        }
//ultimately returns the maximum possible sum of elements 
//in any sub array of given array E    
    return Y;  
}

 

edited by
7 7 votes

 

here, you clearly see that y is updated when sub-array elements 3,1,5 sum is maximum.

So the function returns the maximum possible sum of elements in any sub-array of the array E.

 

Answer : A

4 4 votes

Y = sum of all elements (initially)

Z = sum of subarrays

Y>Z = sum of subarrays that exceed the overall max

doing Y=Z is finding overall max value that could occur in a subarray

 

Scenarios : 

  1. If array has all positive elements, then each Z will always be less than Y , Z<Y is always the case
  2. If array has “x” number of zeros. If these zeros are present anywhere , then again Z<Y holds only; if these zeros are towards the extreme left or extreme right, then Y=Z can occur.
  3. If array even has negative elements, then Y<Z or Y=Z or Y>Z all these can occur. So, overall in such an array, you really see the effect, and you’ll get a subarray of max value in Y.
2 2 votes

So by now everyone must have found out the answer is: 

A. maximum possible sum of elements in any sub-array of array E.

But I see alot of people having doubt why choose negative as it wasn't mentioned anywhere. So here is the answer for it. 

Lets assume you take all numbers as postive the 1st option would be applicable there as well as the option D, which is sum of all the elements because in case of all the elements are postivie it is always going to return sum of all the elements. (Sum of subarray cant be bigger than sum of all the elements) Now you might ask why option 1st will be applicable there: The answer for that is very simple 

The entire array can be considered a subarray of itself. A subarray is defined as a contiguous portion of an array, and since the entire array is contiguous, it qualifies as a subarray. So, if you have an array of size 𝑛, there is exactly one subarray that is the entire array.

Now you have 2 options that qualifies as a probable answer but that shouldn't be possible unless there is some issue with the question so that is when you think of negative int values as well. Which won't satisfy the option D. Hence is the correct answer for all the test cases.

Hope this helps. 

1 1 vote
The function does following Y is used to store maximum sum seen so far and Z is used to store current sum

1) Initialize Y as sum of all elements

2) For every element, calculate sum of all subarrays starting with arr[i]. Store the current sum in Z. If Z is greater than Y, then update Y.
edited by
0 0 votes
sum of subarray can never be greater than sum of all elements of an array .therefore ans is D
Answer:
Position:
Show:

Related questions

78 78 votes
11 answers 11 answers
33.2k
33.2k views
go_editor asked Sep 28, 2014
33,224 views
Consider a $6$-stage instruction pipeline, where all stages are perfectly balanced. Assume that there is no cycle-time overhead of pipelining. When an application is exec...
111 111 votes
15 answers 15 answers
51.8k
51.8k views
go_editor asked Sep 28, 2014
51,791 views
Consider the following pseudo code. What is the total number of multiplications to be performed?D = 2 for i = 1 to n do for j = i to n do for k = j + 1 to n do D = D * 3H...
137 137 votes
18 answers 18 answers
79.6k
79.6k views
go_editor asked Sep 28, 2014
79,569 views
The minimum number of comparisons required to find the minimum and the maximum of $100$ numbers is ________
68 68 votes
13 answers 13 answers
18.4k
18.4k views
go_editor asked Sep 28, 2014
18,385 views
There are $5$ bags labeled $1$ to $5$. All the coins in a given bag have the same weight. Some bags have coins of weight $10$ gm, others have coins of weight $11$ gm. I p...