GateAI
Programming in C

GATE CSE 2014 Set 1 · Q41

MCQ2 marksMedium

Time

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

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: A) maximum possible sum of elements in any sub-array of array E.

Programming in C

Let us trace the given function:

  1. The first for loop calculates the sum of all elements of array E and stores it in Y:

    Y=∑m=0size−1E[m]Y = \sum_{m=0}^{\text{size}-1} E[m]
  2. The nested loops iterate over all possible sub-arrays of array E starting at index i and ending at index j (where 0≤i≤j<size0 \le i \le j < \text{size}):

    • The innermost loop calculates Z=∑k=ijE[k]Z = \sum_{k=i}^j E[k], which is the sum of elements in the sub-array E[i…j]E[i \dots j].

    • If Z>YZ > Y, then YY is updated with ZZ.

  3. Thus, YY is updated whenever a sub-array sum exceeds the current maximum found so far (initialized to the total array sum).

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

Hence, the correct option is (A).

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Programming in C previous year question is Q41 of the GATE CSE 2014 Set 1 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 19 of them are 2-mark MCQs like this one, and GATE CSE 2014 Set 1 has 2.

How GATE tests Programming in C

More from GATE CSE 2014 Set 1

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2014 Set 1 · Q41 · MCQ

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 Y) Y = Z; } return Y; } The value returned by the function MyX is the

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.